Connection.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Stomp.php 14504 2009-03-27 05:32:18Z danlo $
  21. */
  22. /**
  23. * @see Zend_Queue_Stomp_Client_ConnectionInterface
  24. */
  25. require_once 'Zend/Queue/Stomp/Client/ConnectionInterface.php';
  26. /**
  27. * The Stomp client interacts with a Stomp server.
  28. *
  29. * @category Zend
  30. * @package Zend_Queue
  31. * @subpackage Stomp
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Queue_Stomp_Client_Connection
  36. implements Zend_Queue_Stomp_Client_ConnectionInterface
  37. {
  38. const READ_TIMEOUT_DEFAULT_USEC = 0; // 0 microseconds
  39. const READ_TIMEOUT_DEFAULT_SEC = 5; // 5 seconds
  40. /**
  41. * Connection options
  42. * @var array
  43. */
  44. protected $_options;
  45. /**
  46. * tcp/udp socket
  47. *
  48. * @var resource
  49. */
  50. protected $_socket = false;
  51. /**
  52. * open() opens a socket to the Stomp server
  53. *
  54. * @param array $options ('scheme', 'host', 'port')
  55. * @param string $scheme
  56. * @param string $host
  57. * @param int $port
  58. * @param array $options Accepts "timeout_sec" and "timeout_usec" keys
  59. * @return true;
  60. * @throws Zend_Queue_Exception
  61. */
  62. public function open($scheme, $host, $port, array $options = array())
  63. {
  64. $str = $scheme . '://' . $host;
  65. $this->_socket = fsockopen($str, $port, $errno, $errstr);
  66. if ($this->_socket === false) {
  67. // aparently there is some reason that fsockopen will return false
  68. // but it normally throws an error.
  69. require_once 'Zend/Queue/Exception.php';
  70. throw new Zend_Queue_Exception("Unable to connect to $str; error = $errstr ( errno = $errno )");
  71. }
  72. stream_set_blocking($this->_socket, 0); // non blocking
  73. if (!isset($options['timeout_sec'])) {
  74. $options['timeout_sec'] = self::READ_TIMEOUT_DEFAULT_SEC;
  75. }
  76. if (! isset($options['timeout_usec'])) {
  77. $options['timeout_usec'] = self::READ_TIMEOUT_DEFAULT_USEC;
  78. }
  79. $this->_options = $options;
  80. return true;
  81. }
  82. /**
  83. * Close the socket explicitly when destructed
  84. *
  85. * @return void
  86. */
  87. public function __destruct()
  88. {
  89. }
  90. /**
  91. * Close connection
  92. *
  93. * @param boolean $destructor
  94. * @return void
  95. */
  96. public function close($destructor = false)
  97. {
  98. // Gracefully disconnect
  99. if (!$destructor) {
  100. $frame = $this->createFrame();
  101. $frame->command('DISCONNECT');
  102. $this->write($frame->toFrame());
  103. }
  104. if (is_resource($this->_socket)) {
  105. fclose($this->_socket);
  106. }
  107. $this->_socket = null;
  108. }
  109. /**
  110. * Check whether we are connected to the server
  111. *
  112. * @return true
  113. * @throws Zend_Queue_Exception
  114. */
  115. public function ping()
  116. {
  117. if (!is_resource($this->_socket)) {
  118. require_once 'Zend/Queue/Exception.php';
  119. throw new Zend_Queue_Exception('Not connected to Stomp server');
  120. }
  121. return true;
  122. }
  123. /**
  124. * Write a frame to the stomp server
  125. * $response = $client->write($frame)->read();
  126. *
  127. * @param Zend_Queue_Stom_FrameInterface $frame
  128. * @return $this
  129. */
  130. public function write(Zend_Queue_Stomp_FrameInterface $frame)
  131. {
  132. $this->ping();
  133. $output = $frame->toFrame();
  134. $bytes = fwrite($this->_socket, $output, strlen($output));
  135. if ($bytes === false || $bytes == 0) {
  136. require_once 'Zend/Queue/Exception.php';
  137. throw new Zend_Queue_Exception('No bytes written');
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Tests the socket to see if there is data for us
  143. *
  144. * @return boolean
  145. */
  146. public function canRead()
  147. {
  148. $read = array($this->_socket);
  149. $write = null;
  150. $except = null;
  151. return stream_select(
  152. $read,
  153. $write,
  154. $except,
  155. $this->_options['timeout_sec'],
  156. $this->_options['timeout_usec']
  157. ) == 1;
  158. // see http://us.php.net/manual/en/function.stream-select.php
  159. }
  160. /**
  161. * Reads in a frame from the socket or returns false.
  162. *
  163. * @return Zend_Queue_Stomp_FrameInterface|false
  164. * @throws Zend_Queue_Exception
  165. */
  166. public function read()
  167. {
  168. $this->ping();
  169. $response = '';
  170. $prev = '';
  171. // while not end of file.
  172. while (!feof($this->_socket)) {
  173. // read in one character until "\0\n" is found
  174. $data = fread($this->_socket, 1);
  175. // check to make sure that the connection is not lost.
  176. if ($data === false) {
  177. require_once 'Zend/Queue/Exception.php';
  178. throw new Zend_Queue_Exception('Connection lost');
  179. }
  180. // append last character read to $response
  181. $response .= $data;
  182. // is this \0 (prev) \n (data)? END_OF_FRAME
  183. if (ord($data) == 10 && ord($prev) == 0) {
  184. break;
  185. }
  186. $prev = $data;
  187. }
  188. if ($response === '') {
  189. return false;
  190. }
  191. $frame = $this->createFrame();
  192. $frame->fromFrame($response);
  193. return $frame;
  194. }
  195. /**
  196. * Set the frameClass to be used
  197. *
  198. * This must be a Zend_Queue_Stomp_FrameInterface.
  199. *
  200. * @param string $classname - class is an instance of Zend_Queue_Stomp_FrameInterface
  201. * @return $this;
  202. */
  203. public function setFrameClass($classname)
  204. {
  205. $this->_options['frameClass'] = $classname;
  206. return $this;
  207. }
  208. /**
  209. * Get the frameClass
  210. *
  211. * @return string
  212. */
  213. public function getFrameClass()
  214. {
  215. return isset($this->_options['frameClass'])
  216. ? $this->_options['frameClass']
  217. : 'Zend_Queue_Stomp_Frame';
  218. }
  219. /**
  220. * Create an empty frame
  221. *
  222. * @return Zend_Queue_Stomp_FrameInterface
  223. */
  224. public function createFrame()
  225. {
  226. $class = $this->getFrameClass();
  227. if (!class_exists($class)) {
  228. require_once 'Zend/Loader.php';
  229. Zend_Loader::loadClass($class);
  230. }
  231. $frame = new $class();
  232. if (!$frame instanceof Zend_Queue_Stomp_FrameInterface) {
  233. require_once 'Zend/Queue/Exception.php';
  234. throw new Zend_Queue_Exception('Invalid Frame class provided; must implement Zend_Queue_Stomp_FrameInterface');
  235. }
  236. return $frame;
  237. }
  238. }