stomper 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -64,5 +64,5 @@ API compatibility with the original gem, and thus Stomper was conceived.
64
64
 
65
65
  Primary Author:: Ian D. Eccles
66
66
  Source Repository:: http://github.com/iande/stomper
67
- Current Version:: 0.3.1
68
- Last Updated:: 2010-03-04
67
+ Current Version:: 0.3.2
68
+ Last Updated:: 2010-03-05
@@ -191,24 +191,30 @@ module Stomper
191
191
  # incur some performance penalties depending upon which
192
192
  # Ruby environment this library is used with. The receiver
193
193
  # thread may be stopped by calling the +stop+ instance method.
194
+ # If the receiver is set to non-blocking (default behavior), the
195
+ # receiving thread will sleep for a number of seconds specified by the
196
+ # :receive_delay option between receive calls.
197
+ #
198
+ # The +opts+ parameter is a hash of options, and can include:
199
+ #
200
+ # [:block] Sets the receiver to either blocking if true (default: false)
201
+ # [:receive_delay] Sets the delay in seconds between receive calls when the receiver is non-blocking (default: 0.2)
194
202
  #
195
203
  # See also: stop, receiving?
196
- def start
204
+ def start(opts={})
197
205
  @connection.connect unless connected?
198
- return self if receiving?
199
- start_thread = false
206
+ do_start = false
200
207
  @receiver_lock.synchronize do
201
- @receiving = true
202
- start_thread = @run_thread.nil?
208
+ do_start = !receiving?
203
209
  end
204
- if start_thread
205
- @run_thread = Thread.new do
210
+ if do_start
211
+ blocking = opts.delete(:block) { false }
212
+ sleep_time = opts.delete(:receive_delay) { 0.2 }
213
+ @receiving = true
214
+ @run_thread = Thread.new(blocking) do |block|
206
215
  while receiving?
207
- # This was running a little too tightly...
208
- # still not terribly happy with this approach, event driven
209
- # receiving would be better than polling.
210
- receive
211
- sleep(1.0)
216
+ receive(block)
217
+ sleep(sleep_time) unless block
212
218
  end
213
219
  end
214
220
  end
@@ -227,13 +233,14 @@ module Stomper
227
233
  #
228
234
  # See also: start, receiving?
229
235
  def stop
230
- return self unless receiving?
236
+ do_stop = false
231
237
  @receiver_lock.synchronize do
232
- if receiving?
233
- @receiving = false
234
- @run_thread.join
235
- @run_thread = nil
236
- end
238
+ do_stop = receiving?
239
+ end
240
+ if do_stop
241
+ @receiving = false
242
+ @run_thread.join
243
+ @run_thread = nil
237
244
  end
238
245
  self
239
246
  end
@@ -260,8 +267,8 @@ module Stomper
260
267
  # stomp broker.
261
268
  #
262
269
  # See also: Stomper::Subscription
263
- def receive
264
- msg = @receive_lock.synchronize { @connection.receive }
270
+ def receive(block=false)
271
+ msg = @receive_lock.synchronize { @connection.receive(block) }
265
272
  @subscriptions.perform(msg) if msg.is_a?(Stomper::Frames::Message)
266
273
  msg
267
274
  end
@@ -29,14 +29,16 @@ module Stomper
29
29
  def initialize(uri, opts = {})
30
30
  connect_now = opts.delete(:connect_now) { true }
31
31
  @uri = (uri.is_a?(URI) && uri) || URI.parse(uri)
32
- @uri.port = (@uri.scheme == "stomp+ssl") ? 61612 : 61613 if @uri.port.nil?
33
- @uri.host = 'localhost' if @uri.host.nil?
34
- @uri.freeze
35
32
  @use_ssl = (@uri.scheme == "stomp+ssl")
33
+ @uri.host ||= 'localhost'
36
34
  if @use_ssl
35
+ @uri.port ||= 61612
37
36
  @ssl_context = OpenSSL::SSL::SSLContext.new
38
37
  @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
38
+ else
39
+ @uri.port ||= 61613
39
40
  end
41
+ @uri.freeze
40
42
  @connected = false
41
43
  connect if connect_now
42
44
  end
@@ -48,13 +50,13 @@ module Stomper
48
50
  #
49
51
  # See also: new
50
52
  def connect
51
- s = TCPSocket.open(@uri.host, @uri.port)
53
+ stomp_socket = TCPSocket.open(@uri.host, @uri.port)
52
54
  if @use_ssl
53
- s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
54
- s.sync_close = true
55
- s.connect
55
+ stomp_socket = OpenSSL::SSL::SSLSocket.new(stomp_socket, @ssl_context)
56
+ stomp_socket.sync_close = true
57
+ stomp_socket.connect
56
58
  end
57
- @socket = s
59
+ @socket = stomp_socket
58
60
  transmit Stomper::Frames::Connect.new(@uri.user, @uri.password)
59
61
  # Block until the first frame is received
60
62
  connect_frame = receive(true)
@@ -155,22 +157,11 @@ module Stomper
155
157
  end
156
158
 
157
159
  def socket_c_to_i(c)
158
- if c.respond_to?(:ord)
159
- def socket_c_to_i(char); char.ord; end
160
- c.ord
161
- else
162
- def socket_c_to_i(char); char; end
163
- c
164
- end
160
+ (c.respond_to?(:ord)) ? c.ord : c
165
161
  end
162
+
166
163
  def socket_c_to_chr(c)
167
- if c.respond_to?(:chr)
168
- def socket_c_to_chr(char); char.chr; end
169
- c.chr
170
- else
171
- def socket_c_to_chr(char); char; end
172
- c
173
- end
164
+ (c.respond_to?(:chr)) ? c.chr : c
174
165
  end
175
166
  end
176
167
  end
data/spec/client_spec.rb CHANGED
@@ -83,6 +83,16 @@ module Stomper
83
83
  @client.stop
84
84
  @client.receiving?.should be_false
85
85
  end
86
+ it "should allow for a blocking threaded receiver" do
87
+ @mock_connection.should_receive(:receive).with(true).at_least(:once).and_return(nil)
88
+ @mock_connection.should_receive(:connected?).any_number_of_times.and_return(true)
89
+ @client.receiving?.should be_false
90
+ @client.start(:block => true)
91
+ @client.receiving?.should be_true
92
+ @client.stop
93
+ @client.receiving?.should be_false
94
+ end
95
+
86
96
  end
87
97
 
88
98
  describe "subscribing to queue" do
@@ -4,7 +4,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'shared_connection_ex
4
4
  module Stomper
5
5
  describe Connection do
6
6
  before(:each) do
7
- @connection = Connection.new("stomp:///")
7
+ @connection = Connection.new("stomp:///", :connect_now => false)
8
8
  end
9
9
 
10
10
  it_should_behave_like "All Client Connections"
@@ -2,27 +2,27 @@ shared_examples_for "All Client Connections" do
2
2
  describe "connection initializers" do
3
3
  describe "from uri" do
4
4
  it "should accept the stomp:/// uri (no host specified)" do
5
- lambda { @connection.class.new("stomp:///") }.should_not raise_error
5
+ lambda { @connection.class.new("stomp:///", :connect_now => false) }.should_not raise_error
6
6
  end
7
7
 
8
8
  it "should accept a uri specifying just the host" do
9
- lambda { @connection.class.new("stomp://localhost/") }.should_not raise_error
9
+ lambda { @connection.class.new("stomp://localhost/", :connect_now => false) }.should_not raise_error
10
10
  end
11
11
 
12
12
  it "should accept a uri specifying host and port" do
13
- lambda { @connection.class.new("stomp://localhost:61613/") }.should_not raise_error
13
+ lambda { @connection.class.new("stomp://localhost:61613/", :connect_now => false) }.should_not raise_error
14
14
  end
15
15
 
16
16
  it "should accept a uri specifying host, port and credentials" do
17
- lambda { @connection.class.new("stomp://test_user:s3cr3tz@localhost:61613/") }.should_not raise_error
17
+ lambda { @connection.class.new("stomp://test_user:s3cr3tz@localhost:61613/", :connect_now => false) }.should_not raise_error
18
18
  end
19
19
 
20
20
  it "should accept a uri specifying a secure connection" do
21
- lambda { @connection.class.new("stomp+ssl://localhost") }.should_not raise_error
21
+ lambda { @connection.class.new("stomp+ssl://localhost", :connect_now => false) }.should_not raise_error
22
22
  end
23
23
 
24
24
  it "should not accept a bogus URI" do
25
- lambda { @connection.class.new("stomp://localhost:garbage") }.should raise_error
25
+ lambda { @connection.class.new("stomp://localhost:garbage", :connect_now => false) }.should raise_error
26
26
  end
27
27
 
28
28
  end
@@ -69,7 +69,7 @@ shared_examples_for "All Client Connections" do
69
69
 
70
70
  describe "secure connection" do
71
71
  before(:each) do
72
- @secure_connection = @connection.class.new("stomp+ssl:///")
72
+ @secure_connection = @connection.class.new("stomp+ssl:///", :connect_now => false)
73
73
  end
74
74
  it "should transmit frames" do
75
75
  @connection.connect
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ian D. Eccles
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-04 00:00:00 -05:00
17
+ date: 2010-03-05 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20