Class: AMQ::Protocol::Table

Inherits:
Object
  • Object
show all
Includes:
TypeConstants
Defined in:
lib/amq/protocol/table.rb

Defined Under Namespace

Classes: InvalidTableError

Constant Summary

Constants included from TypeConstants

AMQ::Protocol::TypeConstants::BOOLEAN_FALSE, AMQ::Protocol::TypeConstants::BOOLEAN_TRUE, AMQ::Protocol::TypeConstants::TEN, AMQ::Protocol::TypeConstants::TYPE_32BIT_FLOAT, AMQ::Protocol::TypeConstants::TYPE_64BIT_FLOAT, AMQ::Protocol::TypeConstants::TYPE_ARRAY, AMQ::Protocol::TypeConstants::TYPE_BOOLEAN, AMQ::Protocol::TypeConstants::TYPE_BYTE_ARRAY, AMQ::Protocol::TypeConstants::TYPE_DECIMAL, AMQ::Protocol::TypeConstants::TYPE_HASH, AMQ::Protocol::TypeConstants::TYPE_INTEGER, AMQ::Protocol::TypeConstants::TYPE_SIGNED_16BIT, AMQ::Protocol::TypeConstants::TYPE_SIGNED_64BIT, AMQ::Protocol::TypeConstants::TYPE_SIGNED_8BIT, AMQ::Protocol::TypeConstants::TYPE_STRING, AMQ::Protocol::TypeConstants::TYPE_TIME, AMQ::Protocol::TypeConstants::TYPE_VOID

Class Method Summary collapse

Class Method Details

.decode(data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/amq/protocol/table.rb', line 57

def self.decode(data)
  table        = Hash.new
  table_length = data.unpack(PACK_UINT32).first

  return table if table_length.zero?

  offset       = 4
  while offset <= table_length
    key, offset  = decode_table_key(data, offset)
    type, offset = TableValueDecoder.decode_value_type(data, offset)

    table[key] = case type
                 when TYPE_STRING
                   v, offset = TableValueDecoder.decode_string(data, offset)
                   v
                 when TYPE_INTEGER
                   v, offset = TableValueDecoder.decode_integer(data, offset)
                   v
                 when TYPE_DECIMAL
                   v, offset = TableValueDecoder.decode_big_decimal(data, offset)
                   v
                 when TYPE_TIME
                   v, offset = TableValueDecoder.decode_time(data, offset)
                   v
                 when TYPE_HASH
                   v, offset = TableValueDecoder.decode_hash(data, offset)
                   v
                 when TYPE_BOOLEAN
                   v, offset = TableValueDecoder.decode_boolean(data, offset)
                   v
                 when TYPE_SIGNED_8BIT  then raise NotImplementedError.new
                 when TYPE_SIGNED_16BIT then raise NotImplementedError.new
                 when TYPE_SIGNED_64BIT then raise NotImplementedError.new
                 when TYPE_32BIT_FLOAT then
                   v, offset = TableValueDecoder.decode_32bit_float(data, offset)
                   v
                 when TYPE_64BIT_FLOAT then
                   v, offset = TableValueDecoder.decode_64bit_float(data, offset)
                   v
                 when TYPE_VOID
                   nil
                 when TYPE_ARRAY
                   v, offset = TableValueDecoder.decode_array(data, offset)
                   v
                 else
                   raise ArgumentError, "Not a valid type: #{type.inspect}\nData: #{data.inspect}\nUnprocessed data: #{data[offset..-1].inspect}\nOffset: #{offset}\nTotal size: #{table_length}\nProcessed data: #{table.inspect}"
                 end
  end

  table
end

.decode_table_key(data, offset) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/amq/protocol/table.rb', line 126

def self.decode_table_key(data, offset)
  key_length = data.slice(offset, 1).unpack(PACK_CHAR).first
  offset += 1
  key = data.slice(offset, key_length)
  offset += key_length

  [key, offset]
end

.encode(table) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amq/protocol/table.rb', line 33

def self.encode(table)
  buffer = String.new

  table ||= {}

  table.each do |key, value|
    key = key.to_s # it can be a symbol as well
    buffer << key.bytesize.chr + key

    case value
    when Hash then
      buffer << TYPE_HASH
      buffer << self.encode(value)
    else
      buffer << TableValueEncoder.encode(value)
    end
  end

  [buffer.bytesize].pack(PACK_UINT32) + buffer
end

.hash_size(value) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/amq/protocol/table.rb', line 115

def self.hash_size(value)
  acc = 0
  value.each do |k, v|
    acc += (1 + k.to_s.bytesize)
    acc += TableValueEncoder.field_value_size(v)
  end

  acc
end

.length(data) ⇒ Object



110
111
112
# File 'lib/amq/protocol/table.rb', line 110

def self.length(data)
  data.unpack(PACK_UINT32).first
end