Sha256: 42baff86e251a79fbe2303617c8b74465fa72d642bb7623d6db309f5719206dc

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8

require "amqp/spec"
require "amqp/buffer"
require "amqp/protocol"

module AMQP
  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 Invalid < StandardError; 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 = Buffer.new(buf) unless buf.is_a? Buffer
      buf.extract do
        id, channel, payload, footer = buf.read(:octet, :short, :longstr, :octet)
        Frame.types[id].new(payload, channel) if footer == FOOTER
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
amqp-0.7.5 lib/amqp/frame.rb
amqp-0.7.4 lib/amqp/frame.rb
amqp-0.7.3 lib/amqp/frame.rb
amqp-0.7.2 lib/amqp/frame.rb
amqp-0.7.1 lib/amqp/frame.rb