KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > service > PhysicalTime


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): Nikolay Diakov */
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;
26
27 import java.util.*;
28 import java.net.*;
29 import java.io.*;
30 import org.coach.tracing.service.ntp.*;
31 import org.omg.CORBA.*;
32 import org.omg.CosNaming.*;
33
34 public class PhysicalTime
35 {
36     private static boolean NTP_enabled = true;
37     private static String JavaDoc NTP_server = "ntp0.fau.de";
38     private static int avg_from = 3;
39     private static long NTP_offset = 0;
40     private static long NTP_last_updated = 0;
41     private static long NTP_delta = 3600000; //every hour sync
42
private static PhysicalTime clock = new PhysicalTime(System.getProperty("tracing.ntp.server", "none"));
43
44     private PhysicalTime(String JavaDoc server)
45     {
46         if (server.equalsIgnoreCase("none"))
47         {
48             NTP_enabled = false;
49         }
50         else if (server != null)
51         {
52             NTP_server = server;
53         }
54     }
55
56     public static PhysicalTime getInstance()
57     {
58         return clock;
59     }
60
61     //get the current time, but updated with the local offset to a central NTP server
62
public long getTime()
63     {
64         long t = System.currentTimeMillis();
65         long o;
66         synchronized ( clock )
67         {
68             o = NTP_offset;
69         }
70
71         if ((t - NTP_last_updated) > NTP_delta)
72         {
73             //refresh the offset
74
o = calcOffset();
75             NTP_last_updated = t;
76         }
77         //call it again to be more precise
78
t = System.currentTimeMillis();
79         return (t + NTP_offset);
80     }
81
82     private long seed;
83     //NTP offset calculation
84
private synchronized long calcOffset()
85     {
86         long res = 0, d = 0;
87
88         if (NTP_enabled)
89         {
90             //here calculate the NTP offset using the NTP servers
91
for (int i = 0; i < avg_from; i++)
92             {
93                 try
94                 {
95                     System.out.println("Updating NTP offset from: " + NTP_server);
96                     NtpConnection ntpConnection = new NtpConnection(InetAddress.getByName(NTP_server));
97                     res += ntpConnection.getInfo().offset;
98                     d++;
99                     ntpConnection.close();
100                 }
101                 catch (Exception JavaDoc e)
102                 {
103                     System.out.println("Couldn't update the NTP offset from: " + NTP_server);
104                 }
105             }
106
107             if (d > 0)
108             {
109                 res = res / d;
110             }
111         }
112         System.out.println("New NTP offset value: " + res);
113         NTP_offset = res;
114         return res;
115     }
116 }
117
Popular Tags