4 * Copyright 2005-2006 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 /* vim: set expandtab tabstop=3 shiftwidth=3: */
21 require_once 'Stomp/Frame.php';
28 * @author Hiram Chirino <hiram@hiramchirino.com>
29 * @author Dejan Bosanac <dejan@nighttale.net>
30 * @author Michael Caplan <mcaplan@labnet.net>
31 * @version $Revision: 43 $
36 * Perform request synchronously
43 * Default prefetch size
47 public $prefetchSize = 1;
50 * Client id used for durable subscriptions
54 public $clientId = null;
56 protected $_brokerUri = null;
57 protected $_socket = null;
58 protected $_hosts = array();
59 protected $_params = array();
60 protected $_subscriptions = array();
61 protected $_defaultPort = 61613;
62 protected $_currentHost = - 1;
63 protected $_attempts = 10;
64 protected $_username = '';
65 protected $_password = '';
66 protected $_sessionId;
67 protected $_read_timeout_seconds = 60;
68 protected $_read_timeout_milliseconds = 0;
69 protected $_connect_timeout_seconds = 60;
74 * @param string $brokerUri Broker URL
75 * @throws StompException
77 public function __construct ($brokerUri)
79 $this->_brokerUri = $brokerUri;
83 * Initialize connection
85 * @throws StompException
87 protected function _init ()
89 $pattern = "|^(([a-zA-Z]+)://)+\(*([a-zA-Z0-9\.:/i,-]+)\)*\??([a-zA-Z0-9=]*)$|i";
90 if (preg_match($pattern, $this->_brokerUri, $regs)) {
94 if ($scheme != "failover") {
95 $this->_processUrl($this->_brokerUri);
97 $urls = explode(",", $hosts);
98 foreach ($urls as $url) {
99 $this->_processUrl($url);
102 if ($params != null) {
103 parse_str($params, $this->_params);
106 require_once 'Stomp/Exception.php';
107 throw new StompException("Bad Broker URL {$this->_brokerUri}");
113 * @param string $url Broker URL
114 * @throws StompException
117 protected function _processUrl ($url)
119 $parsed = parse_url($url);
121 array_push($this->_hosts, array($parsed['host'] , $parsed['port'] , $parsed['scheme']));
123 require_once 'Stomp/Exception.php';
124 throw new StompException("Bad Broker URL $url");
128 * Make socket connection to the server
130 * @throws StompException
132 protected function _makeConnection ()
134 if (count($this->_hosts) == 0) {
135 require_once 'Stomp/Exception.php';
136 throw new StompException("No broker defined");
139 // force disconnect, if previous established connection exists
142 $i = $this->_currentHost;
145 $connect_errno = null;
146 $connect_errstr = null;
148 while (! $connected && $att ++ < $this->_attempts) {
149 if (isset($this->_params['randomize']) && $this->_params['randomize'] == 'true') {
150 $i = rand(0, count($this->_hosts) - 1);
152 $i = ($i + 1) % count($this->_hosts);
154 $broker = $this->_hosts[$i];
157 $scheme = $broker[2];
159 $port = $this->_defaultPort;
161 if ($this->_socket != null) {
162 fclose($this->_socket);
163 $this->_socket = null;
165 $this->_socket = @fsockopen($scheme . '://' . $host, $port, $connect_errno, $connect_errstr, $this->_connect_timeout_seconds);
166 if (!is_resource($this->_socket) && $att >= $this->_attempts && !array_key_exists($i + 1, $this->_hosts)) {
167 require_once 'Stomp/Exception.php';
168 throw new StompException("Could not connect to $host:$port ($att/{$this->_attempts})");
169 } else if (is_resource($this->_socket)) {
171 $this->_currentHost = $i;
176 require_once 'Stomp/Exception.php';
177 throw new StompException("Could not connect to a broker");
183 * @param string $username
184 * @param string $password
186 * @throws StompException
188 public function connect ($username = '', $password = '')
190 $this->_makeConnection();
191 if ($username != '') {
192 $this->_username = $username;
194 if ($password != '') {
195 $this->_password = $password;
197 $headers = array('login' => $this->_username , 'passcode' => $this->_password);
198 if ($this->clientId != null) {
199 $headers["client-id"] = $this->clientId;
201 $frame = new StompFrame("CONNECT", $headers);
202 $this->_writeFrame($frame);
203 $frame = $this->readFrame();
204 if ($frame instanceof StompFrame && $frame->command == 'CONNECTED') {
205 $this->_sessionId = $frame->headers["session"];
208 require_once 'Stomp/Exception.php';
209 if ($frame instanceof StompFrame) {
210 throw new StompException("Unexpected command: {$frame->command}", 0, $frame->body);
212 throw new StompException("Connection not acknowledged");
218 * Check if client session has ben established
222 public function isConnected ()
224 return !empty($this->_sessionId) && is_resource($this->_socket);
227 * Current stomp session ID
231 public function getSessionId()
233 return $this->_sessionId;
236 * Send a message to a destination in the messaging system
238 * @param string $destination Destination queue
239 * @param string|StompFrame $msg Message
240 * @param array $properties
241 * @param boolean $sync Perform request synchronously
244 public function send ($destination, $msg, $properties = array(), $sync = null)
246 if ($msg instanceof StompFrame) {
247 $msg->headers['destination'] = $destination;
248 if (is_array($properties)) $msg->headers = array_merge($msg->headers, $properties);
251 $headers = $properties;
252 $headers['destination'] = $destination;
253 $frame = new StompFrame('SEND', $headers, $msg);
255 $this->_prepareReceipt($frame, $sync);
256 $this->_writeFrame($frame);
257 return $this->_waitForReceipt($frame, $sync);
260 * Prepair frame receipt
262 * @param StompFrame $frame
263 * @param boolean $sync
265 protected function _prepareReceipt (StompFrame $frame, $sync)
267 $receive = $this->sync;
268 if ($sync !== null) {
271 if ($receive == true) {
272 $frame->headers['receipt'] = md5(microtime());
278 * @param StompFrame $frame
279 * @param boolean $sync
281 * @throws StompException
283 protected function _waitForReceipt (StompFrame $frame, $sync)
286 $receive = $this->sync;
287 if ($sync !== null) {
290 if ($receive == true) {
291 $id = (isset($frame->headers['receipt'])) ? $frame->headers['receipt'] : null;
295 $frame = $this->readFrame();
296 if ($frame instanceof StompFrame && $frame->command == 'RECEIPT') {
297 if ($frame->headers['receipt-id'] == $id) {
300 require_once 'Stomp/Exception.php';
301 throw new StompException("Unexpected receipt id {$frame->headers['receipt-id']}", 0, $frame->body);
304 require_once 'Stomp/Exception.php';
305 if ($frame instanceof StompFrame) {
306 throw new StompException("Unexpected command {$frame->command}", 0, $frame->body);
308 throw new StompException("Receipt not received");
315 * Register to listen to a given destination
317 * @param string $destination Destination queue
318 * @param array $properties
319 * @param boolean $sync Perform request synchronously
321 * @throws StompException
323 public function subscribe ($destination, $properties = null, $sync = null)
325 $headers = array('ack' => 'client');
326 $headers['activemq.prefetchSize'] = $this->prefetchSize;
327 if ($this->clientId != null) {
328 $headers["activemq.subcriptionName"] = $this->clientId;
330 if (isset($properties)) {
331 foreach ($properties as $name => $value) {
332 $headers[$name] = $value;
335 $headers['destination'] = $destination;
336 $frame = new StompFrame('SUBSCRIBE', $headers);
337 $this->_prepareReceipt($frame, $sync);
338 $this->_writeFrame($frame);
339 if ($this->_waitForReceipt($frame, $sync) == true) {
340 $this->_subscriptions[$destination] = $properties;
347 * Remove an existing subscription
349 * @param string $destination
350 * @param array $properties
351 * @param boolean $sync Perform request synchronously
353 * @throws StompException
355 public function unsubscribe ($destination, $properties = null, $sync = null)
358 if (isset($properties)) {
359 foreach ($properties as $name => $value) {
360 $headers[$name] = $value;
363 $headers['destination'] = $destination;
364 $frame = new StompFrame('UNSUBSCRIBE', $headers);
365 $this->_prepareReceipt($frame, $sync);
366 $this->_writeFrame($frame);
367 if ($this->_waitForReceipt($frame, $sync) == true) {
368 unset($this->_subscriptions[$destination]);
375 * Start a transaction
377 * @param string $transactionId
378 * @param boolean $sync Perform request synchronously
380 * @throws StompException
382 public function begin ($transactionId = null, $sync = null)
385 if (isset($transactionId)) {
386 $headers['transaction'] = $transactionId;
388 $frame = new StompFrame('BEGIN', $headers);
389 $this->_prepareReceipt($frame, $sync);
390 $this->_writeFrame($frame);
391 return $this->_waitForReceipt($frame, $sync);
394 * Commit a transaction in progress
396 * @param string $transactionId
397 * @param boolean $sync Perform request synchronously
399 * @throws StompException
401 public function commit ($transactionId = null, $sync = null)
404 if (isset($transactionId)) {
405 $headers['transaction'] = $transactionId;
407 $frame = new StompFrame('COMMIT', $headers);
408 $this->_prepareReceipt($frame, $sync);
409 $this->_writeFrame($frame);
410 return $this->_waitForReceipt($frame, $sync);
413 * Roll back a transaction in progress
415 * @param string $transactionId
416 * @param boolean $sync Perform request synchronously
418 public function abort ($transactionId = null, $sync = null)
421 if (isset($transactionId)) {
422 $headers['transaction'] = $transactionId;
424 $frame = new StompFrame('ABORT', $headers);
425 $this->_prepareReceipt($frame, $sync);
426 $this->_writeFrame($frame);
427 return $this->_waitForReceipt($frame, $sync);
430 * Acknowledge consumption of a message from a subscription
431 * Note: This operation is always asynchronous
433 * @param string|StompFrame $messageMessage ID
434 * @param string $transactionId
436 * @throws StompException
438 public function ack ($message, $transactionId = null)
440 if ($message instanceof StompFrame) {
441 $headers = $message->headers;
442 if (isset($transactionId)) {
443 $headers['transaction'] = $transactionId;
445 $frame = new StompFrame('ACK', $headers);
446 $this->_writeFrame($frame);
450 if (isset($transactionId)) {
451 $headers['transaction'] = $transactionId;
453 $headers['message-id'] = $message;
454 $frame = new StompFrame('ACK', $headers);
455 $this->_writeFrame($frame);
460 * Graceful disconnect from the server
463 public function disconnect ()
467 if ($this->clientId != null) {
468 $headers["client-id"] = $this->clientId;
471 if (is_resource($this->_socket)) {
472 $this->_writeFrame(new StompFrame('DISCONNECT', $headers));
473 fclose($this->_socket);
475 $this->_socket = null;
476 $this->_sessionId = null;
477 $this->_currentHost = -1;
478 $this->_subscriptions = array();
479 $this->_username = '';
480 $this->_password = '';
483 * Write frame to server
485 * @param StompFrame $stompFrame
487 protected function _writeFrame (StompFrame $stompFrame)
489 if (!is_resource($this->_socket)) {
490 require_once 'Stomp/Exception.php';
491 throw new StompException('Socket connection hasn\'t been established');
494 $data = $stompFrame->__toString();
495 $r = fwrite($this->_socket, $data, strlen($data));
496 if ($r === false || $r == 0) {
498 $this->_writeFrame($stompFrame);
503 * Set timeout to wait for content to read
505 * @param int $seconds_to_wait Seconds to wait for a frame
506 * @param int $milliseconds Milliseconds to wait for a frame
508 public function setReadTimeout($seconds, $milliseconds = 0)
510 $this->_read_timeout_seconds = $seconds;
511 $this->_read_timeout_milliseconds = $milliseconds;
515 * Read response frame from server
517 * @return StompFrame False when no frame to read
519 public function readFrame ()
521 if (!$this->hasFrameToRead()) {
530 $read = fread($this->_socket, $rb);
531 if ($read === false) {
533 return $this->readFrame();
536 if (strpos($data, "\x00") !== false) {
538 $data = rtrim($data, "\n");
540 $len = strlen($data);
541 } while ($len < 2 || $end == false);
543 list ($header, $body) = explode("\n\n", $data, 2);
544 $header = explode("\n", $header);
547 foreach ($header as $v) {
548 if (isset($command)) {
549 list ($name, $value) = explode(':', $v, 2);
550 $headers[$name] = $value;
555 $frame = new StompFrame($command, $headers, trim($body));
556 if (isset($frame->headers['transformation']) && $frame->headers['transformation'] == 'jms-map-json') {
557 require_once 'Stomp/Message/Map.php';
558 return new StompMessageMap($frame);
566 * Check if there is a frame to read
570 public function hasFrameToRead()
572 $read = array($this->_socket);
576 $has_frame_to_read = @stream_select($read, $write, $except, $this->_read_timeout_seconds, $this->_read_timeout_milliseconds);
578 if ($has_frame_to_read !== false)
579 $has_frame_to_read = count($read);
582 if ($has_frame_to_read === false) {
583 throw new StompException('Check failed to determine if the socket is readable');
584 } else if ($has_frame_to_read > 0) {
592 * Reconnects and renews subscriptions (if there were any)
593 * Call this method when you detect connection problems
595 protected function _reconnect ()
597 $subscriptions = $this->_subscriptions;
599 $this->connect($this->_username, $this->_password);
600 foreach ($subscriptions as $dest => $properties) {
601 $this->subscribe($dest, $properties);
605 * Graceful object desruction
608 public function __destruct()