Sha256: c384cb404f8815da6cdb8b077c9f882fc811ba2fef98f1ab4512e40a4af1c47f

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Hydra #:nodoc:
  # Module that implemets methods that auto-serialize and deserialize messaging
  # objects.
  module MessagingIO
    # Read a Message from the input IO object. Automatically build
    # a message from the response and return it.
    #
    #  IO.gets
    #    => Hydra::Message # or subclass
    def gets
      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"
      return gets
    end

    # Write a Message to the output IO object. It will automatically
    # serialize a Message object.
    #  IO.write Hydra::Message.new
    def write(message)
      raise IOError unless @writer
      raise UnprocessableMessage unless message.is_a?(Hydra::Message)
      @writer.write(message.serialize+"\n")
    rescue Errno::EPIPE
      $stderr.write $!.inspect
      $stderr.write $!.backtrace.join("\n")
      raise IOError
    end

    # Closes the IO object.
    def close
      @reader.close if @reader
      @writer.close if @writer
    end

    # IO will return this error if it cannot process a message.
    # For example, if you tried to write a string, it would fail,
    # because the string is not a message.
    class UnprocessableMessage < RuntimeError
      # Custom error message
      attr_accessor :message
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sskirby-hydra-0.16.9 lib/hydra/messaging_io.rb