-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerEndpoint.php
158 lines (134 loc) · 4.99 KB
/
ServerEndpoint.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @author Marwan Al-Soltany <[email protected]>
* @copyright Marwan Al-Soltany 2020
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MAKS\AmqpAgent\RPC;
use PhpAmqpLib\Message\AMQPMessage;
use MAKS\AmqpAgent\Helper\ClassProxy;
use MAKS\AmqpAgent\RPC\AbstractEndpoint;
use MAKS\AmqpAgent\RPC\ServerEndpointInterface;
use MAKS\AmqpAgent\Exception\RPCEndpointException;
/**
* A class specialized in responding. Implementing only the methods needed for a server.
*
* Example:
* ```
* $serverEndpoint = new ServerEndpoint();
* $serverEndpoint->on('some.event', function () { ... });
* $serverEndpoint->connect();
* $serverEndpoint->respond('Namespace\SomeClass::someMethod', 'queue.name');
* $serverEndpoint->disconnect();
* ```
*
* @since 2.0.0
* @api
*/
class ServerEndpoint extends AbstractEndpoint implements ServerEndpointInterface
{
/**
* The callback to use when processing the requests.
* @var callable
*/
protected $callback;
/**
* Listens on requests coming via the passed queue and processes them with the passed callback.
* @param callable|null $callback [optional] The callback to process the request. This callback will be passed an `AMQPMessage` and must return a string.
* @param string|null $queueName [optional] The name of the queue to listen on.
* @return string The last processed request.
* @throws RPCEndpointException If the server is not connected yet or if the passed callback didn't return a string.
*/
public function respond(?callable $callback = null, ?string $queueName = null): string
{
$this->callback = $callback ?? [$this, 'callback'];
$this->queueName = $queueName ?? $this->queueName;
if ($this->isConnected()) {
$this->requestQueue = $this->queueName;
$this->channel->queue_declare(
$this->requestQueue,
false,
false,
false,
false
);
$this->channel->basic_qos(
null,
1,
null
);
$this->channel->basic_consume(
$this->requestQueue,
null,
false,
false,
false,
false,
function ($message) {
ClassProxy::call($this, 'onRequest', $message);
}
);
while ($this->channel->is_consuming()) {
$this->channel->wait();
}
return $this->requestBody;
}
throw new RPCEndpointException('Server is not connected yet!');
}
/**
* Listens on requests coming via the passed queue and processes them with the passed callback.
* Alias for `self::respond()`.
* @param callable|null $callback [optional] The callback to process the request. This callback will be passed an `AMQPMessage` and must return a string.
* @param string|null $queueName [optional] The queue to listen on.
* @return string The last processed request.
* @throws RPCEndpointException If the server is not connected yet or if the passed callback didn't return a string.
*/
public function serve(?callable $callback = null, ?string $queueName = null): string
{
return $this->respond($callback, $queueName);
}
/**
* Replies to the client.
* @param AMQPMessage $request
* @return void
* @throws RPCEndpointException
*/
protected function onRequest(AMQPMessage $request): void
{
$this->trigger('request.on.get', [$request]);
$this->requestBody = $request->body;
$this->responseBody = call_user_func($this->callback, $request);
$this->responseQueue = (string)$request->get('reply_to');
$this->correlationId = (string)$request->get('correlation_id');
if (!is_string($this->responseBody)) {
throw new RPCEndpointException(
sprintf(
'The passed processing callback must return a string, instead it returned (data-type: %s)!',
gettype($this->responseBody)
)
);
}
$message = new AMQPMessage($this->responseBody);
$message->set('correlation_id', $this->correlationId);
$message->set('timestamp', time());
$this->trigger('response.before.send', [$message]);
$request->getChannel()->basic_publish(
$message,
null,
$this->responseQueue
);
$request->ack();
$this->trigger('response.after.send', [$message]);
}
/**
* Returns the final request body. This method will be ignored if a callback in `self::respond()` is specified.
* @param AMQPMessage $message
* @return string
*/
protected function callback(AMQPMessage $message): string
{
return $message->body;
}
}