TimeUDPClient.java

  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.  *      https://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.time;

  18. import java.io.IOException;
  19. import java.net.DatagramPacket;
  20. import java.net.InetAddress;
  21. import java.util.Date;

  22. import org.apache.commons.net.DatagramSocketClient;

  23. /**
  24.  * The TimeUDPClient class is a UDP implementation of a client for the Time protocol described in RFC 868. To use the class, merely open a local datagram socket
  25.  * with {@link org.apache.commons.net.DatagramSocketClient#open open} and call {@link #getTime getTime} or {@link #getTime getDate} to retrieve the time.
  26.  * Then call {@link org.apache.commons.net.DatagramSocketClient#close close} to close the connection properly. Unlike
  27.  * {@link org.apache.commons.net.time.TimeTCPClient}, successive calls to {@link #getTime getTime} or {@link #getDate getDate} are permitted without
  28.  * re-establishing a connection. That is because UDP is a connectionless protocol and the Time protocol is stateless.
  29.  *
  30.  *
  31.  * @see TimeTCPClient
  32.  */
  33. public final class TimeUDPClient extends DatagramSocketClient {

  34.     /** The default time port. It is set to 37 according to RFC 868. */
  35.     public static final int DEFAULT_PORT = 37;

  36.     /**
  37.      * The number of seconds between 00:00 1 January 1900 and 00:00 1 January 1970. This value can be useful for converting time values to other formats.
  38.      */
  39.     public static final long SECONDS_1900_TO_1970 = 2208988800L;

  40.     static long toTime(final byte[] timeData) {
  41.         long time = 0L;
  42.         time |= (timeData[0] & 0xff) << 24 & 0xffffffffL;
  43.         time |= (timeData[1] & 0xff) << 16 & 0xffffffffL;
  44.         time |= (timeData[2] & 0xff) << 8 & 0xffffffffL;
  45.         time |= timeData[3] & 0xff & 0xffffffffL;
  46.         return time;
  47.     }

  48.     /**
  49.      * Constructs a new instance.
  50.      */
  51.     public TimeUDPClient() {
  52.         // empty
  53.     }

  54.     /**
  55.      * Gets the time from a server and returns a Java Date containing the time converted to the local time zone.
  56.      *
  57.      * @param host the time-server
  58.      * @return the date
  59.      * @throws IOException on error
  60.      */
  61.     public Date getDate(final InetAddress host) throws IOException {
  62.         return new Date((getTime(host, DEFAULT_PORT) - SECONDS_1900_TO_1970) * 1000L);
  63.     }

  64.     /**
  65.      * Gets the time from a server and returns a Java Date containing the time converted to the local time zone.
  66.      *
  67.      * @param host The address of the server.
  68.      * @param port The port of the service.
  69.      * @return A Date value containing the time retrieved from the server converted to the local time zone.
  70.      * @throws IOException If an error occurs while fetching the time.
  71.      */
  72.     public Date getDate(final InetAddress host, final int port) throws IOException {
  73.         return new Date((getTime(host, port) - SECONDS_1900_TO_1970) * 1000L);
  74.     }

  75.     /**
  76.      * Gets the time from the specified server and default port.
  77.      *
  78.      * @param host the time-server
  79.      * @return the time returned from the server
  80.      * @throws IOException on error
  81.      */
  82.     public long getTime(final InetAddress host) throws IOException {
  83.         return getTime(host, DEFAULT_PORT);
  84.     }

  85.     /**
  86.      * Gets the time from the specified server and port. The time is the number of seconds since 00:00 (midnight) 1 January 1900 GMT, as
  87.      * specified by RFC 868. This method reads the raw 32-bit big-endian unsigned integer from the server, converts it to a Java long, and returns the value.
  88.      *
  89.      * @param host The address of the server.
  90.      * @param port The port of the service.
  91.      * @return The time value retrieved from the server.
  92.      * @throws IOException If an error occurs while retrieving the time.
  93.      */
  94.     public long getTime(final InetAddress host, final int port) throws IOException {
  95.         final byte[] dummyData = new byte[1];
  96.         final byte[] timeData = new byte[4];

  97.         final DatagramPacket sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port);
  98.         final DatagramPacket receivePacket = new DatagramPacket(timeData, timeData.length);

  99.         checkOpen().send(sendPacket);
  100.         checkOpen().receive(receivePacket);

  101.         return toTime(timeData);
  102.     }

  103. }