    001    package examples.ntp;
    002    /*
    003     * Licensed to the Apache Software Foundation (ASF) under one or more
    004     * contributor license agreements.  See the NOTICE file distributed with
    005     * this work for additional information regarding copyright ownership.
    006     * The ASF licenses this file to You under the Apache License, Version 2.0
    007     * (the "License"); you may not use this file except in compliance with
    008     * the License.  You may obtain a copy of the License at
    009     *
    010     *      http://www.apache.org/licenses/LICENSE-2.0
    011     *
    012     * Unless required by applicable law or agreed to in writing, software
    013     * distributed under the License is distributed on an "AS IS" BASIS,
    014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    015     * See the License for the specific language governing permissions and
    016     * limitations under the License.
    017     */
    018
    019
    020    import java.io.IOException;
    021    import java.net.InetAddress;
    022    import java.net.SocketException;
    023    import java.net.UnknownHostException;
    024    import java.text.NumberFormat;
    025
    026    import org.apache.commons.net.ntp.*;
    027
    028    /***
    029     * This is an example program demonstrating how to use the NTPUDPClient
    030     * class. This program sends a Datagram client request packet to a
    031     * Network time Protocol (NTP) service port on a specified server,
    032     * retrieves the time, and prints it to standard output along with
    033     * the fields from the NTP message header (e.g. stratum level, reference id,
    034     * poll interval, root delay, mode, ...)
    035     * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
    036     * for details.
    037     * <p>
    038     * Usage: NTPClient <hostname-or-address-list>
    039     * <br>
    040     * Example: NTPClient clock.psu.edu
    041     *
    042     * @author Jason Mathews, MITRE Corp
    043     ***/
    044    public final class NTPClient
    045    {
    046
    047        private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
    048
    049        /**
    050         * Process <code>TimeInfo</code> object and print its details.
    051         * @param info <code>TimeInfo</code> object.
    052         */
    053        public static void processResponse(TimeInfo info)
    054        {
    055            NtpV3Packet message = info.getMessage();
    056            int stratum = message.getStratum();
    057            String refType;
    058            if (stratum <= 0)
    059                refType = "(Unspecified or Unavailable)";
    060            else if (stratum == 1)
    061                refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
    062            else
    063                refType = "(Secondary Reference; e.g. via NTP or SNTP)";
    064            // stratum should be 0..15...
    065            System.out.println(" Stratum: " + stratum + " " + refType);
    066            int version = message.getVersion();
    067            int li = message.getLeapIndicator();
    068            System.out.println(" leap=" + li + ", version="
    069                    + version + ", precision=" + message.getPrecision());
    070
    071            System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
    072            int poll = message.getPoll();
    073            // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
    074            System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll))
    075                    + " seconds" + " (2 ** " + poll + ")");
    076            double disp = message.getRootDispersionInMillisDouble();
    077            System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble())
    078                    + ", rootdispersion(ms): " + numberFormat.format(disp));
    079
    080            int refId = message.getReferenceId();
    081            String refAddr = NtpUtils.getHostAddress(refId);
    082            String refName = null;
    083            if (refId != 0) {
    084                if (refAddr.equals("127.127.1.0")) {
    085                    refName = "LOCAL"; // This is the ref address for the Local Clock
    086                } else if (stratum >= 2) {
    087                    // If reference id has 127.127 prefix then it uses its own reference clock
    088                    // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
    089                    // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
    090                    if (!refAddr.startsWith("127.127")) {
    091                        try {
    092                            InetAddress addr = InetAddress.getByName(refAddr);
    093                            String name = addr.getHostName();
    094                            if (name != null && !name.equals(refAddr))
    095                                refName = name;
    096                        } catch (UnknownHostException e) {
    097                            // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
    098                            // ref not valid host maybe it's a reference clock name?
    099                            // otherwise just show the ref IP address.
    100                            refName = NtpUtils.getReferenceClock(message);
    101                        }
    102                    }
    103                } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
    104                    refName = NtpUtils.getReferenceClock(message);
    105                    // refname usually have at least 3 characters (e.g. GPS, WWV, LCL, etc.)
    106                }
    107                // otherwise give up on naming the beast...
    108            }
    109            if (refName != null && refName.length() > 1)
    110                refAddr += " (" + refName + ")";
    111            System.out.println(" Reference Identifier:\t" + refAddr);
    112
    113            TimeStamp refNtpTime = message.getReferenceTimeStamp();
    114            System.out.println(" Reference Timestamp:\t" + refNtpTime + "  " + refNtpTime.toDateString());
    115
    116            // Originate Time is time request sent by client (t1)
    117            TimeStamp origNtpTime = message.getOriginateTimeStamp();
    118            System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());
    119
    120            long destTime = info.getReturnTime();
    121            // Receive Time is time request received by server (t2)
    122            TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
    123            System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());
    124
    125            // Transmit time is time reply sent by server (t3)
    126            TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
    127            System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());
    128
    129            // Destination time is time reply received by client (t4)
    130            TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
    131            System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());
    132
    133            info.computeDetails(); // compute offset/delay if not already done
    134            Long offsetValue = info.getOffset();
    135            Long delayValue = info.getDelay();
    136            String delay = (delayValue == null) ? "N/A" : delayValue.toString();
    137            String offset = (offsetValue == null) ? "N/A" : offsetValue.toString();
    138
    139            System.out.println(" Roundtrip delay(ms)=" + delay
    140                    + ", clock offset(ms)=" + offset); // offset in ms
    141        }
    142
    143        public static final void main(String[] args)
    144        {
    145            if (args == null || args.length == 0) {
    146                System.err.println("Usage: NTPClient <hostname-or-address-list>");
    147                System.exit(1);
    148            }
    149
    150            NTPUDPClient client = new NTPUDPClient();
    151            // We want to timeout if a response takes longer than 10 seconds
    152            client.setDefaultTimeout(10000);
    153            try {
    154                client.open();
    155                for (int i = 0; i < args.length; i++)
    156                {
    157                    System.out.println();
    158                    try {
    159                        InetAddress hostAddr = InetAddress.getByName(args[i]);
    160                        System.out.println("> " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
    161                        TimeInfo info = client.getTime(hostAddr);
    162                        processResponse(info);
    163                    } catch (IOException ioe) {
    164                        ioe.printStackTrace();
    165                    }
    166                }
    167            } catch (SocketException e) {
    168                e.printStackTrace();
    169            }
    170
    171            client.close();
    172        }
    173
    174    }