View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  package org.apache.commons.net;
18  
19  import java.io.IOException;
20  import java.net.DatagramPacket;
21  import java.net.InetAddress;
22  import java.util.Date;
23  
24  /***
25   * The TimeUDPClient class is a UDP implementation of a client for the
26   * Time protocol described in RFC 868.  To use the class, merely
27   * open a local datagram socket with
28   * {@link org.apache.commons.net.DatagramSocketClient#open  open }
29   * and call {@link #getTime  getTime } or
30   * {@link #getTime  getDate } to retrieve the time. Then call
31   * {@link org.apache.commons.net.DatagramSocketClient#close  close }
32   * to close the connection properly.  Unlike
33   * {@link org.apache.commons.net.TimeTCPClient},
34   * successive calls to {@link #getTime  getTime } or
35   * {@link #getDate  getDate } are permitted
36   * without re-establishing a connection.  That is because UDP is a
37   * connectionless protocol and the Time protocol is stateless.
38   * <p>
39   * <p>
40   * @author Daniel F. Savarese
41   * @see TimeTCPClient
42   ***/
43  
44  public final class TimeUDPClient extends DatagramSocketClient
45  {
46      /*** The default time port.  It is set to 37 according to RFC 868. ***/
47      public static final int DEFAULT_PORT = 37;
48  
49      /***
50       * The number of seconds between 00:00 1 January 1900 and
51       * 00:00 1 January 1970.  This value can be useful for converting
52       * time values to other formats.
53       ***/
54      public static final long SECONDS_1900_TO_1970 = 2208988800L;
55  
56      private byte[] __dummyData = new byte[1];
57      private byte[] __timeData = new byte[4];
58  
59      /***
60       * Retrieves the time from the specified server and port and
61       * returns it. The time is the number of seconds since
62       * 00:00 (midnight) 1 January 1900 GMT, as specified by RFC 868.
63       * This method reads the raw 32-bit big-endian
64       * unsigned integer from the server, converts it to a Java long, and
65       * returns the value.
66       * <p>
67       * @param host The address of the server.
68       * @param port The port of the service.
69       * @return The time value retrieved from the server.
70       * @exception IOException If an error occurs while retrieving the time.
71       ***/
72      public long getTime(InetAddress host, int port) throws IOException
73      {
74          long time;
75          DatagramPacket sendPacket, receivePacket;
76  
77          sendPacket =
78              new DatagramPacket(__dummyData, __dummyData.length, host, port);
79          receivePacket = new DatagramPacket(__timeData, __timeData.length);
80  
81          _socket_.send(sendPacket);
82          _socket_.receive(receivePacket);
83  
84          time = 0L;
85          time |= (((__timeData[0] & 0xff) << 24) & 0xffffffffL);
86          time |= (((__timeData[1] & 0xff) << 16) & 0xffffffffL);
87          time |= (((__timeData[2] & 0xff) << 8) & 0xffffffffL);
88          time |= ((__timeData[3] & 0xff) & 0xffffffffL);
89  
90          return time;
91      }
92  
93      /*** Same as <code> getTime(host, DEFAULT_PORT); </code> ***/
94      public long getTime(InetAddress host) throws IOException
95      {
96          return getTime(host, DEFAULT_PORT);
97      }
98  
99  
100     /***
101      * Retrieves the time from the server and returns a Java Date
102      * containing the time converted to the local timezone.
103      * <p>
104      * @param host The address of the server.
105      * @param port The port of the service.
106      * @return A Date value containing the time retrieved from the server
107      *     converted to the local timezone.
108      * @exception IOException  If an error occurs while fetching the time.
109      ***/
110     public Date getDate(InetAddress host, int port) throws IOException
111     {
112         return new Date((getTime(host, port) - SECONDS_1900_TO_1970)*1000L);
113     }
114 
115 
116     /*** Same as <code> getTime(host, DEFAULT_PORT); </code> ***/
117     public Date getDate(InetAddress host) throws IOException
118     {
119         return new Date((getTime(host, DEFAULT_PORT) -
120                          SECONDS_1900_TO_1970)*1000L);
121     }
122 
123 }
124