Connection.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 Adapter
  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 Adapter
  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. *
  126. * @example $response = $client->write($frame)->read();
  127. *
  128. * @param Zend_Queue_Stom_FrameInterface $frame
  129. * @return $this
  130. */
  131. public function write(Zend_Queue_Stomp_FrameInterface $frame)
  132. {
  133. $this->ping();
  134. $output = $frame->toFrame();
  135. $bytes = fwrite($this->_socket, $output, strlen($output));
  136. if ($bytes === false || $bytes == 0) {
  137. require_once 'Zend/Queue/Exception.php';
  138. throw new Zend_Queue_Exception('No bytes written');
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Tests the socket to see if there is data for us
  144. *
  145. * @return boolean
  146. */
  147. public function canRead()
  148. {
  149. $read = array($this->_socket);
  150. $write = null;
  151. $except = null;
  152. return stream_select(
  153. $read,
  154. $write,
  155. $except,
  156. $this->_options['timeout_sec'],
  157. $this->_options['timeout_usec']
  158. ) == 1;
  159. // see http://us.php.net/manual/en/function.stream-select.php
  160. }
  161. /**
  162. * Reads in a frame from the socket or returns false.
  163. *
  164. * @return Zend_Queue_Stomp_FrameInterface|false
  165. * @throws Zend_Queue_Exception
  166. */
  167. public function read()
  168. {
  169. $this->ping();
  170. $response = '';
  171. $prev = '';
  172. // while not end of file.
  173. while (!feof($this->_socket)) {
  174. // read in one character until "\0\n" is found
  175. $data = fread($this->_socket, 1);
  176. // check to make sure that the connection is not lost.
  177. if ($data === false) {
  178. require_once 'Zend/Queue/Exception.php';
  179. throw new Zend_Queue_Exception('Connection lost');
  180. }
  181. // append last character read to $response
  182. $response .= $data;
  183. // is this \0 (prev) \n (data)? END_OF_FRAME
  184. if (ord($data) == 10 && ord($prev) == 0) {
  185. break;
  186. }
  187. $prev = $data;
  188. }
  189. if ($response === '') {
  190. return false;
  191. }
  192. $frame = $this->createFrame();
  193. $frame->fromFrame($response);
  194. return $frame;
  195. }
  196. /**
  197. * Set the frameClass to be used
  198. *
  199. * This must be a Zend_Queue_Stomp_FrameInterface.
  200. *
  201. * @param string $classname - class is an instance of Zend_Queue_Stomp_FrameInterface
  202. * @return $this;
  203. */
  204. public function setFrameClass($classname)
  205. {
  206. $this->_options['frameClass'] = $classname;
  207. return $this;
  208. }
  209. /**
  210. * Get the frameClass
  211. *
  212. * @return string
  213. */
  214. public function getFrameClass()
  215. {
  216. return isset($this->_options['frameClass'])
  217. ? $this->_options['frameClass']
  218. : 'Zend_Queue_Stomp_Frame';
  219. }
  220. /**
  221. * Create an empty frame
  222. *
  223. * @return Zend_Queue_Stomp_FrameInterface
  224. */
  225. public function createFrame()
  226. {
  227. $class = $this->getFrameClass();
  228. if (!class_exists($class)) {
  229. require_once 'Zend/Loader.php';
  230. Zend_Loader::loadClass($class);
  231. }
  232. $frame = new $class();
  233. if (!$frame instanceof Zend_Queue_Stomp_FrameInterface) {
  234. require_once 'Zend/Queue/Exception.php';
  235. throw new Zend_Queue_Exception('Invalid Frame class provided; must implement Zend_Queue_Stomp_FrameInterface');
  236. }
  237. return $frame;
  238. }
  239. }