View Javadoc

1   package examples.ntp;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.SocketException;
23  import java.net.UnknownHostException;
24  import java.text.NumberFormat;
25  
26  import org.apache.commons.net.ntp.NTPUDPClient;
27  import org.apache.commons.net.ntp.NtpUtils;
28  import org.apache.commons.net.ntp.NtpV3Packet;
29  import org.apache.commons.net.ntp.TimeInfo;
30  import org.apache.commons.net.ntp.TimeStamp;
31  
32  /***
33   * This is an example program demonstrating how to use the NTPUDPClient
34   * class. This program sends a Datagram client request packet to a
35   * Network time Protocol (NTP) service port on a specified server,
36   * retrieves the time, and prints it to standard output along with
37   * the fields from the NTP message header (e.g. stratum level, reference id,
38   * poll interval, root delay, mode, ...)
39   * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
40   * for details.
41   * <p>
42   * Usage: NTPClient <hostname-or-address-list>
43   * <br>
44   * Example: NTPClient clock.psu.edu
45   *
46   * @author Jason Mathews, MITRE Corp
47   ***/
48  public final class NTPClient
49  {
50  
51      private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
52  
53      /**
54       * Process <code>TimeInfo</code> object and print its details.
55       * @param info <code>TimeInfo</code> object.
56       */
57      public static void processResponse(TimeInfo info)
58      {
59          NtpV3Packet message = info.getMessage();
60          int stratum = message.getStratum();
61          String refType;
62          if (stratum <= 0) {
63              refType = "(Unspecified or Unavailable)";
64          } else if (stratum == 1) {
65              refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
66          } else {
67              refType = "(Secondary Reference; e.g. via NTP or SNTP)";
68          }
69          // stratum should be 0..15...
70          System.out.println(" Stratum: " + stratum + " " + refType);
71          int version = message.getVersion();
72          int li = message.getLeapIndicator();
73          System.out.println(" leap=" + li + ", version="
74                  + version + ", precision=" + message.getPrecision());
75  
76          System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
77          int poll = message.getPoll();
78          // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
79          System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll))
80                  + " seconds" + " (2 ** " + poll + ")");
81          double disp = message.getRootDispersionInMillisDouble();
82          System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble())
83                  + ", rootdispersion(ms): " + numberFormat.format(disp));
84  
85          int refId = message.getReferenceId();
86          String refAddr = NtpUtils.getHostAddress(refId);
87          String refName = null;
88          if (refId != 0) {
89              if (refAddr.equals("127.127.1.0")) {
90                  refName = "LOCAL"; // This is the ref address for the Local Clock
91              } else if (stratum >= 2) {
92                  // If reference id has 127.127 prefix then it uses its own reference clock
93                  // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
94                  // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
95                  if (!refAddr.startsWith("127.127")) {
96                      try {
97                          InetAddress addr = InetAddress.getByName(refAddr);
98                          String name = addr.getHostName();
99                          if (name != null && !name.equals(refAddr)) {
100                             refName = name;
101                         }
102                     } catch (UnknownHostException e) {
103                         // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
104                         // ref not valid host maybe it's a reference clock name?
105                         // otherwise just show the ref IP address.
106                         refName = NtpUtils.getReferenceClock(message);
107                     }
108                 }
109             } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
110                 refName = NtpUtils.getReferenceClock(message);
111                 // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
112             }
113             // otherwise give up on naming the beast...
114         }
115         if (refName != null && refName.length() > 1) {
116             refAddr += " (" + refName + ")";
117         }
118         System.out.println(" Reference Identifier:\t" + refAddr);
119 
120         TimeStamp refNtpTime = message.getReferenceTimeStamp();
121         System.out.println(" Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString());
122 
123         // Originate Time is time request sent by client (t1)
124         TimeStamp origNtpTime = message.getOriginateTimeStamp();
125         System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());
126 
127         long destTime = info.getReturnTime();
128         // Receive Time is time request received by server (t2)
129         TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
130         System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());
131 
132         // Transmit time is time reply sent by server (t3)
133         TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
134         System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());
135 
136         // Destination time is time reply received by client (t4)
137         TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
138         System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());
139 
140         info.computeDetails(); // compute offset/delay if not already done
141         Long offsetValue = info.getOffset();
142         Long delayValue = info.getDelay();
143         String delay = (delayValue == null) ? "N/A" : delayValue.toString();
144         String offset = (offsetValue == null) ? "N/A" : offsetValue.toString();
145 
146         System.out.println(" Roundtrip delay(ms)=" + delay
147                 + ", clock offset(ms)=" + offset); // offset in ms
148     }
149 
150     public static void main(String[] args)
151     {
152         if (args.length == 0) {
153             System.err.println("Usage: NTPClient <hostname-or-address-list>");
154             System.exit(1);
155         }
156 
157         NTPUDPClient client = new NTPUDPClient();
158         // We want to timeout if a response takes longer than 10 seconds
159         client.setDefaultTimeout(10000);
160         try {
161             client.open();
162             for (String arg : args)
163             {
164                 System.out.println();
165                 try {
166                     InetAddress hostAddr = InetAddress.getByName(arg);
167                     System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
168                     TimeInfo info = client.getTime(hostAddr);
169                     processResponse(info);
170                 } catch (IOException ioe) {
171                     ioe.printStackTrace();
172                 }
173             }
174         } catch (SocketException e) {
175             e.printStackTrace();
176         }
177 
178         client.close();
179     }
180 
181 }