Client.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * The Stomp client interacts with a Stomp server.
  24. *
  25. * @category Zend
  26. * @package Zend_Queue
  27. * @subpackage Stomp
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Queue_Stomp_Client
  32. {
  33. /**
  34. * Array of $client Zend_Queue_Stomp_Client_Interface
  35. *
  36. * @var array
  37. */
  38. protected $_connection;
  39. /**
  40. * Add a server to connections
  41. *
  42. * @param string scheme
  43. * @param string host
  44. * @param integer port
  45. */
  46. public function __construct(
  47. $scheme = null, $host = null, $port = null,
  48. $connectionClass = 'Zend_Queue_Stomp_Client_Connection',
  49. $frameClass = 'Zend_Queue_Stomp_Frame'
  50. ) {
  51. if (($scheme !== null)
  52. && ($host !== null)
  53. && ($port !== null)
  54. ) {
  55. $this->addConnection($scheme, $host, $port, $connectionClass);
  56. $this->getConnection()->setFrameClass($frameClass);
  57. }
  58. }
  59. /**
  60. * Shutdown
  61. *
  62. * @return void
  63. */
  64. public function __destruct()
  65. {
  66. if ($this->getConnection()) {
  67. $this->getConnection()->close(true);
  68. }
  69. }
  70. /**
  71. * Add a connection to this client.
  72. *
  73. * Attempts to add this class to the client. Returns a boolean value
  74. * indicating success of operation.
  75. *
  76. * You cannot add more than 1 connection to the client at this time.
  77. *
  78. * @param string $scheme ['tcp', 'udp']
  79. * @param string host
  80. * @param integer port
  81. * @param string class - create a connection with this class; class must support Zend_Queue_Stomp_Client_ConnectionInterface
  82. * @return boolean
  83. */
  84. public function addConnection($scheme, $host, $port, $class = 'Zend_Queue_Stomp_Client_Connection')
  85. {
  86. if (!class_exists($class)) {
  87. require_once 'Zend/Loader.php';
  88. Zend_Loader::loadClass($class);
  89. }
  90. $connection = new $class();
  91. if ($connection->open($scheme, $host, $port)) {
  92. $this->setConnection($connection);
  93. return true;
  94. }
  95. $connection->close();
  96. return false;
  97. }
  98. /**
  99. * Set client connection
  100. *
  101. * @param Zend_Queue_Stomp_Client_ConnectionInterface $connection
  102. * @return void
  103. */
  104. public function setConnection(Zend_Queue_Stomp_Client_ConnectionInterface $connection)
  105. {
  106. $this->_connection = $connection;
  107. return $this;
  108. }
  109. /**
  110. * Get client connection
  111. *
  112. * @return Zend_Queue_Stomp_Client_ConnectionInterface|null
  113. */
  114. public function getConnection()
  115. {
  116. return $this->_connection;
  117. }
  118. /**
  119. * Send a stomp frame
  120. *
  121. * Returns true if the frame was successfully sent.
  122. *
  123. * @param Zend_Queue_Stomp_FrameInterface $frame
  124. * @return boolean
  125. */
  126. public function send(Zend_Queue_Stomp_FrameInterface $frame)
  127. {
  128. $this->getConnection()->write($frame);
  129. return $this;
  130. }
  131. /**
  132. * Receive a frame
  133. *
  134. * Returns a frame or false if none were to be read.
  135. *
  136. * @return Zend_Queue_Stomp_FrameInterface|boolean
  137. */
  138. public function receive()
  139. {
  140. return $this->getConnection()->read();
  141. }
  142. /**
  143. * canRead()
  144. *
  145. * @return boolean
  146. */
  147. public function canRead()
  148. {
  149. return $this->getConnection()->canRead();
  150. }
  151. /**
  152. * creates a frame class
  153. *
  154. * @return Zend_Queue_Stomp_FrameInterface
  155. */
  156. public function createFrame()
  157. {
  158. return $this->getConnection()->createFrame();
  159. }
  160. }