View Javadoc

    1   package org.apache.commons.net.ntp;
    2   /*
    3    * Copyright 2001-2005 The Apache Software Foundation
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  import java.io.IOException;
    19  import java.net.DatagramPacket;
    20  import java.net.InetAddress;
    21  import org.apache.commons.net.DatagramSocketClient;
    22
    23  /****
    24   * The NTPUDPClient class is a UDP implementation of a client for the
    25   * Network Time Protocol (NTP) described in RFC 1305 as well as the
    26   * Simple Network Time Protocol (SNTP) in RFC-2030. To use the class,
    27   * merely open a local datagram socket with <a href="#open"> open </a>
    28   * and call <a href="#getTime"> getTime </a> to retrieve the time. Then call
    29   * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close </a>
    30   * to close the connection properly.
    31   * Successive calls to <a href="#getTime"> getTime </a> are permitted
    32   * without re-establishing a connection.  That is because UDP is a
    33   * connectionless protocol and the Network Time Protocol is stateless.
    34   *
    35   * @author Jason Mathews, MITRE Corp
    36   * @version $Revision: 165675 $ $Date: 2005-05-02 21:09:55 +0100 (Mon, 02 May 2005) $
    37   ***/
    38
    39  public final class NTPUDPClient extends DatagramSocketClient
    40  {
    41      /**** The default NTP port.  It is set to 123 according to RFC 1305. ***/
    42      public static final int DEFAULT_PORT = 123;
    43
    44      private int _version = NtpV3Packet.VERSION_3;
    45
    46      /****
    47       * Retrieves the time information from the specified server and port and
    48       * returns it. The time is the number of miliiseconds since
    49       * 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305.
    50       * This method reads the raw NTP packet and constructs a <i>TimeInfo</i>
    51       * object that allows access to all the fields of the NTP message header.
    52       * <p>
    53       * @param host The address of the server.
    54       * @param port The port of the service.
    55       * @return The time value retrieved from the server.
    56       * @exception IOException If an error occurs while retrieving the time.
    57       ***/
    58      public TimeInfo getTime(InetAddress host, int port) throws IOException
    59      {
    60          // if not connected then open to next available UDP port
    61          if (!isOpen())
    62          {
    63              open();
    64          }
    65
    66          NtpV3Packet message = new NtpV3Impl();
    67          message.setMode(NtpV3Packet.MODE_CLIENT);
    68          message.setVersion(_version);
    69          DatagramPacket sendPacket = message.getDatagramPacket();
    70          sendPacket.setAddress(host);
    71          sendPacket.setPort(port);
    72
    73          NtpV3Packet recMessage = new NtpV3Impl();
    74          DatagramPacket receivePacket = recMessage.getDatagramPacket();
    75
    76          /*
    77           * Must minimize the time between getting the current time,
    78           * timestamping the packet, and sending it out which
    79           * introduces an error in the delay time.
    80           * No extraneous logging and initializations here !!!
    81           */
    82          TimeStamp now = TimeStamp.getCurrentTime();
    83
    84          // Note that if you do not set the transmit time field then originating time
    85          // in server response is all 0's which is "Thu Feb 07 01:28:16 EST 2036".
    86          message.setTransmitTime(now);
    87
    88          _socket_.send(sendPacket);
    89          _socket_.receive(receivePacket);
    90
    91          long returnTime = System.currentTimeMillis();
    92          // create TimeInfo message container but don't pre-compute the details yet
    93          TimeInfo info = new TimeInfo(recMessage, returnTime, false);
    94
    95          return info;
    96      }
    97
    98      /****
    99       * Retrieves the time information from the specified server on the
    100      * default NTP port and returns it. The time is the number of miliiseconds
    101      * since 00:00 (midnight) 1 January 1900 UTC, as specified by RFC 1305.
    102      * This method reads the raw NTP packet and constructs a <i>TimeInfo</i>
    103      * object that allows access to all the fields of the NTP message header.
    104      * <p>
    105      * @param host The address of the server.
    106      * @return The time value retrieved from the server.
    107      * @exception IOException If an error occurs while retrieving the time.
    108      ***/
    109     public TimeInfo getTime(InetAddress host) throws IOException
    110     {
    111         return getTime(host, NtpV3Packet.NTP_PORT);
    112     }
    113
    114     /****
    115      * Returns the NTP protocol version number that client sets on request packet
    116      * that is sent to remote host (e.g. 3=NTP v3, 4=NTP v4, etc.)
    117      *
    118      * @return  the NTP protocol version number that client sets on request packet.
    119      * @see #setVersion(int)
    120      ***/
    121     public int getVersion()
    122     {
    123         return _version;
    124     }
    125
    126     /****
    127      * Sets the NTP protocol version number that client sets on request packet
    128      * communicate with remote host.
    129      *
    130      * @param version the NTP protocol version number
    131      ***/
    132     public void setVersion(int version)
    133     {
    134         _version = version;
    135     }
    136
    137 }