KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
28 import java.net.*;
29 import java.util.*;
30
31 public class NtpConnection {
32     // Default port for the NTP protocol.
33
public static final int defaultNtpPort = 123;
34     private InetAddress ntpServer;
35     private int ntpPort;
36     private DatagramSocket datagramSocket;
37     private int maxHops = 15;
38     private int timeout = 10000;
39     public NtpConnection(InetAddress iaddr, int iport) throws SocketException {
40         ntpServer = iaddr;
41         ntpPort = iport;
42         datagramSocket = new DatagramSocket();
43         datagramSocket.setSoTimeout(timeout);
44     }
45
46     public NtpConnection(InetAddress iaddr) throws SocketException {
47         ntpServer = iaddr;
48         ntpPort = defaultNtpPort;
49         datagramSocket = new DatagramSocket();
50         datagramSocket.setSoTimeout(timeout);
51     }
52
53     public int getTimeout() {
54         return timeout;
55     }
56
57     public void setTimeout(int timeout) throws SocketException {
58         this.timeout = timeout;
59         datagramSocket.setSoTimeout(timeout);
60     }
61
62     public void send(NtpDatagramPacket ntpDatagramPacket) throws IOException {
63         datagramSocket.send(ntpDatagramPacket.getDatagramPacket());
64     }
65
66     public void receive(NtpDatagramPacket ntpDatagramPacket) throws IOException {
67         datagramSocket.receive(ntpDatagramPacket.getDatagramPacket());
68         ntpDatagramPacket.setReceptionTimeStamp(new TimeStamp(new Date()));
69     }
70
71     public NtpInfo getInfo() throws IOException {
72         NtpDatagramPacket dpSend = new NtpDatagramPacket(ntpServer, ntpPort);
73         NtpDatagramPacket dpReceive = new NtpDatagramPacket();
74         send(dpSend);
75         receive(dpReceive);
76         return dpReceive.getInfo();
77     }
78
79     /**
80    * Traces a server to the primary server.
81    * @return Vector containing the NtpInfo objects associated with
82    * the servers on the path to the primary server. Sometimes only a
83    * partial list will be generated due to timeouts or other problems.
84    */

85     public Vector getTrace() {
86         Vector traceList = new Vector();
87         int hops = 0;
88         boolean finished = false;
89         NtpConnection currentNtpConnection = this;
90         while ((!finished) && (hops < maxHops)) {
91             try {
92                 NtpInfo info = currentNtpConnection.getInfo();
93
94                 if (currentNtpConnection != this) {
95                     currentNtpConnection.close();
96                 }
97                 traceList.addElement(info);
98
99                 if (info.referenceIdentifier instanceof InetAddress) {
100                     currentNtpConnection = new NtpConnection((InetAddress)info.referenceIdentifier);
101                     hops++;
102                 } else {
103                     finished = true;
104                 }
105             } catch (Exception JavaDoc e) {
106                 finished = true;
107             }
108         }
109         return traceList;
110     }
111
112     /**
113    * Get the time from the server.
114    * @return A Date object containing the server time, adjusted for
115    * roundtrip delay. Note that it is better to use getInfo() and then
116    * to use the offset field of the returned NtpInfo object.
117    */

118     public Date getTime() {
119         try {
120             long offset = getInfo().offset;
121             return new Date(System.currentTimeMillis() + offset);
122         } catch (Exception JavaDoc e) {
123             System.out.println(e.getMessage());
124             e.printStackTrace();
125             return null;
126         }
127     }
128
129     public void close() {
130         datagramSocket.close();
131     }
132
133     public void finalize() {
134         close();
135     }
136 }
137
Popular Tags