File indexing completed on 2024-12-22 05:36:57

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Queue
0017  * @subpackage Stomp
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * The Stomp client interacts with a Stomp server.
0025  *
0026  * @category   Zend
0027  * @package    Zend_Queue
0028  * @subpackage Stomp
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Queue_Stomp_Client
0033 {
0034     /**
0035      * Array of $client Zend_Queue_Stomp_Client_Interface
0036      *
0037      * @var array
0038      */
0039     protected $_connection;
0040 
0041     /**
0042      * Add a server to connections
0043      *
0044      * @param string scheme
0045      * @param string host
0046      * @param integer port
0047      */
0048     public function __construct(
0049         $scheme = null, $host = null, $port = null,
0050         $connectionClass = 'Zend_Queue_Stomp_Client_Connection',
0051         $frameClass = 'Zend_Queue_Stomp_Frame'
0052     ) {
0053         if (($scheme !== null)
0054             && ($host !== null)
0055             && ($port !== null)
0056         ) {
0057             $this->addConnection($scheme, $host, $port, $connectionClass);
0058             $this->getConnection()->setFrameClass($frameClass);
0059         }
0060     }
0061 
0062     /**
0063      * Shutdown
0064      *
0065      * @return void
0066      */
0067     public function __destruct()
0068     {
0069         if ($this->getConnection()) {
0070             $this->getConnection()->close(true);
0071         }
0072     }
0073 
0074     /**
0075      * Add a connection to this client.
0076      *
0077      * Attempts to add this class to the client.  Returns a boolean value
0078      * indicating success of operation.
0079      *
0080      * You cannot add more than 1 connection to the client at this time.
0081      *
0082      * @param string  $scheme ['tcp', 'udp']
0083      * @param string  host
0084      * @param integer port
0085      * @param string  class - create a connection with this class; class must support Zend_Queue_Stomp_Client_ConnectionInterface
0086      * @return boolean
0087      */
0088     public function addConnection($scheme, $host, $port, $class = 'Zend_Queue_Stomp_Client_Connection')
0089     {
0090         if (!class_exists($class)) {
0091             // require_once 'Zend/Loader.php';
0092             Zend_Loader::loadClass($class);
0093         }
0094 
0095         $connection = new $class();
0096 
0097         if ($connection->open($scheme, $host, $port)) {
0098             $this->setConnection($connection);
0099             return true;
0100         }
0101 
0102         $connection->close();
0103         return false;
0104     }
0105 
0106     /**
0107      * Set client connection
0108      *
0109      * @param Zend_Queue_Stomp_Client_ConnectionInterface $connection
0110      * @return void
0111      */
0112     public function setConnection(Zend_Queue_Stomp_Client_ConnectionInterface $connection)
0113     {
0114         $this->_connection = $connection;
0115         return $this;
0116     }
0117 
0118     /**
0119      * Get client connection
0120      *
0121      * @return Zend_Queue_Stomp_Client_ConnectionInterface|null
0122      */
0123     public function getConnection()
0124     {
0125         return $this->_connection;
0126     }
0127 
0128     /**
0129      * Send a stomp frame
0130      *
0131      * Returns true if the frame was successfully sent.
0132      *
0133      * @param Zend_Queue_Stomp_FrameInterface $frame
0134      * @return boolean
0135      */
0136     public function send(Zend_Queue_Stomp_FrameInterface $frame)
0137     {
0138         $this->getConnection()->write($frame);
0139         return $this;
0140     }
0141 
0142     /**
0143      * Receive a frame
0144      *
0145      * Returns a frame or false if none were to be read.
0146      *
0147      * @return Zend_Queue_Stomp_FrameInterface|boolean
0148      */
0149     public function receive()
0150     {
0151         return $this->getConnection()->read();
0152     }
0153 
0154     /**
0155      * canRead()
0156      *
0157      * @return boolean
0158      */
0159     public function canRead()
0160     {
0161         return $this->getConnection()->canRead();
0162     }
0163 
0164     /**
0165      * creates a frame class
0166      *
0167      * @return Zend_Queue_Stomp_FrameInterface
0168      */
0169     public function createFrame()
0170     {
0171         return $this->getConnection()->createFrame();
0172     }
0173 }