tmm1-amqp 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,148 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+
4
+ s = JSON.parse(File.read('amqp-0.8.json'))
5
+
6
+ # require 'pp'
7
+ # pp(s)
8
+ # exit
9
+
10
+ require 'erb'
11
+
12
+ puts ERB.new(%q[
13
+ module AMQP
14
+ HEADER = <%= s['name'].dump %>.freeze
15
+ VERSION_MAJOR = <%= s['major-version'] %>
16
+ VERSION_MINOR = <%= s['minor-version'] %>
17
+ PORT = <%= s['port'] %>
18
+
19
+ class Frame
20
+ TYPES = [
21
+ nil,
22
+ <%- s['constants'].select{|c| (1..8).include? c['value'] }.each do |c| -%>
23
+ :<%= c['name'].tr('-', '_').gsub(/^FRAME_/,'').upcase -%>,
24
+ <%- end -%>
25
+ ]
26
+ FOOTER = <%= frame_end = s['constants'].find{|c| c['name'] == 'FRAME-END' }['value'] %>
27
+ end
28
+
29
+ RESPONSES = {
30
+ <%- s['constants'].select{|c| c['value'] != frame_end and (200..500).include? c['value'] }.each do |c| -%>
31
+ <%= c['value'] %> => :<%= c['name'].tr('-', '_').gsub(/^FRAME_/,'').upcase -%>,
32
+ <%- end -%>
33
+ }
34
+
35
+ FIELDS = [
36
+ <%- s['domains'].select{|d| d.first == d.last }.each do |d| -%>
37
+ :<%= d.first -%>,
38
+ <%- end -%>
39
+ ]
40
+
41
+ module Protocol
42
+ class Class
43
+ class << self
44
+ FIELDS.each do |f|
45
+ class_eval %[
46
+ def #{f} name
47
+ properties << [ :#{f}, name ] unless properties.include?([:#{f}, name])
48
+ attr_accessor name
49
+ end
50
+ ]
51
+ end
52
+
53
+ def properties() @properties ||= [] end
54
+
55
+ def id() self::ID end
56
+ def name() self::NAME end
57
+ end
58
+
59
+ class Method
60
+ class << self
61
+ FIELDS.each do |f|
62
+ class_eval %[
63
+ def #{f} name
64
+ arguments << [ :#{f}, name ] unless arguments.include?([:#{f}, name])
65
+ attr_accessor name
66
+ end
67
+ ]
68
+ end
69
+
70
+ def arguments() @arguments ||= [] end
71
+
72
+ def parent() Protocol.const_get(self.to_s[/Protocol::(.+?)::/,1]) end
73
+ def id() self::ID end
74
+ def name() self::NAME end
75
+ end
76
+
77
+ def == b
78
+ self.class.arguments.inject(true) do |eql, (type, name)|
79
+ eql and __send__("#{name}") == b.__send__("#{name}")
80
+ end
81
+ end
82
+ end
83
+
84
+ def self.methods() @methods ||= {} end
85
+
86
+ def self.Method(id, name)
87
+ @_base_methods ||= {}
88
+ @_base_methods[id] ||= ::Class.new(Method) do
89
+ class_eval %[
90
+ def self.inherited klass
91
+ klass.const_set(:ID, #{id})
92
+ klass.const_set(:NAME, :#{name.to_s})
93
+ klass.parent.methods[#{id}] = klass
94
+ klass.parent.methods[klass::NAME] = klass
95
+ end
96
+ ]
97
+ end
98
+ end
99
+ end
100
+
101
+ def self.classes() @classes ||= {} end
102
+
103
+ def self.Class(id, name)
104
+ @_base_classes ||= {}
105
+ @_base_classes[id] ||= ::Class.new(Class) do
106
+ class_eval %[
107
+ def self.inherited klass
108
+ klass.const_set(:ID, #{id})
109
+ klass.const_set(:NAME, :#{name.to_s})
110
+ Protocol.classes[#{id}] = klass
111
+ Protocol.classes[klass::NAME] = klass
112
+ end
113
+ ]
114
+ end
115
+ end
116
+
117
+ <%- s['classes'].each do |c| -%>
118
+ class <%= c['name'].capitalize.ljust(12) %> < Class(<%= c['id'] %>, :<%= c['name'] %>); end
119
+ <%- end -%>
120
+
121
+ <%- s['classes'].each do |c| -%>
122
+ class <%= c['name'].capitalize %>
123
+ <%- c['properties'].each do |p| -%>
124
+ <%= p['type'].ljust(10) %> :<%= p['name'].tr('-','_') %>
125
+ <%- end if c['properties'] -%>
126
+
127
+ <%- c['methods'].each do |m| -%>
128
+ class <%= m['name'].capitalize.gsub(/-(.)/){ "#{$1.upcase}"}.ljust(12) %> < Method(<%= m['id'] %>, :<%= m['name'].tr('- ','_') %>); end
129
+ <%- end -%>
130
+
131
+ <%- c['methods'].each do |m| -%>
132
+ class <%= m['name'].capitalize.gsub(/-(.)/){ "#{$1.upcase}"} %>
133
+ <%- m['arguments'].each do |a| -%>
134
+ <%- if a['domain'] -%>
135
+ <%= s['domains'].find{|k,v| k == a['domain']}.last.ljust(10) %> :<%= a['name'].tr('- ','_') %>
136
+ <%- else -%>
137
+ <%= a['type'].ljust(10) %> :<%= a['name'].tr('- ','_') %>
138
+ <%- end -%>
139
+ <%- end if m['arguments'] -%>
140
+ end
141
+
142
+ <%- end -%>
143
+ end
144
+
145
+ <%- end -%>
146
+ end
147
+ end
148
+ ].gsub!(/^ /,''), nil, '>-%').result(binding)
@@ -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
+ }
@@ -0,0 +1,135 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'mq'
3
+
4
+ MAX = 500
5
+
6
+ EM.run{
7
+
8
+ def log *args
9
+ p args
10
+ end
11
+
12
+ # MQ.logging = true
13
+
14
+ if ARGV[0] == 'worker'
15
+
16
+ log "prime checker #{Process.pid}", :started
17
+
18
+ class Fixnum
19
+ def prime?
20
+ ('1' * self) !~ /^1?$|^(11+?)\1+$/
21
+ end
22
+ end
23
+
24
+ MQ.queue('prime checker').subscribe{ |info, num|
25
+ log "prime checker #{Process.pid}", :prime?, num
26
+ if Integer(num).prime?
27
+ MQ.queue(info.reply_to).publish(num, :reply_to => Process.pid)
28
+ EM.stop_event_loop if num == (MAX-1).to_s
29
+ end
30
+ }
31
+
32
+ elsif ARGV[0] == 'controller'
33
+
34
+ MQ.queue('prime collector').subscribe{ |info, prime|
35
+ log 'prime collector', :received, prime, :from, info.reply_to
36
+ (@primes ||= []) << Integer(prime)
37
+ }
38
+
39
+ MAX.times do |i|
40
+ EM.next_tick do
41
+ MQ.queue('prime checker').publish((i+1).to_s, :reply_to => 'prime collector')
42
+ end
43
+ end
44
+
45
+ else # run n workers and 1 controller as an example
46
+
47
+ workers = ARGV[0] ? (Integer(ARGV[0]) rescue 2) : 2
48
+
49
+ ([ :worker ] * workers + [ :controller ]).each do |type|
50
+ log :spawning, "`ruby #{$0} #{type}`"
51
+
52
+ EM.popen("ruby #{$0} #{type}") do |c|
53
+ def c.receive_data data
54
+ puts data
55
+ end
56
+
57
+ def c.unbind
58
+ EM.stop_event_loop
59
+ end
60
+ end
61
+ end
62
+
63
+ end
64
+ }
65
+
66
+ __END__
67
+
68
+ ["prime checker", :started, {:pid=>1958}]
69
+ ["prime checker", :started, {:pid=>1957}]
70
+ ["prime checker", :started, {:pid=>1956}]
71
+ ["prime checker", :prime?, "1", {:pid=>1958}]
72
+ ["prime checker", :prime?, "2", {:pid=>1957}]
73
+ ["prime collector", :received, "2", {:from_pid=>"1957"}]
74
+ ["prime checker", :prime?, "3", {:pid=>1956}]
75
+ ["prime collector", :received, "3", {:from_pid=>"1956"}]
76
+ ["prime checker", :prime?, "4", {:pid=>1958}]
77
+ ["prime checker", :prime?, "5", {:pid=>1957}]
78
+ ["prime collector", :received, "5", {:from_pid=>"1957"}]
79
+ ["prime checker", :prime?, "6", {:pid=>1956}]
80
+ ["prime checker", :prime?, "7", {:pid=>1958}]
81
+ ["prime collector", :received, "7", {:from_pid=>"1958"}]
82
+ ["prime checker", :prime?, "8", {:pid=>1957}]
83
+ ["prime checker", :prime?, "9", {:pid=>1956}]
84
+ ["prime checker", :prime?, "10", {:pid=>1958}]
85
+ ["prime checker", :prime?, "11", {:pid=>1957}]
86
+ ["prime collector", :received, "11", {:from_pid=>"1957"}]
87
+ ["prime checker", :prime?, "12", {:pid=>1956}]
88
+ ["prime checker", :prime?, "13", {:pid=>1958}]
89
+ ["prime collector", :received, "13", {:from_pid=>"1958"}]
90
+ ["prime checker", :prime?, "14", {:pid=>1957}]
91
+ ["prime checker", :prime?, "15", {:pid=>1956}]
92
+ ["prime checker", :prime?, "16", {:pid=>1958}]
93
+ ["prime checker", :prime?, "17", {:pid=>1957}]
94
+ ["prime collector", :received, "17", {:from_pid=>"1957"}]
95
+ ["prime checker", :prime?, "18", {:pid=>1956}]
96
+ ["prime checker", :prime?, "19", {:pid=>1958}]
97
+ ["prime collector", :received, "19", {:from_pid=>"1958"}]
98
+ ["prime checker", :prime?, "20", {:pid=>1957}]
99
+ ["prime checker", :prime?, "21", {:pid=>1956}]
100
+ ["prime checker", :prime?, "22", {:pid=>1958}]
101
+ ["prime checker", :prime?, "23", {:pid=>1957}]
102
+ ["prime collector", :received, "23", {:from_pid=>"1957"}]
103
+ ["prime checker", :prime?, "24", {:pid=>1956}]
104
+ ["prime checker", :prime?, "25", {:pid=>1958}]
105
+ ["prime checker", :prime?, "26", {:pid=>1957}]
106
+ ["prime checker", :prime?, "27", {:pid=>1956}]
107
+ ["prime checker", :prime?, "28", {:pid=>1958}]
108
+ ["prime checker", :prime?, "29", {:pid=>1957}]
109
+ ["prime collector", :received, "29", {:from_pid=>"1957"}]
110
+ ["prime checker", :prime?, "30", {:pid=>1956}]
111
+ ["prime checker", :prime?, "31", {:pid=>1958}]
112
+ ["prime collector", :received, "31", {:from_pid=>"1958"}]
113
+ ["prime checker", :prime?, "32", {:pid=>1957}]
114
+ ["prime checker", :prime?, "33", {:pid=>1956}]
115
+ ["prime checker", :prime?, "34", {:pid=>1958}]
116
+ ["prime checker", :prime?, "35", {:pid=>1957}]
117
+ ["prime checker", :prime?, "36", {:pid=>1956}]
118
+ ["prime checker", :prime?, "37", {:pid=>1958}]
119
+ ["prime collector", :received, "37", {:from_pid=>"1958"}]
120
+ ["prime checker", :prime?, "38", {:pid=>1957}]
121
+ ["prime checker", :prime?, "39", {:pid=>1956}]
122
+ ["prime checker", :prime?, "40", {:pid=>1958}]
123
+ ["prime checker", :prime?, "41", {:pid=>1957}]
124
+ ["prime collector", :received, "41", {:from_pid=>"1957"}]
125
+ ["prime checker", :prime?, "42", {:pid=>1956}]
126
+ ["prime checker", :prime?, "43", {:pid=>1958}]
127
+ ["prime collector", :received, "43", {:from_pid=>"1958"}]
128
+ ["prime checker", :prime?, "44", {:pid=>1957}]
129
+ ["prime checker", :prime?, "45", {:pid=>1956}]
130
+ ["prime checker", :prime?, "46", {:pid=>1958}]
131
+ ["prime checker", :prime?, "47", {:pid=>1957}]
132
+ ["prime collector", :received, "47", {:from_pid=>"1957"}]
133
+ ["prime checker", :prime?, "48", {:pid=>1956}]
134
+ ["prime checker", :prime?, "49", {:pid=>1958}]
135
+ ["prime checker", :prime?, "50", {:pid=>1957}]
@@ -0,0 +1,49 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'mq'
3
+
4
+ MAX = 500
5
+
6
+ def log *args
7
+ p args
8
+ end
9
+
10
+ # MQ.logging = true
11
+
12
+ EM.run{
13
+
14
+ # worker
15
+
16
+ log "prime checker", Process.pid, :started
17
+
18
+ class Fixnum
19
+ def prime?
20
+ ('1' * self) !~ /^1?$|^(11+?)\1+$/
21
+ end
22
+ end
23
+
24
+ MQ.queue('prime checker').subscribe{ |info, num|
25
+ EM.defer(proc{
26
+
27
+ log "prime checker #{Process.pid}-#{Thread.current.object_id}", :prime?, num
28
+ if Integer(num).prime?
29
+ MQ.queue(info.reply_to).publish(num, :reply_to => "#{Process.pid}-#{Thread.current.object_id}")
30
+ EM.stop_event_loop if num == '499'
31
+ end
32
+
33
+ })
34
+ }
35
+
36
+ # controller
37
+
38
+ MQ.queue('prime collector').subscribe{ |info, prime|
39
+ log 'prime collector', :received, prime, :from, info.reply_to
40
+ (@primes ||= []) << Integer(prime)
41
+ }
42
+
43
+ MAX.times do |i|
44
+ EM.next_tick do
45
+ MQ.queue('prime checker').publish((i+1).to_s, :reply_to => 'prime collector')
46
+ end
47
+ end
48
+
49
+ }