forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_client.php
105 lines (86 loc) · 2.36 KB
/
rpc_client.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class FibonacciRpcClient {
private $connection;
private $channel;
private $callbackQueueName;
private $queueName = 'rpc_queue';
private $rpcQueue = 'rpc_queue';
private $response;
protected $queue;
protected $corrId;
public function __construct() {
$this->connection = $this->getAMQPConnection();
$this->setChannel();
$this->setExchange();
}
/**
AMQP Connection
*/
protected function getAMQPConnection() {
$connection = new AMQPConnection();
$connection->setHost('127.0.0.1');
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
return $connection;
}
/**
Declare Channel
*/
protected function setChannel() {
$this->channel = new AMQPChannel($this->connection);
$this->channel->setPrefetchCount(1);
}
/**
Declare Exchange
*/
protected function setExchange() {
$this->exchange = new AMQPExchange($this->channel);
}
public function on_response(AMQPEnvelope $message, AMQPQueue $queue) {
print_r(func_get_args());
}
public function call($value) {
$this->response = NULL;
$this->corrId = uniqid();
try {
//Declare an nonymous channel
$this->queue = new AMQPQueue($this->channel);
$this->queue->setFlags(AMQP_EXCLUSIVE);
$this->queue->declareQueue();
$this->callbackQueueName = $this->queue->getName();
//Set Publish Attributes
$attributes = array(
'correlation_id' => $this->corrId,
'reply_to' => $this->callbackQueueName
);
$this->exchange->publish(
$value,
$this->rpcQueue,
AMQP_NOPARAM,
$attributes
);
$callback = function(AMQPEnvelope $message, AMQPQueue $q) {
if($message->getCorrelationId() == $this->corrId) {
//echo sprintf("CorrelationID: %s",$message->getCorrelationId()), PHP_EOL;
//echo sprintf("Response: %s",$message->getBody()), PHP_EOL;
$this->response = $message->getBody();
$q->nack($message->getDeliveryTag());
return false;
}
};
$this->queue->consume($callback);
//Return RPC Results
return $this->response;
} catch(AMQPQueueException $ex) {
print_r($ex);
} catch(Exception $ex) {
print_r($ex);
}
}
}
$value = (isset($argv[1]))? $argv[1] : 5;
$fibonacciRpc = new FibonacciRpcClient();
echo sprintf(" [x] Requesting fib(%s)",$value), PHP_EOL;
$response = $fibonacciRpc->call($value);
echo sprintf(" [.] Received: %s",$response), PHP_EOL;