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