KickJava   Java API By Example, From Geeks To Geeks.

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


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 the header of a NtpDatagram. See rfc2030 for more
31  * details.
32  */

33 public class NtpHeader {
34     /**
35    * The default header data for a client datagram. Version=3, Mode=client.
36    */

37     public static final byte[] defaultHeaderData = { (byte)0x1B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
38     /**
39    * The default header for a client datagram. This is a wrapper around
40    * 'defaultHeaderData'
41    */

42     public static final NtpHeader defaultHeader = new NtpHeader(defaultHeaderData);
43     private byte[] data;
44     /**
45    * Reference identifier is InetAddress.
46    */

47     private static final byte RI_IP_ADDRESS = 0;
48     /**
49    * Reference identifier is String.
50    */

51     private static final byte RI_CODE = 1;
52     /**
53    * Reference identifier is 4 byte array.
54    */

55     private static final byte RI_OTHER = 2;
56     private int mp(byte b) {
57         int bb = b;
58         return (bb < 0) ? 256 + bb : bb;
59     }
60
61     /**
62    * Construct a NtpHeader from a 16 byte array.
63    */

64     public NtpHeader(byte[] data) {
65         this.data = data;
66     }
67
68     /**
69    * Gets the 16 byte array constituting the header.
70    */

71     public byte[] getData() {
72         return data;
73     }
74
75     public int getLeapYearIndicator() {
76         return (int)((data[0] & 0xc0) >>> 6);
77     }
78
79     public int getVersionNumber() {
80         return (int)((data[0] & 0x38) >>> 3);
81     }
82
83     public int getMode() {
84         return (int)(data[0] & 0x07);
85     }
86
87     public int getStratum() {
88         return (int)data[1];
89     }
90
91     public int getPollInterval() {
92         return (int)Math.round(Math.pow(2, data[2]));
93     }
94
95     /**
96    * Get precision in milliseconds.
97    */

98     public double getPrecision() {
99         return 1000 * Math.pow(2, data[3]);
100     }
101
102     /**
103    * Get root delay in milliseconds.
104    */

105     public double getRootDelay() {
106         int temp = 0;
107         temp = 256 * (256 * (256 * data[4] + data[5]) + data[6]) + data[7];
108         return 1000 * (((double)temp) / 0x10000);
109     }
110
111     /**
112    * Get root dispersion in milliseconds.
113    */

114     public double getRootDispersion() {
115         long temp = 0;
116         temp = 256 * (256 * (256 * data[8] + data[9]) + data[10]) + data[11];
117         return 1000 * (((double)temp) / 0x10000);
118     }
119
120     /**
121    * Gets the type of the reference identifier.
122    */

123     private int getReferenceIdentifierType() {
124
125         if (getMode() == NtpInfo.MODE_CLIENT) {
126             return RI_OTHER;
127         } else if (getStratum() < 2) {
128             return RI_CODE;
129         } else if (getVersionNumber() <= 3) {
130             return RI_IP_ADDRESS;
131         } else {
132             return RI_OTHER;
133         }
134     }
135
136     private InetAddress getReferenceAddress() throws IllegalArgumentException JavaDoc, UnknownHostException {
137
138         if (getReferenceIdentifierType() != RI_IP_ADDRESS) {
139             throw new IllegalArgumentException JavaDoc();
140         }
141         String JavaDoc temp = "" + mp(data[12]) + "." + mp(data[13]) + "." + mp(data[14]) + "." + mp(data[15]);
142         return InetAddress.getByName(temp);
143     }
144
145     private String JavaDoc getReferenceCode() throws IllegalArgumentException JavaDoc {
146
147         if (getReferenceIdentifierType() != RI_CODE) {
148             throw new IllegalArgumentException JavaDoc();
149         }
150         int codeLength = 0;
151         int index = 12;
152         boolean zeroFound = false;
153
154         while ((!zeroFound) && (index <= 15)) {
155
156             if (data[index] == 0) {
157                 zeroFound = true;
158             } else {
159                 index++;
160                 codeLength++;
161             }
162         }
163         return new String JavaDoc(data, 12, codeLength);
164     }
165
166     private byte[] getReferenceData() {
167         byte[] temp = new byte[4];
168         temp[0] = data[12];
169         temp[1] = data[13];
170         temp[2] = data[14];
171         temp[3] = data[15];
172         return temp;
173     }
174
175     /**
176    * Gets the reference identifier as an object. It can be either a
177    * String, a InetAddress or a 4 byte array. Use 'instanceof' to find out what
178    * the true class is.
179    */

180     public Object JavaDoc getReferenceIdentifier() {
181
182         if (getReferenceIdentifierType() == RI_IP_ADDRESS) {
183             try {
184                 return getReferenceAddress();
185             } catch (Exception JavaDoc e) {
186                 return getReferenceData();
187             }
188         } else if (getReferenceIdentifierType() == RI_CODE) {
189             return getReferenceCode();
190         } else {
191             return getReferenceData();
192         }
193     }
194
195     public String JavaDoc toString() {
196         String JavaDoc s = "Leap year indicator : " + getLeapYearIndicator() + "\n" + "Version number : " + getVersionNumber() + "\n" + "Mode : " + getMode() + "\n" + "Stratum : " + getStratum() + "\n" + "Poll interval : " + getPollInterval() + " s\n" + "Precision : " + getPrecision() + " ms\n" + "Root delay : " + getRootDelay() + " ms\n" + "Root dispersion : " + getRootDispersion() + " ms\n";
197         Object JavaDoc o = getReferenceIdentifier();
198
199         if (o instanceof InetAddress) {
200             s = s + "Reference address : " + (InetAddress)o;
201         } else if (o instanceof String JavaDoc) {
202             s = s + "Reference code : " + (String JavaDoc)o;
203         } else {
204             byte[] temp = (byte[])o;
205             s = s + "Reference data : " + temp[0] + " " + temp[1] + " " + temp[2] + " " + temp[3];
206         }
207         return s;
208     }
209 }
210
Popular Tags