Class: Carrot::AMQP::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/carrot/amqp/frame.rb,
lib/carrot/amqp/spec.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: Body, Header, Heartbeat, Invalid, Method, OobBody, OobHeader, OobMethod, Trace

Constant Summary collapse

206

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = nil, channel = 0) ⇒ Frame

Returns a new instance of Frame.



3
4
5
# File 'lib/carrot/amqp/frame.rb', line 3

def initialize payload = nil, channel = 0
  @channel, @payload = channel, payload
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



6
7
8
# File 'lib/carrot/amqp/frame.rb', line 6

def channel
  @channel
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/carrot/amqp/frame.rb', line 6

def payload
  @payload
end

Class Method Details

.Frame(id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/carrot/amqp/spec.rb', line 7

def self.Frame id
  (@_base_frames ||= {})[id] ||= Class.new(Frame) do
    class_eval %[
      def self.inherited klass
        klass.const_set(:ID, #{id})
        Frame.types[#{id}] = klass
      end
    ]
  end
end

.parse(buf) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/carrot/amqp/frame.rb', line 54

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

.typesObject



3
4
5
# File 'lib/carrot/amqp/spec.rb', line 3

def self.types
  @types ||= {}
end

Instance Method Details

#==(frame) ⇒ Object



26
27
28
29
30
# File 'lib/carrot/amqp/frame.rb', line 26

def == frame
  [ :id, :channel, :payload ].inject(true) do |eql, field|
    eql and __send__(field) == frame.__send__(field)
  end
end

#idObject



8
9
10
# File 'lib/carrot/amqp/frame.rb', line 8

def id
  self.class::ID
end

#to_binaryObject



12
13
14
15
16
17
18
19
20
# File 'lib/carrot/amqp/frame.rb', line 12

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

#to_sObject



22
23
24
# File 'lib/carrot/amqp/frame.rb', line 22

def to_s
  to_binary.to_s
end