module Hydra::MessagingIO

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

Public Instance Methods

close() click to toggle source

Closes the IO object.

# File lib/hydra/messaging_io.rb, line 36
def close
  @reader.close if @reader
  @writer.close if @writer
end
gets() click to toggle source

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

IO.gets
  => Hydra::Message # or subclass
# File lib/hydra/messaging_io.rb, line 10
def gets
  while true
    begin
      raise IOError unless @reader
      message = @reader.gets
      return nil unless message
      return Message.build(eval(message.chomp))
    rescue SyntaxError, NameError
      # uncomment to help catch remote errors by seeing all traffic
      #$stderr.write "Not a message: [#{message.inspect}]\n"
    end
  end
end
write(message) click to toggle source

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

IO.write Hydra::Message.new
# File lib/hydra/messaging_io.rb, line 27
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