forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_server.dart
36 lines (31 loc) · 841 Bytes
/
rpc_server.dart
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
import "dart:io";
import "package:dart_amqp/dart_amqp.dart";
// Slow implementation of fib
int fib(int n) {
if (n >= 0 && n <= 1) {
return n;
} else
return fib(n - 1) + fib(n - 2);
}
void main(List<String> args) {
Client client = new Client();
// Setup a signal handler to cleanly exit if CTRL+C is pressed
ProcessSignal.sigint.watch().listen((_) {
client.close().then((_) {
exit(0);
});
});
client
.channel()
.then((Channel channel) => channel.qos(0, 1))
.then((Channel channel) => channel.queue("rpc_queue"))
.then((Queue queue) => queue.consume())
.then((Consumer consumer) {
print(" [x] Awaiting RPC request");
consumer.listen((AmqpMessage message) {
var n = message.payloadAsJson["n"];
print(" [.] fib(${n})");
message.reply(fib(n).toString());
});
});
}