stomp 1.0.6 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +25 -0
- data/LICENSE +202 -0
- data/README.rdoc +36 -0
- data/Rakefile +52 -0
- data/bin/catstomp +55 -0
- data/bin/stompcat +56 -0
- data/lib/stomp.rb +4 -398
- data/lib/stomp/client.rb +207 -0
- data/lib/stomp/connection.rb +256 -0
- data/lib/stomp/message.rb +17 -0
- data/test/test_client.rb +182 -0
- data/test/test_connection.rb +95 -0
- data/test/test_helper.rb +5 -0
- metadata +41 -15
data/CHANGELOG
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
== 1.1 2009-27-02
|
2
|
+
|
3
|
+
* Ruby 1.9 Support
|
4
|
+
* Add support for connect_headers, to control the CONNECT command.
|
5
|
+
* Refactored lib dir to separate concerns.
|
6
|
+
* Better test coverage
|
7
|
+
* General code cleanup.
|
8
|
+
|
9
|
+
== 1.0.6 2008-05-08
|
10
|
+
|
11
|
+
* Whitespace cleanup
|
12
|
+
* Refactored Rakefile and added stomp.gemspec for GitHub friendliness.
|
13
|
+
* Added .gitignore file
|
14
|
+
* Refactored layout of lib dir to separate concerns
|
15
|
+
* Cleanup of initializers, and provide Client accessors for reading values (for testing)
|
16
|
+
* Removed test/test_url_* files as they only differed from the test_client.rb in their
|
17
|
+
setup. Super UnDry. Added URL tests to cover stomp URL as param.
|
18
|
+
* Created initial RSpec specs which stub/mock objects and should not require a running
|
19
|
+
Stomp server instance.
|
20
|
+
|
21
|
+
== v1.0.5
|
22
|
+
|
23
|
+
SVN rev 86 clone from http://svn.codehaus.org/stomp/ruby/trunk
|
24
|
+
|
25
|
+
git-svn-id: http://svn.codehaus.org/stomp/ruby/trunk@86 fd4e7336-3dff-0310-b68a-b6615a75f13b
|
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,36 @@
|
|
1
|
+
==README
|
2
|
+
|
3
|
+
http://stomp.rubyforge.org/
|
4
|
+
|
5
|
+
===Overview
|
6
|
+
|
7
|
+
An implementation of the Stomp protocol (http://stomp.codehaus.org/Protocol) for Ruby.
|
8
|
+
|
9
|
+
|
10
|
+
===Example Usage
|
11
|
+
|
12
|
+
client = Stomp::Client.new("test", "user", "localhost", 61613)
|
13
|
+
client.send("/my/queue", "hello world!")
|
14
|
+
client.subscribe("/my/queue") do |msg|
|
15
|
+
p msg
|
16
|
+
end
|
17
|
+
|
18
|
+
===Contact info
|
19
|
+
|
20
|
+
Up until March 2009 the project was maintained and primarily developed by Brian McCallister.
|
21
|
+
|
22
|
+
The project is now maintained by Johan Sørensen <johan@johansorensen.com>
|
23
|
+
|
24
|
+
===Source Code
|
25
|
+
|
26
|
+
http://gitorious.org/projects/stomp/
|
27
|
+
http://github.com/js/stomp/
|
28
|
+
|
29
|
+
===Project urls
|
30
|
+
|
31
|
+
Project Home :
|
32
|
+
http://gitorious.org/projects/stomp/
|
33
|
+
http://rubyforge.org/projects/stomp/
|
34
|
+
|
35
|
+
Stomp Protocol Info :
|
36
|
+
http://stomp.codehaus.org/Protocol
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Copyright 2005-2006 Brian McCallister
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'rubygems'
|
16
|
+
require 'rake/gempackagetask'
|
17
|
+
require 'rake/testtask'
|
18
|
+
require 'rake/rdoctask'
|
19
|
+
require 'spec/rake/spectask'
|
20
|
+
|
21
|
+
# read the contents of the gemspec, eval it, and assign it to 'spec'
|
22
|
+
# this lets us maintain all gemspec info in one place. Nice and DRY.
|
23
|
+
spec = eval(IO.read("stomp.gemspec"))
|
24
|
+
|
25
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
26
|
+
pkg.gem_spec = spec
|
27
|
+
pkg.need_tar = true
|
28
|
+
end
|
29
|
+
|
30
|
+
task :install => [:package] do
|
31
|
+
sh %{sudo gem install pkg/#{GEM}-#{VERSION}}
|
32
|
+
end
|
33
|
+
|
34
|
+
Rake::TestTask.new do |t|
|
35
|
+
t.libs << "test"
|
36
|
+
t.test_files = FileList['test/test*.rb']
|
37
|
+
t.verbose = true
|
38
|
+
end
|
39
|
+
|
40
|
+
Rake::RDocTask.new do |rd|
|
41
|
+
rd.main = "README.rdoc"
|
42
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
43
|
+
rd.rdoc_dir = 'doc'
|
44
|
+
rd.options = spec.rdoc_options
|
45
|
+
end
|
46
|
+
|
47
|
+
desc "RSpec : run all"
|
48
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
49
|
+
t.spec_files = FileList['spec/**/*.rb']
|
50
|
+
t.spec_opts = ["--color", "--format", "specdoc"]
|
51
|
+
end
|
52
|
+
|
data/bin/catstomp
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright 2006 LogicBlaze Inc.
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
begin; require 'rubygems'; rescue; end
|
18
|
+
require 'stomp'
|
19
|
+
|
20
|
+
#
|
21
|
+
# This simple script is inspired by the netcat utility. It allows you to send
|
22
|
+
# input into this process to stomp destination.
|
23
|
+
#
|
24
|
+
# Usage: catstomp (destination-name)
|
25
|
+
#
|
26
|
+
# Example: ls | catstomp /topic/foo
|
27
|
+
# Would send the output of the ls command to the stomp destination /topic/foo
|
28
|
+
#
|
29
|
+
begin
|
30
|
+
|
31
|
+
@port = 61613
|
32
|
+
@host = "localhost"
|
33
|
+
@user = ENV["STOMP_USER"];
|
34
|
+
@password = ENV["STOMP_PASSWORD"]
|
35
|
+
|
36
|
+
@host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != nil
|
37
|
+
@port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != nil
|
38
|
+
|
39
|
+
@destination = "/topic/default"
|
40
|
+
@destination = $*[0] if $*[0] != nil
|
41
|
+
|
42
|
+
$stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
|
43
|
+
@conn = Stomp::Connection.open(@user, @password, @host, @port, true)
|
44
|
+
$stderr.print "Sending input to #{@destination}\n"
|
45
|
+
|
46
|
+
@headers = {'persistent'=>'false'}
|
47
|
+
@headers['reply-to'] = $*[1] if $*[1] != nil
|
48
|
+
|
49
|
+
STDIN.each_line { |line|
|
50
|
+
@conn.send @destination, line, @headers
|
51
|
+
}
|
52
|
+
|
53
|
+
rescue
|
54
|
+
end
|
55
|
+
|
data/bin/stompcat
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Copyright 2006 LogicBlaze Inc.
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
begin; require 'rubygems'; rescue; end
|
18
|
+
require 'stomp'
|
19
|
+
|
20
|
+
#
|
21
|
+
# This simple script is inspired by the netcat utility. It allows you to receive
|
22
|
+
# data from a stomp destination and output it.
|
23
|
+
#
|
24
|
+
# Usage: stompcat (destination-name)
|
25
|
+
#
|
26
|
+
# Example: stompcat /topic/foo
|
27
|
+
# Would display output that arrives at the /topic/foo stomp destination
|
28
|
+
#
|
29
|
+
begin
|
30
|
+
|
31
|
+
@port = 61613
|
32
|
+
@host = "localhost"
|
33
|
+
@user = ENV["STOMP_USER"];
|
34
|
+
@password = ENV["STOMP_PASSWORD"]
|
35
|
+
|
36
|
+
@host = ENV["STOMP_HOST"] if ENV["STOMP_HOST"] != nil
|
37
|
+
@port = ENV["STOMP_PORT"] if ENV["STOMP_PORT"] != nil
|
38
|
+
|
39
|
+
@destination = "/topic/default"
|
40
|
+
@destination = $*[0] if $*[0] != nil
|
41
|
+
|
42
|
+
$stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
|
43
|
+
@conn = Stomp::Connection.open(@user, @password, @host, @port, true)
|
44
|
+
$stderr.print "Getting output from #{@destination}\n"
|
45
|
+
|
46
|
+
@conn.subscribe(@destination, { :ack =>"client" })
|
47
|
+
while true
|
48
|
+
@msg = @conn.receive
|
49
|
+
$stdout.print @msg.body
|
50
|
+
$stdout.flush
|
51
|
+
@conn.ack @msg.headers["message-id"]
|
52
|
+
end
|
53
|
+
|
54
|
+
rescue
|
55
|
+
end
|
56
|
+
|
data/lib/stomp.rb
CHANGED
@@ -16,404 +16,10 @@
|
|
16
16
|
require 'io/wait'
|
17
17
|
require 'socket'
|
18
18
|
require 'thread'
|
19
|
+
require 'stomp/connection'
|
20
|
+
require 'stomp/client'
|
21
|
+
require 'stomp/message'
|
19
22
|
|
20
23
|
module Stomp
|
21
|
-
|
22
|
-
# Low level connection which maps commands and supports
|
23
|
-
# synchronous receives
|
24
|
-
class Connection
|
25
|
-
|
26
|
-
def Connection.open(login = "", passcode = "", host='localhost', port=61613, reliable=FALSE, reconnectDelay=5, clientId=nil)
|
27
|
-
Connection.new login, passcode, host, port, reliable, reconnectDelay, clientId
|
28
|
-
end
|
29
|
-
|
30
|
-
# Create a connection, requires a login and passcode.
|
31
|
-
# Can accept a host (default is localhost), and port
|
32
|
-
# (default is 61613) to connect to
|
33
|
-
def initialize(login, passcode, host='localhost', port=61613, reliable=false, reconnectDelay=5, clientId=nil)
|
34
|
-
@host = host
|
35
|
-
@port = port
|
36
|
-
@login = login
|
37
|
-
@passcode = passcode
|
38
|
-
@clientId = clientId
|
39
|
-
@transmit_semaphore = Mutex.new
|
40
|
-
@read_semaphore = Mutex.new
|
41
|
-
@socket_semaphore = Mutex.new
|
42
|
-
@reliable = reliable
|
43
|
-
@reconnectDelay = reconnectDelay
|
44
|
-
@closed = FALSE
|
45
|
-
@subscriptions = {}
|
46
|
-
@failure = NIL
|
47
|
-
socket
|
48
|
-
end
|
49
|
-
|
50
|
-
def socket
|
51
|
-
# Need to look into why the following synchronize does not work.
|
52
|
-
#@read_semaphore.synchronize do
|
53
|
-
s = @socket;
|
54
|
-
while s == NIL or @failure != NIL
|
55
|
-
@failure = NIL
|
56
|
-
begin
|
57
|
-
s = TCPSocket.open @host, @port
|
58
|
-
connect_headers = {:login => @login, :passcode => @passcode}
|
59
|
-
connect_headers[:"client-id"] = @clientId if @clientId
|
60
|
-
_transmit(s, "CONNECT", connect_headers)
|
61
|
-
@connect = _receive(s)
|
62
|
-
# replay any subscriptions.
|
63
|
-
@subscriptions.each { |k,v| _transmit(s, "SUBSCRIBE", v) }
|
64
|
-
rescue
|
65
|
-
@failure = $!;
|
66
|
-
s=NIL;
|
67
|
-
raise unless @reliable
|
68
|
-
$stderr.print "connect failed: " + $! +" will retry in #{@reconnectDelay}\n";
|
69
|
-
sleep(@reconnectDelay);
|
70
|
-
end
|
71
|
-
end
|
72
|
-
@socket = s
|
73
|
-
return s;
|
74
|
-
#end
|
75
|
-
end
|
76
|
-
|
77
|
-
# Is this connection open?
|
78
|
-
def open?
|
79
|
-
!@closed
|
80
|
-
end
|
81
|
-
|
82
|
-
# Is this connection closed?
|
83
|
-
def closed?
|
84
|
-
@closed
|
85
|
-
end
|
86
|
-
|
87
|
-
# Begin a transaction, requires a name for the transaction
|
88
|
-
def begin name, headers={}
|
89
|
-
headers[:transaction] = name
|
90
|
-
transmit "BEGIN", headers
|
91
|
-
end
|
92
|
-
|
93
|
-
# Acknowledge a message, used then a subscription has specified
|
94
|
-
# client acknowledgement ( connection.subscribe "/queue/a", :ack => 'client'g
|
95
|
-
#
|
96
|
-
# Accepts a transaction header ( :transaction => 'some_transaction_id' )
|
97
|
-
def ack message_id, headers={}
|
98
|
-
headers['message-id'] = message_id
|
99
|
-
transmit "ACK", headers
|
100
|
-
end
|
101
|
-
|
102
|
-
# Commit a transaction by name
|
103
|
-
def commit name, headers={}
|
104
|
-
headers[:transaction] = name
|
105
|
-
transmit "COMMIT", headers
|
106
|
-
end
|
107
|
-
|
108
|
-
# Abort a transaction by name
|
109
|
-
def abort name, headers={}
|
110
|
-
headers[:transaction] = name
|
111
|
-
transmit "ABORT", headers
|
112
|
-
end
|
113
|
-
|
114
|
-
# Subscribe to a destination, must specify a name
|
115
|
-
def subscribe(name, headers = {}, subId=NIL)
|
116
|
-
headers[:destination] = name
|
117
|
-
transmit "SUBSCRIBE", headers
|
118
|
-
|
119
|
-
# Store the sub so that we can replay if we reconnect.
|
120
|
-
if @reliable
|
121
|
-
subId = name if subId==NIL
|
122
|
-
@subscriptions[subId]=headers
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
# Unsubscribe from a destination, must specify a name
|
127
|
-
def unsubscribe(name, headers = {}, subId=NIL)
|
128
|
-
headers[:destination] = name
|
129
|
-
transmit "UNSUBSCRIBE", headers
|
130
|
-
if @reliable
|
131
|
-
subId = name if subId==NIL
|
132
|
-
@subscriptions.delete(subId)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
# Send message to destination
|
137
|
-
#
|
138
|
-
# Accepts a transaction header ( :transaction => 'some_transaction_id' )
|
139
|
-
def send(destination, message, headers={})
|
140
|
-
headers[:destination] = destination
|
141
|
-
transmit "SEND", headers, message
|
142
|
-
end
|
143
|
-
|
144
|
-
# Close this connection
|
145
|
-
def disconnect(headers = {})
|
146
|
-
transmit "DISCONNECT", headers
|
147
|
-
end
|
148
|
-
|
149
|
-
# Return a pending message if one is available, otherwise
|
150
|
-
# return nil
|
151
|
-
def poll
|
152
|
-
@read_semaphore.synchronize do
|
153
|
-
return nil if @socket==NIL or !@socket.ready?
|
154
|
-
return receive
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
# Receive a frame, block until the frame is received
|
159
|
-
def __old_receive
|
160
|
-
# The recive my fail so we may need to retry.
|
161
|
-
while TRUE
|
162
|
-
begin
|
163
|
-
s = socket
|
164
|
-
return _receive(s)
|
165
|
-
rescue
|
166
|
-
@failure = $!;
|
167
|
-
raise unless @reliable
|
168
|
-
$stderr.print "receive failed: " + $!;
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
def receive
|
174
|
-
super_result = __old_receive()
|
175
|
-
if super_result.nil? && @reliable
|
176
|
-
$stderr.print "connection.receive returning EOF as nil - resetting connection.\n"
|
177
|
-
@socket = nil
|
178
|
-
super_result = __old_receive()
|
179
|
-
end
|
180
|
-
return super_result
|
181
|
-
end
|
182
|
-
|
183
|
-
private
|
184
|
-
def _receive( s )
|
185
|
-
line = ' '
|
186
|
-
@read_semaphore.synchronize do
|
187
|
-
line = s.gets while line =~ /^\s*$/
|
188
|
-
return NIL if line == NIL
|
189
|
-
Message.new do |m|
|
190
|
-
m.command = line.chomp
|
191
|
-
m.headers = {}
|
192
|
-
until (line = s.gets.chomp) == ''
|
193
|
-
k = (line.strip[0, line.strip.index(':')]).strip
|
194
|
-
v = (line.strip[line.strip.index(':') + 1, line.strip.length]).strip
|
195
|
-
m.headers[k] = v
|
196
|
-
end
|
197
|
-
|
198
|
-
if (m.headers['content-length'])
|
199
|
-
m.body = s.read m.headers['content-length'].to_i
|
200
|
-
c = s.getc
|
201
|
-
raise "Invalid content length received" unless c == 0
|
202
|
-
else
|
203
|
-
m.body = ''
|
204
|
-
until (c = s.getc) == 0
|
205
|
-
m.body << c.chr
|
206
|
-
end
|
207
|
-
end
|
208
|
-
#c = s.getc
|
209
|
-
#raise "Invalid frame termination received" unless c == 10
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
private
|
215
|
-
def transmit(command, headers={}, body='')
|
216
|
-
# The transmit my fail so we may need to retry.
|
217
|
-
while TRUE
|
218
|
-
begin
|
219
|
-
s = socket
|
220
|
-
_transmit(s, command, headers, body)
|
221
|
-
return
|
222
|
-
rescue
|
223
|
-
@failure = $!;
|
224
|
-
raise unless @reliable
|
225
|
-
$stderr.print "transmit failed: " + $!+"\n";
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|
229
|
-
|
230
|
-
private
|
231
|
-
def _transmit(s, command, headers={}, body='')
|
232
|
-
@transmit_semaphore.synchronize do
|
233
|
-
s.puts command
|
234
|
-
headers.each {|k,v| s.puts "#{k}:#{v}" }
|
235
|
-
s.puts "content-length: #{body.length}"
|
236
|
-
s.puts "content-type: text/plain; charset=UTF-8"
|
237
|
-
s.puts
|
238
|
-
s.write body
|
239
|
-
s.write "\0"
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
# Container class for frames, misnamed technically
|
245
|
-
class Message
|
246
|
-
attr_accessor :headers, :body, :command
|
247
|
-
|
248
|
-
def initialize
|
249
|
-
yield(self) if block_given?
|
250
|
-
end
|
251
|
-
|
252
|
-
def to_s
|
253
|
-
"<Stomp::Message headers=#{headers.inspect} body='#{body}' command='#{command}' >"
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
# Typical Stomp client class. Uses a listener thread to receive frames
|
258
|
-
# from the server, any thread can send.
|
259
|
-
#
|
260
|
-
# Receives all happen in one thread, so consider not doing much processing
|
261
|
-
# in that thread if you have much message volume.
|
262
|
-
class Client
|
263
|
-
|
264
|
-
# Accepts a username (default ""), password (default ""),
|
265
|
-
# host (default localhost), and port (default 61613)
|
266
|
-
def initialize user="", pass="", host="localhost", port=61613, reliable=false
|
267
|
-
if user =~ /stomp:\/\/([\w\.]+):(\d+)/
|
268
|
-
user = ""
|
269
|
-
pass = ""
|
270
|
-
host = $1
|
271
|
-
port = $2
|
272
|
-
reliable = false
|
273
|
-
elsif user =~ /stomp:\/\/([\w\.]+):(\w+)@(\w+):(\d+)/
|
274
|
-
user = $1
|
275
|
-
pass = $2
|
276
|
-
host = $3
|
277
|
-
port = $4
|
278
|
-
reliable = false
|
279
|
-
end
|
280
|
-
|
281
|
-
@id_mutex = Mutex.new
|
282
|
-
@ids = 1
|
283
|
-
@connection = Connection.open user, pass, host, port, reliable
|
284
|
-
@listeners = {}
|
285
|
-
@receipt_listeners = {}
|
286
|
-
@running = true
|
287
|
-
@replay_messages_by_txn = Hash.new
|
288
|
-
@listener_thread = Thread.start do
|
289
|
-
while @running
|
290
|
-
message = @connection.receive
|
291
|
-
case
|
292
|
-
when message == NIL:
|
293
|
-
break
|
294
|
-
when message.command == 'MESSAGE':
|
295
|
-
if listener = @listeners[message.headers['destination']]
|
296
|
-
listener.call(message)
|
297
|
-
end
|
298
|
-
when message.command == 'RECEIPT':
|
299
|
-
if listener = @receipt_listeners[message.headers['receipt-id']]
|
300
|
-
listener.call(message)
|
301
|
-
end
|
302
|
-
end
|
303
|
-
end
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
# Join the listener thread for this client,
|
308
|
-
# generally used to wait for a quit signal
|
309
|
-
def join
|
310
|
-
@listener_thread.join
|
311
|
-
end
|
312
|
-
|
313
|
-
# Accepts a username (default ""), password (default ""),
|
314
|
-
# host (default localhost), and port (default 61613)
|
315
|
-
def self.open user="", pass="", host="localhost", port=61613, reliable=false
|
316
|
-
Client.new user, pass, host, port, reliable
|
317
|
-
end
|
318
|
-
|
319
|
-
# Begin a transaction by name
|
320
|
-
def begin name, headers={}
|
321
|
-
@connection.begin name, headers
|
322
|
-
end
|
323
|
-
|
324
|
-
# Abort a transaction by name
|
325
|
-
def abort name, headers={}
|
326
|
-
@connection.abort name, headers
|
327
|
-
|
328
|
-
# lets replay any ack'd messages in this transaction
|
329
|
-
replay_list = @replay_messages_by_txn[name]
|
330
|
-
if replay_list
|
331
|
-
replay_list.each do |message|
|
332
|
-
if listener = @listeners[message.headers['destination']]
|
333
|
-
listener.call(message)
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
# Commit a transaction by name
|
340
|
-
def commit name, headers={}
|
341
|
-
txn_id = headers[:transaction]
|
342
|
-
@replay_messages_by_txn.delete(txn_id)
|
343
|
-
@connection.commit name, headers
|
344
|
-
end
|
345
|
-
|
346
|
-
# Subscribe to a destination, must be passed a block
|
347
|
-
# which will be used as a callback listener
|
348
|
-
#
|
349
|
-
# Accepts a transaction header ( :transaction => 'some_transaction_id' )
|
350
|
-
def subscribe destination, headers={}
|
351
|
-
raise "No listener given" unless block_given?
|
352
|
-
@listeners[destination] = lambda {|msg| yield msg}
|
353
|
-
@connection.subscribe destination, headers
|
354
|
-
end
|
355
|
-
|
356
|
-
# Unsubecribe from a channel
|
357
|
-
def unsubscribe name, headers={}
|
358
|
-
@connection.unsubscribe name, headers
|
359
|
-
@listeners[name] = nil
|
360
|
-
end
|
361
|
-
|
362
|
-
# Acknowledge a message, used then a subscription has specified
|
363
|
-
# client acknowledgement ( connection.subscribe "/queue/a", :ack => 'client'g
|
364
|
-
#
|
365
|
-
# Accepts a transaction header ( :transaction => 'some_transaction_id' )
|
366
|
-
def acknowledge message, headers={}
|
367
|
-
txn_id = headers[:transaction]
|
368
|
-
if txn_id
|
369
|
-
# lets keep around messages ack'd in this transaction in case we rollback
|
370
|
-
replay_list = @replay_messages_by_txn[txn_id]
|
371
|
-
if replay_list == nil
|
372
|
-
replay_list = []
|
373
|
-
@replay_messages_by_txn[txn_id] = replay_list
|
374
|
-
end
|
375
|
-
replay_list << message
|
376
|
-
end
|
377
|
-
if block_given?
|
378
|
-
headers['receipt'] = register_receipt_listener lambda {|r| yield r}
|
379
|
-
end
|
380
|
-
@connection.ack message.headers['message-id'], headers
|
381
|
-
end
|
382
|
-
|
383
|
-
# Send message to destination
|
384
|
-
#
|
385
|
-
# If a block is given a receipt will be requested and passed to the
|
386
|
-
# block on receipt
|
387
|
-
#
|
388
|
-
# Accepts a transaction header ( :transaction => 'some_transaction_id' )
|
389
|
-
def send destination, message, headers = {}
|
390
|
-
if block_given?
|
391
|
-
headers['receipt'] = register_receipt_listener lambda {|r| yield r}
|
392
|
-
end
|
393
|
-
@connection.send destination, message, headers
|
394
|
-
end
|
395
|
-
|
396
|
-
# Is this client open?
|
397
|
-
def open?
|
398
|
-
@connection.open?
|
399
|
-
end
|
400
|
-
|
401
|
-
# Close out resources in use by this client
|
402
|
-
def close
|
403
|
-
@connection.disconnect
|
404
|
-
@running = false
|
405
|
-
end
|
406
|
-
|
407
|
-
private
|
408
|
-
def register_receipt_listener listener
|
409
|
-
id = -1
|
410
|
-
@id_mutex.synchronize do
|
411
|
-
id = @ids.to_s
|
412
|
-
@ids = @ids.succ
|
413
|
-
end
|
414
|
-
@receipt_listeners[id] = listener
|
415
|
-
id
|
416
|
-
end
|
417
|
-
|
418
|
-
end
|
419
24
|
end
|
25
|
+
|