KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > ntp > NtpUtils


1 package org.apache.commons.net.ntp;
2 /*
3  * Copyright 2001-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /***
19  * Common NtpUtils Helper class.
20  *
21  * @author Jason Mathews, MITRE Corp
22  *
23  * @version $Revision: 165675 $ $Date: 2005-05-02 15:09:55 -0500 (Mon, 02 May 2005) $
24  */

25 public final class NtpUtils {
26
27     /***
28       * Returns 32-bit integer address to IPv4 address string "%d.%d.%d.%d" format.
29       *
30       * @param address the 32-bit address
31       * @return the raw IP address in a string format.
32       */

33      public static String JavaDoc getHostAddress(int address)
34      {
35           return ((address >>> 24) & 0xFF) + "." +
36                  ((address >>> 16) & 0xFF) + "." +
37                  ((address >>> 8) & 0xFF) + "." +
38                  ((address >>> 0) & 0xFF);
39      }
40
41     /***
42      * Returns NTP packet reference identifier as IP address.
43      *
44      * @param packet NTP packet
45      * @return the packet reference id (as IP address) in "%d.%d.%d.%d" format.
46      */

47      public static String JavaDoc getRefAddress(NtpV3Packet packet)
48      {
49          int address = (packet == null) ? 0 : packet.getReferenceId();
50          return getHostAddress(address);
51      }
52
53     /***
54      * Get refId as reference clock string (e.g. GPS, WWV, LCL). If string is
55      * invalid (non-ASCII character) then returns empty string "".
56      * For details refer to the <A HREF="http://www.eecis.udel.edu/~mills/ntp/html/refclock.html#list">Comprehensive
57      * List of Clock Drivers</A>.
58      *
59      * @param message
60      * @return reference clock string if primary NTP server
61      */

62     public static String JavaDoc getReferenceClock(NtpV3Packet message) {
63         if (message == null)
64             return "";
65         int refId = message.getReferenceId();
66         if (refId == 0)
67             return "";
68         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(4);
69         // start at highest-order byte (0x4c434c00 -> LCL)
70
for (int shiftBits = 24; shiftBits >= 0; shiftBits -= 8)
71         {
72             char c = (char) ((refId >>> shiftBits) & 0xff);
73             if (c == 0) break; // 0-terminated ASCII string
74
if (!Character.isLetterOrDigit(c))
75                 return "";
76             buf.append(c);
77         }
78         return buf.toString();
79     }
80
81     /***
82      * Return human-readable name of message mode type (RFC 1305).
83      *
84      * @param mode
85      * @return mode name
86      */

87     public static String JavaDoc getModeName(int mode)
88     {
89         switch (mode) {
90             case NtpV3Packet.MODE_RESERVED:
91                 return "Reserved";
92             case NtpV3Packet.MODE_SYMMETRIC_ACTIVE:
93                 return "Symmetric Active";
94             case NtpV3Packet.MODE_SYMMETRIC_PASSIVE:
95                 return "Symmetric Passive";
96             case NtpV3Packet.MODE_CLIENT:
97                 return "Client";
98             case NtpV3Packet.MODE_SERVER:
99                 return "Server";
100             case NtpV3Packet.MODE_BROADCAST:
101                 return "Broadcast";
102             case NtpV3Packet.MODE_CONTROL_MESSAGE:
103                 return "Control";
104             case NtpV3Packet.MODE_PRIVATE:
105                 return "Private";
106             default:
107                 return "Unknown";
108         }
109     }
110
111 }
112
Popular Tags