1 package examples.ntp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public final class NTPClient
49 {
50
51 private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
52
53
54
55
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)";
66 } else {
67 refType = "(Secondary Reference; e.g. via NTP or SNTP)";
68 }
69
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
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";
91 } else if (stratum >= 2) {
92
93
94
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
104
105
106 refName = NtpUtils.getReferenceClock(message);
107 }
108 }
109 } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
110 refName = NtpUtils.getReferenceClock(message);
111
112 }
113
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
124 TimeStamp origNtpTime = message.getOriginateTimeStamp();
125 System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString());
126
127 long destTime = info.getReturnTime();
128
129 TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
130 System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString());
131
132
133 TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
134 System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString());
135
136
137 TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
138 System.out.println(" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString());
139
140 info.computeDetails();
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);
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
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 }