2
0

Zend_TimeSync-Working.xml 12 KB

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