-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNTSTime.java
More file actions
144 lines (127 loc) · 5.72 KB
/
Copy pathNTSTime.java
File metadata and controls
144 lines (127 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import nts.AuthenticationFailureException;
import nts.NTSUDPClient;
import nts.NtpUtils;
import nts.NtpV3Packet;
import nts.TimeInfo;
import nts.TimeStamp;
public class NTSTime {
public static void main(String[] args) {
// String host = "paris.time.system76.com";
final List<String> TIME_SERVERS =
new ArrayList<>(List.of("paris.time.system76.com", "time.cloudflare.com"));
NTSUDPClient client = new NTSUDPClient();
for (String host : TIME_SERVERS) {
try {
InetAddress hostAddr = InetAddress.getByName(host);
TimeInfo info = client.getTime(hostAddr);
System.out.println(" Server: " + host);
processResponse(info);
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + e.getMessage());
} catch (AuthenticationFailureException e) {
System.err.println("Authentication failure: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
client.close();
}
public static void processResponse(final TimeInfo info) {
final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
final NtpV3Packet message = info.getMessage();
final int stratum = message.getStratum();
final String refType;
if (stratum <= 0) {
refType = "(Unspecified or Unavailable)";
} else if (stratum == 1) {
refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
} else {
refType = "(Secondary Reference; e.g. via NTP or SNTP)";
}
// stratum should be 0..15...
System.out.println(" Stratum: " + stratum + " " + refType);
final int version = message.getVersion();
final int li = message.getLeapIndicator();
System.out.println(
" leap=" + li + ", version=" + version + ", precision=" + message.getPrecision());
System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
final int poll = message.getPoll();
// poll value typically btwn MINPOLL (4) and MAXPOLL (14)
System.out.println(
" poll: "
+ (poll <= 0 ? 1 : (int) Math.pow(2, poll))
+ " seconds"
+ " (2 ** "
+ poll
+ ")");
final double disp = message.getRootDispersionInMillisDouble();
System.out.println(
" rootdelay="
+ numberFormat.format(message.getRootDelayInMillisDouble())
+ ", rootdispersion(ms): "
+ numberFormat.format(disp));
final int refId = message.getReferenceId();
String refAddr = NtpUtils.getHostAddress(refId);
String refName = null;
if (refId != 0) {
if (refAddr.equals("127.127.1.0")) {
refName = "LOCAL"; // This is the ref address for the Local Clock
} else if (stratum >= 2) {
// If reference id has 127.127 prefix then it uses its own reference clock
// defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
// for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
if (!refAddr.startsWith("127.127")) {
try {
final InetAddress addr = InetAddress.getByName(refAddr);
final String name = addr.getHostName();
if (name != null && !name.equals(refAddr)) {
refName = name;
}
} catch (final UnknownHostException e) {
// some stratum-2 servers sync to ref clock device but fudge stratum level higher...
// (e.g. 2)
// ref not valid host maybe it's a reference clock name?
// otherwise just show the ref IP address.
refName = NtpUtils.getReferenceClock(message);
}
}
} else if (version >= 3 && (stratum == 0 || stratum == 1)) {
refName = NtpUtils.getReferenceClock(message);
// refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
}
// otherwise give up on naming the beast...
}
if (refName != null && refName.length() > 1) {
refAddr += " (" + refName + ")";
}
System.out.println(" Reference Identifier:\t" + refAddr);
final TimeStamp refNtpTime = message.getReferenceTimeStamp();
System.out.println(" Reference Timestamp:\t" + refNtpTime + " " + refNtpTime.toDateString());
// Originate Time is time request sent by client (t1)
final TimeStamp origNtpTime = message.getOriginateTimeStamp();
System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString());
final long destTimeMillis = info.getReturnTime();
// Receive Time is time request received by server (t2)
final TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString());
// Transmit time is time reply sent by server (t3)
final TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString());
// Destination time is time reply received by client (t4)
final TimeStamp destNtpTime = TimeStamp.getNtpTime(destTimeMillis);
System.out.println(
" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString());
info.computeDetails(); // compute offset/delay if not already done
final Long offsetMillis = info.getOffset();
final Long delayMillis = info.getDelay();
final String delay = delayMillis == null ? "N/A" : delayMillis.toString();
final String offset = offsetMillis == null ? "N/A" : offsetMillis.toString();
System.out.println(
" Roundtrip delay(ms)=" + delay + ", clock offset(ms)=" + offset); // offset in ms
}
}