-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathrpc_server.js
More file actions
executable file
·42 lines (35 loc) · 971 Bytes
/
rpc_server.js
File metadata and controls
executable file
·42 lines (35 loc) · 971 Bytes
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
#!/usr/bin/env node
var amqp = require('amqplib');
var conn = amqp.connect('amqp://localhost');
conn.then(createChannel).then(null, console.warn);
function createChannel(conn) {
process.once('SIGINT', function() { conn.close(); });
return conn.createChannel().then(consume);
}
function consume(ch) {
var ok = ch.assertQueue('rpc_queue', {durable: false});
ok = ok.then(function() {
ch.prefetch(1);
return ch.consume('rpc_queue', reply);
});
return ok.then(function(_ignore) {
console.log(' [x] Awaiting RPC requests');
});
function reply(msg) {
var n = parseInt(msg.content.toString());
console.log(' [.] fib(%d)', n);
var response = fib(n);
ch.sendToQueue( msg.properties.replyTo,
new Buffer(response.toString()),
{correlationId: msg.properties.correlationId});
ch.ack(msg);
}
}
function fib(n) {
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
}