-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStompConnection.inc
585 lines (521 loc) · 14.5 KB
/
StompConnection.inc
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?php
/**
* StompFrames are messages that are sent and received on a StompConnection.
*
* @package Stomp
* @author Hiram Chirino <[email protected]>
* @author Dejan Bosanac <[email protected]>
* @author Joe Hansche <[email protected]> - ported to PHP5 and sockets extension
* @author Gavin M. Roy <[email protected]> - Cleaned up readFrame and writeFrame for stricter protocol adherence.
*/
class StompFrame
{
public $command;
public $headers = array();
public $body;
function __construct ($command = null, $headers = null, $body = null)
{
$this->init($command, $headers, $body);
}
function init ($command = null, $headers = null, $body = null)
{
$this->command = $command;
if ( $headers != null )
$this->headers = $headers;
$this->body = $body;
}
}
/**
* Basic text stomp message
*
* @package Stomp
* @author Dejan Bosanac <[email protected]>
* @version $Revision: 23794 $
*/
class StompMessage extends StompFrame
{
function __construct($body, $headers = null)
{
$this->init('SEND', $headers, $body);
}
}
/**
* Message that contains a stream of uninterpreted bytes
*
* @package Stomp
* @author Dejan Bosanac <[email protected]>
*/
class BytesMessage extends StompMessage
{
function __construct ($body, $headers = null)
{
$this->init('SEND', $headers, $body);
if ( $this->headers == null )
{
$this->headers = array();
}
$this->headers['content-length'] = count($body);
}
}
/**
* Message that contains a set of name-value pairs
*
* @package Stomp
* @author Dejan Bosanac <[email protected]>
*/
class MapMessage extends StompMessage
{
public $map;
function __construct ($msg, $headers = null)
{
if ( $msg instanceOf StompFrame )
{
$this->init($msg->command, $msg->headers, $msg->body);
$this->map = json_decode($msg->body);
} else
{
$this->init('SEND', $headers, $msg);
if ( $this->headers == null )
{
$this->headers = array();
}
$this->headers['amq-msg-type'] = 'MapMessage';
$this->body = json_encode($msg);
}
}
}
/**
* A Stomp Connection
*
* @package Stomp
* @author Hiram Chirino <[email protected]>
* @author Dejan Bosanac <[email protected]>
* @author Joe Hansche <[email protected]> - Sockets Extension
* @version $Revision: 23794 $
*/
class StompConnection
{
const DEFAULT_PORT = 61613;
protected $socket = null;
protected $hosts = array();
protected $params = array();
protected $subscriptions = array();
protected $currentHost = -1;
protected $attempts = 10;
protected $username = 'guest';
protected $password = 'guest';
protected static $usageStats = array( 'calls' => array( ) );
function __construct($brokerUri)
{
$uri = parse_url($brokerUri);
// Failover takes a format of: failover:(tcp://host1:port1,tcp://host2:port2)?params=vals
if ( $uri['scheme'] === 'failover' )
{
$urls = explode(',', trim($uri['path'], '()'));
foreach( $urls as $url )
{
$tempuri = parse_url($url);
if ( ! isset( $tempuri['port'] ) )
{
$tempuri['port'] = self::DEFAULT_PORT;
}
$this->hosts[] = array( $tempuri['host'], $tempuri['port']);
}
unset($uri['path'], $uri['scheme']);
}
// Non-failover format is: tcp://host:port?params=vals
else
{
if ( ! isset($uri['port']) )
{
$uri['port'] = self::DEFAULT_PORT;
}
$this->hosts[] = array( $uri['host'], $uri['port'] );
unset($uri['host'], $uri['scheme']);
}
if ( isset($uri['user']) )
{
$this->username = $uri['user'];
}
if ( isset($uri['pass']) )
{
$this->password = $uri['pass'];
}
// Parse the query string as parameters
if ( isset($uri['query']) )
{
parse_str( $uri['query'], $this->params );
}
if ( ! is_numeric( $this->params['connectionTimeout'] ) )
{
// 1 second connection timeout
$this->params['connectionTimeout'] = 1000;
}
if ( ! is_numeric( $this->params['soTimeout'] ) )
{
// No socket timeout (wait forever)
$this->params['soTimeout'] = 0;
}
if ( ! is_numeric( $this->params['socketBufferSize']) )
{
// 64KB
$this->params['socketBufferSize'] = 65536;
}
$this->makeConnection();
$this->connect( $this->username, $this->password );
}
/**
* Builds the socket and connection to the server
*
* @return boolean
*/
function makeConnection()
{
$startTime = microtime( true );
if ( count($this->hosts) == 0 )
{
self::$usageStats['calls'][] = array( 'command' => 'makeConnection', 'duration' => microtime( true ) - $startTime );
trigger_error('No broker defined', E_USER_ERROR);
return false;
}
$i = $this->currentHost;
$attempt = 0;
$connected = false;
$numHosts = count($this->hosts);
while ( false === $connected && ++$attempt <= $this->attempts )
{
if ( $numHosts > 1 && isset( $this->params['randomize'] ) && (bool)($this->params['randomize']) === true )
{
$i = rand(0, $numHosts - 1);
} else
{
$i = ($i + 1) % $numHosts;
}
$broker = $this->hosts[$i];
list( $host, $port ) = $broker;
if ( ! is_numeric( $port ) )
{
$port = self::DEFAULT_PORT;
}
if ( $this->socket !== NULL )
{
socket_close($this->socket);
$this->socket = NULL;
}
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ( $this->params['socketBufferSize'] > 0 )
{
socket_set_option($this->socket, SOL_SOCKET, SO_SNDBUF, $this->params['socketBufferSize']);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVBUF, $this->params['socketBufferSize']);
}
if ( $this->params['soTimeout'] > 0 )
{
stream_set_timeout($this->socket, (int)($this->params['soTimeout'] / 1000), (int)($this->params['soTimeout'] % 1000) * 1000 );
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, $this->params['soTimeout']);
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $this->params['soTimeout']);
}
if ( $this->params['connectionTimeout'] > 0 )
{
// Use socket_select() on a non-blocking socket to see if it connected successfully
socket_set_nonblock( $this->socket );
socket_connect( $this->socket, $host, $port);
socket_set_block( $this->socket );
// socket_select() takes a timeout, so use that for the connection timeout
$tempSelect = socket_select( $r = array($this->socket), $w = array($this->socket), $f = array($this->socket), (int)($this->params['connectionTimeout'] / 1000), (int)($this->params['connectionTimeout'] % 1000) * 1000 );
switch ( $tempSelect )
{
case 2:
throw new Exception('Stomp Connection refused. Is the service running?');
break;
case 0:
throw new Exception('Stomp Connection timed out. Possibly network problems?');
break;
case 1:
$result = true;
break;
}
}
else
{
// For no timeout, just use socket_connect()
$result = socket_connect( $this->socket, $host, $port);
}
if ( false === $this->socket || $result !== true )
{
trigger_error('Could not connect to '.$host.':'.$port.' (Try #'.$attempt.'/'.$this->attempts.')', E_USER_WARNING);
} else
{
$connected = true;
$this->currentHost = $i;
break;
}
}
if ( $connected === false )
{
socket_shutdown( $this->socket, 2 );
socket_close( $this->socket );
$this->socket = NULL;
self::$usageStats['calls'][] = array( 'command' => 'makeConnection', 'duration' => microtime( true ) - $startTime );
trigger_error('Could not connect to a broker', E_USER_ERROR);
return false;
}
self::$usageStats['calls'][] = array( 'command' => 'makeConnection', 'duration' => microtime( true ) - $startTime );
return $connected;
}
/**
* Connect to the amq server
*
* @param string $username
* @param string $password
* @return StompFrame
*/
protected function connect ($username = '', $password = '')
{
$startTime = microtime( true );
if ( $username !== '' )
$this->username = $username;
if ( $password !== '' )
$this->password = $password;
$this->writeFrame(new StompFrame('CONNECT', array('login' => $this->username, 'passcode' => $this->password)));
$result = $this->readFrame();
self::$usageStats['calls'][] = array( 'command' => 'connect', 'duration' => microtime( true ) - $startTime );
return $result;
}
/**
* Send a message to a queue
*
* @param string $destination
* @param mixed $msg String body, or StompFrame object
* @param array $properties
*/
function send ($destination, $msg, $properties = null)
{
$startTime = microtime( true );
if ( $msg instanceOf StompFrame )
{
$msg->headers['destination'] = $destination;
return $this->writeFrame($msg);
} else
{
$headers = array();
if ( isset($properties) )
{
foreach ( $properties as $name => $value )
{
$headers[$name] = $value;
}
}
$headers['destination'] = $destination;
return $this->writeFrame(new StompFrame('SEND', $headers, $msg));
}
self::$usageStats['calls'][] = array( 'command' => 'send', 'duration' => microtime( true ) - $startTime );
}
/**
* Get the usage stats across stomp connections
*
* @return array $usageStats
*/
static function getUsageStats( )
{
return self::$usageStats;
}
/**
* Register a subscription to a queue
*
* @param string $destination Queue name
* @param array $properties
*/
function subscribe ($destination, $properties = null)
{
$headers = array('ack' => 'client');
if ( isset($properties) )
{
foreach ( $properties as $name => $value )
{
$headers[$name] = $value;
}
}
$headers['destination'] = $destination;
$this->writeFrame(new StompFrame('SUBSCRIBE', $headers));
$this->subscriptions[$destination] = $properties;
}
/**
* Unsubscribe from a queue destination
*
* @param string $destination Queue name
* @param array $properties
*/
function unsubscribe ($destination, $properties = null)
{
$headers = array();
if ( isset($properties) )
{
foreach ( $properties as $name => $value )
{
$headers[$name] = $value;
}
}
$headers['destination'] = $destination;
$this->writeFrame(new StompFrame('UNSUBSCRIBE', $headers));
unset($this->subscriptions[$destination]);
}
/**
* Begin a Stomp/AMQ transaction
*
* @param string $transactionId
*/
function begin ($transactionId = null)
{
$headers = array();
if ( isset($transactionId) )
{
$headers['transaction'] = $transactionId;
}
$this->writeFrame(new StompFrame('BEGIN', $headers));
}
/**
* Commit a pending AMQ transaction
*
* @param string $transactionId
*/
function commit ($transactionId = null)
{
$headers = array();
if ( isset($transactionId) )
{
$headers['transaction'] = $transactionId;
}
$this->writeFrame(new StompFrame('COMMIT', $headers));
}
/**
* Abort a pending AMQ transaction
*
* @param string $transactionId
*/
function abort ($transactionId = null)
{
$headers = array();
if ( isset($transactionId) )
{
$headers['transaction'] = $transactionId;
}
$this->writeFrame(new StompFrame('ABORT', $headers));
}
/**
* Acknowledge a pending AMQ message
*
* @param string $message amq "message-id" header being acknowledged
* @param string $transactionId
*/
function ack ($message, $transactionId = null)
{
if ( $message instanceOf StompFrame )
{
$this->writeFrame(new StompFrame('ACK', $message->headers));
} else
{
$headers = array();
if ( isset($transactionId) )
{
$headers['transaction'] = $transactionId;
}
$headers['message-id'] = $message;
$this->writeFrame(new StompFrame('ACK', $headers));
}
}
/**
* Disconnect from AMQ server
*/
function disconnect ()
{
if ( $this->socket )
{
$this->writeFrame(new StompFrame('DISCONNECT'));
}
socket_shutdown($this->socket, 1);
usleep(500);
socket_shutdown($this->socket, 2);
socket_close($this->socket);
$this->socket = NULL;
}
/**
* Write a frame
*
* @param StompFrame $stompFrame
*/
protected function writeFrame ($stompFrame)
{
$data = $stompFrame->command . "\n";
if ( isset($stompFrame->headers) )
{
foreach ( $stompFrame->headers as $name => $value )
{
$data .= $name . ': ' . $value . "\n";
}
}
$data .= "\n";
if ( isset($stompFrame->body) )
{
$data .= $stompFrame->body;
}
// End the Frame
$data .= "\0";
$r = socket_write($this->socket, $data);
if ( $r === false || $r == 0 )
{
throw new Exception('Could not send Stomp Frame to the broker.');
}
return ( $r == strlen( $data ) );
}
/**
* Read a StompFrame from the queue
*
* @return StompFrame
*/
function readFrame ()
{
$start = microtime( true );
$data = '';
$byte = 0x00;
while ( 1 )
{
$result = socket_recv($this->socket, $byte, 1, 0);
if ( $result === false )
{
throw Exception('Stomp Disconnected');
}
// Null byte == end of frame
if ( ord($byte) == 0 )
{
break;
}
$data .= $byte;
}
list ($header, $body) = explode("\n\n", $data, 2);
$header = explode("\n", $header);
$headers = array();
$command = NULL;
foreach ( $header as $v )
{
if ( isset($command) )
{
list ($name, $value) = explode(':', $v, 2);
$headers[$name] = trim($value);
} else
{
$command = $v;
}
}
$frame = new StompFrame($command, $headers, trim($body));
if ( $frame->command == 'ERROR' )
{
throw new Exception("Stomp returned Error Frame: " . $body);
}
if ( isset($frame->headers['amq-msg-type']) && $frame->headers['amq-msg-type'] == 'MapMessage' )
{
return new MapMessage($frame);
} else {
return $frame;
}
}
}