|
1 | 1 | #!/usr/bin/env ruby
|
2 |
| -# encoding: utf-8 |
| 2 | +require 'bunny' |
| 3 | +require 'thread' |
3 | 4 |
|
4 |
| -require "bunny" |
5 |
| -require "thread" |
| 5 | +class FibonacciClient |
| 6 | + attr_accessor :call_id, :response, :lock, :condition, :connection, |
| 7 | + :channel, :server_queue_name, :reply_queue, :exchange |
6 | 8 |
|
7 |
| -conn = Bunny.new(:automatically_recover => false) |
8 |
| -conn.start |
| 9 | + def initialize(server_queue_name) |
| 10 | + @connection = Bunny.new(automatically_recover: false) |
| 11 | + @connection.start |
9 | 12 |
|
10 |
| -ch = conn.create_channel |
| 13 | + @channel = connection.create_channel |
| 14 | + @exchange = channel.default_exchange |
| 15 | + @server_queue_name = server_queue_name |
11 | 16 |
|
| 17 | + setup_reply_queue |
| 18 | + end |
12 | 19 |
|
13 |
| -class FibonacciClient |
14 |
| - attr_reader :reply_queue |
15 |
| - attr_accessor :response, :call_id |
16 |
| - attr_reader :lock, :condition |
| 20 | + def call(n) |
| 21 | + @call_id = generate_uuid |
| 22 | + |
| 23 | + exchange.publish(n.to_s, |
| 24 | + routing_key: server_queue_name, |
| 25 | + correlation_id: call_id, |
| 26 | + reply_to: reply_queue.name) |
17 | 27 |
|
18 |
| - def initialize(ch, server_queue) |
19 |
| - @ch = ch |
20 |
| - @x = ch.default_exchange |
| 28 | + # wait for the signal to continue the execution |
| 29 | + lock.synchronize { condition.wait(lock) } |
21 | 30 |
|
22 |
| - @server_queue = server_queue |
23 |
| - @reply_queue = ch.queue("", :exclusive => true) |
| 31 | + response |
| 32 | + end |
| 33 | + |
| 34 | + def stop |
| 35 | + channel.close |
| 36 | + connection.close |
| 37 | + end |
24 | 38 |
|
| 39 | + private |
25 | 40 |
|
26 |
| - @lock = Mutex.new |
| 41 | + def setup_reply_queue |
| 42 | + @lock = Mutex.new |
27 | 43 | @condition = ConditionVariable.new
|
28 |
| - that = self |
| 44 | + that = self |
| 45 | + @reply_queue = channel.queue('', exclusive: true) |
29 | 46 |
|
30 |
| - @reply_queue.subscribe do |delivery_info, properties, payload| |
| 47 | + reply_queue.subscribe do |_delivery_info, properties, payload| |
31 | 48 | if properties[:correlation_id] == that.call_id
|
32 | 49 | that.response = payload.to_i
|
33 |
| - that.lock.synchronize{that.condition.signal} |
| 50 | + |
| 51 | + # sends the signal to continue the execution of #call |
| 52 | + that.lock.synchronize { that.condition.signal } |
34 | 53 | end
|
35 | 54 | end
|
36 | 55 | end
|
37 | 56 |
|
38 |
| - def call(n) |
39 |
| - self.call_id = self.generate_uuid |
40 |
| - |
41 |
| - @x.publish(n.to_s, |
42 |
| - :routing_key => @server_queue, |
43 |
| - :correlation_id => call_id, |
44 |
| - :reply_to => @reply_queue.name) |
45 |
| - |
46 |
| - lock.synchronize{condition.wait(lock)} |
47 |
| - response |
48 |
| - end |
49 |
| - |
50 |
| - protected |
51 |
| - |
52 | 57 | def generate_uuid
|
53 |
| - # very naive but good enough for code |
54 |
| - # examples |
| 58 | + # very naive but good enough for code examples |
55 | 59 | "#{rand}#{rand}#{rand}"
|
56 | 60 | end
|
57 | 61 | end
|
58 | 62 |
|
| 63 | +client = FibonacciClient.new('rpc_queue') |
59 | 64 |
|
60 |
| -client = FibonacciClient.new(ch, "rpc_queue") |
61 |
| -puts " [x] Requesting fib(30)" |
| 65 | +puts ' [x] Requesting fib(30)' |
62 | 66 | response = client.call(30)
|
| 67 | + |
63 | 68 | puts " [.] Got #{response}"
|
64 | 69 |
|
65 |
| -ch.close |
66 |
| -conn.close |
| 70 | +client.stop |
0 commit comments