Sha256: f66c3feb6bb847768ad472abf70eae9efc8a33bd0a0814515ff2d43b3d6dc841

Contents?: true

Size: 1.46 KB

Versions: 13

Compression:

Stored size: 1.46 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
      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 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
      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

13 entries across 13 versions & 6 rubygems

Version Path
ngauthier-hydra-0.24.0 lib/hydra/messaging_io.rb
nulogy-hydra-0.26.0 lib/hydra/messaging_io.rb
arturop-hydra-0.25.0 lib/hydra/messaging_io.rb
arturop-hydra-0.24.0 lib/hydra/messaging_io.rb
hydra-0.24.0 lib/hydra/messaging_io.rb
nulogy-hydra-0.23.2.1 lib/hydra/messaging_io.rb
justinf-hydra-0.23.8 lib/hydra/messaging_io.rb
justinf-hydra-0.23.7 lib/hydra/messaging_io.rb
justinf-hydra-0.23.6 lib/hydra/messaging_io.rb
justinf-hydra-0.23.5 lib/hydra/messaging_io.rb
justinf-hydra-0.23.4 lib/hydra/messaging_io.rb
arturop-hydra-0.23.4 lib/hydra/messaging_io.rb
sskirby-hydra-0.23.3 lib/hydra/messaging_io.rb