1 package org.apache.commons.net.ntp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.List;
19 import java.util.ArrayList;
20
21 /**
22 * Wrapper class to network time packet messages (NTP, etc) that computes
23 * related timing info and stats.
24 *
25 * @author Jason Mathews, MITRE Corp
26 *
27 * @version $Revision: 165675 $ $Date: 2005-05-02 15:09:55 -0500 (Mon, 02 May 2005) $
28 */
29 public class TimeInfo {
30
31 private NtpV3Packet _message;
32 private List _comments;
33 private Long _delay;
34 private Long _offset;
35
36 /**
37 * time at which time message packet was received by local machine
38 */
39 private long _returnTime;
40
41 /**
42 * flag indicating that the TimeInfo details was processed and delay/offset were computed
43 */
44 private boolean _detailsComputed;
45
46 /**
47 * Create TimeInfo object with raw packet message and destination time received.
48 *
49 * @param message NTP message packet
50 * @param returnTime destination receive time
51 * @throws IllegalArgumentException if message is null
52 */
53 public TimeInfo(NtpV3Packet message, long returnTime) {
54 this(message, returnTime, null, true);
55 }
56
57 /**
58 * Create TimeInfo object with raw packet message and destination time received.
59 *
60 * @param message NTP message packet
61 * @param returnTime destination receive time
62 * @param comments List of errors/warnings identified during processing
63 * @throws IllegalArgumentException if message is null
64 */
65 public TimeInfo(NtpV3Packet message, long returnTime, List comments)
66 {
67 this(message, returnTime, comments, true);
68 }
69
70 /**
71 * Create TimeInfo object with raw packet message and destination time received.
72 * Auto-computes details if computeDetails flag set otherwise this is delayed
73 * until computeDetails() is called. Delayed computation is for fast
74 * intialization when sub-millisecond timing is needed.
75 *
76 * @param msgPacket NTP message packet
77 * @param returnTime destination receive time
78 * @param doComputeDetails flag to pre-compute delay/offset values
79 * @throws IllegalArgumentException if message is null
80 */
81 public TimeInfo(NtpV3Packet msgPacket, long returnTime, boolean doComputeDetails)
82 {
83 this(msgPacket, returnTime, null, doComputeDetails);
84 }
85
86 /**
87 * Create TimeInfo object with raw packet message and destination time received.
88 * Auto-computes details if computeDetails flag set otherwise this is delayed
89 * until computeDetails() is called. Delayed computation is for fast
90 * intialization when sub-millisecond timing is needed.
91 *
92 * @param message NTP message packet
93 * @param returnTime destination receive time
94 * @param comments list of comments used to store errors/warnings with message
95 * @param doComputeDetails flag to pre-compute delay/offset values
96 * @throws IllegalArgumentException if message is null
97 */
98 public TimeInfo(NtpV3Packet message, long returnTime, List comments,
99 boolean doComputeDetails)
100 {
101 if (message == null)
102 throw new IllegalArgumentException("message cannot be null");
103 this._returnTime = returnTime;
104 this._message = message;
105 this._comments = comments;
106 if (doComputeDetails)
107 computeDetails();
108 }
109
110 /**
111 * Add comment (error/warning) to list of comments associated
112 * with processing of NTP parameters. If comment list not create
113 * then one will be created.
114 *
115 * @param comment
116 */
117 public void addComment(String comment)
118 {
119 if (_comments == null) {
120 _comments = new ArrayList();
121 }
122 _comments.add(comment);
123 }
124
125 /**
126 * Compute and validate details of the NTP message packet. Computed
127 * fields include the offset and delay.
128 */
129 public void computeDetails()
130 {
131 if (_detailsComputed) {
132 return;
133 }
134 _detailsComputed = true;
135 if (_comments == null) {
136 _comments = new ArrayList();
137 }
138
139 TimeStamp origNtpTime = _message.getOriginateTimeStamp();
140 long origTime = origNtpTime.getTime();
141
142
143 TimeStamp rcvNtpTime = _message.getReceiveTimeStamp();
144 long rcvTime = rcvNtpTime.getTime();
145
146
147 TimeStamp xmitNtpTime = _message.getTransmitTimeStamp();
148 long xmitTime = xmitNtpTime.getTime();
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 if (origNtpTime.ntpValue() == 0)
167 {
168
169
170 if (xmitNtpTime.ntpValue() != 0)
171 {
172 _offset = new Long(xmitTime - _returnTime);
173 _comments.add("Error: zero orig time -- cannot compute delay");
174 } else
175 _comments.add("Error: zero orig time -- cannot compute delay/offset");
176 } else if (rcvNtpTime.ntpValue() == 0 || xmitNtpTime.ntpValue() == 0)
177 {
178 _comments.add("Warning: zero rcvNtpTime or xmitNtpTime");
179
180 if (origTime > _returnTime)
181 _comments.add("Error: OrigTime > DestRcvTime");
182 else
183 {
184
185
186 _delay = new Long(_returnTime - origTime);
187 }
188
189
190
191
192 if (rcvNtpTime.ntpValue() != 0)
193 {
194
195 _offset = new Long(rcvTime - origTime);
196 } else if (xmitNtpTime.ntpValue() != 0)
197 {
198
199 _offset = new Long(xmitTime - _returnTime);
200 }
201 } else
202 {
203 long delayValue = _returnTime - origTime;
204
205 if (xmitTime < rcvTime)
206 {
207
208 _comments.add("Error: xmitTime < rcvTime");
209 } else
210 {
211
212 long delta = xmitTime - rcvTime;
213
214
215 if (delta <= delayValue)
216 {
217 delayValue -= delta;
218 } else
219 {
220
221
222 if (delta - delayValue == 1)
223 {
224
225 if (delayValue != 0)
226 {
227 _comments.add("Info: processing time > total network time by 1 ms -> assume zero delay");
228 delayValue = 0;
229 }
230 } else
231 _comments.add("Warning: processing time > total network time");
232 }
233 }
234 _delay = new Long(delayValue);
235 if (origTime > _returnTime)
236 _comments.add("Error: OrigTime > DestRcvTime");
237
238 _offset = new Long(((rcvTime - origTime) + (xmitTime - _returnTime)) / 2);
239 }
240 }
241
242 /**
243 * Return list of comments (if any) during processing of NTP packet.
244 *
245 * @return List or null if not yet computed
246 */
247 public List getComments()
248 {
249 return _comments;
250 }
251
252 /**
253 * Get round-trip network delay. If null then could not compute the delay.
254 *
255 * @return Long or null if delay not available.
256 */
257 public Long getDelay()
258 {
259 return _delay;
260 }
261
262 /**
263 * Get clock offset needed to adjust local clock to match remote clock. If null then could not
264 * compute the offset.
265 *
266 * @return Long or null if offset not available.
267 */
268 public Long getOffset()
269 {
270 return _offset;
271 }
272
273 /**
274 * Returns NTP message packet.
275 *
276 * @return NTP message packet.
277 */
278 public NtpV3Packet getMessage()
279 {
280 return _message;
281 }
282
283 /**
284 * Returns time at which time message packet was received by local machine.
285 *
286 * @return packet return time.
287 */
288 public long getReturnTime()
289 {
290 return _returnTime;
291 }
292
293 }