ntpv3impl.java

来自「apache推出的net包」· Java 代码 · 共 584 行 · 第 1/2 页

JAVA
584
字号
     *     * @return the reference id string.     */    public String getReferenceIdString()    {        int version = getVersion();        int stratum = getStratum();        if (version == VERSION_3 || version == VERSION_4) {            if (stratum == 0 || stratum == 1) {                return idAsString(); // 4-character ASCII string (e.g. GPS, USNO)            }            // in NTPv4 servers this is latest transmit timestamp of ref source            if (version == VERSION_4)                return idAsHex();        }        // Stratum 2 and higher this is a four-octet IPv4 address        // of the primary reference host.        if (stratum >= 2) {            return idAsIPAddress();        }        return idAsHex();    }    /***     * Returns Reference id as dotted IP address.     * @return refId as IP address string.     */    private String idAsIPAddress()    {        return ui(buf[REFERENCE_ID_INDEX]) + "." +                ui(buf[REFERENCE_ID_INDEX + 1]) + "." +                ui(buf[REFERENCE_ID_INDEX + 2]) + "." +                ui(buf[REFERENCE_ID_INDEX + 3]);    }    private String idAsString()    {        String id = "";        for (int i = 0; i <= 3; i++) {            char c = (char) buf[REFERENCE_ID_INDEX + i];            if (c == 0) break; // 0-terminated string            id = id + c;        }        return id;    }    private String idAsHex()    {        return Integer.toHexString(getReferenceId());    }    /***     * Returns the transmit timestamp as defined in RFC-1305.     *     * @return the transmit timestamp as defined in RFC-1305.     * Never returns a null object.     */    public TimeStamp getTransmitTimeStamp()    {        return getTimestamp(TRANSMIT_TIMESTAMP_INDEX);    }    /***     * Set transmit time with NTP timestamp.     * If <code>ts</code> is null then zero time is used.     *     * @param ts NTP timestamp     */    public void setTransmitTime(TimeStamp ts)    {        setTimestamp(TRANSMIT_TIMESTAMP_INDEX, ts);    }    /***     * Set originate timestamp given NTP TimeStamp object.     * If <code>ts</code> is null then zero time is used.     *     * @param ts NTP timestamp     */    public void setOriginateTimeStamp(TimeStamp ts)    {        setTimestamp(ORIGINATE_TIMESTAMP_INDEX, ts);    }    /***     * Returns the originate time as defined in RFC-1305.     *     * @return the originate time.     * Never returns null.     */    public TimeStamp getOriginateTimeStamp()    {        return getTimestamp(ORIGINATE_TIMESTAMP_INDEX);    }    /***     * Returns the reference time as defined in RFC-1305.     *     * @return the reference time as <code>TimeStamp</code> object.     * Never returns null.     */    public TimeStamp getReferenceTimeStamp()    {        return getTimestamp(REFERENCE_TIMESTAMP_INDEX);    }    /***     * Set Reference time with NTP timestamp. If <code>ts</code> is null     * then zero time is used.     *     * @param ts NTP timestamp     */    public void setReferenceTime(TimeStamp ts)    {        setTimestamp(REFERENCE_TIMESTAMP_INDEX, ts);    }    /***     * Returns receive timestamp as defined in RFC-1305.     *     * @return the receive time.     * Never returns null.     */    public TimeStamp getReceiveTimeStamp()    {        return getTimestamp(RECEIVE_TIMESTAMP_INDEX);    }    /***     * Set receive timestamp given NTP TimeStamp object.     * If <code>ts</code> is null then zero time is used.     *     * @param ts timestamp     */    public void setReceiveTimeStamp(TimeStamp ts)    {        setTimestamp(RECEIVE_TIMESTAMP_INDEX, ts);    }    /***     * Return type of time packet. The values (e.g. NTP, TIME, ICMP, ...)     * correspond to the protocol used to obtain the timing information.     *     * @return packet type string identifier which in this case is "NTP".     */    public String getType()    {        return "NTP";    }    /***     * @return 4 bytes as 32-bit int     */    private int getInt(int index)    {        int i = ui(buf[index]) << 24 |                ui(buf[index + 1]) << 16 |                ui(buf[index + 2]) << 8 |                ui(buf[index + 3]);        return i;    }    /***     * Get NTP Timestamp at specified starting index.     *     * @param index index into data array     * @return TimeStamp object for 64 bits starting at index     */    private TimeStamp getTimestamp(int index)    {        return new TimeStamp(getLong(index));    }    /***     * Get Long value represented by bits starting at specified index.     *     * @return 8 bytes as 64-bit long     */    private long getLong(int index)    {        long i = ul(buf[index]) << 56 |                ul(buf[index + 1]) << 48 |                ul(buf[index + 2]) << 40 |                ul(buf[index + 3]) << 32 |                ul(buf[index + 4]) << 24 |                ul(buf[index + 5]) << 16 |                ul(buf[index + 6]) << 8 |                ul(buf[index + 7]);        return i;    }    /***     * Sets the NTP timestamp at the given array index.     *     * @param index index into the byte array.     * @param t TimeStamp.     */    private void setTimestamp(int index, TimeStamp t)    {        long ntpTime = (t == null) ? 0 : t.ntpValue();        // copy 64-bits from Long value into 8 x 8-bit bytes of array        // one byte at a time shifting 8-bits for each position.        for (int i = 7; i >= 0; i--) {            buf[index + i] = (byte) (ntpTime & 0xFF);            ntpTime >>>= 8; // shift to next byte        }        // buf[index] |= 0x80;  // only set if 1900 baseline....    }    /***     * Returns the datagram packet with the NTP details already filled in.     *     * @return a datagram packet.     */    public DatagramPacket getDatagramPacket()    {        if (dp == null)            synchronized(this) {                if (dp == null) {                    dp = new DatagramPacket(buf, buf.length);                    dp.setPort(NTP_PORT);                }            }        return dp;    }    /***     * Set the contents of this object from source datagram packet.     *     * @param srcDp source DatagramPacket to copy contents from.     */    public void setDatagramPacket(DatagramPacket srcDp)    {        byte[] incomingBuf = srcDp.getData();        int len = srcDp.getLength();        if (len > buf.length)            len = buf.length;        System.arraycopy(incomingBuf, 0, buf, 0, len);    }    /***     * Convert byte to unsigned integer.     * Java only has signed types so we have to do     * more work to get unsigned ops.     *     * @param b     * @return unsigned int value of byte     */    protected final static int ui(byte b)    {        int i = b & 0xFF;        return i;    }    /***     * Convert byte to unsigned long.     * Java only has signed types so we have to do     * more work to get unsigned ops     *     * @param b     * @return unsigned long value of byte     */    protected final static long ul(byte b)    {        long i = b & 0xFF;        return i;    }    /***     * Returns details of NTP packet as a string.     *     * @return details of NTP packet as a string.     */    public String toString()    {        return "[" +                "version:" + getVersion() +                ", mode:" + getMode() +                ", poll:" + getPoll() +                ", precision:" + getPrecision() +                ", delay:" + getRootDelay() +                ", dispersion(ms):" + getRootDispersionInMillisDouble() +                ", id:" + getReferenceIdString() +                ", xmitTime:" + getTransmitTimeStamp().toDateString() +                " ]";    }}

⌨️ 快捷键说明

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