ntpmessage.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 394 行 · 第 1/2 页

JAVA
394
字号
/** * This class represents a NTP message, as specified in RFC 2030.  The message * format is compatible with all versions of NTP and SNTP.  ToDo: Is this true? * * This class does not support the optional authentication protocol, and * ignores the key ID and message digest fields. *  * For convenience, this class exposes message values as native Java types, not * the NTP-specified data formats.  For example, timestamps are * stored as doubles (as opposed to the NTP unsigned 64-bit fixed point * format). *  * However, the contructor NtpMessage(byte[]) and the method toByteArray() * allow the import and export of the raw NTP message format. *  *  * Usage example *  * // Send message * DatagramSocket socket = new DatagramSocket(); * InetAddress address = InetAddress.getByName("ntp.cais.rnp.br"); * byte[] buf = new NtpMessage().toByteArray(); * DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 123); * socket.send(packet); *  * // Get response * socket.receive(packet); * System.out.println(msg.toString()); *  *   * This code is copyright (c) Adam Buckley 2004 * * This program is free software; you can redistribute it and/or modify it  * under the terms of the GNU General Public License as published by the Free  * Software Foundation; either version 2 of the License, or (at your option)  * any later version.  A HTML version of the GNU General Public License can be * seen at http://www.gnu.org/licenses/gpl.html * * This program is distributed in the hope that it will be useful, but WITHOUT  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for  * more details. *  *  * Comments for member variables are taken from RFC2030 by David Mills, * University of Delaware. *  * Number format conversion code in NtpMessage(byte[] array) and toByteArray() * inspired by http://www.pps.jussieu.fr/~jch/enseignement/reseaux/ * NTPMessage.java which is copyright (c) 2003 by Juliusz Chroboczek *  * @author Adam Buckley */package org.opennms.protocols.ntp;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.Date;public class NtpMessage {    /**     * This is a two-bit code warning of an impending leap second to be     * inserted/deleted in the last minute of the current day. It's values may     * be as follows:     *      * Value Meaning ----- ------- 0 no warning 1 last minute has 61 seconds 2     * last minute has 59 seconds) 3 alarm condition (clock not synchronized)     */    public byte leapIndicator = 0;    /**     * This value indicates the NTP/SNTP version number. The version number is 3     * for Version 3 (IPv4 only) and 4 for Version 4 (IPv4, IPv6 and OSI). If     * necessary to distinguish between IPv4, IPv6 and OSI, the encapsulating     * context must be inspected.     */    public byte version = 3;    /**     * This value indicates the mode, with values defined as follows:     *      * Mode Meaning ---- ------- 0 reserved 1 symmetric active 2 symmetric     * passive 3 client 4 server 5 broadcast 6 reserved for NTP control message     * 7 reserved for private use     *      * In unicast and anycast modes, the client sets this field to 3 (client) in     * the request and the server sets it to 4 (server) in the reply. In     * multicast mode, the server sets this field to 5 (broadcast).     */    public byte mode = 0;    /**     * This value indicates the stratum level of the local clock, with values     * defined as follows:     *      * Stratum Meaning ---------------------------------------------- 0     * unspecified or unavailable 1 primary reference (e.g., radio clock) 2-15     * secondary reference (via NTP or SNTP) 16-255 reserved     */    public short stratum = 0;    /**     * This value indicates the maximum interval between successive messages, in     * seconds to the nearest power of two. The values that can appear in this     * field presently range from 4 (16 s) to 14 (16284 s); however, most     * applications use only the sub-range 6 (64 s) to 10 (1024 s).     */    public byte pollInterval = 0;    /**     * This value indicates the precision of the local clock, in seconds to the     * nearest power of two. The values that normally appear in this field\     * range from -6 for mains-frequency clocks to -20 for microsecond clocks     * found in some workstations.     */    public byte precision = 0;    /**     * This value indicates the total roundtrip delay to the primary reference     * source, in seconds. Note that this variable can take on both positive and     * negative values, depending on the relative time and frequency offsets.     * The values that normally appear in this field range from negative values     * of a few milliseconds to positive values of several hundred milliseconds.     */    public double rootDelay = 0;    /**     * This value indicates the nominal error relative to the primary reference     * source, in seconds. The values that normally appear in this field range     * from 0 to several hundred milliseconds.     */    public double rootDispersion = 0;    /**     * This is a 4-byte array identifying the particular reference source. In     * the case of NTP Version 3 or Version 4 stratum-0 (unspecified) or     * stratum-1 (primary) servers, this is a four-character ASCII string, left     * justified and zero padded to 32 bits. In NTP Version 3 secondary servers,     * this is the 32-bit IPv4 address of the reference source. In NTP Version 4     * secondary servers, this is the low order 32 bits of the latest transmit     * timestamp of the reference source. NTP primary (stratum 1) servers should     * set this field to a code identifying the external reference source     * according to the following list. If the external reference is one of     * those listed, the associated code should be used. Codes for sources not     * listed can be contrived as appropriate.     *      * Code External Reference Source ---- ------------------------- LOCL     * uncalibrated local clock used as a primary reference for a subnet without     * external means of synchronization PPS atomic clock or other     * pulse-per-second source individually calibrated to national standards     * ACTS NIST dialup modem service USNO USNO modem service PTB PTB (Germany)     * modem service TDF Allouis (France) Radio 164 kHz DCF Mainflingen     * (Germany) Radio 77.5 kHz MSF Rugby (UK) Radio 60 kHz WWV Ft. Collins (US)     * Radio 2.5, 5, 10, 15, 20 MHz WWVB Boulder (US) Radio 60 kHz WWVH Kaui     * Hawaii (US) Radio 2.5, 5, 10, 15 MHz CHU Ottawa (Canada) Radio 3330,     * 7335, 14670 kHz LORC LORAN-C radionavigation system OMEG OMEGA     * radionavigation system GPS Global Positioning Service GOES Geostationary     * Orbit Environment Satellite     */    public byte[] referenceIdentifier = { 0, 0, 0, 0 };    /**     * This is the time at which the local clock was last set or corrected, in     * seconds since 00:00 1-Jan-1900.     */    public double referenceTimestamp = 0;    /**     * This is the time at which the request departed the client for the server,     * in seconds since 00:00 1-Jan-1900.     */    public double originateTimestamp = 0;    /**     * This is the time at which the request arrived at the server, in seconds     * since 00:00 1-Jan-1900.     */    public double receiveTimestamp = 0;    /**     * This is the time at which the reply departed the server for the client,     * in seconds since 00:00 1-Jan-1900.     */    public double transmitTimestamp = 0;    /**     * Constructs a new NtpMessage from an array of bytes.     */    public NtpMessage(byte[] array) {        // See the packet format diagram in RFC 2030 for details        leapIndicator = (byte) ((array[0] >> 6) & 0x3);        version = (byte) ((array[0] >> 3) & 0x7);        mode = (byte) (array[0] & 0x7);        stratum = unsignedByteToShort(array[1]);        pollInterval = array[2];        precision = array[3];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?