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.*;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public final class NTPClient
45 {
46
47 private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
48
49
50
51
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)";
62 else
63 refType = "(Secondary Reference; e.g. via NTP or SNTP)";
64
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
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";
86 } else if (stratum >= 2) {
87
88
89
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
98
99
100 refName = NtpUtils.getReferenceClock(message);
101 }
102 }
103 } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
104 refName = NtpUtils.getReferenceClock(message);
105
106 }
107
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
117 TimeStamp origNtpTime = message.getOriginateTimeStamp();
118 System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString());
119
120 long destTime = info.getReturnTime();
121
122 TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
123 System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString());
124
125
126 TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
127 System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString());
128
129
130 TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
131 System.out.println(" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString());
132
133 info.computeDetails();
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);
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
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 }