Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
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.ntp; 26 27 import java.io.*; 28 import java.net.*; 29 import java.util.*; 30 31 public class NtpConnection { 32 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 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 e) { 106 finished = true; 107 } 108 } 109 return traceList; 110 } 111 112 118 public Date getTime() { 119 try { 120 long offset = getInfo().offset; 121 return new Date(System.currentTimeMillis() + offset); 122 } catch (Exception 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
|