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