import v1.1.0_RC2 | 2009-09-20
This commit is contained in:
173
libs/Zend/Queue/Stomp/Client.php
Normal file
173
libs/Zend/Queue/Stomp/Client.php
Normal file
@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Client.php 17189 2009-07-27 17:41:34Z matthew $
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Stomp client interacts with a Stomp server.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Queue_Stomp_Client
|
||||
{
|
||||
/**
|
||||
* Array of $client Zend_Queue_Stomp_Client_Interface
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_connection;
|
||||
|
||||
/**
|
||||
* Add a server to connections
|
||||
*
|
||||
* @param string scheme
|
||||
* @param string host
|
||||
* @param integer port
|
||||
*/
|
||||
public function __construct(
|
||||
$scheme = null, $host = null, $port = null,
|
||||
$connectionClass = 'Zend_Queue_Stomp_Client_Connection',
|
||||
$frameClass = 'Zend_Queue_Stomp_Frame'
|
||||
) {
|
||||
if (($scheme !== null)
|
||||
&& ($host !== null)
|
||||
&& ($port !== null)
|
||||
) {
|
||||
$this->addConnection($scheme, $host, $port, $connectionClass);
|
||||
$this->getConnection()->setFrameClass($frameClass);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->getConnection()) {
|
||||
$this->getConnection()->close(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a connection to this client.
|
||||
*
|
||||
* Attempts to add this class to the client. Returns a boolean value
|
||||
* indicating success of operation.
|
||||
*
|
||||
* You cannot add more than 1 connection to the client at this time.
|
||||
*
|
||||
* @param string $scheme ['tcp', 'udp']
|
||||
* @param string host
|
||||
* @param integer port
|
||||
* @param string class - create a connection with this class; class must support Zend_Queue_Stomp_Client_ConnectionInterface
|
||||
* @return boolean
|
||||
*/
|
||||
public function addConnection($scheme, $host, $port, $class = 'Zend_Queue_Stomp_Client_Connection')
|
||||
{
|
||||
if (!class_exists($class)) {
|
||||
require_once 'Zend/Loader.php';
|
||||
Zend_Loader::loadClass($class);
|
||||
}
|
||||
|
||||
$connection = new $class();
|
||||
|
||||
if ($connection->open($scheme, $host, $port)) {
|
||||
$this->setConnection($connection);
|
||||
return true;
|
||||
}
|
||||
|
||||
$connection->close();
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set client connection
|
||||
*
|
||||
* @param Zend_Queue_Stomp_Client_ConnectionInterface $connection
|
||||
* @return void
|
||||
*/
|
||||
public function setConnection(Zend_Queue_Stomp_Client_ConnectionInterface $connection)
|
||||
{
|
||||
$this->_connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get client connection
|
||||
*
|
||||
* @return Zend_Queue_Stomp_Client_ConnectionInterface|null
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->_connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a stomp frame
|
||||
*
|
||||
* Returns true if the frame was successfully sent.
|
||||
*
|
||||
* @param Zend_Queue_Stomp_FrameInterface $frame
|
||||
* @return boolean
|
||||
*/
|
||||
public function send(Zend_Queue_Stomp_FrameInterface $frame)
|
||||
{
|
||||
$this->getConnection()->write($frame);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a frame
|
||||
*
|
||||
* Returns a frame or false if none were to be read.
|
||||
*
|
||||
* @return Zend_Queue_Stomp_FrameInterface|boolean
|
||||
*/
|
||||
public function receive()
|
||||
{
|
||||
return $this->getConnection()->read();
|
||||
}
|
||||
|
||||
/**
|
||||
* canRead()
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function canRead()
|
||||
{
|
||||
return $this->getConnection()->canRead();
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a frame class
|
||||
*
|
||||
* @return Zend_Queue_Stomp_FrameInterface
|
||||
*/
|
||||
public function createFrame()
|
||||
{
|
||||
return $this->getConnection()->createFrame();
|
||||
}
|
||||
}
|
280
libs/Zend/Queue/Stomp/Client/Connection.php
Normal file
280
libs/Zend/Queue/Stomp/Client/Connection.php
Normal file
@ -0,0 +1,280 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Connection.php 17241 2009-07-28 13:01:20Z matthew $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Queue_Stomp_Client_ConnectionInterface
|
||||
*/
|
||||
require_once 'Zend/Queue/Stomp/Client/ConnectionInterface.php';
|
||||
|
||||
/**
|
||||
* The Stomp client interacts with a Stomp server.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Queue_Stomp_Client_Connection
|
||||
implements Zend_Queue_Stomp_Client_ConnectionInterface
|
||||
{
|
||||
const READ_TIMEOUT_DEFAULT_USEC = 0; // 0 microseconds
|
||||
const READ_TIMEOUT_DEFAULT_SEC = 5; // 5 seconds
|
||||
|
||||
/**
|
||||
* Connection options
|
||||
* @var array
|
||||
*/
|
||||
protected $_options;
|
||||
|
||||
/**
|
||||
* tcp/udp socket
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $_socket = false;
|
||||
|
||||
/**
|
||||
* open() opens a socket to the Stomp server
|
||||
*
|
||||
* @param array $options ('scheme', 'host', 'port')
|
||||
* @param string $scheme
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param array $options Accepts "timeout_sec" and "timeout_usec" keys
|
||||
* @return true;
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function open($scheme, $host, $port, array $options = array())
|
||||
{
|
||||
$str = $scheme . '://' . $host;
|
||||
$this->_socket = fsockopen($str, $port, $errno, $errstr);
|
||||
|
||||
if ($this->_socket === false) {
|
||||
// aparently there is some reason that fsockopen will return false
|
||||
// but it normally throws an error.
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception("Unable to connect to $str; error = $errstr ( errno = $errno )");
|
||||
}
|
||||
|
||||
stream_set_blocking($this->_socket, 0); // non blocking
|
||||
|
||||
if (!isset($options['timeout_sec'])) {
|
||||
$options['timeout_sec'] = self::READ_TIMEOUT_DEFAULT_SEC;
|
||||
}
|
||||
if (! isset($options['timeout_usec'])) {
|
||||
$options['timeout_usec'] = self::READ_TIMEOUT_DEFAULT_USEC;
|
||||
}
|
||||
|
||||
$this->_options = $options;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the socket explicitly when destructed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Close connection
|
||||
*
|
||||
* @param boolean $destructor
|
||||
* @return void
|
||||
*/
|
||||
public function close($destructor = false)
|
||||
{
|
||||
// Gracefully disconnect
|
||||
if (!$destructor) {
|
||||
$frame = $this->createFrame();
|
||||
$frame->setCommand('DISCONNECT');
|
||||
$this->write($frame);
|
||||
}
|
||||
|
||||
// @todo: Should be fixed.
|
||||
// When the socket is "closed", it will trigger the below error when php exits
|
||||
// Fatal error: Exception thrown without a stack frame in Unknown on line 0
|
||||
|
||||
// Danlo: I suspect this is because this has already been claimed by the interpeter
|
||||
// thus trying to shutdown this resources, which is already shutdown is a problem.
|
||||
if (is_resource($this->_socket)) {
|
||||
// fclose($this->_socket);
|
||||
}
|
||||
|
||||
// $this->_socket = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether we are connected to the server
|
||||
*
|
||||
* @return true
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function ping()
|
||||
{
|
||||
if (!is_resource($this->_socket)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('Not connected to Stomp server');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a frame to the stomp server
|
||||
*
|
||||
* @example $response = $client->write($frame)->read();
|
||||
*
|
||||
* @param Zend_Queue_Stom_FrameInterface $frame
|
||||
* @return $this
|
||||
*/
|
||||
public function write(Zend_Queue_Stomp_FrameInterface $frame)
|
||||
{
|
||||
$this->ping();
|
||||
$output = $frame->toFrame();
|
||||
|
||||
$bytes = fwrite($this->_socket, $output, strlen($output));
|
||||
if ($bytes === false || $bytes == 0) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('No bytes written');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the socket to see if there is data for us
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function canRead()
|
||||
{
|
||||
$read = array($this->_socket);
|
||||
$write = null;
|
||||
$except = null;
|
||||
|
||||
return stream_select(
|
||||
$read,
|
||||
$write,
|
||||
$except,
|
||||
$this->_options['timeout_sec'],
|
||||
$this->_options['timeout_usec']
|
||||
) == 1;
|
||||
// see http://us.php.net/manual/en/function.stream-select.php
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads in a frame from the socket or returns false.
|
||||
*
|
||||
* @return Zend_Queue_Stomp_FrameInterface|false
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$this->ping();
|
||||
|
||||
$response = '';
|
||||
$prev = '';
|
||||
|
||||
// while not end of file.
|
||||
while (!feof($this->_socket)) {
|
||||
// read in one character until "\0\n" is found
|
||||
$data = fread($this->_socket, 1);
|
||||
|
||||
// check to make sure that the connection is not lost.
|
||||
if ($data === false) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('Connection lost');
|
||||
}
|
||||
|
||||
// append last character read to $response
|
||||
$response .= $data;
|
||||
|
||||
// is this \0 (prev) \n (data)? END_OF_FRAME
|
||||
if (ord($data) == 10 && ord($prev) == 0) {
|
||||
break;
|
||||
}
|
||||
$prev = $data;
|
||||
}
|
||||
|
||||
if ($response === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$frame = $this->createFrame();
|
||||
$frame->fromFrame($response);
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the frameClass to be used
|
||||
*
|
||||
* This must be a Zend_Queue_Stomp_FrameInterface.
|
||||
*
|
||||
* @param string $classname - class is an instance of Zend_Queue_Stomp_FrameInterface
|
||||
* @return $this;
|
||||
*/
|
||||
public function setFrameClass($classname)
|
||||
{
|
||||
$this->_options['frameClass'] = $classname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the frameClass
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFrameClass()
|
||||
{
|
||||
return isset($this->_options['frameClass'])
|
||||
? $this->_options['frameClass']
|
||||
: 'Zend_Queue_Stomp_Frame';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an empty frame
|
||||
*
|
||||
* @return Zend_Queue_Stomp_FrameInterface
|
||||
*/
|
||||
public function createFrame()
|
||||
{
|
||||
$class = $this->getFrameClass();
|
||||
|
||||
if (!class_exists($class)) {
|
||||
require_once 'Zend/Loader.php';
|
||||
Zend_Loader::loadClass($class);
|
||||
}
|
||||
|
||||
$frame = new $class();
|
||||
|
||||
if (!$frame instanceof Zend_Queue_Stomp_FrameInterface) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('Invalid Frame class provided; must implement Zend_Queue_Stomp_FrameInterface');
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
}
|
103
libs/Zend/Queue/Stomp/Client/ConnectionInterface.php
Normal file
103
libs/Zend/Queue/Stomp/Client/ConnectionInterface.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: ConnectionInterface.php 17241 2009-07-28 13:01:20Z matthew $
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Stomp client interacts with a Stomp server.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Queue_Stomp_Client_ConnectionInterface
|
||||
{
|
||||
/**
|
||||
* @param string $scheme ['tcp', 'udp']
|
||||
* @param string host
|
||||
* @param integer port
|
||||
* @param string class - create a connection with this class; class must support Zend_Queue_Stomp_Client_Connection_Interface
|
||||
* @return boolean
|
||||
*/
|
||||
public function open($scheme, $host, $port);
|
||||
|
||||
/**
|
||||
* @param boolean $destructor
|
||||
* @return void
|
||||
*/
|
||||
public function close($destructor = false);
|
||||
|
||||
/**
|
||||
* Check whether we are connected to the server
|
||||
*
|
||||
* @return true
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function ping();
|
||||
|
||||
/**
|
||||
* write a frame to the stomp server
|
||||
*
|
||||
* @example $response = $client->write($frame)->read();
|
||||
*
|
||||
* @param Zend_Queue_Stomp_FrameInterface $frame
|
||||
* @return $this
|
||||
*/
|
||||
public function write(Zend_Queue_Stomp_FrameInterface $frame);
|
||||
|
||||
/**
|
||||
* tests the socket to see if there is data for us
|
||||
*/
|
||||
public function canRead();
|
||||
|
||||
/**
|
||||
* reads in a frame from the socket or returns false.
|
||||
*
|
||||
* @return Zend_Queue_Stomp_Frame|false
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function read();
|
||||
|
||||
/**
|
||||
* Set the frame class to be used
|
||||
*
|
||||
* This must be a Zend_Queue_Stomp_FrameInterface.
|
||||
*
|
||||
* @param string $class
|
||||
* @return Zend_Queue_Stomp_Client_ConnectionInterface;
|
||||
*/
|
||||
public function setFrameClass($class);
|
||||
|
||||
/**
|
||||
* Get the frameClass
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFrameClass();
|
||||
|
||||
/**
|
||||
* create an empty frame
|
||||
*
|
||||
* @return Zend_Queue_Stomp_FrameInterface class
|
||||
*/
|
||||
public function createFrame();
|
||||
}
|
363
libs/Zend/Queue/Stomp/Frame.php
Normal file
363
libs/Zend/Queue/Stomp/Frame.php
Normal file
@ -0,0 +1,363 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Frame.php 16971 2009-07-22 18:05:45Z mikaelkael $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Queue_Stomp_FrameInterface
|
||||
*/
|
||||
require_once 'Zend/Queue/Stomp/FrameInterface.php';
|
||||
|
||||
/**
|
||||
* This class represents a Stomp Frame
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Queue_Stomp_Frame
|
||||
implements Zend_Queue_Stomp_FrameInterface
|
||||
{
|
||||
const END_OF_FRAME = "\x00\n";
|
||||
const CONTENT_LENGTH = 'content-length';
|
||||
const EOL = "\n";
|
||||
|
||||
/**
|
||||
* Headers for the frame
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_headers = array();
|
||||
|
||||
/**
|
||||
* The command for the frame
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_command = null;
|
||||
|
||||
/**
|
||||
* The body of the frame
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_body = null;
|
||||
|
||||
/**
|
||||
* Do the content-length automatically?
|
||||
*/
|
||||
protected $_autoContentLength = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->setHeaders(array());
|
||||
$this->setBody(null);
|
||||
$this->setCommand(null);
|
||||
$this->setAutoContentLength(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the status of the auto content length
|
||||
*
|
||||
* If AutoContentLength is true this code will automatically put the
|
||||
* content-length header in, even if it is already set by the user.
|
||||
*
|
||||
* This is done to make the message sending more reliable.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getAutoContentLength()
|
||||
{
|
||||
return $this->_autoContentLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* setAutoContentLength()
|
||||
*
|
||||
* Set the value on or off.
|
||||
*
|
||||
* @param boolean $auto
|
||||
* @return $this;
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setAutoContentLength($auto)
|
||||
{
|
||||
if (!is_bool($auto)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$auto is not a boolean');
|
||||
}
|
||||
|
||||
$this->_autoContentLength = $auto;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the headers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->_headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the headers
|
||||
*
|
||||
* Throws an exception if the array values are not strings.
|
||||
*
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setHeaders(array $headers)
|
||||
{
|
||||
foreach ($headers as $header => $value) {
|
||||
$this->setHeader($header, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value for a header
|
||||
*
|
||||
* @param string $header
|
||||
* @param string $value
|
||||
* @return Zend_Queue_Stomp_Frame
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setHeader($header, $value) {
|
||||
if (!is_string($header)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$header is not a string: ' . print_r($header, true));
|
||||
}
|
||||
|
||||
if (!is_scalar($value)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$value is not a string: ' . print_r($value, true));
|
||||
}
|
||||
|
||||
$this->_headers[$header] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a value for a header
|
||||
*
|
||||
* Returns false if the header does not exist.
|
||||
*
|
||||
* @param string $header
|
||||
* @return string|false
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function getHeader($header)
|
||||
{
|
||||
if (!is_string($header)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$header is not a string');
|
||||
}
|
||||
|
||||
return isset($this->_headers[$header])
|
||||
? $this->_headers[$header]
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the body for this frame
|
||||
*
|
||||
* Returns false if the body does not exist
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public function getBody()
|
||||
{
|
||||
return is_null($this->_body)
|
||||
? false
|
||||
: $this->_body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the body for this frame
|
||||
*
|
||||
* Set to null for no body.
|
||||
*
|
||||
* @param string|null $body
|
||||
* @return Zend_Queue_Stomp_Frame
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setBody($body)
|
||||
{
|
||||
if (!is_string($body) && !is_null($body)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$body is not a string or null');
|
||||
}
|
||||
|
||||
$this->_body = $body;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the command for this frame
|
||||
*
|
||||
* Return false if the command does not exist
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
public function getCommand()
|
||||
{
|
||||
return is_null($this->_command)
|
||||
? false
|
||||
: $this->_command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the body for this frame
|
||||
*
|
||||
* @param string|null
|
||||
* @return Zend_Queue_Stomp_Frame
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setCommand($command)
|
||||
{
|
||||
if (!is_string($command) && !is_null($command)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$command is not a string or null');
|
||||
}
|
||||
|
||||
$this->_command = $command;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the current parameters and returns a Stomp Frame
|
||||
*
|
||||
* @return string
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function toFrame()
|
||||
{
|
||||
if ($this->getCommand() === false) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('You must set the command');
|
||||
}
|
||||
|
||||
$command = $this->getCommand();
|
||||
$headers = $this->getHeaders();
|
||||
$body = $this->getBody();
|
||||
$frame = '';
|
||||
|
||||
// add a content-length to the SEND command.
|
||||
// @see http://stomp.codehaus.org/Protocol
|
||||
if ($this->getAutoContentLength()) {
|
||||
$headers[self::CONTENT_LENGTH] = strlen($this->getBody());
|
||||
}
|
||||
|
||||
// Command
|
||||
$frame = $command . self::EOL;
|
||||
|
||||
// Headers
|
||||
foreach ($headers as $key=>$value) {
|
||||
$frame .= $key . ': ' . $value . self::EOL;
|
||||
}
|
||||
|
||||
// Seperator
|
||||
$frame .= self::EOL; // blank line required by protocol
|
||||
|
||||
// add the body if any
|
||||
if ($body !== false) {
|
||||
$frame .= $body;
|
||||
}
|
||||
$frame .= self::END_OF_FRAME;
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see toFrame()
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
try {
|
||||
$return = $this->toFrame();
|
||||
} catch (Zend_Queue_Exception $e) {
|
||||
$return = '';
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts a frame and deconstructs the frame into its component parts
|
||||
*
|
||||
* @param string $frame - a stomp frame
|
||||
* @return $this
|
||||
*/
|
||||
public function fromFrame($frame)
|
||||
{
|
||||
if (!is_string($frame)) {
|
||||
require_once 'Zend/Queue/Exception.php';
|
||||
throw new Zend_Queue_Exception('$frame is not a string');
|
||||
}
|
||||
|
||||
$headers = array();
|
||||
$body = null;
|
||||
$command = false;
|
||||
$header = '';
|
||||
|
||||
// separate the headers and the body
|
||||
$match = self::EOL . self::EOL;
|
||||
if (preg_match('/' . $match . '/', $frame)) {
|
||||
list ($header, $body) = explode($match, $frame, 2);
|
||||
} else {
|
||||
$header = $frame;
|
||||
}
|
||||
|
||||
// blow up headers
|
||||
$headers = explode(self::EOL, $header);
|
||||
unset($header);
|
||||
|
||||
// get the command (first line)
|
||||
$this->setCommand(array_shift($headers));
|
||||
|
||||
// set each of the headers.
|
||||
foreach ($headers as $header) {
|
||||
if (strpos($header, ':') > 0) {
|
||||
list($name, $value) = explode(':', $header, 2);
|
||||
$this->setHeader($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
// crop the body if content-length is present
|
||||
if ($this->getHeader(self::CONTENT_LENGTH) !== false ) {
|
||||
$length = (int) $this->getHeader(self::CONTENT_LENGTH);
|
||||
$body = substr($body, 0, $length);
|
||||
}
|
||||
|
||||
$this->setBody($body);
|
||||
return $this;
|
||||
}
|
||||
}
|
154
libs/Zend/Queue/Stomp/FrameInterface.php
Normal file
154
libs/Zend/Queue/Stomp/FrameInterface.php
Normal file
@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: FrameInterface.php 16971 2009-07-22 18:05:45Z mikaelkael $
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class represents a Stomp Frame Interface
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Queue
|
||||
* @subpackage Stomp
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Queue_Stomp_FrameInterface
|
||||
{
|
||||
/**
|
||||
* Get the status of the auto content length
|
||||
*
|
||||
* If AutoContentLength is true this code will automatically put the
|
||||
* content-length header in, even if it is already set by the user.
|
||||
*
|
||||
* This is done to make the message sending more reliable.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getAutoContentLength();
|
||||
|
||||
/**
|
||||
* setAutoContentLength()
|
||||
*
|
||||
* Set the value on or off.
|
||||
*
|
||||
* @param boolean $auto
|
||||
* @return $this;
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setAutoContentLength($auto);
|
||||
|
||||
/**
|
||||
* Get the headers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getHeaders();
|
||||
|
||||
/**
|
||||
* Set the headers
|
||||
*
|
||||
* Throws an exception if the array values are not strings.
|
||||
*
|
||||
* @param array $headers
|
||||
* @return $this
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setHeaders(array $headers);
|
||||
|
||||
/**
|
||||
* Returns a value for a header
|
||||
* returns false if the header does not exist
|
||||
*
|
||||
* @param string $header
|
||||
* @return $string
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function getHeader($header);
|
||||
|
||||
/**
|
||||
* Returns a value for a header
|
||||
* returns false if the header does not exist
|
||||
*
|
||||
* @param string $header
|
||||
* @param string $value
|
||||
* @return $this
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setHeader($header, $value);
|
||||
|
||||
/**
|
||||
* Return the body for this frame
|
||||
* returns false if the body does not exist
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function getBody();
|
||||
|
||||
/**
|
||||
* Set the body for this frame
|
||||
* returns false if the body does not exist
|
||||
*
|
||||
* Set to null for no body.
|
||||
*
|
||||
* @param string|null $body
|
||||
* @return $this
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setBody($body);
|
||||
|
||||
/**
|
||||
* Return the command for this frame
|
||||
* return false if the command does not exist
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function getCommand();
|
||||
|
||||
/**
|
||||
* Set the body for this frame
|
||||
* returns false if the body does not exist
|
||||
*
|
||||
* @return $this
|
||||
* @throws Zend_Queue_Exception
|
||||
*/
|
||||
public function setCommand($command);
|
||||
|
||||
|
||||
/**
|
||||
* Takes the current parameters and returns a Stomp Frame
|
||||
*
|
||||
* @throws Zend_Queue_Exception
|
||||
* @return string
|
||||
*/
|
||||
public function toFrame();
|
||||
|
||||
/**
|
||||
* @see toFrame()
|
||||
*/
|
||||
public function __toString();
|
||||
|
||||
/**
|
||||
* Accepts a frame and deconstructs the frame into its' component parts
|
||||
*
|
||||
* @param string $frame - a stomp frame
|
||||
* @return $this
|
||||
*/
|
||||
public function fromFrame($frame);
|
||||
}
|
Reference in New Issue
Block a user