001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.net.time; 019 020import java.io.IOException; 021import java.net.DatagramPacket; 022import java.net.InetAddress; 023import java.util.Date; 024 025import org.apache.commons.net.DatagramSocketClient; 026 027/** 028 * 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 029 * with {@link org.apache.commons.net.DatagramSocketClient#open open} and call {@link #getTime getTime} or {@link #getTime getDate} to retrieve the time. 030 * Then call {@link org.apache.commons.net.DatagramSocketClient#close close} to close the connection properly. Unlike 031 * {@link org.apache.commons.net.time.TimeTCPClient}, successive calls to {@link #getTime getTime} or {@link #getDate getDate} are permitted without 032 * re-establishing a connection. That is because UDP is a connectionless protocol and the Time protocol is stateless. 033 * 034 * 035 * @see TimeTCPClient 036 */ 037public final class TimeUDPClient extends DatagramSocketClient { 038 039 /** The default time port. It is set to 37 according to RFC 868. */ 040 public static final int DEFAULT_PORT = 37; 041 042 /** 043 * 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. 044 */ 045 public static final long SECONDS_1900_TO_1970 = 2208988800L; 046 047 static long toTime(final byte[] timeData) { 048 long time = 0L; 049 time |= (timeData[0] & 0xff) << 24 & 0xffffffffL; 050 time |= (timeData[1] & 0xff) << 16 & 0xffffffffL; 051 time |= (timeData[2] & 0xff) << 8 & 0xffffffffL; 052 time |= timeData[3] & 0xff & 0xffffffffL; 053 return time; 054 } 055 056 /** 057 * Constructs a new instance. 058 */ 059 public TimeUDPClient() { 060 // empty 061 } 062 063 /** 064 * Gets the time from a server and returns a Java Date containing the time converted to the local time zone. 065 * 066 * @param host the time-server 067 * @return the date 068 * @throws IOException on error 069 */ 070 public Date getDate(final InetAddress host) throws IOException { 071 return new Date((getTime(host, DEFAULT_PORT) - SECONDS_1900_TO_1970) * 1000L); 072 } 073 074 /** 075 * Gets the time from a server and returns a Java Date containing the time converted to the local time zone. 076 * 077 * @param host The address of the server. 078 * @param port The port of the service. 079 * @return A Date value containing the time retrieved from the server converted to the local time zone. 080 * @throws IOException If an error occurs while fetching the time. 081 */ 082 public Date getDate(final InetAddress host, final int port) throws IOException { 083 return new Date((getTime(host, port) - SECONDS_1900_TO_1970) * 1000L); 084 } 085 086 /** 087 * Gets the time from the specified server and default port. 088 * 089 * @param host the time-server 090 * @return the time returned from the server 091 * @throws IOException on error 092 */ 093 public long getTime(final InetAddress host) throws IOException { 094 return getTime(host, DEFAULT_PORT); 095 } 096 097 /** 098 * 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 099 * 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. 100 * 101 * @param host The address of the server. 102 * @param port The port of the service. 103 * @return The time value retrieved from the server. 104 * @throws IOException If an error occurs while retrieving the time. 105 */ 106 public long getTime(final InetAddress host, final int port) throws IOException { 107 final byte[] dummyData = new byte[1]; 108 final byte[] timeData = new byte[4]; 109 110 final DatagramPacket sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port); 111 final DatagramPacket receivePacket = new DatagramPacket(timeData, timeData.length); 112 113 checkOpen().send(sendPacket); 114 checkOpen().receive(receivePacket); 115 116 return toTime(timeData); 117 } 118 119}