1 package org.apache.commons.net.ntp; 2 17 18 import java.util.List ; 19 import java.util.ArrayList ; 20 21 29 public class TimeInfo { 30 31 private NtpV3Packet _message; 32 private List _comments; 33 private Long _delay; 34 private Long _offset; 35 36 39 private long _returnTime; 40 41 44 private boolean _detailsComputed; 45 46 53 public TimeInfo(NtpV3Packet message, long returnTime) { 54 this(message, returnTime, null, true); 55 } 56 57 65 public TimeInfo(NtpV3Packet message, long returnTime, List comments) 66 { 67 this(message, returnTime, comments, true); 68 } 69 70 81 public TimeInfo(NtpV3Packet msgPacket, long returnTime, boolean doComputeDetails) 82 { 83 this(msgPacket, returnTime, null, doComputeDetails); 84 } 85 86 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 117 public void addComment(String comment) 118 { 119 if (_comments == null) { 120 _comments = new ArrayList (); 121 } 122 _comments.add(comment); 123 } 124 125 129 public void computeDetails() 130 { 131 if (_detailsComputed) { 132 return; } 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 TimeStamp rcvNtpTime = _message.getReceiveTimeStamp(); 144 long rcvTime = rcvNtpTime.getTime(); 145 146 TimeStamp xmitNtpTime = _message.getTransmitTimeStamp(); 148 long xmitTime = xmitNtpTime.getTime(); 149 150 166 if (origNtpTime.ntpValue() == 0) 167 { 168 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 if (origTime > _returnTime) 181 _comments.add("Error: OrigTime > DestRcvTime"); 182 else 183 { 184 _delay = new Long (_returnTime - origTime); 187 } 188 if (rcvNtpTime.ntpValue() != 0) 193 { 194 _offset = new Long (rcvTime - origTime); 196 } else if (xmitNtpTime.ntpValue() != 0) 197 { 198 _offset = new Long (xmitTime - _returnTime); 200 } 201 } else 202 { 203 long delayValue = _returnTime - origTime; 204 if (xmitTime < rcvTime) 206 { 207 _comments.add("Error: xmitTime < rcvTime"); } else 210 { 211 long delta = xmitTime - rcvTime; 213 if (delta <= delayValue) 216 { 217 delayValue -= delta; } else 219 { 220 if (delta - delayValue == 1) 223 { 224 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) _comments.add("Error: OrigTime > DestRcvTime"); 237 238 _offset = new Long (((rcvTime - origTime) + (xmitTime - _returnTime)) / 2); 239 } 240 } 241 242 247 public List getComments() 248 { 249 return _comments; 250 } 251 252 257 public Long getDelay() 258 { 259 return _delay; 260 } 261 262 268 public Long getOffset() 269 { 270 return _offset; 271 } 272 273 278 public NtpV3Packet getMessage() 279 { 280 return _message; 281 } 282 283 288 public long getReturnTime() 289 { 290 return _returnTime; 291 } 292 293 } 294 | Popular Tags |