Module: Hydra::MessagingIO

Included in:
Pipe, SSH, Stdio
Defined in:
lib/hydra/messaging_io.rb

Overview

Module that implemets methods that auto-serialize and deserialize messaging objects.

Defined Under Namespace

Classes: UnprocessableMessage

Instance Method Summary collapse

Instance Method Details

#closeObject

Closes the IO object.



32
33
34
35
# File 'lib/hydra/messaging_io.rb', line 32

def close
  @reader.close if @reader
  @writer.close if @writer
end

#getsObject

Read a Message from the input IO object. Automatically build a message from the response and return it.

IO.gets
  => Hydra::Message # or subclass


10
11
12
13
14
15
16
17
18
# File 'lib/hydra/messaging_io.rb', line 10

def gets
  raise IOError unless @reader
  message = @reader.gets
  return nil unless message
  return Message.build(eval(message.chomp))
rescue SyntaxError, NameError
  #$stderr.write "Not a message: [#{message.inspect}]\n"
  return gets
end

#write(message) ⇒ Object

Write a Message to the output IO object. It will automatically serialize a Message object.

IO.write Hydra::Message.new


23
24
25
26
27
28
29
# File 'lib/hydra/messaging_io.rb', line 23

def write(message)
  raise IOError unless @writer
  raise UnprocessableMessage unless message.is_a?(Hydra::Message)
  @writer.write(message.serialize+"\n")
rescue Errno::EPIPE
  raise IOError
end