1 package examples.ntp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)";
60 else
61 refType = "(Secondary Reference; e.g. via NTP or SNTP)";
62
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
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";
84 } else if (stratum >= 2) {
85
86
87
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
96
97
98 refName = NtpUtils.getReferenceClock(message);
99 }
100 }
101 } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
102 refName = NtpUtils.getReferenceClock(message);
103
104 }
105
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
115 TimeStamp origNtpTime = message.getOriginateTimeStamp();
116 System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString());
117
118 long destTime = info.getReturnTime();
119
120 TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
121 System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString());
122
123
124 TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
125 System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString());
126
127
128 TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
129 System.out.println(" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString());
130
131 info.computeDetails();
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);
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
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 }