001    package examples.ntp;
002    /*
003     * Copyright 2001-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * 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    
018    import java.io.IOException;
019    import java.net.InetAddress;
020    import java.net.SocketException;
021    import java.net.UnknownHostException;
022    import java.text.NumberFormat;
023    
024    import org.apache.commons.net.ntp.*;
025    
026    /***
027     * This is an example program demonstrating how to use the NTPUDPClient
028     * class. This program sends a Datagram client request packet to a
029     * Network time Protocol (NTP) service port on a specified server,
030     * retrieves the time, and prints it to standard output along with
031     * the fields from the NTP message header (e.g. stratum level, reference id,
032     * poll interval, root delay, mode, ...)
033     * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
034     * for details.
035     * <p>
036     * Usage: NTPClient <hostname-or-address-list>
037     * <br>
038     * Example: NTPClient clock.psu.edu
039     *
040     * @author Jason Mathews, MITRE Corp
041     ***/
042    public final class NTPClient
043    {
044    
045        private static final NumberFormat numberFormat = new java.text.DecimalFormat("0.00");
046    
047        /**
048         * Process <code>TimeInfo</code> object and print its details.
049         * @param info <code>TimeInfo</code> object.
050         */
051        public static void processResponse(TimeInfo info)
052        {
053            NtpV3Packet message = info.getMessage();
054            int stratum = message.getStratum();
055            String refType;
056            if (stratum <= 0)
057                refType = "(Unspecified or Unavailable)";
058            else if (stratum == 1)
059                refType = "(Primary Reference; e.g., GPS)"; // GPS, radio clock, etc.
060            else
061                refType = "(Secondary Reference; e.g. via NTP or SNTP)";
062            // stratum should be 0..15...
063            System.out.println(" Stratum: " + stratum + " " + refType);
064            int version = message.getVersion();
065            int li = message.getLeapIndicator();
066            System.out.println(" leap=" + li + ", version="
067                    + version + ", precision=" + message.getPrecision());
068    
069            System.out.println(" mode: " + message.getModeName() + " (" + message.getMode() + ")");
070            int poll = message.getPoll();
071            // poll value typically btwn MINPOLL (4) and MAXPOLL (14)
072            System.out.println(" poll: " + (poll <= 0 ? 1 : (int) Math.pow(2, poll))
073                    + " seconds" + " (2 ** " + poll + ")");
074            double disp = message.getRootDispersionInMillisDouble();
075            System.out.println(" rootdelay=" + numberFormat.format(message.getRootDelayInMillisDouble())
076                    + ", rootdispersion(ms): " + numberFormat.format(disp));
077    
078            int refId = message.getReferenceId();
079            String refAddr = NtpUtils.getHostAddress(refId);
080            String refName = null;
081            if (refId != 0) {
082                if (refAddr.equals("127.127.1.0")) {
083                    refName = "LOCAL"; // This is the ref address for the Local Clock
084                } else if (stratum >= 2) {
085                    // If reference id has 127.127 prefix then it uses its own reference clock
086                    // defined in the form 127.127.clock-type.unit-num (e.g. 127.127.8.0 mode 5
087                    // for GENERIC DCF77 AM; see refclock.htm from the NTP software distribution.
088                    if (!refAddr.startsWith("127.127")) {
089                        try {
090                            InetAddress addr = InetAddress.getByName(refAddr);
091                            String name = addr.getHostName();
092                            if (name != null && !name.equals(refAddr))
093                                refName = name;
094                        } catch (UnknownHostException e) {
095                            // some stratum-2 servers sync to ref clock device but fudge stratum level higher... (e.g. 2)
096                            // ref not valid host maybe it's a reference clock name?
097                            // otherwise just show the ref IP address.
098                            refName = NtpUtils.getReferenceClock(message);
099                        }
100                    }
101                } else if (version >= 3 && (stratum == 0 || stratum == 1)) {
102                    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            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            // Originate Time is time request sent by client (t1)
115            TimeStamp origNtpTime = message.getOriginateTimeStamp();
116            System.out.println(" Originate Timestamp:\t" + origNtpTime + "  " + origNtpTime.toDateString());
117    
118            long destTime = info.getReturnTime();
119            // Receive Time is time request received by server (t2)
120            TimeStamp rcvNtpTime = message.getReceiveTimeStamp();
121            System.out.println(" Receive Timestamp:\t" + rcvNtpTime + "  " + rcvNtpTime.toDateString());
122    
123            // Transmit time is time reply sent by server (t3)
124            TimeStamp xmitNtpTime = message.getTransmitTimeStamp();
125            System.out.println(" Transmit Timestamp:\t" + xmitNtpTime + "  " + xmitNtpTime.toDateString());
126    
127            // Destination time is time reply received by client (t4)
128            TimeStamp destNtpTime = TimeStamp.getNtpTime(destTime);
129            System.out.println(" Destination Timestamp:\t" + destNtpTime + "  " + destNtpTime.toDateString());
130    
131            info.computeDetails(); // compute offset/delay if not already done
132            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); // offset in ms
139        }
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            // We want to timeout if a response takes longer than 10 seconds
150            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    }