-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathProtocol.php
More file actions
334 lines (301 loc) · 8.2 KB
/
Copy pathProtocol.php
File metadata and controls
334 lines (301 loc) · 8.2 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/*
* This file is part of the Stomp package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Stomp\Protocol;
use Stomp\Exception\StompException;
use Stomp\Transport\Frame;
/**
* Stomp base protocol
*
*
* @package Stomp
* @author Hiram Chirino <hiram@hiramchirino.com>
* @author Dejan Bosanac <dejan@nighttale.net>
* @author Michael Caplan <mcaplan@labnet.net>
* @author Jens Radtke <swefl.oss@fin-sn.de>
*/
class Protocol
{
/**
* Client id used for durable subscriptions
*
* @var string
*/
private $clientId;
/**
* @var string
*/
private $version;
/**
* Server Version
*
* @var string
*/
private $server;
/**
* Setup stomp protocol with configuration.
*
* @param string $clientId
* @param string $version
* @param string $server
*/
public function __construct($clientId, $version = Version::VERSION_1_0, $server = null)
{
$this->clientId = $clientId;
$this->server = $server;
$this->version = $version;
}
/**
* Get the connect frame
*
* @param string $login
* @param string $passcode
* @param array $versions
* @param string $host
* @param int[] $heartbeat
* @return \Stomp\Transport\Frame
*/
final public function getConnectFrame(
$login = '',
$passcode = '',
array $versions = [],
$host = null,
$heartbeat = [0, 0]
) {
$frame = $this->createFrame('CONNECT');
$frame->legacyMode(true);
if ($login || $passcode) {
$frame->addHeaders(['login' => $login, 'passcode' => $passcode]);
}
if ($this->hasClientId()) {
$frame['client-id'] = $this->getClientId();
}
if (!empty($versions)) {
$frame['accept-version'] = implode(',', $versions);
}
$frame['host'] = $host;
$frame['heart-beat'] = $heartbeat[0] . ',' . $heartbeat[1];
return $frame;
}
/**
* Get subscribe frame.
*
* @param string $destination
* @param string $subscriptionId
* @param string $ack
* @param string $selector
* @return \Stomp\Transport\Frame
* @throws StompException
*/
public function getSubscribeFrame($destination, $subscriptionId = null, $ack = 'auto', $selector = null)
{
// validate ACK types per spec
// https://stomp.github.io/stomp-specification-1.0.html#frame-ACK
// https://stomp.github.io/stomp-specification-1.1.html#ACK
// https://stomp.github.io/stomp-specification-1.2.html#ACK
if ($this->hasVersion(Version::VERSION_1_1)) {
$validAcks = ['auto', 'client', 'client-individual'];
} else {
$validAcks = ['auto', 'client'];
}
if (!in_array($ack, $validAcks)) {
throw new StompException(
sprintf(
'"%s" is not a valid ack value for STOMP %s. A valid value is one of %s',
$ack,
$this->version,
implode(',', $validAcks)
)
);
}
$frame = $this->createFrame('SUBSCRIBE');
$frame['destination'] = $destination;
$frame['ack'] = $ack;
$frame['id'] = $subscriptionId;
$frame['selector'] = $selector;
return $frame;
}
/**
* Get unsubscribe frame.
*
* @param string $destination
* @param string $subscriptionId
* @return \Stomp\Transport\Frame
*/
public function getUnsubscribeFrame($destination, $subscriptionId = null)
{
$frame = $this->createFrame('UNSUBSCRIBE');
$frame['destination'] = $destination;
$frame['id'] = $subscriptionId;
return $frame;
}
/**
* Get transaction begin frame.
*
* @param string $transactionId
* @return \Stomp\Transport\Frame
*/
public function getBeginFrame($transactionId = null)
{
$frame = $this->createFrame('BEGIN');
$frame['transaction'] = $transactionId;
return $frame;
}
/**
* Get transaction commit frame.
*
* @param string $transactionId
* @return \Stomp\Transport\Frame
*/
public function getCommitFrame($transactionId = null)
{
$frame = $this->createFrame('COMMIT');
$frame['transaction'] = $transactionId;
return $frame;
}
/**
* Get transaction abort frame.
*
* @param string $transactionId
* @return \Stomp\Transport\Frame
*/
public function getAbortFrame($transactionId = null)
{
$frame = $this->createFrame('ABORT');
$frame['transaction'] = $transactionId;
return $frame;
}
/**
* Get message acknowledge frame.
*
* @param Frame $frame
* @param string $transactionId
* @return Frame
*/
public function getAckFrame(Frame $frame, $transactionId = null)
{
$ack = $this->createFrame('ACK');
$ack['transaction'] = $transactionId;
if ($this->hasVersion(Version::VERSION_1_2)) {
if (isset($frame['ack'])) {
$ack['id'] = $frame['ack'];
} else {
$ack['id'] = $frame->getMessageId();
}
} else {
$ack['message-id'] = $frame->getMessageId();
if ($this->hasVersion(Version::VERSION_1_1)) {
$ack['subscription'] = $frame['subscription'];
}
}
return $ack;
}
/**
* Get message not acknowledge frame.
*
* @param \Stomp\Transport\Frame $frame
* @param string $transactionId
* @param bool $requeue Requeue header
* @return \Stomp\Transport\Frame
* @throws StompException
* @throws \LogicException
*/
public function getNackFrame(Frame $frame, $transactionId = null, $requeue = null)
{
if ($requeue !== null) {
throw new \LogicException('requeue header not supported');
}
if ($this->version === Version::VERSION_1_0) {
throw new StompException('Stomp Version 1.0 has no support for NACK Frames.');
}
$nack = $this->createFrame('NACK');
$nack['transaction'] = $transactionId;
if ($this->hasVersion(Version::VERSION_1_2)) {
$nack['id'] = $frame->getMessageId();
} else {
$nack['message-id'] = $frame->getMessageId();
if ($this->hasVersion(Version::VERSION_1_1)) {
$nack['subscription'] = $frame['subscription'];
}
}
$nack['message-id'] = $frame->getMessageId();
return $nack;
}
/**
* Get the disconnect frame.
*
* @return \Stomp\Transport\Frame
*/
public function getDisconnectFrame()
{
$frame = $this->createFrame('DISCONNECT');
if ($this->hasClientId()) {
$frame['client-id'] = $this->getClientId();
}
return $frame;
}
/**
* Client Id is set
*
* @return bool
*/
public function hasClientId()
{
return (bool) $this->clientId;
}
/**
* Client Id is set
*
* @return string
*/
public function getClientId()
{
return $this->clientId;
}
/**
* Stomp Version
*
* @return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Server Version Info
*
* @return string
*/
public function getServer()
{
return $this->server;
}
/**
* Checks if given version is included (equal or lower) in active protocol version.
*
* @param string $version
* @return bool
*/
public function hasVersion($version)
{
return version_compare($this->version, $version, '>=');
}
/**
* Creates a Frame according to the detected STOMP version.
*
* @param string $command
* @return Frame
*/
protected function createFrame($command)
{
$frame = new Frame($command);
if ($this->version === Version::VERSION_1_0) {
$frame->legacyMode(true);
}
return $frame;
}
}