Zend_TimeSync-Working.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.timesync.working">
  4. <title>Working with Zend_TimeSync</title>
  5. <para>
  6. <classname>Zend_TimeSync</classname> can return the actual time from any given
  7. <emphasis>NTP</emphasis> or <emphasis>SNTP</emphasis> time server.
  8. It can automatically handle multiple servers and provides a simple interface.
  9. </para>
  10. <note>
  11. <para>
  12. All examples in this chapter use a public, generic time server: <emphasis>0.europe.pool.ntp.org</emphasis>. You should use a public, generic time server which is close to your application server.
  13. See <ulink url="http://www.pool.ntp.org">http://www.pool.ntp.org</ulink> for
  14. information.
  15. </para>
  16. </note>
  17. <sect2 id="zend.timesync.working.generic">
  18. <title>Generic Time Server Request</title>
  19. <para>
  20. Requesting the time from a time server is simple. First, you provide the time server
  21. from which you want to request the time.
  22. </para>
  23. <programlisting language="php"><![CDATA[
  24. $server = new Zend_TimeSync('0.pool.ntp.org');
  25. print $server->getDate()->getIso();
  26. ]]></programlisting>
  27. <para>
  28. So what is happening in the background of <classname>Zend_TimeSync</classname>? First
  29. the syntax of the time server is checked. In our example,
  30. '<emphasis>0.pool.ntp.org</emphasis>' is checked and recognised as a possible address
  31. for a time server. Then when calling <methodname>getDate()</methodname> the actual set
  32. time server is requested and it will return its own time.
  33. <classname>Zend_TimeSync</classname> then calculates the difference to the actual time
  34. of the server running the script and returns a <classname>Zend_Date</classname> object
  35. with the correct time.
  36. </para>
  37. <para>
  38. For details about <classname>Zend_Date</classname> and its methods see the
  39. <link linkend="zend.date.introduction"><classname>Zend_Date</classname> documentation</link>.
  40. </para>
  41. </sect2>
  42. <sect2 id="zend.timesync.working.multiple">
  43. <title>Multiple Time Servers</title>
  44. <para>
  45. Not all time servers are always available to return their time. Servers may be unavailable during maintenance, for example.
  46. When the time cannot be requested from the time server, you will get an exception.
  47. </para>
  48. <para>
  49. <classname>Zend_TimeSync</classname> is a simple solution that can handle multiple time servers and supports an
  50. automatic fallback mechanism. There are two supported ways; you can either specify an array
  51. of time servers when creating the instance, or you can add additional time servers to the instance
  52. using the <methodname>addServer()</methodname> method.
  53. </para>
  54. <programlisting language="php"><![CDATA[
  55. $server = new Zend_TimeSync(array('0.pool.ntp.org',
  56. '1.pool.ntp.org',
  57. '2.pool.ntp.org'));
  58. $server->addServer('3.pool.ntp.org');
  59. print $server->getDate()->getIso();
  60. ]]></programlisting>
  61. <para>
  62. There is no limit to the number of time servers you can add. When a time server can not
  63. be reached, <classname>Zend_TimeSync</classname> will fallback and try to connect to the next time server.
  64. </para>
  65. <para>
  66. When you supply more than one time server- which is considered a best practice for <classname>Zend_TimeSync</classname>- you should name
  67. each server. You can name your servers with array keys, with the second
  68. parameter at instantiation, or with the second parameter when adding another time server.
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. $server = new Zend_TimeSync(array('generic' => '0.pool.ntp.org',
  72. 'fallback' => '1.pool.ntp.org',
  73. 'reserve' => '2.pool.ntp.org'));
  74. $server->addServer('3.pool.ntp.org', 'additional');
  75. print $server->getDate()->getIso();
  76. ]]></programlisting>
  77. <para>
  78. Naming the time servers allows you to request a specific time server as we will see
  79. later in this chapter.
  80. </para>
  81. </sect2>
  82. <sect2 id="zend.timesync.working.protocol">
  83. <title>Protocols of Time Servers</title>
  84. <para>
  85. There are different types of time servers. Most public time servers use the
  86. <emphasis>NTP</emphasis> protocol. But there are other time synchronization
  87. protocols available.
  88. </para>
  89. <para>
  90. You set the proper protocol in the address of the time server. There are two
  91. protocols which are supported by <classname>Zend_TimeSync</classname>: <emphasis>NTP</emphasis> and <emphasis>SNTP</emphasis>. The default protocol is
  92. <emphasis>NTP</emphasis>. If you are using <emphasis>NTP</emphasis>, you can omit the protocol
  93. in the address as demonstrated in the previous examples.
  94. </para>
  95. <programlisting language="php"><![CDATA[
  96. $server = new Zend_TimeSync(array('generic' => 'ntp:\\0.pool.ntp.org',
  97. 'fallback' => 'ntp:\\1.pool.ntp.org',
  98. 'reserve' => 'ntp:\\2.pool.ntp.org'));
  99. $server->addServer('sntp:\\internal.myserver.com', 'additional');
  100. print $server->getDate()->getIso();
  101. ]]></programlisting>
  102. <para>
  103. <classname>Zend_TimeSync</classname> can handle mixed time servers. So you are not restricted to
  104. only one protocol; you can add any server independently from its protocol.
  105. </para>
  106. </sect2>
  107. <sect2 id="zend.timesync.working.ports">
  108. <title>Using Ports for Time Servers</title>
  109. <para>
  110. As with every protocol within the world wide web, the <emphasis>NTP</emphasis> and
  111. <emphasis>SNTP</emphasis> protocols use standard ports. NTP uses port
  112. <emphasis>123</emphasis> and SNTP uses port <emphasis>37</emphasis>.
  113. </para>
  114. <para>
  115. But sometimes the port that the protocols use differs from the standard one. You can define the port which
  116. has to be used for each server within the address. Just add the number of the port after the
  117. address. If no port is defined, then <classname>Zend_TimeSync</classname> will use the standard port.
  118. </para>
  119. <programlisting language="php"><![CDATA[
  120. $server = new Zend_TimeSync(array('generic' => 'ntp:\\0.pool.ntp.org:200',
  121. 'fallback' => 'ntp:\\1.pool.ntp.org'));
  122. $server->addServer('sntp:\\internal.myserver.com:399', 'additional');
  123. print $server->getDate()->getIso();
  124. ]]></programlisting>
  125. </sect2>
  126. <sect2 id="zend.timesync.working.options">
  127. <title>Time Servers Options</title>
  128. <para>
  129. There is only one option within <classname>Zend_TimeSync</classname> which will be used internally: <emphasis>timeout</emphasis>.
  130. You can set any self-defined option you are in need of and request it, however.
  131. </para>
  132. <para>
  133. The option <emphasis>timeout</emphasis> defines the number of seconds after which
  134. a connection is detected as broken when there was no response. The default value is
  135. <emphasis>1</emphasis>, which means that <classname>Zend_TimeSync</classname> will
  136. fallback to the next time server if the requested time server does not respond in one second.
  137. </para>
  138. <para>
  139. With the <methodname>setOptions()</methodname> method, you can set any option. This function accepts an array where the
  140. key is the option to set and the value is the value of that option. Any previously set option will
  141. be overwritten by the new value. If you want to know which options are set, use the
  142. <methodname>getOptions()</methodname> method. It accepts either a key which returns the given option if specified,
  143. or, if no key is set, it will return all set options.
  144. </para>
  145. <programlisting language="php"><![CDATA[
  146. Zend_TimeSync::setOptions(array('timeout' => 3, 'myoption' => 'timesync'));
  147. $server = new Zend_TimeSync(array('generic' => 'ntp:\\0.pool.ntp.org',
  148. 'fallback' => 'ntp:\\1.pool.ntp.org'));
  149. $server->addServer('sntp:\\internal.myserver.com', 'additional');
  150. print $server->getDate()->getIso();
  151. print_r(Zend_TimeSync::getOptions();
  152. print "Timeout = " . Zend_TimeSync::getOptions('timeout');
  153. ]]></programlisting>
  154. <para>
  155. As you can see, the options for <classname>Zend_TimeSync</classname> are static. Each
  156. instance of <classname>Zend_TimeSync</classname> will use the same options.
  157. </para>
  158. </sect2>
  159. <sect2 id="zend.timesync.working.different">
  160. <title>Using Different Time Servers</title>
  161. <para>
  162. <classname>Zend_TimeSync</classname>'s default behavior for requesting a time is to request it from the first given server.
  163. But sometimes it is useful to set a different time server from which to request the time.
  164. This can be done with the <methodname>setServer()</methodname> method. To define the used time server
  165. set the alias as a parameter within the method. To get the actual used time server
  166. call the <methodname>getServer()</methodname> method. It accepts an alias as a parameter which
  167. defines the time server to be returned. If no parameter is given, the current time server will
  168. be returned.
  169. </para>
  170. <programlisting language="php"><![CDATA[
  171. $server = new Zend_TimeSync(array('generic' => 'ntp:\\0.pool.ntp.org',
  172. 'fallback' => 'ntp:\\1.pool.ntp.org'));
  173. $server->addServer('sntp:\\internal.myserver.com', 'additional');
  174. $actual = $server->getServer();
  175. $server = $server->setServer('additional');
  176. ]]></programlisting>
  177. </sect2>
  178. <sect2 id="zend.timesync.working.informations">
  179. <title>Information from Time Servers</title>
  180. <para>
  181. Time servers not only offer the time itself, but also additional information. You can
  182. get this information with the <methodname>getInfo()</methodname> method.
  183. </para>
  184. <programlisting language="php"><![CDATA[
  185. $server = new Zend_TimeSync(array('generic' => 'ntp:\\0.pool.ntp.org',
  186. 'fallback' => 'ntp:\\1.pool.ntp.org'));
  187. print_r ($server->getInfo());
  188. ]]></programlisting>
  189. <para>
  190. The returned information differs with the protocol used and can also differ with the
  191. server used.
  192. </para>
  193. </sect2>
  194. <sect2 id="zend.timesync.working.exceptions">
  195. <title>Handling Exceptions</title>
  196. <para>
  197. Exceptions are collected for all time servers and returned as an array. So you can
  198. iterate through all thrown exceptions as shown in the following example:
  199. </para>
  200. <programlisting language="php"><![CDATA[
  201. $serverlist = array(
  202. // invalid servers
  203. 'invalid_a' => 'ntp://a.foo.bar.org',
  204. 'invalid_b' => 'sntp://b.foo.bar.org',
  205. );
  206. $server = new Zend_TimeSync($serverlist);
  207. try {
  208. $result = $server->getDate();
  209. echo $result->getIso();
  210. } catch (Zend_TimeSync_Exception $e) {
  211. $exceptions = $e->get();
  212. foreach ($exceptions as $key => $myException) {
  213. echo $myException->getMessage();
  214. echo '<br />';
  215. }
  216. }
  217. ]]></programlisting>
  218. </sect2>
  219. </sect1>