totty-amqp 0.6.7.1.totty

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/README +143 -0
  2. data/Rakefile +20 -0
  3. data/amqp.todo +32 -0
  4. data/doc/EXAMPLE_01_PINGPONG +2 -0
  5. data/doc/EXAMPLE_02_CLOCK +2 -0
  6. data/doc/EXAMPLE_03_STOCKS +2 -0
  7. data/doc/EXAMPLE_04_MULTICLOCK +2 -0
  8. data/doc/EXAMPLE_05_ACK +2 -0
  9. data/doc/EXAMPLE_05_POP +2 -0
  10. data/doc/EXAMPLE_06_HASHTABLE +2 -0
  11. data/examples/amqp/simple.rb +79 -0
  12. data/examples/mq/ack.rb +45 -0
  13. data/examples/mq/clock.rb +56 -0
  14. data/examples/mq/hashtable.rb +52 -0
  15. data/examples/mq/internal.rb +49 -0
  16. data/examples/mq/logger.rb +88 -0
  17. data/examples/mq/multiclock.rb +49 -0
  18. data/examples/mq/pingpong.rb +45 -0
  19. data/examples/mq/pop.rb +43 -0
  20. data/examples/mq/primes-simple.rb +19 -0
  21. data/examples/mq/primes.rb +99 -0
  22. data/examples/mq/stocks.rb +58 -0
  23. data/lib/amqp/buffer.rb +395 -0
  24. data/lib/amqp/client.rb +210 -0
  25. data/lib/amqp/frame.rb +124 -0
  26. data/lib/amqp/protocol.rb +212 -0
  27. data/lib/amqp/server.rb +99 -0
  28. data/lib/amqp/spec.rb +832 -0
  29. data/lib/amqp/version.rb +3 -0
  30. data/lib/amqp.rb +152 -0
  31. data/lib/ext/blankslate.rb +7 -0
  32. data/lib/ext/em.rb +8 -0
  33. data/lib/ext/emfork.rb +69 -0
  34. data/lib/mq/exchange.rb +314 -0
  35. data/lib/mq/header.rb +33 -0
  36. data/lib/mq/logger.rb +89 -0
  37. data/lib/mq/queue.rb +455 -0
  38. data/lib/mq/rpc.rb +100 -0
  39. data/lib/mq.rb +877 -0
  40. data/old/README +30 -0
  41. data/old/Rakefile +12 -0
  42. data/old/amqp-0.8.json +606 -0
  43. data/old/amqp_spec.rb +796 -0
  44. data/old/amqpc.rb +695 -0
  45. data/old/codegen.rb +148 -0
  46. data/protocol/amqp-0.8.json +617 -0
  47. data/protocol/amqp-0.8.xml +3908 -0
  48. data/protocol/codegen.rb +173 -0
  49. data/protocol/doc.txt +281 -0
  50. data/research/api.rb +88 -0
  51. data/research/primes-forked.rb +63 -0
  52. data/research/primes-processes.rb +135 -0
  53. data/research/primes-threaded.rb +49 -0
  54. data/totty-amqp.gemspec +87 -0
  55. metadata +142 -0
@@ -0,0 +1,173 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+ name = 'amqp-0.8.json'
5
+ path = File.dirname(__FILE__)+'/'+name
6
+ s = JSON.parse(File.read(path))
7
+
8
+ # require 'pp'
9
+ # pp(s)
10
+ # exit
11
+
12
+ require 'erb'
13
+
14
+ puts ERB.new(%q[
15
+ #:stopdoc:
16
+ # this file was autogenerated on <%= Time.now.to_s %>
17
+ # using <%= name.ljust(16) %> (mtime: <%= File.mtime(path) %>)
18
+ #
19
+ # DO NOT EDIT! (edit protocol/codegen.rb instead, and run `rake codegen`)
20
+
21
+ module AMQP
22
+ HEADER = <%= s['name'].dump %>.freeze
23
+ VERSION_MAJOR = <%= s['major-version'] %>
24
+ VERSION_MINOR = <%= s['minor-version'] %>
25
+ PORT = <%= s['port'] %>
26
+
27
+ class Frame
28
+ def self.types
29
+ @types ||= {}
30
+ end
31
+
32
+ def self.Frame id
33
+ (@_base_frames ||= {})[id] ||= Class.new(Frame) do
34
+ class_eval %[
35
+ def self.inherited klass
36
+ klass.const_set(:ID, #{id})
37
+ Frame.types[#{id}] = klass
38
+ end
39
+ ]
40
+ end
41
+ end
42
+
43
+ <%- s['constants'].select{|c| (1..8).include? c['value'] }.each do |c| -%>
44
+ class <%= c['name'].gsub(/^FRAME-/,'').split('-').map{|w| w.downcase.capitalize}.join.ljust(9) -%> < Frame( <%= c['value'] -%> ); end
45
+ <%- end -%>
46
+
47
+ FOOTER = <%= frame_end = s['constants'].find{|c| c['name'] == 'FRAME-END' }['value'] %>
48
+ end
49
+
50
+ RESPONSES = {
51
+ <%- s['constants'].select{|c| c['value'] != frame_end and (200..500).include? c['value'] }.each do |c| -%>
52
+ <%= c['value'] %> => :<%= c['name'].tr('-', '_').gsub(/^FRAME_/,'').upcase -%>,
53
+ <%- end -%>
54
+ }
55
+
56
+ FIELDS = [
57
+ <%- s['domains'].select{|d| d.first == d.last }.each do |d| -%>
58
+ :<%= d.first -%>,
59
+ <%- end -%>
60
+ ]
61
+
62
+ module Protocol
63
+ class Class
64
+ class << self
65
+ FIELDS.each do |f|
66
+ class_eval %[
67
+ def #{f} name
68
+ properties << [ :#{f}, name ] unless properties.include?([:#{f}, name])
69
+ attr_accessor name
70
+ end
71
+ ]
72
+ end
73
+
74
+ def properties() @properties ||= [] end
75
+
76
+ def id() self::ID end
77
+ def name() self::NAME end
78
+ end
79
+
80
+ class Method
81
+ class << self
82
+ FIELDS.each do |f|
83
+ class_eval %[
84
+ def #{f} name
85
+ arguments << [ :#{f}, name ] unless arguments.include?([:#{f}, name])
86
+ attr_accessor name
87
+ end
88
+ ]
89
+ end
90
+
91
+ def arguments() @arguments ||= [] end
92
+
93
+ def section() Protocol.const_get(self.to_s[/Protocol::(.+?)::/,1]) end
94
+ def id() self::ID end
95
+ def name() self::NAME end
96
+ end
97
+
98
+ def == b
99
+ self.class.arguments.inject(true) do |eql, (type, name)|
100
+ eql and __send__("#{name}") == b.__send__("#{name}")
101
+ end
102
+ end
103
+ end
104
+
105
+ def self.methods() @methods ||= {} end
106
+
107
+ def self.Method(id, name)
108
+ @_base_methods ||= {}
109
+ @_base_methods[id] ||= ::Class.new(Method) do
110
+ class_eval %[
111
+ def self.inherited klass
112
+ klass.const_set(:ID, #{id})
113
+ klass.const_set(:NAME, :#{name.to_s})
114
+ klass.section.methods[#{id}] = klass
115
+ klass.section.methods[klass::NAME] = klass
116
+ end
117
+ ]
118
+ end
119
+ end
120
+ end
121
+
122
+ def self.classes() @classes ||= {} end
123
+
124
+ def self.Class(id, name)
125
+ @_base_classes ||= {}
126
+ @_base_classes[id] ||= ::Class.new(Class) do
127
+ class_eval %[
128
+ def self.inherited klass
129
+ klass.const_set(:ID, #{id})
130
+ klass.const_set(:NAME, :#{name.to_s})
131
+ Protocol.classes[#{id}] = klass
132
+ Protocol.classes[klass::NAME] = klass
133
+ end
134
+ ]
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ module AMQP
141
+ module Protocol
142
+ <%- s['classes'].each do |c| -%>
143
+ class <%= c['name'].capitalize.ljust(12) %> < Class( <%= c['id'].to_s.rjust(3) %>, :<%= c['name'].ljust(12) %> ); end
144
+ <%- end -%>
145
+
146
+ <%- s['classes'].each do |c| -%>
147
+ class <%= c['name'].capitalize %>
148
+ <%- c['properties'].each do |p| -%>
149
+ <%= p['type'].ljust(10) %> :<%= p['name'].tr('-','_') %>
150
+ <%- end if c['properties'] -%>
151
+
152
+ <%- c['methods'].each do |m| -%>
153
+ class <%= m['name'].capitalize.gsub(/-(.)/){ "#{$1.upcase}"}.ljust(12) %> < Method( <%= m['id'].to_s.rjust(3) %>, :<%= m['name'].tr('- ','_').ljust(14) %> ); end
154
+ <%- end -%>
155
+
156
+ <%- c['methods'].each do |m| -%>
157
+ class <%= m['name'].capitalize.gsub(/-(.)/){ "#{$1.upcase}"} %>
158
+ <%- m['arguments'].each do |a| -%>
159
+ <%- if a['domain'] -%>
160
+ <%= s['domains'].find{|k,v| k == a['domain']}.last.ljust(10) %> :<%= a['name'].tr('- ','_') %>
161
+ <%- else -%>
162
+ <%= a['type'].ljust(10) %> :<%= a['name'].tr('- ','_') %>
163
+ <%- end -%>
164
+ <%- end if m['arguments'] -%>
165
+ end
166
+
167
+ <%- end -%>
168
+ end
169
+
170
+ <%- end -%>
171
+ end
172
+ end
173
+ ].gsub!(/^ /,''), nil, '>-%').result(binding)
data/protocol/doc.txt ADDED
@@ -0,0 +1,281 @@
1
+ ["connected"]
2
+
3
+ ["send_data", "AMQP"]
4
+
5
+ ["send_data", "\001\001\b\000"]
6
+
7
+ ["receive_data",
8
+ "\001\000\000\000\000\001&\000\n\000\n\b\000\000\000\001\001\aproductS\000\000\000\bRabbitMQ\aversionS\000\000\000\v%%VERSION%%\bplatformS\000\000\000\nErlang/OTP\tcopyrightS\000\000\000gCopyright (C) 2007-2008 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.\vinformationS\000\000\0005Licensed under the MPL. See http://www.rabbitmq.com/\000\000\000\016PLAIN AMQPLAIN\000\000\000\005en_US\316"]
9
+
10
+ ["receive",
11
+ #<AMQP::Frame::Method:0x11b90d0
12
+ @channel=0,
13
+ @payload=
14
+ #<AMQP::Protocol::Connection::Start:0x11b8d38
15
+ @debug=1,
16
+ @locales="en_US",
17
+ @mechanisms="PLAIN AMQPLAIN",
18
+ @server_properties=
19
+ {:information=>"Licensed under the MPL. See http://www.rabbitmq.com/",
20
+ :copyright=>
21
+ "Copyright (C) 2007-2008 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.",
22
+ :platform=>"Erlang/OTP",
23
+ :version=>"%%VERSION%%",
24
+ :product=>"RabbitMQ"},
25
+ @version_major=8,
26
+ @version_minor=0>>]
27
+
28
+ ["send",
29
+ #<AMQP::Frame::Method:0x11a3078
30
+ @channel=0,
31
+ @payload=
32
+ #<AMQP::Protocol::Connection::StartOk:0x11a3294
33
+ @client_properties=
34
+ {:information=>"http://github.com/tmm1/amqp",
35
+ :version=>"0.1.0",
36
+ :product=>"AMQP",
37
+ :platform=>"Ruby/EventMachine"},
38
+ @debug=1,
39
+ @locale="en_US",
40
+ @mechanism="AMQPLAIN",
41
+ @response={:LOGIN=>"guest", :PASSWORD=>"guest"}>>]
42
+
43
+ ["send_data",
44
+ "\001\000\000\000\000\000\254\000\n\000\v\000\000\000n\vinformationS\000\000\000\ehttp://github.com/tmm1/amqp\aversionS\000\000\000\0050.1.0\aproductS\000\000\000\004AMQP\bplatformS\000\000\000\021Ruby/EventMachine\bAMQPLAIN\000\000\000#\005LOGINS\000\000\000\005guest\bPASSWORDS\000\000\000\005guest\005en_US\316"]
45
+
46
+ ["receive_data",
47
+ "\001\000\000\000\000\000\f\000\n\000\036\000\000\000\002\000\000\000\000\316"]
48
+
49
+ ["receive",
50
+ #<AMQP::Frame::Method:0x11898e4
51
+ @channel=0,
52
+ @payload=
53
+ #<AMQP::Protocol::Connection::Tune:0x118954c
54
+ @channel_max=0,
55
+ @debug=1,
56
+ @frame_max=131072,
57
+ @heartbeat=0>>]
58
+
59
+ ["send",
60
+ #<AMQP::Frame::Method:0x117fb14
61
+ @channel=0,
62
+ @payload=
63
+ #<AMQP::Protocol::Connection::TuneOk:0x117fd1c
64
+ @channel_max=0,
65
+ @debug=1,
66
+ @frame_max=131072,
67
+ @heartbeat=0>>]
68
+
69
+ ["send_data",
70
+ "\001\000\000\000\000\000\f\000\n\000\037\000\000\000\002\000\000\000\000\316"]
71
+
72
+ ["send",
73
+ #<AMQP::Frame::Method:0x11741d8
74
+ @channel=0,
75
+ @payload=
76
+ #<AMQP::Protocol::Connection::Open:0x1174408
77
+ @capabilities="",
78
+ @debug=1,
79
+ @insist=nil,
80
+ @virtual_host="/">>]
81
+
82
+ ["send_data", "\001\000\000\000\000\000\b\000\n\000(\001/\000\000\316"]
83
+
84
+ ["receive_data",
85
+ "\001\000\000\000\000\000\025\000\n\000)\020julie.local:5672\316"]
86
+
87
+ ["receive",
88
+ #<AMQP::Frame::Method:0x11665ec
89
+ @channel=0,
90
+ @payload=
91
+ #<AMQP::Protocol::Connection::OpenOk:0x1166254
92
+ @debug=1,
93
+ @known_hosts="julie.local:5672">>]
94
+
95
+ ["send",
96
+ #<AMQP::Frame::Method:0x115e950
97
+ @channel=1,
98
+ @payload=
99
+ #<AMQP::Protocol::Channel::Open:0x115ebf8 @debug=1, @out_of_band=nil>>]
100
+
101
+ ["send_data", "\001\000\001\000\000\000\005\000\024\000\n\000\316"]
102
+
103
+ ["receive_data", "\001\000\001\000\000\000\004\000\024\000\v\316"]
104
+
105
+ ["receive",
106
+ #<AMQP::Frame::Method:0x11534d8
107
+ @channel=1,
108
+ @payload=#<AMQP::Protocol::Channel::OpenOk:0x1153140 @debug=1>>]
109
+
110
+ ["send",
111
+ #<AMQP::Frame::Method:0x114d0ec
112
+ @channel=1,
113
+ @payload=
114
+ #<AMQP::Protocol::Access::Request:0x114d434
115
+ @active=true,
116
+ @debug=1,
117
+ @exclusive=nil,
118
+ @passive=nil,
119
+ @read=true,
120
+ @realm="/data",
121
+ @write=true>>]
122
+
123
+ ["send_data", "\001\000\001\000\000\000\v\000\036\000\n\005/data\034\316"]
124
+
125
+ ["receive_data", "\001\000\001\000\000\000\006\000\036\000\v\000e\316"]
126
+
127
+ ["receive",
128
+ #<AMQP::Frame::Method:0x113c9a4
129
+ @channel=1,
130
+ @payload=
131
+ #<AMQP::Protocol::Access::RequestOk:0x113c60c @debug=1, @ticket=101>>]
132
+
133
+ ["send",
134
+ #<AMQP::Frame::Method:0x1135000
135
+ @channel=1,
136
+ @payload=
137
+ #<AMQP::Protocol::Queue::Declare:0x1135460
138
+ @arguments=nil,
139
+ @auto_delete=true,
140
+ @debug=1,
141
+ @durable=nil,
142
+ @exclusive=nil,
143
+ @nowait=nil,
144
+ @passive=nil,
145
+ @queue="",
146
+ @ticket=101>>]
147
+
148
+ ["send_data",
149
+ "\001\000\001\000\000\000\f\0002\000\n\000e\000\b\000\000\000\000\316"]
150
+
151
+ ["receive_data",
152
+ "\001\000\001\000\000\000-\0002\000\v amq.gen-RCSkW3cCvMc1I0wXBcLYSg==\000\000\000\000\000\000\000\000\316"]
153
+
154
+ ["receive",
155
+ #<AMQP::Frame::Method:0x1122838
156
+ @channel=1,
157
+ @payload=
158
+ #<AMQP::Protocol::Queue::DeclareOk:0x11224a0
159
+ @consumer_count=0,
160
+ @debug=1,
161
+ @message_count=0,
162
+ @queue="amq.gen-RCSkW3cCvMc1I0wXBcLYSg==">>]
163
+
164
+ ["send",
165
+ #<AMQP::Frame::Method:0x1118860
166
+ @channel=1,
167
+ @payload=
168
+ #<AMQP::Protocol::Queue::Bind:0x1118ba8
169
+ @arguments=nil,
170
+ @debug=1,
171
+ @exchange="",
172
+ @nowait=nil,
173
+ @queue="amq.gen-RCSkW3cCvMc1I0wXBcLYSg==",
174
+ @routing_key="test_route",
175
+ @ticket=101>>]
176
+
177
+ ["send_data",
178
+ "\001\000\001\000\000\0008\0002\000\024\000e amq.gen-RCSkW3cCvMc1I0wXBcLYSg==\000\ntest_route\000\000\000\000\000\316"]
179
+
180
+ ["receive_data", "\001\000\001\000\000\000\004\0002\000\025\316"]
181
+
182
+ ["receive",
183
+ #<AMQP::Frame::Method:0x1107a4c
184
+ @channel=1,
185
+ @payload=#<AMQP::Protocol::Queue::BindOk:0x11076b4 @debug=1>>]
186
+
187
+ ["send",
188
+ #<AMQP::Frame::Method:0x11015d4
189
+ @channel=1,
190
+ @payload=
191
+ #<AMQP::Protocol::Basic::Consume:0x11019bc
192
+ @consumer_tag=nil,
193
+ @debug=1,
194
+ @exclusive=nil,
195
+ @no_ack=true,
196
+ @no_local=nil,
197
+ @nowait=nil,
198
+ @queue="amq.gen-RCSkW3cCvMc1I0wXBcLYSg==",
199
+ @ticket=101>>]
200
+
201
+ ["send_data",
202
+ "\001\000\001\000\000\000)\000<\000\024\000e amq.gen-RCSkW3cCvMc1I0wXBcLYSg==\000\002\316"]
203
+
204
+ ["receive_data",
205
+ "\001\000\001\000\000\000&\000<\000\025!amq.ctag-wFbDeuYKGEm7tXh8oaE5Qg==\316"]
206
+
207
+ ["receive",
208
+ #<AMQP::Frame::Method:0x5f6cfc
209
+ @channel=1,
210
+ @payload=
211
+ #<AMQP::Protocol::Basic::ConsumeOk:0x5f67e8
212
+ @consumer_tag="amq.ctag-wFbDeuYKGEm7tXh8oaE5Qg==",
213
+ @debug=1>>]
214
+
215
+ ["send",
216
+ #<AMQP::Frame::Method:0x5e3dc8
217
+ @channel=1,
218
+ @payload=
219
+ #<AMQP::Protocol::Basic::Publish:0x5e4264
220
+ @debug=1,
221
+ @exchange="",
222
+ @immediate=nil,
223
+ @mandatory=nil,
224
+ @routing_key="test_route",
225
+ @ticket=101>>]
226
+
227
+ ["send_data",
228
+ "\001\000\001\000\000\000\023\000<\000(\000e\000\ntest_route\000\316"]
229
+
230
+ ["send",
231
+ #<AMQP::Frame::Header:0x5bc994
232
+ @channel=1,
233
+ @payload=
234
+ #<AMQP::Protocol::Header:0x5bcb88
235
+ @klass=AMQP::Protocol::Basic,
236
+ @properties=
237
+ {:delivery_mode=>1,
238
+ :priority=>1,
239
+ :content_type=>"application/octet-stream"},
240
+ @size=15,
241
+ @weight=0>>]
242
+
243
+ ["send_data",
244
+ "\002\000\001\000\000\000)\000<\000\000\000\000\000\000\000\000\000\017\230\000\030application/octet-stream\001\001\316"]
245
+
246
+ ["send", #<AMQP::Frame::Body:0x57cfc4 @channel=1, @payload="this is a test!">]
247
+
248
+ ["send_data", "\003\000\001\000\000\000\017this is a test!\316"]
249
+
250
+ ["receive_data",
251
+ "\001\000\001\000\000\000;\000<\000<!amq.ctag-wFbDeuYKGEm7tXh8oaE5Qg==\000\000\000\000\000\000\000\001\000\000\ntest_route\316\002\000\001\000\000\000)\000<\000\000\000\000\000\000\000\000\000\017\230\000\030application/octet-stream\001\001\316\003\000\001\000\000\000\017this is a test!\316"]
252
+
253
+ ["receive",
254
+ #<AMQP::Frame::Method:0x55cbe8
255
+ @channel=1,
256
+ @payload=
257
+ #<AMQP::Protocol::Basic::Deliver:0x55b810
258
+ @consumer_tag="amq.ctag-wFbDeuYKGEm7tXh8oaE5Qg==",
259
+ @debug=1,
260
+ @delivery_tag=1,
261
+ @exchange="",
262
+ @redelivered=false,
263
+ @routing_key="test_route">>]
264
+
265
+ ["receive",
266
+ #<AMQP::Frame::Header:0x537af0
267
+ @channel=1,
268
+ @payload=
269
+ #<AMQP::Protocol::Header:0x537a28
270
+ @klass=AMQP::Protocol::Basic,
271
+ @properties=
272
+ {:delivery_mode=>1,
273
+ :priority=>1,
274
+ :content_type=>"application/octet-stream"},
275
+ @size=15,
276
+ @weight=0>>]
277
+
278
+ ["receive",
279
+ #<AMQP::Frame::Body:0x505f64 @channel=1, @payload="this is a test!">]
280
+
281
+ ^C["disconnected"]
data/research/api.rb ADDED
@@ -0,0 +1,88 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'rubygems'
3
+ require 'amqp'
4
+
5
+ # AMQP.start do |amqp|
6
+ # amqp.channel!(1)
7
+ #
8
+ # q = amqp.queue.declare(:queue => 'test',
9
+ # :exclusive => false,
10
+ # :auto_delete => true)
11
+ #
12
+ # q.bind(:exchange => '',
13
+ # :routing_key => 'test_route')
14
+ #
15
+ # amqp.basic.consume(:queue => q,
16
+ # :no_local => false,
17
+ # :no_ack => true) { |header, body|
18
+ # p ['got', header, body]
19
+ # }
20
+ # end
21
+
22
+ AMQP.start do |amqp|
23
+ amqp.exchange('my_exchange', :topic) do |e|
24
+ e.publish(routing_key, data, :header => 'blah')
25
+ end
26
+
27
+ amqp.queue('my_queue').subscribe do |header, body|
28
+ p ['got', header, body]
29
+ end
30
+ end
31
+
32
+ def MQ.method_missing meth, *args, &blk
33
+ (Thread.current[:mq] ||= MQ.new).__send__(meth, *args, &blk)
34
+ end
35
+
36
+ mq = MQ.new
37
+ mq.direct.publish('alkjsdf', :key => 'name')
38
+ mq.topic # 'amq.topic'
39
+ mq.topic('test').publish('some data', :key => 'stock.usd.*')
40
+
41
+ # amq.queue('user1').bind(amq.topic('conversation.1'))
42
+
43
+ mq.queue('abc').get{}
44
+ mq.queue('abc').peek{}
45
+ mq.queue('abc').subscribe{ |body|
46
+
47
+ }
48
+
49
+ mq.queue('abc').bind(:exchange => mq.topic, :routing_key => 'abc', :nowait => true, :arguments => {})
50
+
51
+ if $0 =~ /bacon/ or __FILE__ == $0
52
+ require 'bacon'
53
+
54
+ describe MQ do
55
+ before do
56
+ @mq = MQ.new
57
+ end
58
+
59
+ should 'have a channel' do
60
+ @mq.channel.should.be.kind_of? Fixnum
61
+ @mq.channel.should == 1
62
+ end
63
+
64
+ should 'give each thread a message queue' do
65
+ class MQ
66
+ @@cur_channel = 0
67
+ end
68
+ MQ.channel.should == 1
69
+ Thread.new{ MQ.channel }.value.should == 2
70
+ Thread.new{ MQ.channel }.value.should == 3
71
+ end
72
+
73
+ should 'create direct exchanges' do
74
+ @mq.direct.name.should == 'amq.direct'
75
+ @mq.direct(nil).name.should =~ /^\d+$/
76
+ @mq.direct('name').name.should == 'name'
77
+ end
78
+
79
+ should 'create fanout and topic exchanges' do
80
+ @mq.fanout.name.should == 'amq.fanout'
81
+ @mq.topic.name.should == 'amq.topic'
82
+ end
83
+
84
+ should 'create queues' do
85
+ q = @mq.queue('test')
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,63 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'mq'
3
+
4
+ MAX = 5000
5
+
6
+ def EM.fork &blk
7
+ raise if reactor_running?
8
+
9
+ unless @forks
10
+ at_exit{
11
+ @forks.each{ |pid| Process.kill('KILL', pid) }
12
+ }
13
+ end
14
+
15
+ (@forks ||= []) << Kernel.fork do
16
+ EM.run(&blk)
17
+ end
18
+ end
19
+
20
+ def log *args
21
+ p args
22
+ end
23
+
24
+ # MQ.logging = true
25
+
26
+ # worker
27
+
28
+ workers = ARGV[0] ? (Integer(ARGV[0]) rescue 2) : 2
29
+
30
+ workers.times do
31
+ EM.fork{
32
+ log "prime checker", Process.pid, :started
33
+
34
+ class Fixnum
35
+ def prime?
36
+ ('1' * self) !~ /^1?$|^(11+?)\1+$/
37
+ end
38
+ end
39
+
40
+ MQ.queue('prime checker').subscribe{ |info, num|
41
+ log "prime checker #{Process.pid}", :prime?, num
42
+ if Integer(num).prime?
43
+ MQ.queue(info.reply_to).publish(num, :reply_to => Process.pid)
44
+ end
45
+ }
46
+ }
47
+ end
48
+
49
+ # controller
50
+
51
+ EM.run{
52
+ MQ.queue('prime collector').subscribe{ |info, prime|
53
+ log 'prime collector', :received, prime, :from, info.reply_to
54
+ (@primes ||= []) << Integer(prime)
55
+ EM.stop_event_loop if prime == '499'
56
+ }
57
+
58
+ MAX.times do |i|
59
+ EM.next_tick do
60
+ MQ.queue('prime checker').publish((i+1).to_s, :reply_to => 'prime collector')
61
+ end
62
+ end
63
+ }