protected $_sessionId;
protected $_read_timeout_seconds = 60;
protected $_read_timeout_milliseconds = 0;
+ protected $_connect_timeout_seconds = 60;
/**
* Constructor
*
* @param string $brokerUri Broker URL
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function __construct ($brokerUri)
{
/**
* Initialize connection
*
- * @throws Stomp_Exception
+ * @throws StompException
*/
protected function _init ()
{
}
} else {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception("Bad Broker URL {$this->_brokerUri}");
+ throw new StompException("Bad Broker URL {$this->_brokerUri}");
}
}
/**
* Process broker URL
*
* @param string $url Broker URL
- * @throws Stomp_Exception
+ * @throws StompException
* @return boolean
*/
protected function _processUrl ($url)
array_push($this->_hosts, array($parsed['host'] , $parsed['port'] , $parsed['scheme']));
} else {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception("Bad Broker URL $url");
+ throw new StompException("Bad Broker URL $url");
}
}
/**
* Make socket connection to the server
*
- * @throws Stomp_Exception
+ * @throws StompException
*/
protected function _makeConnection ()
{
if (count($this->_hosts) == 0) {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception("No broker defined");
+ throw new StompException("No broker defined");
}
// force disconnect, if previous established connection exists
$i = $this->_currentHost;
$att = 0;
$connected = false;
+ $connect_errno = null;
+ $connect_errstr = null;
+
while (! $connected && $att ++ < $this->_attempts) {
if (isset($this->_params['randomize']) && $this->_params['randomize'] == 'true') {
$i = rand(0, count($this->_hosts) - 1);
fclose($this->_socket);
$this->_socket = null;
}
- $this->_socket = @fsockopen($scheme . '://' . $host, $port);
+ $this->_socket = @fsockopen($scheme . '://' . $host, $port, $connect_errno, $connect_errstr, $this->_connect_timeout_seconds);
if (!is_resource($this->_socket) && $att >= $this->_attempts && !array_key_exists($i + 1, $this->_hosts)) {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception("Could not connect to $host:$port ($att/{$this->_attempts})");
+ throw new StompException("Could not connect to $host:$port ($att/{$this->_attempts})");
} else if (is_resource($this->_socket)) {
$connected = true;
$this->_currentHost = $i;
}
if (! $connected) {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception("Could not connect to a broker");
+ throw new StompException("Could not connect to a broker");
}
}
/**
* @param string $username
* @param string $password
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function connect ($username = '', $password = '')
{
if ($this->clientId != null) {
$headers["client-id"] = $this->clientId;
}
- $frame = new Stomp_Frame("CONNECT", $headers);
+ $frame = new StompFrame("CONNECT", $headers);
$this->_writeFrame($frame);
$frame = $this->readFrame();
- if ($frame instanceof Stomp_Frame && $frame->command == 'CONNECTED') {
+ if ($frame instanceof StompFrame && $frame->command == 'CONNECTED') {
$this->_sessionId = $frame->headers["session"];
return true;
} else {
require_once 'Stomp/Exception.php';
- if ($frame instanceof Stomp_Frame) {
- throw new Stomp_Exception("Unexpected command: {$frame->command}", 0, $frame->body);
+ if ($frame instanceof StompFrame) {
+ throw new StompException("Unexpected command: {$frame->command}", 0, $frame->body);
} else {
- throw new Stomp_Exception("Connection not acknowledged");
+ throw new StompException("Connection not acknowledged");
}
}
}
* Send a message to a destination in the messaging system
*
* @param string $destination Destination queue
- * @param string|Stomp_Frame $msg Message
+ * @param string|StompFrame $msg Message
* @param array $properties
* @param boolean $sync Perform request synchronously
* @return boolean
*/
- public function send ($destination, $msg, $properties = null, $sync = null)
+ public function send ($destination, $msg, $properties = array(), $sync = null)
{
- if ($msg instanceof Stomp_Frame) {
+ if ($msg instanceof StompFrame) {
$msg->headers['destination'] = $destination;
- $msg->headers = array_merge($msg->headers, $properties);
+ if (is_array($properties)) $msg->headers = array_merge($msg->headers, $properties);
$frame = $msg;
} else {
$headers = $properties;
$headers['destination'] = $destination;
- $frame = new Stomp_Frame('SEND', $headers, $msg);
+ $frame = new StompFrame('SEND', $headers, $msg);
}
$this->_prepareReceipt($frame, $sync);
$this->_writeFrame($frame);
/**
* Prepair frame receipt
*
- * @param Stomp_Frame $frame
+ * @param StompFrame $frame
* @param boolean $sync
*/
- protected function _prepareReceipt (Stomp_Frame $frame, $sync)
+ protected function _prepareReceipt (StompFrame $frame, $sync)
{
$receive = $this->sync;
if ($sync !== null) {
/**
* Wait for receipt
*
- * @param Stomp_Frame $frame
+ * @param StompFrame $frame
* @param boolean $sync
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
- protected function _waitForReceipt (Stomp_Frame $frame, $sync)
+ protected function _waitForReceipt (StompFrame $frame, $sync)
{
$receive = $this->sync;
return true;
}
$frame = $this->readFrame();
- if ($frame instanceof Stomp_Frame && $frame->command == 'RECEIPT') {
+ if ($frame instanceof StompFrame && $frame->command == 'RECEIPT') {
if ($frame->headers['receipt-id'] == $id) {
return true;
} else {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception("Unexpected receipt id {$frame->headers['receipt-id']}", 0, $frame->body);
+ throw new StompException("Unexpected receipt id {$frame->headers['receipt-id']}", 0, $frame->body);
}
} else {
require_once 'Stomp/Exception.php';
- if ($frame instanceof Stomp_Frame) {
- throw new Stomp_Exception("Unexpected command {$frame->command}", 0, $frame->body);
+ if ($frame instanceof StompFrame) {
+ throw new StompException("Unexpected command {$frame->command}", 0, $frame->body);
} else {
- throw new Stomp_Exception("Receipt not received");
+ throw new StompException("Receipt not received");
}
}
}
* @param array $properties
* @param boolean $sync Perform request synchronously
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function subscribe ($destination, $properties = null, $sync = null)
{
}
}
$headers['destination'] = $destination;
- $frame = new Stomp_Frame('SUBSCRIBE', $headers);
+ $frame = new StompFrame('SUBSCRIBE', $headers);
$this->_prepareReceipt($frame, $sync);
$this->_writeFrame($frame);
if ($this->_waitForReceipt($frame, $sync) == true) {
* @param array $properties
* @param boolean $sync Perform request synchronously
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function unsubscribe ($destination, $properties = null, $sync = null)
{
}
}
$headers['destination'] = $destination;
- $frame = new Stomp_Frame('UNSUBSCRIBE', $headers);
+ $frame = new StompFrame('UNSUBSCRIBE', $headers);
$this->_prepareReceipt($frame, $sync);
$this->_writeFrame($frame);
if ($this->_waitForReceipt($frame, $sync) == true) {
* @param string $transactionId
* @param boolean $sync Perform request synchronously
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function begin ($transactionId = null, $sync = null)
{
if (isset($transactionId)) {
$headers['transaction'] = $transactionId;
}
- $frame = new Stomp_Frame('BEGIN', $headers);
+ $frame = new StompFrame('BEGIN', $headers);
$this->_prepareReceipt($frame, $sync);
$this->_writeFrame($frame);
return $this->_waitForReceipt($frame, $sync);
* @param string $transactionId
* @param boolean $sync Perform request synchronously
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function commit ($transactionId = null, $sync = null)
{
if (isset($transactionId)) {
$headers['transaction'] = $transactionId;
}
- $frame = new Stomp_Frame('COMMIT', $headers);
+ $frame = new StompFrame('COMMIT', $headers);
$this->_prepareReceipt($frame, $sync);
$this->_writeFrame($frame);
return $this->_waitForReceipt($frame, $sync);
if (isset($transactionId)) {
$headers['transaction'] = $transactionId;
}
- $frame = new Stomp_Frame('ABORT', $headers);
+ $frame = new StompFrame('ABORT', $headers);
$this->_prepareReceipt($frame, $sync);
$this->_writeFrame($frame);
return $this->_waitForReceipt($frame, $sync);
* Acknowledge consumption of a message from a subscription
* Note: This operation is always asynchronous
*
- * @param string|Stomp_Frame $messageMessage ID
+ * @param string|StompFrame $messageMessage ID
* @param string $transactionId
* @return boolean
- * @throws Stomp_Exception
+ * @throws StompException
*/
public function ack ($message, $transactionId = null)
{
- if ($message instanceof Stomp_Frame) {
- $frame = new Stomp_Frame('ACK', $message->headers);
+ if ($message instanceof StompFrame) {
+ $headers = $message->headers;
+ if (isset($transactionId)) {
+ $headers['transaction'] = $transactionId;
+ }
+ $frame = new StompFrame('ACK', $headers);
$this->_writeFrame($frame);
return true;
} else {
$headers['transaction'] = $transactionId;
}
$headers['message-id'] = $message;
- $frame = new Stomp_Frame('ACK', $headers);
+ $frame = new StompFrame('ACK', $headers);
$this->_writeFrame($frame);
return true;
}
}
if (is_resource($this->_socket)) {
- $this->_writeFrame(new Stomp_Frame('DISCONNECT', $headers));
+ $this->_writeFrame(new StompFrame('DISCONNECT', $headers));
fclose($this->_socket);
}
$this->_socket = null;
/**
* Write frame to server
*
- * @param Stomp_Frame $stompFrame
+ * @param StompFrame $stompFrame
*/
- protected function _writeFrame (Stomp_Frame $stompFrame)
+ protected function _writeFrame (StompFrame $stompFrame)
{
if (!is_resource($this->_socket)) {
require_once 'Stomp/Exception.php';
- throw new Stomp_Exception('Socket connection hasn\'t been established');
+ throw new StompException('Socket connection hasn\'t been established');
}
$data = $stompFrame->__toString();
}
/**
- * Read responce frame from server
+ * Read response frame from server
*
- * @return Stomp_Frame|Stomp_Message_Map|boolean False when no frame to read
+ * @return StompFrame False when no frame to read
*/
public function readFrame ()
{
$rb = 1024;
$data = '';
+ $end = false;
+
do {
- $read = fgets($this->_socket, $rb);
+ $read = fread($this->_socket, $rb);
if ($read === false) {
$this->_reconnect();
return $this->readFrame();
}
$data .= $read;
+ if (strpos($data, "\x00") !== false) {
+ $end = true;
+ $data = rtrim($data, "\n");
+ }
$len = strlen($data);
- } while (($len < 2 || ! ($data[$len - 2] == "\x00" && $data[$len - 1] == "\n")));
+ } while ($len < 2 || $end == false);
list ($header, $body) = explode("\n\n", $data, 2);
$header = explode("\n", $header);
$command = $v;
}
}
- $frame = new Stomp_Frame($command, $headers, trim($body));
- if (isset($frame->headers['amq-msg-type']) && $frame->headers['amq-msg-type'] == 'MapMessage') {
+ $frame = new StompFrame($command, $headers, trim($body));
+ if (isset($frame->headers['transformation']) && $frame->headers['transformation'] == 'jms-map-json') {
require_once 'Stomp/Message/Map.php';
- return new Stomp_Message_Map($frame);
+ return new StompMessageMap($frame);
} else {
return $frame;
}
+ return $frame;
}
/**
$write = null;
$except = null;
- $has_frame_to_read = stream_select($read, $write, $except, $this->_read_timeout_seconds, $this->_read_timeout_milliseconds);
+ $has_frame_to_read = @stream_select($read, $write, $except, $this->_read_timeout_seconds, $this->_read_timeout_milliseconds);
+
+ if ($has_frame_to_read !== false)
+ $has_frame_to_read = count($read);
+
if ($has_frame_to_read === false) {
- throw new Stomp_Exception('Check failed to determin if the socket is readable');
+ throw new StompException('Check failed to determine if the socket is readable');
} else if ($has_frame_to_read > 0) {
return true;
} else {
-<?php
-/**
- *
- * Copyright 2005-2006 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* vim: set expandtab tabstop=3 shiftwidth=3: */
+<?php\r
+/**\r
+ *\r
+ * Copyright 2005-2006 The Apache Software Foundation\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/* vim: set expandtab tabstop=3 shiftwidth=3: */\r
\r
/**\r
- * Stomp Frames are messages that are sent and received on a StompConnection.\r
+ * Stomp Frames are messages that are sent and received on a stomp connection.\r
*\r
* @package Stomp\r
- * @author Hiram Chirino <hiram@hiramchirino.com>\r
- * @author Dejan Bosanac <dejan@nighttale.net>\r
- * @author Michael Caplan <mcaplan@labnet.net>\r
- * @version $Revision: 36 $\r
*/\r
-class Stomp_Frame\r
+class StompFrame\r
{\r
public $command;\r
public $headers = array();\r
$this->body = $body;\r
\r
if ($this->command == 'ERROR') {\r
- require_once 'Stomp/Exception.php';\r
- throw new Stomp_Exception($this->headers['message'], 0, $this->body);\r
+ require_once 'Exception.php';\r
+ throw new StompException($this->headers['message'], 0, $this->body);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Convert frame to transportable string\r
+ *\r
+ * @return string\r
+ */\r
+ public function __toString()\r
+ {\r
+ $data = $this->command . "\n";\r
+ \r
+ foreach ($this->headers as $name => $value) {\r
+ $data .= $name . ": " . $value . "\n";\r
}\r
- }
-
- /**
- * Convert frame to transportable string
- *
- * @return string
- */
- public function __toString()
- {
- $data = $this->command . "\n";
-
- foreach ($this->headers as $name => $value) {
- $data .= $name . ": " . $value . "\n";
- }
-
- $data .= "\n";
- $data .= $this->body;
- return $data .= "\x00\n";
+ \r
+ $data .= "\n";\r
+ $data .= $this->body;\r
+ return $data .= "\x00";\r
}\r
}\r
?>
\ No newline at end of file