PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Queue/Stomp/Client/Connection.php

https://bitbucket.org/fabiancarlos/feature_seguimentos
PHP | 280 lines | 122 code | 36 blank | 122 comment | 19 complexity | b5534f26794c2677f32af6e08266c220 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Connection.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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->setCommand('DISCONNECT');
  102. $this->write($frame);
  103. }
  104. // @todo: Should be fixed.
  105. // When the socket is "closed", it will trigger the below error when php exits
  106. // Fatal error: Exception thrown without a stack frame in Unknown on line 0
  107. // Danlo: I suspect this is because this has already been claimed by the interpeter
  108. // thus trying to shutdown this resources, which is already shutdown is a problem.
  109. if (is_resource($this->_socket)) {
  110. // fclose($this->_socket);
  111. }
  112. // $this->_socket = null;
  113. }
  114. /**
  115. * Check whether we are connected to the server
  116. *
  117. * @return true
  118. * @throws Zend_Queue_Exception
  119. */
  120. public function ping()
  121. {
  122. if (!is_resource($this->_socket)) {
  123. require_once 'Zend/Queue/Exception.php';
  124. throw new Zend_Queue_Exception('Not connected to Stomp server');
  125. }
  126. return true;
  127. }
  128. /**
  129. * Write a frame to the stomp server
  130. *
  131. * example: $response = $client->write($frame)->read();
  132. *
  133. * @param Zend_Queue_Stom_FrameInterface $frame
  134. * @return $this
  135. */
  136. public function write(Zend_Queue_Stomp_FrameInterface $frame)
  137. {
  138. $this->ping();
  139. $output = $frame->toFrame();
  140. $bytes = fwrite($this->_socket, $output, strlen($output));
  141. if ($bytes === false || $bytes == 0) {
  142. require_once 'Zend/Queue/Exception.php';
  143. throw new Zend_Queue_Exception('No bytes written');
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Tests the socket to see if there is data for us
  149. *
  150. * @return boolean
  151. */
  152. public function canRead()
  153. {
  154. $read = array($this->_socket);
  155. $write = null;
  156. $except = null;
  157. return stream_select(
  158. $read,
  159. $write,
  160. $except,
  161. $this->_options['timeout_sec'],
  162. $this->_options['timeout_usec']
  163. ) == 1;
  164. // see http://us.php.net/manual/en/function.stream-select.php
  165. }
  166. /**
  167. * Reads in a frame from the socket or returns false.
  168. *
  169. * @return Zend_Queue_Stomp_FrameInterface|false
  170. * @throws Zend_Queue_Exception
  171. */
  172. public function read()
  173. {
  174. $this->ping();
  175. $response = '';
  176. $prev = '';
  177. // while not end of file.
  178. while (!feof($this->_socket)) {
  179. // read in one character until "\0\n" is found
  180. $data = fread($this->_socket, 1);
  181. // check to make sure that the connection is not lost.
  182. if ($data === false) {
  183. require_once 'Zend/Queue/Exception.php';
  184. throw new Zend_Queue_Exception('Connection lost');
  185. }
  186. // append last character read to $response
  187. $response .= $data;
  188. // is this \0 (prev) \n (data)? END_OF_FRAME
  189. if (ord($data) == 10 && ord($prev) == 0) {
  190. break;
  191. }
  192. $prev = $data;
  193. }
  194. if ($response === '') {
  195. return false;
  196. }
  197. $frame = $this->createFrame();
  198. $frame->fromFrame($response);
  199. return $frame;
  200. }
  201. /**
  202. * Set the frameClass to be used
  203. *
  204. * This must be a Zend_Queue_Stomp_FrameInterface.
  205. *
  206. * @param string $classname - class is an instance of Zend_Queue_Stomp_FrameInterface
  207. * @return $this;
  208. */
  209. public function setFrameClass($classname)
  210. {
  211. $this->_options['frameClass'] = $classname;
  212. return $this;
  213. }
  214. /**
  215. * Get the frameClass
  216. *
  217. * @return string
  218. */
  219. public function getFrameClass()
  220. {
  221. return isset($this->_options['frameClass'])
  222. ? $this->_options['frameClass']
  223. : 'Zend_Queue_Stomp_Frame';
  224. }
  225. /**
  226. * Create an empty frame
  227. *
  228. * @return Zend_Queue_Stomp_FrameInterface
  229. */
  230. public function createFrame()
  231. {
  232. $class = $this->getFrameClass();
  233. if (!class_exists($class)) {
  234. require_once 'Zend/Loader.php';
  235. Zend_Loader::loadClass($class);
  236. }
  237. $frame = new $class();
  238. if (!$frame instanceof Zend_Queue_Stomp_FrameInterface) {
  239. require_once 'Zend/Queue/Exception.php';
  240. throw new Zend_Queue_Exception('Invalid Frame class provided; must implement Zend_Queue_Stomp_FrameInterface');
  241. }
  242. return $frame;
  243. }
  244. }