Skip to content

Commit 1f9dbc5

Browse files
author
Caique Hitoshi Mitsuoka
committed
Update ruby tutorial six
1 parent d241706 commit 1f9dbc5

File tree

2 files changed

+76
-71
lines changed

2 files changed

+76
-71
lines changed

ruby/rpc_client.rb

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
2+
require 'bunny'
3+
require 'thread'
34

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
68

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
912

10-
ch = conn.create_channel
13+
@channel = connection.create_channel
14+
@exchange = channel.default_exchange
15+
@server_queue_name = server_queue_name
1116

17+
setup_reply_queue
18+
end
1219

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)
1727

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) }
2130

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
2438

39+
private
2540

26-
@lock = Mutex.new
41+
def setup_reply_queue
42+
@lock = Mutex.new
2743
@condition = ConditionVariable.new
28-
that = self
44+
that = self
45+
@reply_queue = channel.queue('', exclusive: true)
2946

30-
@reply_queue.subscribe do |delivery_info, properties, payload|
47+
reply_queue.subscribe do |_delivery_info, properties, payload|
3148
if properties[:correlation_id] == that.call_id
3249
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 }
3453
end
3554
end
3655
end
3756

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-
5257
def generate_uuid
53-
# very naive but good enough for code
54-
# examples
58+
# very naive but good enough for code examples
5559
"#{rand}#{rand}#{rand}"
5660
end
5761
end
5862

63+
client = FibonacciClient.new('rpc_queue')
5964

60-
client = FibonacciClient.new(ch, "rpc_queue")
61-
puts " [x] Requesting fib(30)"
65+
puts ' [x] Requesting fib(30)'
6266
response = client.call(30)
67+
6368
puts " [.] Got #{response}"
6469

65-
ch.close
66-
conn.close
70+
client.stop

ruby/rpc_server.rb

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
11
#!/usr/bin/env ruby
2-
# encoding: utf-8
3-
4-
require "bunny"
5-
6-
conn = Bunny.new(:automatically_recover => false)
7-
conn.start
8-
9-
ch = conn.create_channel
2+
require 'bunny'
103

114
class FibonacciServer
12-
13-
def initialize(ch)
14-
@ch = ch
5+
def initialize
6+
@connection = Bunny.new
7+
@connection.start
8+
@channel = @connection.create_channel
159
end
1610

1711
def start(queue_name)
18-
@q = @ch.queue(queue_name)
19-
@x = @ch.default_exchange
12+
@queue = channel.queue(queue_name)
13+
@exchange = channel.default_exchange
14+
subscribe_to_queue
15+
end
2016

21-
@q.subscribe(:block => true) do |delivery_info, properties, payload|
22-
n = payload.to_i
23-
r = self.class.fib(n)
17+
def stop
18+
channel.close
19+
connection.close
20+
end
2421

25-
puts " [.] fib(#{n})"
22+
private
2623

27-
@x.publish(r.to_s, :routing_key => properties.reply_to, :correlation_id => properties.correlation_id)
24+
attr_reader :channel, :exchange, :queue, :connection
25+
26+
def subscribe_to_queue
27+
queue.subscribe(block: true) do |_delivery_info, properties, payload|
28+
result = fibonacci(payload.to_i)
29+
30+
exchange.publish(
31+
result.to_s,
32+
routing_key: properties.reply_to,
33+
correlation_id: properties.correlation_id
34+
)
2835
end
2936
end
3037

38+
def fibonacci(value)
39+
return value if value.zero? || value == 1
3140

32-
def self.fib(n)
33-
case n
34-
when 0 then 0
35-
when 1 then 1
36-
else
37-
fib(n - 1) + fib(n - 2)
38-
end
41+
fibonacci(value - 1) + fibonacci(value - 2)
3942
end
4043
end
4144

4245
begin
43-
server = FibonacciServer.new(ch)
44-
puts " [x] Awaiting RPC requests"
45-
server.start("rpc_queue")
46-
rescue Interrupt => _
47-
ch.close
48-
conn.close
46+
server = FibonacciServer.new
4947

50-
exit(0)
48+
puts ' [x] Awaiting RPC requests'
49+
server.start('rpc_queue')
50+
rescue Interrupt => _
51+
server.stop
5152
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy