-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
rpc_server.php
55 lines (40 loc) · 1.15 KB
/
rpc_server.php
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
<?php
// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';
use Enqueue\AmqpBunny\AmqpConnectionFactory;
$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'receive_method' => 'basic_consume',
];
function fib($n)
{
if ($n == 0) {
return 0;
}
if ($n == 1) {
return 1;
}
return fib($n - 1) + fib($n - 2);
}
$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();
$context->setQos(0, 1, false);
$rpc_queue = $context->createQueue('rpc_queue');
$context->declareQueue($rpc_queue);
$consumer = $context->createConsumer($rpc_queue);
echo " [x] Awaiting RPC requests\n";
while (true) {
if ($req = $consumer->receive()) {
$n = (int) ($req->getBody());
echo ' [.] fib(', $n, ")\n";
$msg = $context->createMessage((string) fib($n));
$msg->setCorrelationId($req->getCorrelationId());
$reply_queue = $context->createQueue($req->getReplyTo());
$context->createProducer()->send($reply_queue, $msg);
$consumer->acknowledge($req);
}
}
$context->close();