-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
rpc_server.pl
62 lines (49 loc) · 1.08 KB
/
rpc_server.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/perl
use strict;
use warnings;
$|++;
use AnyEvent;
use Net::RabbitFoot;
my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
host => 'localhost',
port => 5672,
user => 'guest',
pass => 'guest',
vhost => '/',
);
my $channel = $conn->open_channel();
$channel->declare_queue(queue => 'rpc_queue');
sub fib {
my $n = shift;
if ($n == 0) {
return 0;
} elsif ($n == 1) {
return 1;
} else {
return fib($n-1) + fib($n-2);
}
}
sub on_request {
my $var = shift;
my $body = $var->{body}->{payload};
my $props = $var->{header};
my $n = $body;
print " [.] fib($n)\n";
my $response = fib($n);
$channel->publish(
exchange => '',
routing_key => $props->{reply_to},
header => {
correlation_id => $props->{correlation_id},
},
body => $response,
);
$channel->ack();
}
$channel->qos(prefetch_count => 1);
$channel->consume(
on_consume => \&on_request,
);
print " [x] Awaiting RPC requests\n";
# Wait forever
AnyEvent->condvar->recv;