Protocol.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_TimeSync
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Abstract class definition for all timeserver protocols
  23. *
  24. * @category Zend
  25. * @package Zend_TimeSync
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_TimeSync_Protocol
  30. {
  31. /**
  32. * Holds the current socket connection
  33. *
  34. * @var array
  35. */
  36. protected $_socket;
  37. /**
  38. * Exceptions that might have occured
  39. *
  40. * @var array
  41. */
  42. protected $_exceptions;
  43. /**
  44. * Hostname for timeserver
  45. *
  46. * @var string
  47. */
  48. protected $_timeserver;
  49. /**
  50. * Holds information passed/returned from timeserver
  51. *
  52. * @var array
  53. */
  54. protected $_info = array();
  55. /**
  56. * Abstract method that prepares the data to send to the timeserver
  57. *
  58. * @return mixed
  59. */
  60. abstract protected function _prepare();
  61. /**
  62. * Abstract method that reads the data returned from the timeserver
  63. *
  64. * @return mixed
  65. */
  66. abstract protected function _read();
  67. /**
  68. * Abstract method that writes data to to the timeserver
  69. *
  70. * @param string $data Data to write
  71. * @return void
  72. */
  73. abstract protected function _write($data);
  74. /**
  75. * Abstract method that extracts the binary data returned from the timeserver
  76. *
  77. * @param string|array $data Data returned from the timeserver
  78. * @return integer
  79. */
  80. abstract protected function _extract($data);
  81. /**
  82. * Connect to the specified timeserver.
  83. *
  84. * @return void
  85. * @throws Zend_TimeSync_Exception When the connection failed
  86. */
  87. protected function _connect()
  88. {
  89. $socket = @fsockopen($this->_timeserver, $this->_port, $errno, $errstr,
  90. Zend_TimeSync::$options['timeout']);
  91. if ($socket === false) {
  92. throw new Zend_TimeSync_Exception('could not connect to ' .
  93. "'$this->_timeserver' on port '$this->_port', reason: '$errstr'");
  94. }
  95. $this->_socket = $socket;
  96. }
  97. /**
  98. * Disconnects from the peer, closes the socket.
  99. *
  100. * @return void
  101. */
  102. protected function _disconnect()
  103. {
  104. @fclose($this->_socket);
  105. $this->_socket = null;
  106. }
  107. /**
  108. * Return information sent/returned from the timeserver
  109. *
  110. * @return array
  111. */
  112. public function getInfo()
  113. {
  114. if (empty($this->_info) === true) {
  115. $this->_write($this->_prepare());
  116. $timestamp = $this->_extract($this->_read());
  117. }
  118. return $this->_info;
  119. }
  120. /**
  121. * Query this timeserver without using the fallback mechanism
  122. *
  123. * @param string|Zend_Locale $locale (Optional) Locale
  124. * @return Zend_Date
  125. */
  126. public function getDate($locale = null)
  127. {
  128. $this->_write($this->_prepare());
  129. $timestamp = $this->_extract($this->_read());
  130. $date = new Zend_Date($this, null, $locale);
  131. return $date;
  132. }
  133. }