Sha256: 4d0d2887c81572ac0ea7c8ac549b5ada92b10a40800a9fc82339d2b5e656e096

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module Transport

  class Frame #:nodoc: all
    def initialize payload = nil, channel = 0
      @channel, @payload = channel, payload
    end
    attr_accessor :channel, :payload

    def id
      self.class::ID
    end
    
    def to_binary
      buf = Buffer.new
      buf.write :octet, id
      buf.write :short, channel
      buf.write :longstr, payload
      buf.write :octet, FOOTER
      buf.rewind
      buf
    end

    def to_s
      to_binary.to_s
    end

    def == frame
      [ :id, :channel, :payload ].inject(true) do |eql, field|
        eql and __send__(field) == frame.__send__(field)
      end
    end
    
    class Method
      def initialize payload = nil, channel = 0
        super
        unless @payload.is_a? Protocol::Class::Method or @payload.nil?
          @payload = Protocol.parse(@payload)
        end
      end
    end

    class Header
      def initialize payload = nil, channel = 0
        super
        unless @payload.is_a? Protocol::Header or @payload.nil?
          @payload = Protocol::Header.new(@payload)
        end
      end
    end

    class Body; end

    def self.parse buf
      buf = Transport::Buffer.new(buf) unless buf.is_a? Transport::Buffer
      buf.extract do
        id, channel, payload, footer = buf.read(:octet, :short, :longstr, :octet)
        Transport::Frame.types[id].new(payload, channel) if footer == Transport::Frame::FOOTER
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
celldee-bunny-0.2.0 lib/engineroom/frame.rb
bunny-0.2.0 lib/engineroom/frame.rb