amq-protocol 0.0.1.pre → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,60 +1,139 @@
1
- # encoding: binary
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # Usage:
5
+ # ./examples/00_manual_test.rb
6
+ # ./examples/00_manual_test.rb 5673
2
7
 
3
8
  require "socket"
4
- require_relative "../lib/amq/protocol.rb"
9
+ require_relative "../lib/amq/protocol/client.rb"
5
10
 
6
11
  include AMQ::Protocol
7
12
 
8
- socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
9
- sockaddr = Socket.pack_sockaddr_in((ARGV.first || 5672).to_i, "127.0.0.1") # NOTE: this doesn't work with "localhost", I don't know why.
13
+ # Stolen from amq-client:
14
+ class AMQ::Protocol::Frame
15
+ def self.decode(io)
16
+ header = io.read(7)
17
+ type, channel, size = self.decode_header(header)
18
+ data = io.read(size + 1)
19
+ payload, frame_end = data[0..-2], data[-1]
10
20
 
11
- begin
12
- socket.connect(sockaddr)
13
- rescue Errno::ECONNREFUSED
14
- abort "Don't forget to start an AMQP broker first!"
21
+ if frame_end != FINAL_OCTET
22
+ raise "Frame has to end with #{FINAL_OCTET.inspect}!"
23
+ end
24
+
25
+ self.new(type, payload, channel)
26
+ end
15
27
  end
16
28
 
17
- # helpers
18
- MethodFrame.encode(:method, 0, Connection::TuneOk.encode(0, 131072, 0))
29
+ # NOTE: this doesn't work with "localhost", I don't know why:
30
+ socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
31
+ sockaddr = Socket.pack_sockaddr_in((ARGV.first || 5672).to_i, "127.0.0.1")
19
32
 
33
+ # helpers
20
34
  def socket.encode(klass, *args)
21
35
  STDERR.puts "#{klass}.encode(#{args.inspect[1..-2]})"
22
- klass.encode(*args).tap do |result|
23
- STDERR.puts "=> #{result.inspect}"
24
- STDERR.puts "MethodFrame.encode(:method, 0, #{result.inspect})"
25
- STDERR.puts "=> #{MethodFrame.encode(:method, 0, result).inspect}\n\n"
26
- self.write(MethodFrame.encode(:method, 0, result))
36
+ result = klass.encode(*args)
37
+ STDERR.puts "=> #{result}"
38
+ if result.is_a?(Frame)
39
+ self.write(result.encode)
40
+ else
41
+ data = result.inject("") do |buffer, frame|
42
+ self.write(frame.encode) ###
43
+ buffer += frame.encode
44
+ end
45
+ # self.write(data)
27
46
  end
28
47
  end
29
48
 
30
49
  def socket.decode
31
- frame = MethodFrame.decode(self)
32
- STDERR.puts "MethodFrame.decode(#{self.inspect})"
33
- STDERR.puts "=> #{frame.inspect}\n\n"
50
+ frame = Frame.decode(self)
51
+ STDERR.puts "Frame.decode(#{self.inspect})"
52
+ STDERR.puts "=> #{frame.inspect}"
53
+ STDERR.puts "frame.decode_payload"
54
+ STDERR.puts "=> #{res = frame.decode_payload}\n\n"
55
+ return res
56
+ end
57
+
58
+ begin
59
+ socket.connect(sockaddr)
60
+ rescue Errno::ECONNREFUSED
61
+ abort "Don't forget to start an AMQP broker first!"
34
62
  end
35
63
 
36
- # AMQP preamble
37
- puts "Sending AMQP preamble (#{AMQ::Protocol::PREAMBLE.inspect})\n\n"
38
- socket.write AMQ::Protocol::PREAMBLE
64
+ begin
65
+ # AMQP preamble
66
+ puts "Sending AMQP preamble (#{AMQ::Protocol::PREAMBLE.inspect})\n\n"
67
+ socket.write AMQ::Protocol::PREAMBLE
68
+
69
+ # Connection.Start/Connection.Start-Ok
70
+ connection_start_response = socket.decode
71
+ socket.encode Connection::StartOk, {client: "AMQ Protocol"}, "PLAIN", "\0guest\0guest", "en_GB"
72
+
73
+ # Connection.Tune/Connection.Tune-Ok
74
+ connection_tune_response = socket.decode
75
+ channel_max = connection_tune_response.channel_max
76
+ frame_max = connection_tune_response.frame_max
77
+ heartbeat = connection_tune_response.heartbeat
78
+ socket.encode Connection::TuneOk, channel_max, frame_max, heartbeat
79
+ puts "Max agreed frame size: #{frame_max}"
80
+
81
+ # Connection.Open/Connection.Open-Ok
82
+ socket.encode Connection::Open, "/"
83
+ connection_open_ok_response = socket.decode
84
+
85
+ begin
86
+ # Channel.Open/Channel.Open-Ok
87
+ socket.encode Channel::Open, 1, ""
88
+ channel_open_ok_response = socket.decode
39
89
 
40
- # Start/Start-Ok
41
- socket.decode
42
- socket.encode Connection::StartOk, {client: "AMQ Protocol"}, "PLAIN", "guest\0guest\0", "en_GB"
90
+ begin
91
+ # Exchange.Declare/Exchange.Declare-Ok
92
+ socket.encode Exchange::Declare, 1, "tasks", "fanout", false, false, false, false, false, {}
93
+ exchange_declare_ok_response = socket.decode
43
94
 
44
- # Tune/Tune-Ok
45
- socket.decode
46
- socket.encode Connection::TuneOk, 0, 131072, 0
95
+ # Queue.Declare/Queue.Declare-Ok
96
+ socket.encode Queue::Declare, 1, "", false, false, false, false, false, {}
97
+ queue_declare_ok_response = socket.decode
47
98
 
48
- # Close
49
- # socket.encode Connection::Close
50
- # socket.decode
99
+ puts "Queue name: #{queue_declare_ok_response.queue.inspect}"
51
100
 
52
- socket.close
101
+ # Queue.Bind/Queue.Bind-Ok
102
+ socket.encode Queue::Bind, 1, queue_declare_ok_response.queue, "tasks", "", false, {}
103
+ queue_bind_ok_response = socket.decode
53
104
 
54
- __END__
55
- [CLIENT] conn#4 ch#0 -> {#method<connection.start-ok>(client-properties={product=AMQP, information=http://github.com/tmm1/amqp, platform=Ruby/EventMachine, version=0.6.7},mechanism=AMQPLAIN,response=LOGINSguesPASSWORDSguest,locale=en_US),null,""}
56
- [SERVER] conn#4 ch#0 <- {#method<connection.start>(version-major=8,version-minor=0,server properties={product=RabbitMQ, information=Licensed under the MPL. See http://www.rabbitmq.com/, platform=Erlang/OTP, copyright=Copyright (C) 2007-2010 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd., version=2.1.0},mechanisms=PLAIN AMQPLAIN,locales=en_US),null,""}
57
- [SERVER] conn#4 ch#0 <- {#method<connection.tune>(channel-max=0,frame-max=131072,heartbeat=0),null,""}
58
- [CLIENT] conn#4 ch#0 -> {#method<connection.tune-ok>(channel-max=0,frame-max=131072,heartbeat=0),null,""}
59
- [CLIENT] conn#4 ch#0 -> {#method<connection.open>(virtual-host=/,capabilities=,insist=false),null,""}
60
- [SERVER] conn#4 ch#0 <- {#method<connection.open-ok>(known-hosts=),null,""}
105
+ # Basic.Consume
106
+ socket.encode Basic::Consume, 1, queue_declare_ok_response.queue, "", false, false, false, false, Hash.new
107
+
108
+ # Basic.Publish
109
+ socket.encode Basic::Publish, 1, "this is a payload", {content_type: "text/plain"}, "tasks", "", false, false, frame_max
110
+
111
+ # Basic.Consume-Ok
112
+ basic_consume_ok_response = socket.decode
113
+ puts "Consumed successfully, consumer tag: #{basic_consume_ok_response.consumer_tag}"
114
+
115
+ # Basic.Deliver
116
+ basic_deliver = socket.decode
117
+ basic_deliver_header = socket.decode # header frame: {}
118
+ basic_deliver_body = socket.decode # body frame: "this is a payload"
119
+ puts "[Received] headers: #{basic_deliver_header.inspect}, payload: #{basic_deliver_body.inspect}"
120
+ ensure
121
+ # Channel.Close/Channel.Close-Ok
122
+ socket.encode Channel::Close, 1, 200, "bye", 0, 0
123
+ channel_close_ok_response = socket.decode
124
+ end
125
+ ensure
126
+ # Connection.Close/Connection.Close-Ok
127
+ socket.encode Connection::Close, 200, "", 0, 0
128
+ close_ok_response = socket.decode
129
+ end
130
+ rescue Exception => exception
131
+ STDERR.puts "\n\e[1;31m[#{exception.class}] #{exception.message}\e[0m"
132
+ exception.backtrace.each do |line|
133
+ line = "\e[0;36m#{line}\e[0m" if line.match(Regexp::quote(File.basename(__FILE__)))
134
+ STDERR.puts " - " + line
135
+ end
136
+ exit 1 # Yes, this works with the ensure block, even though the rescue block run first. Magic.
137
+ ensure
138
+ socket.close
139
+ end
data/irb.rb CHANGED
@@ -1,9 +1,81 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: utf-8
2
+ # encoding: binary
3
+
4
+ # This file is supposed to make inspecting AMQ protocol easier.
5
+
6
+ # How does it work:
7
+ # 1) This file is executed.
8
+ # 2) We load irb, redefine where IRB looks for .irbrc and start IRB.
9
+ # 3) IRB loads .irbrc, which we redefined, so it loads this file again.
10
+ # However now the second branch of "if __FILE__ == $0" gets executed,
11
+ # so it runs our custom code which loads the original .irbrc and then
12
+ # it redefines some IRB settings. In this case it add IRB hook which
13
+ # is executed after IRB is started.
14
+
15
+ # Although it looks unnecessarily complicated, I can't see any easier
16
+ # solution to this problem in case that you need to patch original settings.
17
+ # Obviously in case you don't have the need, you'll be happy with simple:
18
+
19
+ # require "irb"
20
+ #
21
+ # require_relative "lib/amq/protocol/client.rb"
22
+ # include AMQ::Protocol
23
+ #
24
+ # IRB.start(__FILE__)
3
25
 
4
26
  require "irb"
5
- require_relative "lib/amq/protocol.rb"
6
27
 
7
- include AMQ::Protocol
28
+ if __FILE__ == $0
29
+ puts "~ Using #{__FILE__} as an executable ..."
30
+
31
+
32
+ def IRB.rc_file_generators
33
+ yield Proc.new { |_| __FILE__ }
34
+ end
35
+
36
+ IRB.start(__FILE__)
37
+ else
38
+ begin
39
+ irbrc = File.join(ENV["HOME"], ".irbrc")
40
+ puts "~ Using #{__FILE__} as a custom .irbrc .."
41
+ puts "~ Loading original #{irbrc} ..."
42
+ load irbrc
43
+
44
+ file = ENV["FILE"] || "client"
45
+ require_relative "lib/amq/protocol/#{file}.rb"
46
+ include AMQ::Protocol
47
+
48
+ begin
49
+ require "amq/client/io/string"
50
+
51
+ class AMQ::Protocol::Frame
52
+ def self.decode(string)
53
+ AMQ::Client::StringAdapter::Frame.decode(string)
54
+ end
55
+ end
56
+ rescue LoadError
57
+ warn "~ AMQ Client isn't available."
58
+ end
59
+
60
+ # "0123456789".chunks(1, 1, 2, 3)
61
+ # => ["0", "1", "23", "456"]
62
+ class String
63
+ def chunks(*parts)
64
+ offset = 0
65
+ parts.map do |number_of_characters|
66
+ self[offset..(offset + number_of_characters - 1)].tap do
67
+ offset += number_of_characters
68
+ end
69
+ end << self[offset..-1]
70
+ end
71
+ end
72
+
73
+ require "stringio"
8
74
 
9
- IRB.start(__FILE__)
75
+ def fd(data)
76
+ Frame.decode(StringIO.new(data))
77
+ end
78
+ rescue Exception => exception # it just discards all the exceptions!
79
+ abort exception.message + "\n - " + exception.backtrace.join("\n - ")
80
+ end
81
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: binary
2
+
3
+ # Ruby doesn't support pack to/unpack from
4
+ # 64bit string in network byte order.
5
+ module AMQ
6
+ module Hacks
7
+ BIG_ENDIAN = ([1].pack("s") == "\x00\x01")
8
+ Q = "Q".freeze
9
+
10
+ if BIG_ENDIAN
11
+ def self.pack_64_big_endian(long_long)
12
+ [long_long].pack(Q)
13
+ end
14
+
15
+ def self.unpack_64_big_endian(data)
16
+ data.unpack(Q)
17
+ end
18
+ else
19
+ def self.pack_64_big_endian(long_long)
20
+ result = [long_long].pack(Q)
21
+ result.chars.to_a.reverse.join
22
+ end
23
+
24
+ def self.unpack_64_big_endian(data)
25
+ data = data.chars.to_a.reverse.join
26
+ data.unpack(Q)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ # AMQ::Hacks.pack_64_big_endian(17)
33
+ # AMQ::Hacks.unpack_64_big_endian("\x00\x00\x00\x00\x00\x00\x00\x11")
@@ -1,1473 +1,4 @@
1
- # encoding: binary
1
+ # -*- coding: utf-8 -*-
2
2
 
3
- # THIS IS AN AUTOGENERATED FILE, DO NOT MODIFY
4
- # IT DIRECTLY ! FOR CHANGES, PLEASE UPDATE CODEGEN.PY
5
- # IN THE ROOT DIRECTORY OF THE AMQP-PROTOCOL REPOSITORY.
6
-
7
- require_relative "protocol/table.rb"
8
- require_relative "protocol/frame.rb"
9
-
10
- module AMQ
11
- module Protocol
12
- PROTOCOL_VERSION = "0.9.1"
13
- PREAMBLE = "AMQP\x00\x00\x09\x01"
14
- DEFAULT_PORT = 5672
15
-
16
- # caching
17
- EMPTY_STRING = "".freeze
18
-
19
- # @version 0.0.1
20
- # @return [Array] Collection of subclasses of AMQ::Protocol::Class.
21
- def self.classes
22
- Class.classes
23
- end
24
-
25
- # @version 0.0.1
26
- # @return [Array] Collection of subclasses of AMQ::Protocol::Method.
27
- def self.methods
28
- Method.methods
29
- end
30
-
31
- class Error < StandardError
32
- def initialize(message = "AMQP error")
33
- super(message)
34
- end
35
- end
36
-
37
- class ConnectionError < Error
38
- def initialize(types)
39
- super("Must be one of #{types.inspect}")
40
- end
41
- end
42
-
43
- # We don"t instantiate the following classes,
44
- # as we don"t actually need any per-instance state.
45
- # Also, this is pretty low-level functionality,
46
- # hence it should have a reasonable performance.
47
- # As everyone knows, garbage collector in MRI performs
48
- # really badly, which is another good reason for
49
- # not creating any objects, but only use class as
50
- # a struct. Creating classes is quite expensive though,
51
- # but here the inheritance comes handy and mainly
52
- # as we can"t simply make a reference to a function,
53
- # we can"t use a hash or an object. I"ve been also
54
- # considering to have just a bunch of methods, but
55
- # here"s the problem, that after we"d require this file,
56
- # all these methods would become global which would
57
- # be a bad, bad thing to do.
58
- class Class
59
- @@classes = Array.new
60
-
61
- def self.method
62
- @method
63
- end
64
-
65
- def self.name
66
- @name
67
- end
68
-
69
- def self.inherited(base)
70
- if self == Class
71
- @@classes << base
72
- end
73
- end
74
-
75
- def self.classes
76
- @@classes
77
- end
78
- end
79
-
80
- class Method
81
- @@methods = Array.new
82
- def self.method
83
- @method
84
- end
85
-
86
- def self.name
87
- @name
88
- end
89
-
90
- def self.index
91
- @index
92
- end
93
-
94
- def self.inherited(base)
95
- if self == Method
96
- @@methods << base
97
- end
98
- end
99
-
100
- def self.methods
101
- @@methods
102
- end
103
-
104
- def self.split_headers(user_headers, properties_set)
105
- properties, headers = {}, {}
106
- user_headers.iteritems.each do |key, value|
107
- if properties_set.has_key?(key)
108
- properties[key] = value
109
- else
110
- headers[key] = value
111
- end
112
- end
113
-
114
- return props, headers
115
- end
116
-
117
- def self.encode_body(body, frame_size)
118
- # Spec is broken: Our errata says that it does define
119
- # something, but it just doesn"t relate do method and
120
- # properties frames. Which makes it, well, suboptimal.
121
- # https://dev.rabbitmq.com/wiki/Amqp091Errata#section_11
122
- limit = frame_size - 7 - 1
123
-
124
- Array.new.tap do |array|
125
- while body
126
- payload, body = body[0..limit], body[limit..-1]
127
- array << [0x03, payload]
128
- end
129
- end
130
- end
131
-
132
- # We can return different:
133
- # - instantiate given subclass of Method
134
- # - create an OpenStruct object
135
- # - create a hash
136
- # - yield params into the block rather than just return
137
- # @api plugin
138
- def self.instantiate(*args, &block)
139
- self.new(*args, &block)
140
- # or OpenStruct.new(args.first)
141
- # or args.first
142
- # or block.call(*args)
143
- end
144
- end
145
-
146
- class Connection < Class
147
- @name = "connection"
148
- @method = 10
149
-
150
- class Start < Method
151
- @name = "connection.start"
152
- @method = 10
153
- @index = 0x000A000A # 10, 10, 655370
154
-
155
- # @return
156
- def self.decode(data)
157
- offset = 0
158
- version_major = data[offset...(offset + 1)].unpack("c").first
159
- offset += 1
160
- version_minor = data[offset...(offset + 1)].unpack("c").first
161
- offset += 1
162
- table_length = Table.length(data[offset..(offset + 4)])
163
- server_properties = Table.decode(data[offset..table_length])
164
- length = data[offset..(offset + 4)].unpack("N").first
165
- offset += 4
166
- mechanisms = data[offset..(offset + length)]
167
- offset += length
168
- length = data[offset..(offset + 4)].unpack("N").first
169
- offset += 4
170
- locales = data[offset..(offset + length)]
171
- offset += length
172
- self.new(version_major, version_minor, server_properties, mechanisms, locales)
173
- end
174
-
175
- attr_reader :version_major, :version_minor, :server_properties, :mechanisms, :locales
176
- def initialize(version_major, version_minor, server_properties, mechanisms, locales)
177
- @version_major = version_major
178
- @version_minor = version_minor
179
- @server_properties = server_properties
180
- @mechanisms = mechanisms
181
- @locales = locales
182
- end
183
- end
184
-
185
- class StartOk < Method
186
- @name = "connection.start-ok"
187
- @method = 11
188
- @index = 0x000A000B # 10, 11, 655371
189
-
190
- # @return
191
- # ["client_properties = nil", "mechanism = "PLAIN"", "response = nil", "locale = "en_US""]
192
- def self.encode(client_properties, mechanism, response, locale)
193
- pieces = []
194
- pieces << [10, 11].pack("n2")
195
- pieces << AMQ::Protocol::Table.encode(client_properties)
196
- pieces << mechanism.bytesize.chr
197
- pieces << mechanism
198
- pieces << [response.bytesize].pack("N")
199
- pieces << response
200
- pieces << locale.bytesize.chr
201
- pieces << locale
202
- pieces.join("")
203
- end
204
- end
205
-
206
- class Secure < Method
207
- @name = "connection.secure"
208
- @method = 20
209
- @index = 0x000A0014 # 10, 20, 655380
210
-
211
- # @return
212
- def self.decode(data)
213
- offset = 0
214
- length = data[offset..(offset + 4)].unpack("N").first
215
- offset += 4
216
- challenge = data[offset..(offset + length)]
217
- offset += length
218
- self.new(challenge)
219
- end
220
-
221
- attr_reader :challenge
222
- def initialize(challenge)
223
- @challenge = challenge
224
- end
225
- end
226
-
227
- class SecureOk < Method
228
- @name = "connection.secure-ok"
229
- @method = 21
230
- @index = 0x000A0015 # 10, 21, 655381
231
-
232
- # @return
233
- # ["response = nil"]
234
- def self.encode(response)
235
- pieces = []
236
- pieces << [10, 21].pack("n2")
237
- pieces << [response.bytesize].pack("N")
238
- pieces << response
239
- pieces.join("")
240
- end
241
- end
242
-
243
- class Tune < Method
244
- @name = "connection.tune"
245
- @method = 30
246
- @index = 0x000A001E # 10, 30, 655390
247
-
248
- # @return
249
- def self.decode(data)
250
- offset = 0
251
- channel_max = data[offset..(offset + 2)].unpack("n").first
252
- offset += 2
253
- frame_max = data[offset..(offset + 4)].unpack("N").first
254
- offset += 4
255
- heartbeat = data[offset..(offset + 2)].unpack("n").first
256
- offset += 2
257
- self.new(channel_max, frame_max, heartbeat)
258
- end
259
-
260
- attr_reader :channel_max, :frame_max, :heartbeat
261
- def initialize(channel_max, frame_max, heartbeat)
262
- @channel_max = channel_max
263
- @frame_max = frame_max
264
- @heartbeat = heartbeat
265
- end
266
- end
267
-
268
- class TuneOk < Method
269
- @name = "connection.tune-ok"
270
- @method = 31
271
- @index = 0x000A001F # 10, 31, 655391
272
-
273
- # @return
274
- # ["channel_max = false", "frame_max = false", "heartbeat = false"]
275
- def self.encode(channel_max, frame_max, heartbeat)
276
- pieces = []
277
- pieces << [10, 31].pack("n2")
278
- pieces << [channel_max].pack("n")
279
- pieces << [frame_max].pack("N")
280
- pieces << [heartbeat].pack("n")
281
- pieces.join("")
282
- end
283
- end
284
-
285
- class Open < Method
286
- @name = "connection.open"
287
- @method = 40
288
- @index = 0x000A0028 # 10, 40, 655400
289
-
290
- # @return
291
- # ["virtual_host = "/"", "capabilities = """, "insist = false"]
292
- def self.encode(virtual_host, capabilities, insist)
293
- pieces = []
294
- pieces << [10, 40].pack("n2")
295
- pieces << virtual_host.bytesize.chr
296
- pieces << virtual_host
297
- pieces << capabilities.bytesize.chr
298
- pieces << capabilities
299
- bit_buffer = 0
300
- bit_buffer = bit_buffer | (1 << 0) if insist
301
- pieces.join("")
302
- end
303
- end
304
-
305
- class OpenOk < Method
306
- @name = "connection.open-ok"
307
- @method = 41
308
- @index = 0x000A0029 # 10, 41, 655401
309
-
310
- # @return
311
- def self.decode(data)
312
- offset = 0
313
- length = data[offset..(offset + 1)].unpack("N")[0]
314
- offset += 1
315
- known_hosts = data[offset..(offset + length)]
316
- offset += length
317
- self.new(known_hosts)
318
- end
319
-
320
- attr_reader :known_hosts
321
- def initialize(known_hosts)
322
- @known_hosts = known_hosts
323
- end
324
- end
325
-
326
- class Close < Method
327
- @name = "connection.close"
328
- @method = 50
329
- @index = 0x000A0032 # 10, 50, 655410
330
-
331
- # @return
332
- def self.decode(data)
333
- offset = 0
334
- reply_code = data[offset..(offset + 2)].unpack("n").first
335
- offset += 2
336
- length = data[offset..(offset + 1)].unpack("N")[0]
337
- offset += 1
338
- reply_text = data[offset..(offset + length)]
339
- offset += length
340
- class_id = data[offset..(offset + 2)].unpack("n").first
341
- offset += 2
342
- method_id = data[offset..(offset + 2)].unpack("n").first
343
- offset += 2
344
- self.new(reply_code, reply_text, class_id, method_id)
345
- end
346
-
347
- attr_reader :reply_code, :reply_text, :class_id, :method_id
348
- def initialize(reply_code, reply_text, class_id, method_id)
349
- @reply_code = reply_code
350
- @reply_text = reply_text
351
- @class_id = class_id
352
- @method_id = method_id
353
- end
354
-
355
- # @return
356
- # ["reply_code = nil", "reply_text = """, "class_id = nil", "method_id = nil"]
357
- def self.encode(reply_code, reply_text, class_id, method_id)
358
- pieces = []
359
- pieces << [10, 50].pack("n2")
360
- pieces << [reply_code].pack("n")
361
- pieces << reply_text.bytesize.chr
362
- pieces << reply_text
363
- pieces << [class_id].pack("n")
364
- pieces << [method_id].pack("n")
365
- pieces.join("")
366
- end
367
- end
368
-
369
- class CloseOk < Method
370
- @name = "connection.close-ok"
371
- @method = 51
372
- @index = 0x000A0033 # 10, 51, 655411
373
-
374
- # @return
375
- def self.decode(data)
376
- offset = 0
377
- self.new()
378
- end
379
-
380
- attr_reader
381
- def initialize()
382
- end
383
-
384
- # @return
385
- # []
386
- def self.encode()
387
- pieces = []
388
- pieces << [10, 51].pack("n2")
389
- pieces.join("")
390
- end
391
- end
392
- end
393
-
394
- class Channel < Class
395
- @name = "channel"
396
- @method = 20
397
-
398
- class Open < Method
399
- @name = "channel.open"
400
- @method = 10
401
- @index = 0x0014000A # 20, 10, 1310730
402
-
403
- # @return
404
- # ["out_of_band = """]
405
- def self.encode(out_of_band)
406
- pieces = []
407
- pieces << [20, 10].pack("n2")
408
- pieces << out_of_band.bytesize.chr
409
- pieces << out_of_band
410
- pieces.join("")
411
- end
412
- end
413
-
414
- class OpenOk < Method
415
- @name = "channel.open-ok"
416
- @method = 11
417
- @index = 0x0014000B # 20, 11, 1310731
418
-
419
- # @return
420
- def self.decode(data)
421
- offset = 0
422
- length = data[offset..(offset + 4)].unpack("N").first
423
- offset += 4
424
- channel_id = data[offset..(offset + length)]
425
- offset += length
426
- self.new(channel_id)
427
- end
428
-
429
- attr_reader :channel_id
430
- def initialize(channel_id)
431
- @channel_id = channel_id
432
- end
433
- end
434
-
435
- class Flow < Method
436
- @name = "channel.flow"
437
- @method = 20
438
- @index = 0x00140014 # 20, 20, 1310740
439
-
440
- # @return
441
- def self.decode(data)
442
- offset = 0
443
- bit_buffer = data[offset..(offset + 1)].unpack("c").first
444
- offset += 1
445
- active = (bit_buffer & (1 << 0)) != 0
446
- self.new(active)
447
- end
448
-
449
- attr_reader :active
450
- def initialize(active)
451
- @active = active
452
- end
453
-
454
- # @return
455
- # ["active = nil"]
456
- def self.encode(active)
457
- pieces = []
458
- pieces << [20, 20].pack("n2")
459
- bit_buffer = 0
460
- bit_buffer = bit_buffer | (1 << 0) if active
461
- pieces.join("")
462
- end
463
- end
464
-
465
- class FlowOk < Method
466
- @name = "channel.flow-ok"
467
- @method = 21
468
- @index = 0x00140015 # 20, 21, 1310741
469
-
470
- # @return
471
- def self.decode(data)
472
- offset = 0
473
- bit_buffer = data[offset..(offset + 1)].unpack("c").first
474
- offset += 1
475
- active = (bit_buffer & (1 << 0)) != 0
476
- self.new(active)
477
- end
478
-
479
- attr_reader :active
480
- def initialize(active)
481
- @active = active
482
- end
483
-
484
- # @return
485
- # ["active = nil"]
486
- def self.encode(active)
487
- pieces = []
488
- pieces << [20, 21].pack("n2")
489
- bit_buffer = 0
490
- bit_buffer = bit_buffer | (1 << 0) if active
491
- pieces.join("")
492
- end
493
- end
494
-
495
- class Close < Method
496
- @name = "channel.close"
497
- @method = 40
498
- @index = 0x00140028 # 20, 40, 1310760
499
-
500
- # @return
501
- def self.decode(data)
502
- offset = 0
503
- reply_code = data[offset..(offset + 2)].unpack("n").first
504
- offset += 2
505
- length = data[offset..(offset + 1)].unpack("N")[0]
506
- offset += 1
507
- reply_text = data[offset..(offset + length)]
508
- offset += length
509
- class_id = data[offset..(offset + 2)].unpack("n").first
510
- offset += 2
511
- method_id = data[offset..(offset + 2)].unpack("n").first
512
- offset += 2
513
- self.new(reply_code, reply_text, class_id, method_id)
514
- end
515
-
516
- attr_reader :reply_code, :reply_text, :class_id, :method_id
517
- def initialize(reply_code, reply_text, class_id, method_id)
518
- @reply_code = reply_code
519
- @reply_text = reply_text
520
- @class_id = class_id
521
- @method_id = method_id
522
- end
523
-
524
- # @return
525
- # ["reply_code = nil", "reply_text = """, "class_id = nil", "method_id = nil"]
526
- def self.encode(reply_code, reply_text, class_id, method_id)
527
- pieces = []
528
- pieces << [20, 40].pack("n2")
529
- pieces << [reply_code].pack("n")
530
- pieces << reply_text.bytesize.chr
531
- pieces << reply_text
532
- pieces << [class_id].pack("n")
533
- pieces << [method_id].pack("n")
534
- pieces.join("")
535
- end
536
- end
537
-
538
- class CloseOk < Method
539
- @name = "channel.close-ok"
540
- @method = 41
541
- @index = 0x00140029 # 20, 41, 1310761
542
-
543
- # @return
544
- def self.decode(data)
545
- offset = 0
546
- self.new()
547
- end
548
-
549
- attr_reader
550
- def initialize()
551
- end
552
-
553
- # @return
554
- # []
555
- def self.encode()
556
- pieces = []
557
- pieces << [20, 41].pack("n2")
558
- pieces.join("")
559
- end
560
- end
561
- end
562
-
563
- class Exchange < Class
564
- @name = "exchange"
565
- @method = 40
566
-
567
- class Declare < Method
568
- @name = "exchange.declare"
569
- @method = 10
570
- @index = 0x0028000A # 40, 10, 2621450
571
-
572
- # @return
573
- # ["ticket = false", "exchange = nil", "type = "direct"", "passive = false", "durable = false", "auto_delete = false", "internal = false", "nowait = false", "arguments = {}"]
574
- def self.encode(ticket, exchange, type, passive, durable, auto_delete, internal, nowait, arguments)
575
- pieces = []
576
- pieces << [40, 10].pack("n2")
577
- pieces << [ticket].pack("n")
578
- pieces << exchange.bytesize.chr
579
- pieces << exchange
580
- pieces << type.bytesize.chr
581
- pieces << type
582
- bit_buffer = 0
583
- bit_buffer = bit_buffer | (1 << 0) if passive
584
- bit_buffer = bit_buffer | (1 << 1) if durable
585
- bit_buffer = bit_buffer | (1 << 2) if auto_delete
586
- bit_buffer = bit_buffer | (1 << 3) if internal
587
- bit_buffer = bit_buffer | (1 << 4) if nowait
588
- pieces << AMQ::Protocol::Table.encode(arguments)
589
- pieces.join("")
590
- end
591
- end
592
-
593
- class DeclareOk < Method
594
- @name = "exchange.declare-ok"
595
- @method = 11
596
- @index = 0x0028000B # 40, 11, 2621451
597
-
598
- # @return
599
- def self.decode(data)
600
- offset = 0
601
- self.new()
602
- end
603
-
604
- attr_reader
605
- def initialize()
606
- end
607
- end
608
-
609
- class Delete < Method
610
- @name = "exchange.delete"
611
- @method = 20
612
- @index = 0x00280014 # 40, 20, 2621460
613
-
614
- # @return
615
- # ["ticket = false", "exchange = nil", "if_unused = false", "nowait = false"]
616
- def self.encode(ticket, exchange, if_unused, nowait)
617
- pieces = []
618
- pieces << [40, 20].pack("n2")
619
- pieces << [ticket].pack("n")
620
- pieces << exchange.bytesize.chr
621
- pieces << exchange
622
- bit_buffer = 0
623
- bit_buffer = bit_buffer | (1 << 0) if if_unused
624
- bit_buffer = bit_buffer | (1 << 1) if nowait
625
- pieces.join("")
626
- end
627
- end
628
-
629
- class DeleteOk < Method
630
- @name = "exchange.delete-ok"
631
- @method = 21
632
- @index = 0x00280015 # 40, 21, 2621461
633
-
634
- # @return
635
- def self.decode(data)
636
- offset = 0
637
- self.new()
638
- end
639
-
640
- attr_reader
641
- def initialize()
642
- end
643
- end
644
-
645
- class Bind < Method
646
- @name = "exchange.bind"
647
- @method = 30
648
- @index = 0x0028001E # 40, 30, 2621470
649
-
650
- # @return
651
- # ["ticket = false", "destination = nil", "source = nil", "routing_key = """, "nowait = false", "arguments = {}"]
652
- def self.encode(ticket, destination, source, routing_key, nowait, arguments)
653
- pieces = []
654
- pieces << [40, 30].pack("n2")
655
- pieces << [ticket].pack("n")
656
- pieces << destination.bytesize.chr
657
- pieces << destination
658
- pieces << source.bytesize.chr
659
- pieces << source
660
- pieces << routing_key.bytesize.chr
661
- pieces << routing_key
662
- bit_buffer = 0
663
- bit_buffer = bit_buffer | (1 << 0) if nowait
664
- pieces << AMQ::Protocol::Table.encode(arguments)
665
- pieces.join("")
666
- end
667
- end
668
-
669
- class BindOk < Method
670
- @name = "exchange.bind-ok"
671
- @method = 31
672
- @index = 0x0028001F # 40, 31, 2621471
673
-
674
- # @return
675
- def self.decode(data)
676
- offset = 0
677
- self.new()
678
- end
679
-
680
- attr_reader
681
- def initialize()
682
- end
683
- end
684
-
685
- class Unbind < Method
686
- @name = "exchange.unbind"
687
- @method = 40
688
- @index = 0x00280028 # 40, 40, 2621480
689
-
690
- # @return
691
- # ["ticket = false", "destination = nil", "source = nil", "routing_key = """, "nowait = false", "arguments = {}"]
692
- def self.encode(ticket, destination, source, routing_key, nowait, arguments)
693
- pieces = []
694
- pieces << [40, 40].pack("n2")
695
- pieces << [ticket].pack("n")
696
- pieces << destination.bytesize.chr
697
- pieces << destination
698
- pieces << source.bytesize.chr
699
- pieces << source
700
- pieces << routing_key.bytesize.chr
701
- pieces << routing_key
702
- bit_buffer = 0
703
- bit_buffer = bit_buffer | (1 << 0) if nowait
704
- pieces << AMQ::Protocol::Table.encode(arguments)
705
- pieces.join("")
706
- end
707
- end
708
-
709
- class UnbindOk < Method
710
- @name = "exchange.unbind-ok"
711
- @method = 51
712
- @index = 0x00280033 # 40, 51, 2621491
713
-
714
- # @return
715
- def self.decode(data)
716
- offset = 0
717
- self.new()
718
- end
719
-
720
- attr_reader
721
- def initialize()
722
- end
723
- end
724
- end
725
-
726
- class Queue < Class
727
- @name = "queue"
728
- @method = 50
729
-
730
- class Declare < Method
731
- @name = "queue.declare"
732
- @method = 10
733
- @index = 0x0032000A # 50, 10, 3276810
734
-
735
- # @return
736
- # ["ticket = false", "queue = """, "passive = false", "durable = false", "exclusive = false", "auto_delete = false", "nowait = false", "arguments = {}"]
737
- def self.encode(ticket, queue, passive, durable, exclusive, auto_delete, nowait, arguments)
738
- pieces = []
739
- pieces << [50, 10].pack("n2")
740
- pieces << [ticket].pack("n")
741
- pieces << queue.bytesize.chr
742
- pieces << queue
743
- bit_buffer = 0
744
- bit_buffer = bit_buffer | (1 << 0) if passive
745
- bit_buffer = bit_buffer | (1 << 1) if durable
746
- bit_buffer = bit_buffer | (1 << 2) if exclusive
747
- bit_buffer = bit_buffer | (1 << 3) if auto_delete
748
- bit_buffer = bit_buffer | (1 << 4) if nowait
749
- pieces << AMQ::Protocol::Table.encode(arguments)
750
- pieces.join("")
751
- end
752
- end
753
-
754
- class DeclareOk < Method
755
- @name = "queue.declare-ok"
756
- @method = 11
757
- @index = 0x0032000B # 50, 11, 3276811
758
-
759
- # @return
760
- def self.decode(data)
761
- offset = 0
762
- length = data[offset..(offset + 1)].unpack("N")[0]
763
- offset += 1
764
- queue = data[offset..(offset + length)]
765
- offset += length
766
- message_count = data[offset..(offset + 4)].unpack("N").first
767
- offset += 4
768
- consumer_count = data[offset..(offset + 4)].unpack("N").first
769
- offset += 4
770
- self.new(queue, message_count, consumer_count)
771
- end
772
-
773
- attr_reader :queue, :message_count, :consumer_count
774
- def initialize(queue, message_count, consumer_count)
775
- @queue = queue
776
- @message_count = message_count
777
- @consumer_count = consumer_count
778
- end
779
- end
780
-
781
- class Bind < Method
782
- @name = "queue.bind"
783
- @method = 20
784
- @index = 0x00320014 # 50, 20, 3276820
785
-
786
- # @return
787
- # ["ticket = false", "queue = nil", "exchange = nil", "routing_key = """, "nowait = false", "arguments = {}"]
788
- def self.encode(ticket, queue, exchange, routing_key, nowait, arguments)
789
- pieces = []
790
- pieces << [50, 20].pack("n2")
791
- pieces << [ticket].pack("n")
792
- pieces << queue.bytesize.chr
793
- pieces << queue
794
- pieces << exchange.bytesize.chr
795
- pieces << exchange
796
- pieces << routing_key.bytesize.chr
797
- pieces << routing_key
798
- bit_buffer = 0
799
- bit_buffer = bit_buffer | (1 << 0) if nowait
800
- pieces << AMQ::Protocol::Table.encode(arguments)
801
- pieces.join("")
802
- end
803
- end
804
-
805
- class BindOk < Method
806
- @name = "queue.bind-ok"
807
- @method = 21
808
- @index = 0x00320015 # 50, 21, 3276821
809
-
810
- # @return
811
- def self.decode(data)
812
- offset = 0
813
- self.new()
814
- end
815
-
816
- attr_reader
817
- def initialize()
818
- end
819
- end
820
-
821
- class Purge < Method
822
- @name = "queue.purge"
823
- @method = 30
824
- @index = 0x0032001E # 50, 30, 3276830
825
-
826
- # @return
827
- # ["ticket = false", "queue = nil", "nowait = false"]
828
- def self.encode(ticket, queue, nowait)
829
- pieces = []
830
- pieces << [50, 30].pack("n2")
831
- pieces << [ticket].pack("n")
832
- pieces << queue.bytesize.chr
833
- pieces << queue
834
- bit_buffer = 0
835
- bit_buffer = bit_buffer | (1 << 0) if nowait
836
- pieces.join("")
837
- end
838
- end
839
-
840
- class PurgeOk < Method
841
- @name = "queue.purge-ok"
842
- @method = 31
843
- @index = 0x0032001F # 50, 31, 3276831
844
-
845
- # @return
846
- def self.decode(data)
847
- offset = 0
848
- message_count = data[offset..(offset + 4)].unpack("N").first
849
- offset += 4
850
- self.new(message_count)
851
- end
852
-
853
- attr_reader :message_count
854
- def initialize(message_count)
855
- @message_count = message_count
856
- end
857
- end
858
-
859
- class Delete < Method
860
- @name = "queue.delete"
861
- @method = 40
862
- @index = 0x00320028 # 50, 40, 3276840
863
-
864
- # @return
865
- # ["ticket = false", "queue = nil", "if_unused = false", "if_empty = false", "nowait = false"]
866
- def self.encode(ticket, queue, if_unused, if_empty, nowait)
867
- pieces = []
868
- pieces << [50, 40].pack("n2")
869
- pieces << [ticket].pack("n")
870
- pieces << queue.bytesize.chr
871
- pieces << queue
872
- bit_buffer = 0
873
- bit_buffer = bit_buffer | (1 << 0) if if_unused
874
- bit_buffer = bit_buffer | (1 << 1) if if_empty
875
- bit_buffer = bit_buffer | (1 << 2) if nowait
876
- pieces.join("")
877
- end
878
- end
879
-
880
- class DeleteOk < Method
881
- @name = "queue.delete-ok"
882
- @method = 41
883
- @index = 0x00320029 # 50, 41, 3276841
884
-
885
- # @return
886
- def self.decode(data)
887
- offset = 0
888
- message_count = data[offset..(offset + 4)].unpack("N").first
889
- offset += 4
890
- self.new(message_count)
891
- end
892
-
893
- attr_reader :message_count
894
- def initialize(message_count)
895
- @message_count = message_count
896
- end
897
- end
898
-
899
- class Unbind < Method
900
- @name = "queue.unbind"
901
- @method = 50
902
- @index = 0x00320032 # 50, 50, 3276850
903
-
904
- # @return
905
- # ["ticket = false", "queue = nil", "exchange = nil", "routing_key = """, "arguments = {}"]
906
- def self.encode(ticket, queue, exchange, routing_key, arguments)
907
- pieces = []
908
- pieces << [50, 50].pack("n2")
909
- pieces << [ticket].pack("n")
910
- pieces << queue.bytesize.chr
911
- pieces << queue
912
- pieces << exchange.bytesize.chr
913
- pieces << exchange
914
- pieces << routing_key.bytesize.chr
915
- pieces << routing_key
916
- pieces << AMQ::Protocol::Table.encode(arguments)
917
- pieces.join("")
918
- end
919
- end
920
-
921
- class UnbindOk < Method
922
- @name = "queue.unbind-ok"
923
- @method = 51
924
- @index = 0x00320033 # 50, 51, 3276851
925
-
926
- # @return
927
- def self.decode(data)
928
- offset = 0
929
- self.new()
930
- end
931
-
932
- attr_reader
933
- def initialize()
934
- end
935
- end
936
- end
937
-
938
- class Basic < Class
939
- @name = "basic"
940
- @method = 60
941
-
942
- PROPERTIES = [
943
- :content_type, # shortstr
944
- :content_encoding, # shortstr
945
- :headers, # table
946
- :delivery_mode, # octet
947
- :priority, # octet
948
- :correlation_id, # shortstr
949
- :reply_to, # shortstr
950
- :expiration, # shortstr
951
- :message_id, # shortstr
952
- :timestamp, # timestamp
953
- :type, # shortstr
954
- :user_id, # shortstr
955
- :app_id, # shortstr
956
- :cluster_id, # shortstr
957
- ]
958
-
959
- # 1 << 15
960
- def self.encode_content_type(value)
961
- pieces = []
962
- pieces << result.bytesize.chr
963
- pieces << result
964
- [0, 0x8000, result]
965
- end
966
-
967
- # 1 << 14
968
- def self.encode_content_encoding(value)
969
- pieces = []
970
- pieces << result.bytesize.chr
971
- pieces << result
972
- [1, 0x4000, result]
973
- end
974
-
975
- # 1 << 13
976
- def self.encode_headers(value)
977
- pieces = []
978
- pieces << AMQ::Protocol::Table.encode(result)
979
- [2, 0x2000, result]
980
- end
981
-
982
- # 1 << 12
983
- def self.encode_delivery_mode(value)
984
- pieces = []
985
- pieces << [result].pack("B")
986
- [3, 0x1000, result]
987
- end
988
-
989
- # 1 << 11
990
- def self.encode_priority(value)
991
- pieces = []
992
- pieces << [result].pack("B")
993
- [4, 0x0800, result]
994
- end
995
-
996
- # 1 << 10
997
- def self.encode_correlation_id(value)
998
- pieces = []
999
- pieces << result.bytesize.chr
1000
- pieces << result
1001
- [5, 0x0400, result]
1002
- end
1003
-
1004
- # 1 << 9
1005
- def self.encode_reply_to(value)
1006
- pieces = []
1007
- pieces << result.bytesize.chr
1008
- pieces << result
1009
- [6, 0x0200, result]
1010
- end
1011
-
1012
- # 1 << 8
1013
- def self.encode_expiration(value)
1014
- pieces = []
1015
- pieces << result.bytesize.chr
1016
- pieces << result
1017
- [7, 0x0100, result]
1018
- end
1019
-
1020
- # 1 << 7
1021
- def self.encode_message_id(value)
1022
- pieces = []
1023
- pieces << result.bytesize.chr
1024
- pieces << result
1025
- [8, 0x0080, result]
1026
- end
1027
-
1028
- # 1 << 6
1029
- def self.encode_timestamp(value)
1030
- pieces = []
1031
- pieces << [result].pack(">Q")
1032
- [9, 0x0040, result]
1033
- end
1034
-
1035
- # 1 << 5
1036
- def self.encode_type(value)
1037
- pieces = []
1038
- pieces << result.bytesize.chr
1039
- pieces << result
1040
- [10, 0x0020, result]
1041
- end
1042
-
1043
- # 1 << 4
1044
- def self.encode_user_id(value)
1045
- pieces = []
1046
- pieces << result.bytesize.chr
1047
- pieces << result
1048
- [11, 0x0010, result]
1049
- end
1050
-
1051
- # 1 << 3
1052
- def self.encode_app_id(value)
1053
- pieces = []
1054
- pieces << result.bytesize.chr
1055
- pieces << result
1056
- [12, 0x0008, result]
1057
- end
1058
-
1059
- # 1 << 2
1060
- def self.encode_cluster_id(value)
1061
- pieces = []
1062
- pieces << result.bytesize.chr
1063
- pieces << result
1064
- [13, 0x0004, result]
1065
- end
1066
-
1067
- def self.encode_properties(body_size, properties)
1068
- pieces = Array.new(14) { AMQ::Protocol::EMPTY_STRING }
1069
- flags = 0
1070
-
1071
- properties.each do |key, value|
1072
- i, f, result = self.send(:"encode_#{key}", value)
1073
- flags |= f
1074
- pieces[i] = result
1075
- end
1076
-
1077
- result = [CLASS_BASIC, 0, body_size, flags].pack("!HHQH")
1078
- [0x02, result, pieces.join("")].join("")
1079
- end
1080
-
1081
- #def self.decode_properties
1082
- # print "def %s(data, offset):" % (c.decode,)
1083
- # print " props = {}"
1084
- # print " flags, = struct.unpack_from("!H", data, offset)"
1085
- # print " offset += 2"
1086
- # print " assert (flags & 0x01) == 0"
1087
- # for i, f in enumerate(c.fields):
1088
- # print " if (flags & 0x%04x): # 1 << %i" % (1 << (15-i), 15-i)
1089
- # fields = codegen_helpers.UnpackWrapper()
1090
- # fields.add(f.n, f.t)
1091
- # fields.do_print(" "*8, "props["%s"]")
1092
- # print " return props, offset"
1093
- #end
1094
-
1095
- class Qos < Method
1096
- @name = "basic.qos"
1097
- @method = 10
1098
- @index = 0x003C000A # 60, 10, 3932170
1099
-
1100
- # @return
1101
- # ["prefetch_size = false", "prefetch_count = false", "global = false"]
1102
- def self.encode(prefetch_size, prefetch_count, global)
1103
- pieces = []
1104
- pieces << [60, 10].pack("n2")
1105
- pieces << [prefetch_size].pack("N")
1106
- pieces << [prefetch_count].pack("n")
1107
- bit_buffer = 0
1108
- bit_buffer = bit_buffer | (1 << 0) if global
1109
- pieces.join("")
1110
- end
1111
- end
1112
-
1113
- class QosOk < Method
1114
- @name = "basic.qos-ok"
1115
- @method = 11
1116
- @index = 0x003C000B # 60, 11, 3932171
1117
-
1118
- # @return
1119
- def self.decode(data)
1120
- offset = 0
1121
- self.new()
1122
- end
1123
-
1124
- attr_reader
1125
- def initialize()
1126
- end
1127
- end
1128
-
1129
- class Consume < Method
1130
- @name = "basic.consume"
1131
- @method = 20
1132
- @index = 0x003C0014 # 60, 20, 3932180
1133
-
1134
- # @return
1135
- # ["ticket = false", "queue = nil", "consumer_tag = """, "no_local = false", "no_ack = false", "exclusive = false", "nowait = false", "arguments = {}"]
1136
- def self.encode(ticket, queue, consumer_tag, no_local, no_ack, exclusive, nowait, arguments)
1137
- pieces = []
1138
- pieces << [60, 20].pack("n2")
1139
- pieces << [ticket].pack("n")
1140
- pieces << queue.bytesize.chr
1141
- pieces << queue
1142
- pieces << consumer_tag.bytesize.chr
1143
- pieces << consumer_tag
1144
- bit_buffer = 0
1145
- bit_buffer = bit_buffer | (1 << 0) if no_local
1146
- bit_buffer = bit_buffer | (1 << 1) if no_ack
1147
- bit_buffer = bit_buffer | (1 << 2) if exclusive
1148
- bit_buffer = bit_buffer | (1 << 3) if nowait
1149
- pieces << AMQ::Protocol::Table.encode(arguments)
1150
- pieces.join("")
1151
- end
1152
- end
1153
-
1154
- class ConsumeOk < Method
1155
- @name = "basic.consume-ok"
1156
- @method = 21
1157
- @index = 0x003C0015 # 60, 21, 3932181
1158
-
1159
- # @return
1160
- def self.decode(data)
1161
- offset = 0
1162
- length = data[offset..(offset + 1)].unpack("N")[0]
1163
- offset += 1
1164
- consumer_tag = data[offset..(offset + length)]
1165
- offset += length
1166
- self.new(consumer_tag)
1167
- end
1168
-
1169
- attr_reader :consumer_tag
1170
- def initialize(consumer_tag)
1171
- @consumer_tag = consumer_tag
1172
- end
1173
- end
1174
-
1175
- class Cancel < Method
1176
- @name = "basic.cancel"
1177
- @method = 30
1178
- @index = 0x003C001E # 60, 30, 3932190
1179
-
1180
- # @return
1181
- # ["consumer_tag = nil", "nowait = false"]
1182
- def self.encode(consumer_tag, nowait)
1183
- pieces = []
1184
- pieces << [60, 30].pack("n2")
1185
- pieces << consumer_tag.bytesize.chr
1186
- pieces << consumer_tag
1187
- bit_buffer = 0
1188
- bit_buffer = bit_buffer | (1 << 0) if nowait
1189
- pieces.join("")
1190
- end
1191
- end
1192
-
1193
- class CancelOk < Method
1194
- @name = "basic.cancel-ok"
1195
- @method = 31
1196
- @index = 0x003C001F # 60, 31, 3932191
1197
-
1198
- # @return
1199
- def self.decode(data)
1200
- offset = 0
1201
- length = data[offset..(offset + 1)].unpack("N")[0]
1202
- offset += 1
1203
- consumer_tag = data[offset..(offset + length)]
1204
- offset += length
1205
- self.new(consumer_tag)
1206
- end
1207
-
1208
- attr_reader :consumer_tag
1209
- def initialize(consumer_tag)
1210
- @consumer_tag = consumer_tag
1211
- end
1212
- end
1213
-
1214
- class Publish < Method
1215
- @name = "basic.publish"
1216
- @method = 40
1217
- @index = 0x003C0028 # 60, 40, 3932200
1218
-
1219
- # @return
1220
- # ["ticket = false", "exchange = """, "routing_key = """, "mandatory = false", "immediate = false", "user_headers = nil", "payload = """, "frame_size = nil"]
1221
- def self.encode(ticket, exchange, routing_key, mandatory, immediate, user_headers, payload, frame_size)
1222
- pieces = []
1223
- pieces << [60, 40].pack("n2")
1224
- pieces << [ticket].pack("n")
1225
- pieces << exchange.bytesize.chr
1226
- pieces << exchange
1227
- pieces << routing_key.bytesize.chr
1228
- pieces << routing_key
1229
- bit_buffer = 0
1230
- bit_buffer = bit_buffer | (1 << 0) if mandatory
1231
- bit_buffer = bit_buffer | (1 << 1) if immediate
1232
- pieces.join("")
1233
- end
1234
- end
1235
-
1236
- class Return < Method
1237
- @name = "basic.return"
1238
- @method = 50
1239
- @index = 0x003C0032 # 60, 50, 3932210
1240
-
1241
- # @return
1242
- def self.decode(data)
1243
- offset = 0
1244
- reply_code = data[offset..(offset + 2)].unpack("n").first
1245
- offset += 2
1246
- length = data[offset..(offset + 1)].unpack("N")[0]
1247
- offset += 1
1248
- reply_text = data[offset..(offset + length)]
1249
- offset += length
1250
- length = data[offset..(offset + 1)].unpack("N")[0]
1251
- offset += 1
1252
- exchange = data[offset..(offset + length)]
1253
- offset += length
1254
- length = data[offset..(offset + 1)].unpack("N")[0]
1255
- offset += 1
1256
- routing_key = data[offset..(offset + length)]
1257
- offset += length
1258
- self.new(reply_code, reply_text, exchange, routing_key)
1259
- end
1260
-
1261
- attr_reader :reply_code, :reply_text, :exchange, :routing_key
1262
- def initialize(reply_code, reply_text, exchange, routing_key)
1263
- @reply_code = reply_code
1264
- @reply_text = reply_text
1265
- @exchange = exchange
1266
- @routing_key = routing_key
1267
- end
1268
- end
1269
-
1270
- class Deliver < Method
1271
- @name = "basic.deliver"
1272
- @method = 60
1273
- @index = 0x003C003C # 60, 60, 3932220
1274
-
1275
- # @return
1276
- def self.decode(data)
1277
- offset = 0
1278
- length = data[offset..(offset + 1)].unpack("N")[0]
1279
- offset += 1
1280
- consumer_tag = data[offset..(offset + length)]
1281
- offset += length
1282
- delivery_tag = data[offset..(offset + 8)].unpack("N2").first
1283
- offset += 8
1284
- bit_buffer = data[offset..(offset + 1)].unpack("c").first
1285
- offset += 1
1286
- redelivered = (bit_buffer & (1 << 0)) != 0
1287
- length = data[offset..(offset + 1)].unpack("N")[0]
1288
- offset += 1
1289
- exchange = data[offset..(offset + length)]
1290
- offset += length
1291
- length = data[offset..(offset + 1)].unpack("N")[0]
1292
- offset += 1
1293
- routing_key = data[offset..(offset + length)]
1294
- offset += length
1295
- self.new(consumer_tag, delivery_tag, redelivered, exchange, routing_key)
1296
- end
1297
-
1298
- attr_reader :consumer_tag, :delivery_tag, :redelivered, :exchange, :routing_key
1299
- def initialize(consumer_tag, delivery_tag, redelivered, exchange, routing_key)
1300
- @consumer_tag = consumer_tag
1301
- @delivery_tag = delivery_tag
1302
- @redelivered = redelivered
1303
- @exchange = exchange
1304
- @routing_key = routing_key
1305
- end
1306
- end
1307
-
1308
- class Get < Method
1309
- @name = "basic.get"
1310
- @method = 70
1311
- @index = 0x003C0046 # 60, 70, 3932230
1312
-
1313
- # @return
1314
- # ["ticket = false", "queue = nil", "no_ack = false"]
1315
- def self.encode(ticket, queue, no_ack)
1316
- pieces = []
1317
- pieces << [60, 70].pack("n2")
1318
- pieces << [ticket].pack("n")
1319
- pieces << queue.bytesize.chr
1320
- pieces << queue
1321
- bit_buffer = 0
1322
- bit_buffer = bit_buffer | (1 << 0) if no_ack
1323
- pieces.join("")
1324
- end
1325
- end
1326
-
1327
- class GetOk < Method
1328
- @name = "basic.get-ok"
1329
- @method = 71
1330
- @index = 0x003C0047 # 60, 71, 3932231
1331
-
1332
- # @return
1333
- def self.decode(data)
1334
- offset = 0
1335
- delivery_tag = data[offset..(offset + 8)].unpack("N2").first
1336
- offset += 8
1337
- bit_buffer = data[offset..(offset + 1)].unpack("c").first
1338
- offset += 1
1339
- redelivered = (bit_buffer & (1 << 0)) != 0
1340
- length = data[offset..(offset + 1)].unpack("N")[0]
1341
- offset += 1
1342
- exchange = data[offset..(offset + length)]
1343
- offset += length
1344
- length = data[offset..(offset + 1)].unpack("N")[0]
1345
- offset += 1
1346
- routing_key = data[offset..(offset + length)]
1347
- offset += length
1348
- message_count = data[offset..(offset + 4)].unpack("N").first
1349
- offset += 4
1350
- self.new(delivery_tag, redelivered, exchange, routing_key, message_count)
1351
- end
1352
-
1353
- attr_reader :delivery_tag, :redelivered, :exchange, :routing_key, :message_count
1354
- def initialize(delivery_tag, redelivered, exchange, routing_key, message_count)
1355
- @delivery_tag = delivery_tag
1356
- @redelivered = redelivered
1357
- @exchange = exchange
1358
- @routing_key = routing_key
1359
- @message_count = message_count
1360
- end
1361
- end
1362
-
1363
- class GetEmpty < Method
1364
- @name = "basic.get-empty"
1365
- @method = 72
1366
- @index = 0x003C0048 # 60, 72, 3932232
1367
-
1368
- # @return
1369
- def self.decode(data)
1370
- offset = 0
1371
- length = data[offset..(offset + 1)].unpack("N")[0]
1372
- offset += 1
1373
- cluster_id = data[offset..(offset + length)]
1374
- offset += length
1375
- self.new(cluster_id)
1376
- end
1377
-
1378
- attr_reader :cluster_id
1379
- def initialize(cluster_id)
1380
- @cluster_id = cluster_id
1381
- end
1382
- end
1383
-
1384
- class Ack < Method
1385
- @name = "basic.ack"
1386
- @method = 80
1387
- @index = 0x003C0050 # 60, 80, 3932240
1388
-
1389
- # @return
1390
- # ["delivery_tag = false", "multiple = false"]
1391
- def self.encode(delivery_tag, multiple)
1392
- pieces = []
1393
- pieces << [60, 80].pack("n2")
1394
- pieces << [delivery_tag].pack(">Q")
1395
- bit_buffer = 0
1396
- bit_buffer = bit_buffer | (1 << 0) if multiple
1397
- pieces.join("")
1398
- end
1399
- end
1400
-
1401
- class Reject < Method
1402
- @name = "basic.reject"
1403
- @method = 90
1404
- @index = 0x003C005A # 60, 90, 3932250
1405
-
1406
- # @return
1407
- # ["delivery_tag = nil", "requeue = true"]
1408
- def self.encode(delivery_tag, requeue)
1409
- pieces = []
1410
- pieces << [60, 90].pack("n2")
1411
- pieces << [delivery_tag].pack(">Q")
1412
- bit_buffer = 0
1413
- bit_buffer = bit_buffer | (1 << 0) if requeue
1414
- pieces.join("")
1415
- end
1416
- end
1417
-
1418
- class RecoverAsync < Method
1419
- @name = "basic.recover-async"
1420
- @method = 100
1421
- @index = 0x003C0064 # 60, 100, 3932260
1422
-
1423
- # @return
1424
- # ["requeue = false"]
1425
- def self.encode(requeue)
1426
- pieces = []
1427
- pieces << [60, 100].pack("n2")
1428
- bit_buffer = 0
1429
- bit_buffer = bit_buffer | (1 << 0) if requeue
1430
- pieces.join("")
1431
- end
1432
- end
1433
-
1434
- class Recover < Method
1435
- @name = "basic.recover"
1436
- @method = 110
1437
- @index = 0x003C006E # 60, 110, 3932270
1438
-
1439
- # @return
1440
- # ["requeue = false"]
1441
- def self.encode(requeue)
1442
- pieces = []
1443
- pieces << [60, 110].pack("n2")
1444
- bit_buffer = 0
1445
- bit_buffer = bit_buffer | (1 << 0) if requeue
1446
- pieces.join("")
1447
- end
1448
- end
1449
-
1450
- class RecoverOk < Method
1451
- @name = "basic.recover-ok"
1452
- @method = 111
1453
- @index = 0x003C006F # 60, 111, 3932271
1454
-
1455
- # @return
1456
- def self.decode(data)
1457
- offset = 0
1458
- self.new()
1459
- end
1460
-
1461
- attr_reader
1462
- def initialize()
1463
- end
1464
- end
1465
- end
1466
-
1467
- METHODS = begin
1468
- Method.methods.inject(Hash.new) do |hash, klass|
1469
- hash.merge!(klass.index => klass)
1470
- end
1471
- end
1472
- end
1473
- end
3
+ require "amq/protocol/version"
4
+ require "amq/protocol/client"