-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathClient.php
271 lines (220 loc) · 8.03 KB
/
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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
namespace React\Stomp;
use Evenement\EventEmitter;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Stomp\Client\IncomingPackageProcessor;
use React\Stomp\Client\OutgoingPackageCreator;
use React\Stomp\Client\State;
use React\Stomp\Client\Command\CommandInterface;
use React\Stomp\Client\Command\CloseCommand;
use React\Stomp\Client\Command\ConnectionEstablishedCommand;
use React\Stomp\Client\Command\NullCommand;
use React\Stomp\Exception\ProcessingException;
use React\Stomp\Exception\ConnectionException;
use React\Stomp\Io\InputStreamInterface;
use React\Stomp\Io\OutputStreamInterface;
use React\Stomp\Protocol\Frame;
use React\EventLoop\LoopInterface;
/**
* @event connect
* @event error
*/
class Client extends EventEmitter
{
private $loop;
private $connectionStatus = 'not-connected';
private $packageProcessor;
private $packageCreator;
private $subscriptions = array();
private $acknowledgements = array();
private $options = array();
/** @var Deferred */
private $connectDeferred;
/** @var PromiseInterface */
private $connectPromise;
public function __construct(LoopInterface $loop, InputStreamInterface $input, OutputStreamInterface $output, array $options)
{
$this->loop = $loop;
$state = new State();
$this->packageProcessor = new IncomingPackageProcessor($state);
$this->packageCreator = new OutgoingPackageCreator($state);
$this->input = $input;
$this->input->on('frame', array($this, 'handleFrameEvent'));
$this->input->on('error', array($this, 'handleErrorEvent'));
$this->input->on('close', array($this, 'handleCloseEvent'));
$this->output = $output;
$this->options = $this->sanatizeOptions($options);
}
public function connect($timeout = 5)
{
if ($this->connectPromise) {
return $this->connectPromise;
}
$this->connectionStatus = 'connecting';
$deferred = $this->connectDeferred = new Deferred();
$client = $this;
$timer = $this->loop->addTimer($timeout, function () use ($client, $deferred) {
$deferred->reject(new ConnectionException('Connection timeout'));
$client->resetConnectDeferred();
$client->setConnectionStatus('not-connected');
});
$this->on('connect', function ($client) use ($timer, $deferred) {
$timer->cancel();
$deferred->resolve($client);
});
$frame = $this->packageCreator->connect(
$this->options['vhost'],
$this->options['login'],
$this->options['passcode']
);
$this->output->sendFrame($frame);
return $this->connectPromise = $deferred->promise()->then(function () use ($client) {
$client->setConnectionStatus('connected');
return $client;
});
}
public function send($destination, $body, array $headers = array())
{
$frame = $this->packageCreator->send($destination, $body, $headers);
$this->output->sendFrame($frame);
}
public function subscribe($destination, $callback, array $headers = array())
{
return $this->doSubscription($destination, $callback, 'auto', $headers);
}
public function subscribeWithAck($destination, $ack, $callback, array $headers = array())
{
if ('auto' === $ack) {
throw new \LogicException("ack 'auto' is not compatible with acknowledgeable subscription");
}
return $this->doSubscription($destination, $callback, $ack, $headers);
}
private function doSubscription($destination, $callback, $ack, array $headers)
{
$frame = $this->packageCreator->subscribe($destination, $ack, $headers);
$this->output->sendFrame($frame);
$subscriptionId = $frame->getHeader('id');
$this->acknowledgements[$subscriptionId] = $ack;
$this->subscriptions[$subscriptionId] = $callback;
return $subscriptionId;
}
public function unsubscribe($subscriptionId, array $headers = array())
{
$frame = $this->packageCreator->unsubscribe($subscriptionId, $headers);
$this->output->sendFrame($frame);
unset($this->acknowledgements[$subscriptionId]);
unset($this->subscriptions[$subscriptionId]);
}
public function ack($subscriptionId, $messageId, array $headers = array())
{
$frame = $this->packageCreator->ack($subscriptionId, $messageId, $headers);
$this->output->sendFrame($frame);
}
public function nack($subscriptionId, $messageId, array $headers = array())
{
$frame = $this->packageCreator->nack($subscriptionId, $messageId, $headers);
$this->output->sendFrame($frame);
}
public function disconnect()
{
$receipt = $this->generateReceiptId();
$frame = $this->packageCreator->disconnect($receipt);
$this->output->sendFrame($frame);
$this->connectDeferred = null;
$this->connectPromise = null;
$this->connectionStatus = 'not-connected';
}
public function resetConnectDeferred()
{
$this->connectDeferred = null;
$this->connectPromise = null;
}
public function handleFrameEvent(Frame $frame)
{
try {
$this->processFrame($frame);
} catch (ProcessingException $e) {
$this->emit('error', array($e));
if ($this->connectionStatus === 'connecting') {
$this->connectDeferred->reject($e);
$this->connectDeferred = null;
$this->connectPromise = null;
$this->connectionStatus = 'not-connected';
}
}
}
public function handleErrorEvent(\Exception $e)
{
$this->emit('error', array($e));
}
public function handleCloseEvent()
{
$this->connectDeferred = null;
$this->connectPromise = null;
$this->connectionStatus = 'not-connected';
$this->emit('close');
}
public function processFrame(Frame $frame)
{
$command = $this->packageProcessor->receiveFrame($frame);
$this->executeCommand($command);
if ('MESSAGE' === $frame->command) {
$this->notifySubscribers($frame);
return;
}
}
public function executeCommand(CommandInterface $command)
{
if ($command instanceof CloseCommand) {
$this->output->close();
return;
}
if ($command instanceof ConnectionEstablishedCommand) {
$this->emit('connect', array($this));
return;
}
if ($command instanceof NullCommand) {
return;
}
throw new \Exception(sprintf("Unknown command '%s'", get_class($command)));
}
public function notifySubscribers(Frame $frame)
{
$subscriptionId = $frame->getHeader('subscription');
if (!isset($this->subscriptions[$subscriptionId])) {
return;
}
$callback = $this->subscriptions[$subscriptionId];
if ('auto' !== $this->acknowledgements[$subscriptionId]) {
$resolver = new AckResolver($this, $subscriptionId, $frame->getHeader('message-id'));
$parameters = array($frame, $resolver);
} else {
$parameters = array($frame);
}
call_user_func_array($callback, $parameters);
}
private function sanatizeOptions($options)
{
if (!isset($options['host']) && !isset($options['vhost'])) {
throw new \InvalidArgumentException('Either host or vhost options must be provided.');
}
return array_merge(array(
'vhost' => isset($options['host']) ? $options['host'] : null,
'login' => null,
'passcode' => null,
), $options);
}
public function isConnected()
{
return $this->connectionStatus === 'connected';
}
public function setConnectionStatus($status)
{
$this->connectionStatus = $status;
}
public function generateReceiptId()
{
return mt_rand();
}
}