KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > service > ntp > NtpDatagramPacket


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.tracing.service.ntp;
26
27 import java.net.*;
28
29 /**
30  * This class encapsulates a ntp-datagram as described in rfc2030.
31  * Such a datagram consists of a header and four timestamps.
32  * The four timestamps are respectively:
33  * <UL>
34  * <LI>
35  * The reference timestamp. This indicates when the local clock was
36  * last set.
37  * Can be set to zero for datagrams originating on the client.
38  * <LI>
39  * The originate timestamp. Indicates when the datagram originated
40  * on the client. Copied by the server from the transmit timestamp.
41  * Can be set to zero for datagrams originating on the client.
42  * <LI>
43  * The receive timestamp. Indicates when the reply left the server.
44  * Can be set to zero for datagrams originating on the client.
45  * <LI>
46  * The transmit timestamp. Indicates when the datagram departed.
47  * </UL>
48  * We have added a fifth timestamp. Namely a 'reception timestamp'
49  * which is normally set by NtpConnection.receive(NtpDatagramPacket).
50  * When transmitted a ntp-datagram is wrapped in a UDP datagram.
51  * @see java.net.Datagram
52  * @see TimeStamp
53  * @see NtpHeader
54  * @see NtpConnection
55  */

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     /**
66    * Construct a NtpDatagram from a header, four timestamps, an
67    * Inetaddress and a portnumber.
68    * @see InetAddress
69    */

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     /**
101    * Construct a NtpDatagram with only the transmit timestamp
102    * filled in (set to the current time). The header is set to a
103    * NtpHeader.defaultHeader.
104    * @see NtpHeader
105    */

106     public NtpDatagramPacket(InetAddress iaddr, int iport) {
107         this(NtpHeader.defaultHeader, TimeStamp.zero, TimeStamp.zero, TimeStamp.zero, new TimeStamp(), iaddr, iport);
108     }
109
110     /**
111    * Constructs an uninitialized NtpDatagram.
112    */

113     public NtpDatagramPacket() {
114         byte[] buffer = new byte[ntpDatagramLength];
115         dp = new DatagramPacket(buffer, ntpDatagramLength);
116     }
117
118     /**
119    * Constructs an uninitialized NtpDatagram from a UDP datagram.
120    */

121     public NtpDatagramPacket(DatagramPacket dp) {
122         this.dp = dp;
123     }
124
125     /**
126    * Returns the UDP datagram associated to an NtpDatagram.
127    */

128     DatagramPacket getDatagramPacket() {
129         return dp;
130     }
131
132     /**
133    * Returns the header associated to a NtpDatagram.
134    * @see NtpHeader
135    */

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     /**
146    * Returns the reference timestamp.
147    */

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     /**
158    * Returns the originate timestamp
159    */

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     /**
170    * Returns the receive timestamp
171    */

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     /**
182    * Returns the transmit timestamp
183    */

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     /**
194    * Returns the reception timestamp
195    */

196     public TimeStamp getReceptionTimeStamp() {
197         return receptionTimeStamp;
198     }
199
200     void setReceptionTimeStamp(TimeStamp receptionTimeStamp) {
201         this.receptionTimeStamp = receptionTimeStamp;
202     }
203
204     /**
205    * A convenience method which returns the useful information
206    * contained in a NtpDatagram.
207    * @see NtpInfo
208    */

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 JavaDoc toString() {
233         String JavaDoc 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