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