bunny 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,7 +20,7 @@ module Bunny
20
20
  class ConnectionError < StandardError; end
21
21
  class MessageError < StandardError; end
22
22
 
23
- VERSION = '0.4.2'
23
+ VERSION = '0.4.3'
24
24
 
25
25
  # Returns the Bunny version number
26
26
 
@@ -34,4 +34,18 @@ module Bunny
34
34
  Bunny::Client.new(opts)
35
35
  end
36
36
 
37
- end
37
+ def self.run(opts = {}, &block)
38
+ raise ArgumentError, 'Bunny#run requires a block' unless block
39
+
40
+ client = Bunny::Client.new(opts)
41
+ client.start
42
+
43
+ block.call(client)
44
+
45
+ client.stop
46
+
47
+ # Return success
48
+ :run_ok
49
+ end
50
+
51
+ end
@@ -126,8 +126,16 @@ Queue
126
126
 
127
127
  =end
128
128
 
129
- def queue(name, opts = {})
130
- queues[name] ||= Bunny::Queue.new(self, name, opts)
129
+ def queue(name = nil, opts = {})
130
+ if name.is_a?(Hash)
131
+ opts = name
132
+ name = nil
133
+ end
134
+
135
+ return queues[name] if queues.has_key?(name)
136
+
137
+ queue = Bunny::Queue.new(self, name, opts)
138
+ queues[queue.name] = queue
131
139
  end
132
140
 
133
141
  =begin rdoc
@@ -221,33 +229,49 @@ _Bunny_::_ProtocolError_ is raised. If successful, _Client_._status_ is set to <
221
229
  =end
222
230
 
223
231
  def start_session
224
- @channel = 0
225
- write(Qrack::Protocol::HEADER)
226
- write([1, 1, Qrack::Protocol::VERSION_MAJOR, Qrack::Protocol::VERSION_MINOR].pack('C4'))
227
- raise Bunny::ProtocolError, 'Connection initiation failed' unless next_method.is_a?(Qrack::Protocol::Connection::Start)
232
+ loop do
233
+ # Create/get socket
234
+ socket
235
+
236
+ @channel = 0
237
+ write(Qrack::Protocol::HEADER)
238
+ write([1, 1, Qrack::Protocol::VERSION_MAJOR, Qrack::Protocol::VERSION_MINOR].pack('C4'))
239
+ raise Bunny::ProtocolError, 'Connection initiation failed' unless next_method.is_a?(Qrack::Protocol::Connection::Start)
228
240
 
229
- send_frame(
230
- Qrack::Protocol::Connection::StartOk.new(
231
- {:platform => 'Ruby', :product => 'Bunny', :information => 'http://github.com/celldee/bunny', :version => VERSION},
232
- 'AMQPLAIN',
233
- {:LOGIN => @user, :PASSWORD => @pass},
234
- 'en_US'
241
+ send_frame(
242
+ Qrack::Protocol::Connection::StartOk.new(
243
+ {:platform => 'Ruby', :product => 'Bunny', :information => 'http://github.com/celldee/bunny', :version => VERSION},
244
+ 'AMQPLAIN',
245
+ {:LOGIN => @user, :PASSWORD => @pass},
246
+ 'en_US'
247
+ )
235
248
  )
236
- )
237
-
238
- method = next_method
239
- raise Bunny::ProtocolError, "Connection failed - user: #{@user}, pass: #{@pass}" if method.nil?
240
249
 
241
- if method.is_a?(Qrack::Protocol::Connection::Tune)
250
+ method = next_method
251
+ raise Bunny::ProtocolError, "Connection failed - user: #{@user}, pass: #{@pass}" if method.nil?
252
+
253
+ if method.is_a?(Qrack::Protocol::Connection::Tune)
254
+ send_frame(
255
+ Qrack::Protocol::Connection::TuneOk.new( :channel_max => 0, :frame_max => 131072, :heartbeat => 0)
256
+ )
257
+ end
258
+
242
259
  send_frame(
243
- Qrack::Protocol::Connection::TuneOk.new( :channel_max => 0, :frame_max => 131072, :heartbeat => 0)
260
+ Qrack::Protocol::Connection::Open.new(:virtual_host => @vhost, :capabilities => '', :insist => @insist)
244
261
  )
245
- end
246
262
 
247
- send_frame(
248
- Qrack::Protocol::Connection::Open.new(:virtual_host => @vhost, :capabilities => '', :insist => @insist)
249
- )
250
- raise Bunny::ProtocolError, 'Cannot open connection' unless next_method.is_a?(Qrack::Protocol::Connection::OpenOk)
263
+ case method = next_method
264
+ when Qrack::Protocol::Connection::OpenOk
265
+ break
266
+ when Qrack::Protocol::Connection::Redirect
267
+ raise Bunny::ConnectionError, "Cannot connect to the specified server - host: #{@host}, port: #{@port}" if @insist
268
+
269
+ @host, @port = method.host.split(':')
270
+ close_socket
271
+ else
272
+ raise Bunny::ProtocolError, 'Cannot open connection'
273
+ end
274
+ end
251
275
 
252
276
  @channel = 1
253
277
  send_frame(Qrack::Protocol::Channel::Open.new)
@@ -297,7 +321,8 @@ the message, potentially then delivering it to an alternative subscriber.
297
321
 
298
322
  def send_command(cmd, *args)
299
323
  begin
300
- socket.__send__(cmd, *args)
324
+ raise Bunny::ConnectionError, 'No connection - socket has not been created' if !@socket
325
+ @socket.__send__(cmd, *args)
301
326
  rescue Errno::EPIPE, IOError => e
302
327
  raise Bunny::ServerDownError, e.message
303
328
  end
@@ -341,4 +366,4 @@ the message, potentially then delivering it to an alternative subscriber.
341
366
  end
342
367
 
343
368
  end
344
- end
369
+ end
@@ -110,7 +110,7 @@ nil
110
110
  :priority => 0
111
111
  }.merge(opts)
112
112
  )
113
- out << Qrack::Transport::Frame::Body.new(data)
113
+ out << Qrack::Transport::Body.new(data)
114
114
 
115
115
  client.send_frame(*out)
116
116
  end
@@ -19,18 +19,31 @@ Queues must be attached to at least one exchange in order to receive messages fr
19
19
 
20
20
  @client = client
21
21
  @opts = opts
22
- @name = name
23
- @delivery_tag = nil
22
+ @delivery_tag = nil
23
+
24
+ # Queues without a given name are named by the server and are generally
25
+ # bound to the process that created them.
26
+ if !name
27
+ opts = {
28
+ :passive => false,
29
+ :durable => false,
30
+ :exclusive => true,
31
+ :auto_delete => true
32
+ }.merge(opts)
33
+ end
24
34
 
25
35
  # ignore the :nowait option if passed, otherwise program will hang waiting for a
26
36
  # response that will not be sent by the server
27
37
  opts.delete(:nowait)
28
38
 
29
39
  client.send_frame(
30
- Qrack::Protocol::Queue::Declare.new({ :queue => name, :nowait => false }.merge(opts))
40
+ Qrack::Protocol::Queue::Declare.new({ :queue => name || '', :nowait => false }.merge(opts))
31
41
  )
32
42
 
33
- raise Bunny::ProtocolError, "Error declaring queue #{name}" unless client.next_method.is_a?(Qrack::Protocol::Queue::DeclareOk)
43
+ method = client.next_method
44
+ raise Bunny::ProtocolError, "Error declaring queue #{name}" unless method.is_a?(Qrack::Protocol::Queue::DeclareOk)
45
+
46
+ @name = method.queue
34
47
  end
35
48
 
36
49
  =begin rdoc
@@ -442,4 +455,4 @@ without any formal "undo" mechanism. If an error occurs raises _Bunny_::_Protoco
442
455
  end
443
456
  end
444
457
 
445
- end
458
+ end
@@ -58,7 +58,7 @@ module Qrack
58
58
  end
59
59
 
60
60
  def to_frame channel = 0
61
- Transport::Frame::Method.new(self, channel)
61
+ Transport::Method.new(self, channel)
62
62
  end
63
63
  end
64
64
 
@@ -110,7 +110,7 @@ module Qrack
110
110
  end
111
111
 
112
112
  def to_frame channel = 0
113
- Transport::Frame::Header.new(self, channel)
113
+ Transport::Header.new(self, channel)
114
114
  end
115
115
 
116
116
  def == header
@@ -1,59 +1,41 @@
1
1
 
2
2
  #:stopdoc:
3
- # this file was autogenerated on Fri May 15 10:11:23 +0100 2009
3
+ # this file was autogenerated on Fri Jun 04 10:11:23 +0100 2009
4
4
  #
5
5
  # DO NOT EDIT! (edit ext/qparser.rb and config.yml instead, and run 'ruby qparser.rb')
6
6
 
7
7
  module Qrack
8
8
  module Transport
9
9
  class Frame
10
- def self.types
11
- @types ||= {}
12
- end
13
-
14
- def self.Frame id
15
- (@_base_frames ||= {})[id] ||= Class.new(Frame) do
16
- class_eval %[
17
- def self.inherited klass
18
- klass.const_set(:ID, #{id})
19
- Frame.types[#{id}] = klass
20
- end
21
- ]
22
- end
23
- end
10
+
11
+ FOOTER = 206
12
+ ID = 0
24
13
 
25
- class Method < Frame( 1 ); end
26
- class Header < Frame( 2 ); end
27
- class Body < Frame( 3 ); end
28
- class OobMethod < Frame( 4 ); end
29
- class OobHeader < Frame( 5 ); end
30
- class OobBody < Frame( 6 ); end
31
- class Trace < Frame( 7 ); end
32
- class Heartbeat < Frame( 8 ); end
14
+ @types = { 1 => 'Method',
15
+ 2 => 'Header',
16
+ 3 => 'Body',
17
+ 4 => 'OobMethod',
18
+ 5 => 'OobHeader',
19
+ 6 => 'OobBody',
20
+ 7 => 'Trace',
21
+ 8 => 'Heartbeat'}
33
22
 
34
- FOOTER = 206
35
- end
36
- end
37
- end
23
+ attr_accessor :channel, :payload
38
24
 
39
- module Qrack
40
- module Transport
41
- class Frame
42
25
  def initialize payload = nil, channel = 0
43
26
  @channel, @payload = channel, payload
44
27
  end
45
- attr_accessor :channel, :payload
46
28
 
47
- def id
48
- self.class::ID
49
- end
29
+ def id
30
+ self.class::ID
31
+ end
50
32
 
51
33
  def to_binary
52
34
  buf = Transport::Buffer.new
53
35
  buf.write :octet, id
54
36
  buf.write :short, channel
55
37
  buf.write :longstr, payload
56
- buf.write :octet, Transport::Frame::FOOTER
38
+ buf.write :octet, FOOTER
57
39
  buf.rewind
58
40
  buf
59
41
  end
@@ -67,34 +49,64 @@ module Qrack
67
49
  eql and __send__(field) == frame.__send__(field)
68
50
  end
69
51
  end
70
-
71
- class Method
72
- def initialize payload = nil, channel = 0
73
- super
74
- unless @payload.is_a? Protocol::Class::Method or @payload.nil?
75
- @payload = Protocol.parse(@payload)
76
- end
52
+
53
+ def self.parse buf
54
+ buf = Transport::Buffer.new(buf) unless buf.is_a? Transport::Buffer
55
+ buf.extract do
56
+ id, channel, payload, footer = buf.read(:octet, :short, :longstr, :octet)
57
+ Qrack::Transport.const_get(@types[id]).new(payload, channel) if footer == FOOTER
77
58
  end
78
59
  end
79
60
 
80
- class Header
81
- def initialize payload = nil, channel = 0
82
- super
83
- unless @payload.is_a? Protocol::Header or @payload.nil?
84
- @payload = Protocol::Header.new(@payload)
85
- end
61
+ end
62
+
63
+ class Method < Frame
64
+
65
+ ID = 1
66
+
67
+ def initialize payload = nil, channel = 0
68
+ super
69
+ unless @payload.is_a? Protocol::Class::Method or @payload.nil?
70
+ @payload = Protocol.parse(@payload)
86
71
  end
87
72
  end
73
+ end
88
74
 
89
- class Body; end
90
-
91
- def self.parse buf
92
- buf = Transport::Buffer.new(buf) unless buf.is_a? Transport::Buffer
93
- buf.extract do
94
- id, channel, payload, footer = buf.read(:octet, :short, :longstr, :octet)
95
- Transport::Frame.types[id].new(payload, channel) if footer == Transport::Frame::FOOTER
75
+ class Header < Frame
76
+
77
+ ID = 2
78
+
79
+ def initialize payload = nil, channel = 0
80
+ super
81
+ unless @payload.is_a? Protocol::Header or @payload.nil?
82
+ @payload = Protocol::Header.new(@payload)
96
83
  end
97
84
  end
98
85
  end
86
+
87
+ class Body < Frame
88
+ ID = 3
89
+ end
90
+
91
+ class OobMethod < Frame
92
+ ID = 4
93
+ end
94
+
95
+ class OobHeader < Frame
96
+ ID = 5
97
+ end
98
+
99
+ class OobBody < Frame
100
+ ID = 6
101
+ end
102
+
103
+ class Trace < Frame
104
+ ID = 7
105
+ end
106
+
107
+ class Heartbeat < Frame
108
+ ID = 8
109
+ end
110
+
99
111
  end
100
- end
112
+ end
@@ -94,5 +94,13 @@ describe Bunny::Queue do
94
94
  q = @b.queue('test0')
95
95
  q.delete(:nowait => true)
96
96
  end
97
+
98
+ it "should support server named queues" do
99
+ q = @b.queue
100
+ q.name.should_not == nil
101
+
102
+ @b.queue(q.name).should == q
103
+ q.delete
104
+ end
97
105
 
98
- end
106
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Duncan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-22 00:00:00 +01:00
12
+ date: 2009-06-10 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15