web/lib/Zend/Queue/Stomp/Client.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Queue
       
    17  * @subpackage Stomp
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Client.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * The Stomp client interacts with a Stomp server.
       
    25  *
       
    26  * @category   Zend
       
    27  * @package    Zend_Queue
       
    28  * @subpackage Stomp
       
    29  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    31  */
       
    32 class Zend_Queue_Stomp_Client
       
    33 {
       
    34     /**
       
    35      * Array of $client Zend_Queue_Stomp_Client_Interface
       
    36      *
       
    37      * @var array
       
    38      */
       
    39     protected $_connection;
       
    40 
       
    41     /**
       
    42      * Add a server to connections
       
    43      *
       
    44      * @param string scheme
       
    45      * @param string host
       
    46      * @param integer port
       
    47      */
       
    48     public function __construct(
       
    49         $scheme = null, $host = null, $port = null,
       
    50         $connectionClass = 'Zend_Queue_Stomp_Client_Connection',
       
    51         $frameClass = 'Zend_Queue_Stomp_Frame'
       
    52     ) {
       
    53         if (($scheme !== null)
       
    54             && ($host !== null)
       
    55             && ($port !== null)
       
    56         ) {
       
    57             $this->addConnection($scheme, $host, $port, $connectionClass);
       
    58             $this->getConnection()->setFrameClass($frameClass);
       
    59         }
       
    60     }
       
    61 
       
    62     /**
       
    63      * Shutdown
       
    64      *
       
    65      * @return void
       
    66      */
       
    67     public function __destruct()
       
    68     {
       
    69         if ($this->getConnection()) {
       
    70             $this->getConnection()->close(true);
       
    71         }
       
    72     }
       
    73 
       
    74     /**
       
    75      * Add a connection to this client.
       
    76      *
       
    77      * Attempts to add this class to the client.  Returns a boolean value
       
    78      * indicating success of operation.
       
    79      *
       
    80      * You cannot add more than 1 connection to the client at this time.
       
    81      *
       
    82      * @param string  $scheme ['tcp', 'udp']
       
    83      * @param string  host
       
    84      * @param integer port
       
    85      * @param string  class - create a connection with this class; class must support Zend_Queue_Stomp_Client_ConnectionInterface
       
    86      * @return boolean
       
    87      */
       
    88     public function addConnection($scheme, $host, $port, $class = 'Zend_Queue_Stomp_Client_Connection')
       
    89     {
       
    90         if (!class_exists($class)) {
       
    91             require_once 'Zend/Loader.php';
       
    92             Zend_Loader::loadClass($class);
       
    93         }
       
    94 
       
    95         $connection = new $class();
       
    96 
       
    97         if ($connection->open($scheme, $host, $port)) {
       
    98             $this->setConnection($connection);
       
    99             return true;
       
   100         }
       
   101 
       
   102         $connection->close();
       
   103         return false;
       
   104     }
       
   105 
       
   106     /**
       
   107      * Set client connection
       
   108      *
       
   109      * @param Zend_Queue_Stomp_Client_ConnectionInterface $connection
       
   110      * @return void
       
   111      */
       
   112     public function setConnection(Zend_Queue_Stomp_Client_ConnectionInterface $connection)
       
   113     {
       
   114         $this->_connection = $connection;
       
   115         return $this;
       
   116     }
       
   117 
       
   118     /**
       
   119      * Get client connection
       
   120      *
       
   121      * @return Zend_Queue_Stomp_Client_ConnectionInterface|null
       
   122      */
       
   123     public function getConnection()
       
   124     {
       
   125         return $this->_connection;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Send a stomp frame
       
   130      *
       
   131      * Returns true if the frame was successfully sent.
       
   132      *
       
   133      * @param Zend_Queue_Stomp_FrameInterface $frame
       
   134      * @return boolean
       
   135      */
       
   136     public function send(Zend_Queue_Stomp_FrameInterface $frame)
       
   137     {
       
   138         $this->getConnection()->write($frame);
       
   139         return $this;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Receive a frame
       
   144      *
       
   145      * Returns a frame or false if none were to be read.
       
   146      *
       
   147      * @return Zend_Queue_Stomp_FrameInterface|boolean
       
   148      */
       
   149     public function receive()
       
   150     {
       
   151         return $this->getConnection()->read();
       
   152     }
       
   153 
       
   154     /**
       
   155      * canRead()
       
   156      *
       
   157      * @return boolean
       
   158      */
       
   159     public function canRead()
       
   160     {
       
   161         return $this->getConnection()->canRead();
       
   162     }
       
   163 
       
   164     /**
       
   165      * creates a frame class
       
   166      *
       
   167      * @return Zend_Queue_Stomp_FrameInterface
       
   168      */
       
   169     public function createFrame()
       
   170     {
       
   171         return $this->getConnection()->createFrame();
       
   172     }
       
   173 }