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