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