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 package org.coach.tracing.service.ntp; 26 27 import java.net.*; 28 29 56 public class NtpDatagramPacket { 57 private static final int headerOffset = 0; 58 private static final int referenceTimeStampOffset = 16; 59 private static final int originateTimeStampOffset = 24; 60 private static final int receiveTimeStampOffset = 32; 61 private static final int transmitTimeStampOffset = 40; 62 private static final int ntpDatagramLength = 48; 63 private DatagramPacket dp; 64 private TimeStamp receptionTimeStamp; 65 70 public NtpDatagramPacket(NtpHeader header, TimeStamp referenceTimeStamp, TimeStamp originateTimeStamp, TimeStamp receiveTimeStamp, TimeStamp transmitTimeStamp, InetAddress iaddr, int iport) { 71 byte[] temp; 72 byte[] buffer = new byte[ntpDatagramLength]; 73 74 for (int i = headerOffset; i < referenceTimeStampOffset; i++) { 75 buffer[i] = (header.getData())[i - headerOffset]; 76 } 77 78 for (int i = referenceTimeStampOffset; i < originateTimeStampOffset; i++) { 79 temp = referenceTimeStamp.getData(); 80 buffer[i] = temp[i - referenceTimeStampOffset]; 81 } 82 83 for (int i = originateTimeStampOffset; i < receiveTimeStampOffset; i++) { 84 temp = originateTimeStamp.getData(); 85 buffer[i] = temp[i - originateTimeStampOffset]; 86 } 87 88 for (int i = receiveTimeStampOffset; i < transmitTimeStampOffset; i++) { 89 temp = receiveTimeStamp.getData(); 90 buffer[i] = temp[i - receiveTimeStampOffset]; 91 } 92 93 for (int i = transmitTimeStampOffset; i < ntpDatagramLength; i++) { 94 temp = transmitTimeStamp.getData(); 95 buffer[i] = temp[i - transmitTimeStampOffset]; 96 } 97 dp = new DatagramPacket(buffer, ntpDatagramLength, iaddr, iport); 98 } 99 100 106 public NtpDatagramPacket(InetAddress iaddr, int iport) { 107 this(NtpHeader.defaultHeader, TimeStamp.zero, TimeStamp.zero, TimeStamp.zero, new TimeStamp(), iaddr, iport); 108 } 109 110 113 public NtpDatagramPacket() { 114 byte[] buffer = new byte[ntpDatagramLength]; 115 dp = new DatagramPacket(buffer, ntpDatagramLength); 116 } 117 118 121 public NtpDatagramPacket(DatagramPacket dp) { 122 this.dp = dp; 123 } 124 125 128 DatagramPacket getDatagramPacket() { 129 return dp; 130 } 131 132 136 public NtpHeader getHeader() { 137 byte[] buffer = dp.getData(); 138 byte[] temp = new byte[16]; 139 for (int i = headerOffset; i < referenceTimeStampOffset; i++) { 140 temp[i - headerOffset] = buffer[i]; 141 } 142 return new NtpHeader(temp); 143 } 144 145 148 public TimeStamp getReferenceTimeStamp() { 149 byte[] buffer = dp.getData(); 150 byte[] temp = new byte[8]; 151 for (int i = referenceTimeStampOffset; i < originateTimeStampOffset; i++) { 152 temp[i - referenceTimeStampOffset] = buffer[i]; 153 } 154 return new TimeStamp(temp); 155 } 156 157 160 public TimeStamp getOriginateTimeStamp() { 161 byte[] buffer = dp.getData(); 162 byte[] temp = new byte[8]; 163 for (int i = originateTimeStampOffset; i < receiveTimeStampOffset; i++) { 164 temp[i - originateTimeStampOffset] = buffer[i]; 165 } 166 return new TimeStamp(temp); 167 } 168 169 172 public TimeStamp getReceiveTimeStamp() { 173 byte[] buffer = dp.getData(); 174 byte[] temp = new byte[8]; 175 for (int i = receiveTimeStampOffset; i < transmitTimeStampOffset; i++) { 176 temp[i - receiveTimeStampOffset] = buffer[i]; 177 } 178 return new TimeStamp(temp); 179 } 180 181 184 public TimeStamp getTransmitTimeStamp() { 185 byte[] buffer = dp.getData(); 186 byte[] temp = new byte[8]; 187 for (int i = transmitTimeStampOffset; i < ntpDatagramLength; i++) { 188 temp[i - transmitTimeStampOffset] = buffer[i]; 189 } 190 return new TimeStamp(temp); 191 } 192 193 196 public TimeStamp getReceptionTimeStamp() { 197 return receptionTimeStamp; 198 } 199 200 void setReceptionTimeStamp(TimeStamp receptionTimeStamp) { 201 this.receptionTimeStamp = receptionTimeStamp; 202 } 203 204 209 public NtpInfo getInfo() { 210 NtpInfo info = new NtpInfo(); 211 NtpHeader h = getHeader(); 212 info.serverAddress = dp.getAddress(); 213 info.leapYearIndicator = h.getLeapYearIndicator(); 214 info.versionNumber = h.getVersionNumber(); 215 info.stratum = h.getStratum(); 216 info.mode = h.getMode(); 217 info.pollInterval = h.getPollInterval(); 218 info.precision = h.getPrecision(); 219 info.rootDelay = h.getRootDelay(); 220 info.rootDispersion = h.getRootDispersion(); 221 info.referenceIdentifier = h.getReferenceIdentifier(); 222 info.referenceTimeStamp = getReferenceTimeStamp(); 223 long originate = getOriginateTimeStamp().getTime().getTime(); 224 long receive = getReceiveTimeStamp().getTime().getTime(); 225 long transmit = getTransmitTimeStamp().getTime().getTime(); 226 long reception = getReceptionTimeStamp().getTime().getTime(); 227 info.roundTripDelay = receive - originate + reception - transmit; 228 info.offset = (receive - originate - reception + transmit) / 2; 229 return info; 230 } 231 232 public String toString() { 233 String s; 234 s = "Header : "; 235 s = s + getHeader(); 236 s = s + "\n"; 237 s = s + "ReferenceTimeStamp : "; 238 s = s + getReferenceTimeStamp(); 239 s = s + "\n"; 240 s = s + "OriginateTimeStamp : "; 241 s = s + getOriginateTimeStamp(); 242 s = s + "\n"; 243 s = s + "ReceiveTimeStamp : "; 244 s = s + getReceiveTimeStamp(); 245 s = s + "\n"; 246 s = s + "TransmitTimeStamp : "; 247 s = s + getTransmitTimeStamp(); 248 return s; 249 } 250 } 251 | Popular Tags |