1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 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; private static PhysicalTime clock = new PhysicalTime(System.getProperty("tracing.ntp.server", "none")); 43 44 private PhysicalTime(String 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 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 o = calcOffset(); 75 NTP_last_updated = t; 76 } 77 t = System.currentTimeMillis(); 79 return (t + NTP_offset); 80 } 81 82 private long seed; 83 private synchronized long calcOffset() 85 { 86 long res = 0, d = 0; 87 88 if (NTP_enabled) 89 { 90 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 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 |