stomper 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/AUTHORS ADDED
@@ -0,0 +1,21 @@
1
+ Stomper authors:
2
+ Ian D. Eccles
3
+
4
+
5
+ Stomper was initially a fork of the Stomp Ruby Gem. Though it
6
+ no longer has a compatible interface with the Stomp gem, and shares
7
+ no common code with it, the Stomp contributors are listed below in
8
+ recognition of their influence on Stomper. Note: these authors have
9
+ not contributed to the Stomper project, nor are they in any way responsible
10
+ for this project. Please do not contact them with questions regarding Stomper.
11
+
12
+ The following people have contributed to Stomp (ordered by commits):
13
+
14
+ Brian McCaliister
15
+ Glenn Rempe <glenn@rempe.us>
16
+ jstrachan
17
+ Marius Mathiesen <marius.mathiesen@gmail.com>
18
+ Johan S√∏rensen <johan@johansorensen.com>
19
+ kookster
20
+ Tony Garnock-Jones <tonyg@lshift.net>
21
+ chirino
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ == 0.2 2009-11-20
2
+
3
+ * Officially moved to "stomper" library.
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ =README
2
+
3
+ Stomper is a library for connecting to and interacting with a message queue
4
+ service that supports the {Stomp protocol}[http://stomp.codehaus.org/Protocol].
5
+ Characteristics that distinguish it from other libraries, such the
6
+ {stomp gem}[http://github.com/js/stomp], are:
7
+
8
+ * Transaction blocks with {transaction blocks}[link:classes/Stomper/Client.html#M000066]
9
+ * Playing nice with Apache ActiveMQ by being able to {disable automatic content-length generation}[link:classes/Stomper/Frames/ClientFrame.html#M000014]
10
+ * A receiver thread that can be bypassed by something else! (whatever that means)
11
+
12
+ == Example Usage
13
+
14
+ client = Stomper::Client.new("stomp://my_username:s3cr3tz@localhost:61613")
15
+ # Clients must be explicitly started to automatically receive incoming
16
+ # messages.
17
+ client.start
18
+
19
+ client.subscribe("/queue/hello") do |msg|
20
+ puts msg.body
21
+ end
22
+
23
+ # Send a simple message
24
+ client.send("/queue/hello", "hello world!")
25
+
26
+ # Send a message within a transaction. This usage is new to stomper
27
+ # however, one can manually manage transactions as well.
28
+ client.transaction do |t|
29
+ t.send("/queue/hello", "a transactioned message")
30
+ end
31
+
32
+ # If the block provided to #transaction accepts a single parameter,
33
+ # the client yields the Transaction object to the block, otherwise
34
+ # the block is instance_eval'd within the Transaction object.
35
+ client.transaction do
36
+ send("/queue/hello", "a message you will never receive")
37
+ # Nested transactions, failures percolate up to parent transactions.
38
+ transaction do
39
+ send("/queue/hello", "because I am about to fail!")
40
+ raise "by forcing an error in a nested transaction"
41
+ end
42
+ end
43
+
44
+ # Later ...
45
+ client.stop
46
+ client.close
47
+
48
+ == To-Do
49
+ * Provide the `pipe` method on Stomper::Client
50
+ * Allow SSL verification if requested (option :verify_ssl?)
51
+ * Re-evaluate how to handle a 'reliable' connection.
52
+
53
+ == License
54
+
55
+ Stomper is released under the Apache License 2.0
56
+
57
+ == Pointless Backstory
58
+
59
+ Stomper began its life as a mere fork of the {Stomp}[http://github.com/js/stomp]
60
+ gem. However, as changes were made and desires grew, the fork began breaking
61
+ API compatibility with the original gem, and thus Stomper was conceived.
62
+
63
+ == Other Stuff
64
+
65
+ Primary Author:: Ian D. Eccles
66
+ Source Repository:: http://github.com/iande/stomper
67
+ Current Version:: 0.2.0
68
+ Last Updated:: 2009-12-11
data/lib/stomper.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'uri'
2
+ require 'io/wait'
3
+ require 'socket'
4
+ require 'thread'
5
+ require 'monitor'
6
+ require 'openssl'
7
+ require 'stomper/frames'
8
+ require 'stomper/connection'
9
+ require 'stomper/transaction'
10
+ require 'stomper/subscription'
11
+ require 'stomper/subscriptions'
12
+ require 'stomper/client'
13
+
14
+ module Stomper
15
+ end
@@ -0,0 +1,300 @@
1
+ module Stomper
2
+ # A high-level representation of a connection to a Stomp message broker.
3
+ # Instances of Client can be shared safely between threads, all mutating
4
+ # methods should be properly synchronized. Interactions with the stomp
5
+ # message broker through instances of Client are generally simpler than
6
+ # doing so through instances of Connection. Client instances do not require
7
+ # the use of Stomper::Frames::ClientFrame objects to transmit and receive
8
+ # information, instead relying on specific method calls to do so.
9
+ #
10
+ # === Example Usage
11
+ # client = Stomper::Client.new("stomp://localhost:61613")
12
+ # client.start
13
+ #
14
+ # client.subscribe("/queue/target1") do |msg|
15
+ # puts "Received Message: #{msg.body}"
16
+ # end
17
+ #
18
+ # client.send("/queue/target1", "this is a test")
19
+ # client.send("/queue/target1", "this persists", { :persistent => true })
20
+ #
21
+ # client.transaction do |t1|
22
+ # t1.send("/queue/target1", "this will never be seen")
23
+ # raise "Forced Exception"
24
+ # end
25
+ #
26
+ # client.unsubscribe("/queue/target1")
27
+ #
28
+ # client.stop
29
+ # client.close
30
+ #
31
+ class Client
32
+ attr_reader :connection, :subscriptions
33
+
34
+ # Creates a new Client instance that will connect to the stomp broker
35
+ # designated by the +uri+ parameter. Additionally, +options+ may be
36
+ # specified as a hash, and are passed along to the underlying connection.
37
+ # For details on the format of +uri+ and the acceptable +options+, see
38
+ # Stomper::Connection.
39
+ def initialize(uri, options={})
40
+ # At this time we only bother with the BasicConnection. We will need
41
+ # to write the ReliableConnection class to handle the particulars of reconnecting
42
+ # on a socket error.
43
+ #if options.has_key?(:max_retries) || options.delete(:reliable) { false }
44
+ #@connection = ReliableConnection.new(uri, options)
45
+ #else
46
+ @connection = Connection.new(uri, options)
47
+ #end
48
+ @subscriptions = Subscriptions.new
49
+ @send_lock = Mutex.new
50
+ @receive_lock = Mutex.new
51
+ @run_thread = nil
52
+ @receiving = false
53
+ @receiver_lock = Mutex.new
54
+ end
55
+
56
+ # Sends a string message specified by +body+ to the appropriate stomp
57
+ # broker destination given by +destination+. Additional headers for the
58
+ # message may be specified by the +headers+ hash where the key is the header
59
+ # property and the value is the corresponding property's value. The
60
+ # keys of +headers+ may be symbols or strings.
61
+ #
62
+ # Examples:
63
+ #
64
+ # client.send("/topic/whatever", "hello world")
65
+ #
66
+ # client.send("/queue/some/destination", "hello world", { :persistent => true })
67
+ #
68
+ def send(destination, body, headers={})
69
+ transmit_frame(Stomper::Frames::Send.new(destination, body, headers))
70
+ end
71
+
72
+ # Acknowledge to the stomp broker that a given message was received.
73
+ # The +id_or_frame+ parameter may be either the message-id header of
74
+ # the received message, or an actual instance of Stomper::Frames::Message.
75
+ # Additional headers may be specified through the +headers+ hash.
76
+ #
77
+ # Examples:
78
+ #
79
+ # client.ack(received_message)
80
+ #
81
+ # client.ack("message-0001-00451-003031")
82
+ #
83
+ def ack(id_or_frame, headers={})
84
+ transmit_frame(Stomper::Frames::Ack.ack_for(id_or_frame, headers))
85
+ end
86
+
87
+ # Tells the stomp broker to commit a transaction named by the
88
+ # supplied +transaction_id+ parameter. When used in conjunction with
89
+ # +begin+, and +abort+, a means for manually handling transactional
90
+ # message passing is provided.
91
+ #
92
+ # See Also: transaction
93
+ def commit(transaction_id)
94
+ transmit_frame(Stomper::Frames::Commit.new(transaction_id))
95
+ end
96
+
97
+ # Tells the stomp broker to abort a transaction named by the
98
+ # supplied +transaction_id+ parameter. When used in conjunction with
99
+ # +begin+, and +commit+, a means for manually handling transactional
100
+ # message passing is provided.
101
+ #
102
+ # See Also: transaction
103
+ def abort(transaction_id)
104
+ transmit_frame(Stomper::Frames::Abort.new(transaction_id))
105
+ end
106
+
107
+ # Tells the stomp broker to begin a transaction named by the
108
+ # supplied +transaction_id+ parameter. When used in conjunction with
109
+ # +commit+, and +abort+, a means for manually handling transactional
110
+ # message passing is provided.
111
+ #
112
+ # See also: transaction
113
+ def begin(transaction_id)
114
+ transmit_frame(Stomper::Frames::Begin.new(transaction_id))
115
+ end
116
+
117
+ # Creates a new Stomper::Transaction object and evaluates
118
+ # the supplied +block+ within a transactional context. If
119
+ # the block executes successfully, the transaction is committed,
120
+ # otherwise it is aborted. This method is meant to provide a less
121
+ # tedious approach to transactional messaging than the +begin+,
122
+ # +abort+ and +commit+ methods.
123
+ #
124
+ # See also: begin, commit, abort, Stomper::Transaction
125
+ def transaction(transaction_id=nil, &block)
126
+ begin
127
+ Stomper::Transaction.new(self, transaction_id, &block)
128
+ rescue Stomper::TransactionAborted
129
+ nil
130
+ end
131
+ self
132
+ end
133
+
134
+ # Subscribes to the specified +destination+, passing along
135
+ # the optional +headers+ inside the subscription frame. When a message
136
+ # is received for this subscription, the supplied +block+ is
137
+ # called with the received message as its argument.
138
+ #
139
+ # Examples:
140
+ #
141
+ # client.subscribe("/queue/test") { |msg| puts "Got message: #{msg.body}" }
142
+ #
143
+ # client.subscribe("/queue/test", :ack => 'client', 'id' => 'subscription-001') do |msg|
144
+ # puts "Got message: #{msg.body}"
145
+ # end
146
+ #
147
+ # client.subscribe("/queue/test", :selector => 'cost > 5') do |msg|
148
+ # puts "Got message: #{msg.body}"
149
+ # end
150
+ #
151
+ # See also: unsubscribe, Stomper::Subscription
152
+ def subscribe(destination, headers={}, &block)
153
+ unless destination.is_a?(Subscription)
154
+ destination = Subscription.new(headers.merge(:destination => destination), &block)
155
+ end
156
+ @subscriptions << destination
157
+ transmit_frame(destination.to_subscribe)
158
+ self
159
+ end
160
+
161
+ # Unsubscribes from the specified +destination+. The +destination+
162
+ # parameter may be either a string, such as "/queue/test", or Stomper::Subscription
163
+ # object. If the optional +sub_id+ is supplied, the client will unsubscribe
164
+ # from the subscription with an id matching +sub_id+, regardless if the
165
+ # +destination+ parameter matches that of the registered subscription. For
166
+ # this reason, it is vital that subscription ids, if manually specified, be
167
+ # unique.
168
+ #
169
+ # Examples:
170
+ #
171
+ # client.unsubscribe("/queue/test")
172
+ # # unsubscribes from all "naive" subscriptions for "/queue/test"
173
+ #
174
+ # client.unsubscribe("/queue/does/not/matter", "sub-0013012031")
175
+ # # unsubscribes from all subscriptions with id of "sub-0013012031"
176
+ #
177
+ # client.unsubscribe(some_subscription)
178
+ #
179
+ # See also: subscribe, Stomper::Subscription
180
+ def unsubscribe(destination, sub_id=nil)
181
+ @subscriptions.remove(destination, sub_id).each do |unsub|
182
+ transmit_frame(unsub.to_unsubscribe)
183
+ end
184
+ self
185
+ end
186
+
187
+ # Starts the receiver for a Client instance. This method
188
+ # must be manually invoked in order to receive frames sent
189
+ # by the stomp broker. Be aware that a Client object's
190
+ # receiver runs in its own separate thread, and so may
191
+ # incur some performance penalties depending upon which
192
+ # Ruby environment this library is used with. The receiver
193
+ # thread may be stopped by calling the +stop+ instance method.
194
+ #
195
+ # See also: stop, receiving?
196
+ def start
197
+ @connection.connect unless connected?
198
+ return self if receiving?
199
+ @receiver_lock.synchronize do
200
+ if @run_thread.nil?
201
+ @receiving = true
202
+ @run_thread = Thread.new do
203
+ while receiving?
204
+ #begin
205
+ receive
206
+ #rescue => err
207
+ #puts "Exception Caught: #{err.to_s}"
208
+ #break
209
+ #end
210
+ end
211
+ end
212
+ end
213
+ end
214
+ self
215
+ end
216
+
217
+ # Stops the receiver for a Client instance. The methodology
218
+ # employed to stop the thread should be safe (it does not
219
+ # make use of Thread.kill) It is also safe to +start+ and
220
+ # +stop+ the receiver thread multiple times, doing so does not
221
+ # interrupt the connection to the stomp broker under normal
222
+ # circumstances. In the interest in proper performance, it is
223
+ # recommend that +stop+ be called when a Client instance is
224
+ # no longer needed (assuming the instance's receiver thread was
225
+ # started, of course.)
226
+ #
227
+ # See also: start, receiving?
228
+ def stop
229
+ return self unless receiving?
230
+ @receiver_lock.synchronize do
231
+ if receiving?
232
+ @receiving = false
233
+ @run_thread.join
234
+ @run_thread = nil
235
+ end
236
+ end
237
+ self
238
+ end
239
+
240
+ # Returns true if the receiver thread has been started
241
+ # by use of the +start+ command. Otherwise, returns false.
242
+ #
243
+ # See also: start, stop
244
+ def receiving?
245
+ @receiving
246
+ end
247
+
248
+ # Receives the next available frame from the stomp broker, if
249
+ # one is available. This method is regularly invoked by the
250
+ # receiver thread if it is created by the +start+ method; however,
251
+ # it may also be invoked manually if so desired, allowing one to
252
+ # by-pass the threaded implementation of receiving found in using
253
+ # +start+ and +stop+. If the received frame is an instance of
254
+ # Stomper::Frames::Message, this method will invoke any subscriptions
255
+ # that are responsible for the message.
256
+ #
257
+ # Note: this method does not block under normal operation, as such
258
+ # +nil+ may be returned if there are no frames available from the
259
+ # stomp broker.
260
+ #
261
+ # See also: Stomper::Subscription
262
+ def receive
263
+ msg = @receive_lock.synchronize { @connection.receive }
264
+ @subscriptions.perform(msg) if msg.is_a?(Stomper::Frames::Message)
265
+ msg
266
+ end
267
+
268
+ # Toying with an idea, probably a very bad one!
269
+ def each # :nodoc:
270
+ while connected?
271
+ yield receive
272
+ end
273
+ end
274
+
275
+ # Returns true if the client is connected, false otherwise.
276
+ def connected?
277
+ @connection.connected?
278
+ end
279
+
280
+ # Disconnects from the stomp broker politely by first transmitting
281
+ # a Stomper::Frames::Disconnect frame to the broker.
282
+ def disconnect
283
+ @connection.disconnect
284
+ end
285
+
286
+ alias close disconnect
287
+
288
+ protected
289
+ # We need to synchronize frame tranmissions to one at a time.
290
+ # My suspicion is that write/puts socket methods are not atomic, so if a message
291
+ # is started then interrupted and a new message is attempted, it will
292
+ # result in either a broken connection or an inconsistent state of our
293
+ # system.
294
+ def transmit_frame(frame)
295
+ @send_lock.synchronize do
296
+ @connection.transmit(frame)
297
+ end
298
+ end
299
+ end
300
+ end