1 package examples.ntp; 2 17 18 import java.io.IOException ; 19 import java.net.InetAddress ; 20 import java.net.SocketException ; 21 import java.net.UnknownHostException ; 22 import java.text.NumberFormat ; 23 24 import org.apache.commons.net.ntp.*; 25 26 42 public final class NTPClient 43 { 44 45 private static final NumberFormat numberFormat = new java.text.DecimalFormat ("0.00"); 46 47 51 public static void processResponse(TimeInfo info) 52 { 53 NtpV3Packet message = info.getMessage(); 54 int stratum = message.getStratum(); 55 String refType; 56 if (stratum <= 0) 57 refType = "(Unspecified or Unavailable)"; 58 else if (stratum == 1) 59 refType = "(Primary Reference; e.g., GPS)"; else 61 refType = "(Secondary Reference; e.g. via NTP or SNTP)"; 62 System.out.println(" Stratum: " + stratum + " " + refType); 64 int version = message.getVersion(); 65 int li = message.getLeapIndicator(); 66 System.out.println(" leap=" + li + ", version=" 67 + version + ", precision=" + message.getPrecision()); 68 69 System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")"); 70 int poll = message.getPoll(); 71 System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll)) 73 + " seconds" + " (2 ** " + poll + ")"); 74 double disp = message.getRootDispersionInMillisDouble(); 75 System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble()) 76 + ", rootdispersion(ms): " + numberFormat.format(disp)); 77 78 int refId = message.getReferenceId(); 79 String refAddr = NtpUtils.getHostAddress(refId); 80 String refName = null; 81 if (refId != 0) { 82 if (refAddr.equals("127.127.1.0")) { 83 refName = "LOCAL"; } else if (stratum >= 2) { 85 if (!refAddr.startsWith("127.127")) { 89 try { 90 InetAddress addr = InetAddress.getByName(refAddr); 91 String name = addr.getHostName(); 92 if (name != null && !name.equals(refAddr)) 93 refName = name; 94 } catch (UnknownHostException e) { 95 refName = NtpUtils.getReferenceClock(message); 99 } 100 } 101 } else if (version >= 3 && (stratum == 0 || stratum == 1)) { 102 refName = NtpUtils.getReferenceClock(message); 103 } 105 } 107 if (refName != null && refName.length() > 1) 108 refAddr += " (" + refName + ")"; 109 System.out.println(" Reference Identifier:\t" + refAddr); 110 111 TimeStamp refNtpTime = message.getReferenceTimeStamp(); 112 System.out.println(" Reference Timestamp:\t" + refNtpTime + " " + refNtpTime.toDateString()); 113 114 TimeStamp origNtpTime = message.getOriginateTimeStamp(); 116 System.out.println(" Originate Timestamp:\t" + origNtpTime + " " + origNtpTime.toDateString()); 117 118 long destTime = info.getReturnTime(); 119 TimeStamp rcvNtpTime = message.getReceiveTimeStamp(); 121 System.out.println(" Receive Timestamp:\t" + rcvNtpTime + " " + rcvNtpTime.toDateString()); 122 123 TimeStamp xmitNtpTime = message.getTransmitTimeStamp(); 125 System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + " " + xmitNtpTime.toDateString()); 126 127 TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime); 129 System.out.println(" Destination Timestamp:\t" + destNtpTime + " " + destNtpTime.toDateString()); 130 131 info.computeDetails(); Long offsetValue = info.getOffset(); 133 Long delayValue = info.getDelay(); 134 String delay = (delayValue == null) ? "N/A" : delayValue.toString(); 135 String offset = (offsetValue == null) ? "N/A" : offsetValue.toString(); 136 137 System.out.println(" Roundtrip delay(ms)=" + delay 138 + ", clock offset(ms)=" + offset); } 140 141 public static final void main(String [] args) 142 { 143 if (args == null || args.length == 0) { 144 System.err.println("Usage: NTPClient <hostname-or-address-list>"); 145 System.exit(1); 146 } 147 148 NTPUDPClient client = new NTPUDPClient(); 149 client.setDefaultTimeout(10000); 151 try { 152 client.open(NtpV3Packet.NTP_PORT); 153 for (int i = 0; i < args.length; i++) 154 { 155 System.out.println(); 156 try { 157 InetAddress hostAddr = InetAddress.getByName(args[i]); 158 System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress()); 159 TimeInfo info = client.getTime(hostAddr); 160 processResponse(info); 161 } catch (IOException ioe) { 162 ioe.printStackTrace(); 163 } 164 } 165 } catch (SocketException e) { 166 e.printStackTrace(); 167 } 168 169 client.close(); 170 } 171 172 } | Popular Tags |