View Javadoc

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