Coverage report

  %line %branch
examples.ntp.NTPClient
0% 
0% 

 1  
 package examples.ntp;
 2  
 /*
 3  
  * Copyright 2001-2005 The Apache Software Foundation
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 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  
 /***
 27  
  * This is an example program demonstrating how to use the NTPUDPClient
 28  
  * class. This program sends a Datagram client request packet to a
 29  
  * Network time Protocol (NTP) service port on a specified server,
 30  
  * retrieves the time, and prints it to standard output along with
 31  
  * the fields from the NTP message header (e.g. stratum level, reference id,
 32  
  * poll interval, root delay, mode, ...)
 33  
  * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
 34  
  * for details.
 35  
  * <p>
 36  
  * Usage: NTPClient <hostname-or-address-list>
 37  
  * <br>
 38  
  * Example: NTPClient clock.psu.edu
 39  
  *
 40  
  * @author Jason Mathews, MITRE Corp
 41  
  ***/
 42  0
 public final class NTPClient
 43  
 {
 44  
 
 45  0
     private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
 46  
 
 47  
     /**
 48  
      * Process <code>TimeInfo</code> object and print its details.
 49  
      * @param info <code>TimeInfo</code> object.
 50  
      */
 51  
     public static void processResponse(TimeInfo info)
 52  
     {
 53  0
         NtpV3Packet message = info.getMessage();
 54  0
         int stratum = message.getStratum();
 55  
         String refType;
 56  0
         if (stratum <= 0)
 57  0
             refType = "(Unspecified or Unavailable)";
 58  0
         else if (stratum == 1)
 59  0
             refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
 60  
         else
 61  0
             refType = "(Secondary Reference; e.g. via NTP or SNTP)";
 62  
         // stratum should be 0..15...
 63  0
         System.out.println(" Stratum: " + stratum + " " + refType);
 64  0
         int version = message.getVersion();
 65  0
         int li = message.getLeapIndicator();
 66  0
         System.out.println(" leap=" + li + ", version="
 67  
                 + version + ", precision=" + message.getPrecision());
 68  
 
 69  0
         System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
 70  0
         int poll = message.getPoll();
 71  
         // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
 72  0
         System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll))
 73  
                 + " seconds" + " (2 ** " + poll + ")");
 74  0
         double disp = message.getRootDispersionInMillisDouble();
 75  0
         System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble())
 76  
                 + ", rootdispersion(ms): " + numberFormat.format(disp));
 77  
 
 78  0
         int refId = message.getReferenceId();
 79  0
         String refAddr = NtpUtils.getHostAddress(refId);
 80  0
         String refName = null;
 81  0
         if (refId != 0) {
 82  0
             if (refAddr.equals("127.127.1.0")) {
 83  0
                 refName = "LOCAL"; // This is the ref address for the Local Clock
 84  0
             } else if (stratum >= 2) {
 85  
                 // If reference id has 127.127 prefix then it uses its own reference clock
 86  
                 // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
 87  
                 // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
 88  0
                 if (!refAddr.startsWith("127.127")) {
 89  
                     try {
 90  0
                         InetAddress addr = InetAddress.getByName(refAddr);
 91  0
                         String name = addr.getHostName();
 92  0
                         if (name != null && !name.equals(refAddr))
 93  0
                             refName = name;
 94  0
                     } catch (UnknownHostException e) {
 95  
                         // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
 96  
                         // ref not valid host maybe it's a reference clock name?
 97  
                         // otherwise just show the ref IP address.
 98  0
                         refName = NtpUtils.getReferenceClock(message);
 99  0
                     }
 100  
                 }
 101  0
             } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
 102  0
                 refName = NtpUtils.getReferenceClock(message);
 103  
                 // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
 104  
             }
 105  
             // otherwise give up on naming the beast...
 106  
         }
 107  0
         if (refName != null && refName.length() > 1)
 108  0
             refAddr += " (" + refName + ")";
 109  0
         System.out.println(" Reference Identifier:\t" + refAddr);
 110  
 
 111  0
         TimeStamp refNtpTime = message.getReferenceTimeStamp();
 112  0
         System.out.println(" Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString());
 113  
 
 114  
         // Originate Time is time request sent by client (t1)
 115  0
         TimeStamp origNtpTime = message.getOriginateTimeStamp();
 116  0
         System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());
 117  
 
 118  0
         long destTime = info.getReturnTime();
 119  
         // Receive Time is time request received by server (t2)
 120  0
         TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
 121  0
         System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());
 122  
 
 123  
         // Transmit time is time reply sent by server (t3)
 124  0
         TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
 125  0
         System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());
 126  
 
 127  
         // Destination time is time reply received by client (t4)
 128  0
         TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
 129  0
         System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());
 130  
 
 131  0
         info.computeDetails(); // compute offset/delay if not already done
 132  0
         Long offsetValue = info.getOffset();
 133  0
         Long delayValue = info.getDelay();
 134  0
         String delay = (delayValue == null) ? "N/A" : delayValue.toString();
 135  0
         String offset = (offsetValue == null) ? "N/A" : offsetValue.toString();
 136  
 
 137  0
         System.out.println(" Roundtrip delay(ms)=" + delay
 138  
                 + ", clock offset(ms)=" + offset); // offset in ms
 139  0
     }
 140  
 
 141  
     public static final void main(String[] args)
 142  
     {
 143  0
         if (args == null || args.length == 0) {
 144  0
             System.err.println("Usage: NTPClient <hostname-or-address-list>");
 145  0
             System.exit(1);
 146  
         }
 147  
 
 148  0
         NTPUDPClient client = new NTPUDPClient();
 149  
         // We want to timeout if a response takes longer than 10 seconds
 150  0
         client.setDefaultTimeout(10000);
 151  
         try {
 152  0
             client.open(NtpV3Packet.NTP_PORT);
 153  0
             for (int i = 0; i < args.length; i++)
 154  
             {
 155  0
                 System.out.println();
 156  
                 try {
 157  0
                     InetAddress hostAddr = InetAddress.getByName(args[i]);
 158  0
                     System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
 159  0
                     TimeInfo info = client.getTime(hostAddr);
 160  0
                     processResponse(info);
 161  0
                 } catch (IOException ioe) {
 162  0
                     ioe.printStackTrace();
 163  0
                 }
 164  
             }
 165  0
         } catch (SocketException e) {
 166  0
             e.printStackTrace();
 167  0
         }
 168  
 
 169  0
         client.close();
 170  0
     }
 171  
 
 172  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.