org.apache.commons.net.ntp

## Class NtpV3Impl

- java.lang.Object
- - org.apache.commons.net.ntp.NtpV3Impl

- All Implemented Interfaces:
    NtpV3Packet

      public class NtpV3Impl
      extends Object
      implements NtpV3Packet

  Implements `NtpV3Packet` to convert Java objects to and from the Network Time Protocol (NTP) data message header format described in RFC-1305.

- - ### Field Summary

    - ### Fields inherited from interface org.apache.commons.net.ntp.NtpV3Packet

      `LI_ALARM_CONDITION, LI_LAST_MINUTE_HAS_59_SECONDS, LI_LAST_MINUTE_HAS_61_SECONDS, LI_NO_WARNING, MODE_BROADCAST, MODE_CLIENT, MODE_CONTROL_MESSAGE, MODE_PRIVATE, MODE_RESERVED, MODE_SERVER, MODE_SYMMETRIC_ACTIVE, MODE_SYMMETRIC_PASSIVE, NTP_MAXCLOCK, NTP_MAXPOLL, NTP_MINCLOCK, NTP_MINPOLL, NTP_PORT, TYPE_DAYTIME, TYPE_ICMP, TYPE_NTP, TYPE_TIME, VERSION_3, VERSION_4`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NtpV3Impl()` Creates a new instance of NtpV3Impl

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `equals(Object obj)` Compares this object against the specified object.
    `DatagramPacket` | `getDatagramPacket()` Gets the datagram packet with the NTP details already filled in.
    `int` | `getLeapIndicator()` Gets leap indicator as defined in RFC-1305 which is a two-bit code: 0=no warning 1=last minute has 61 seconds 2=last minute has 59 seconds 3=alarm condition (clock not synchronized)
    `int` | `getMode()` Gets the mode as defined in RFC-1305 which is a 3-bit integer whose value is indicated by the MODE_xxx parameters.
    `String` | `getModeName()` Gets the human-readable name of message mode type as described in RFC 1305.
    `TimeStamp` | `getOriginateTimeStamp()` Gets the `originate` time as defined in RFC-1305.
    `int` | `getPoll()` Gets the poll interval as defined in RFC-1305, which is an eight-bit signed integer indicating the maximum interval between successive messages, in seconds to the nearest power of two (e.g.
    `int` | `getPrecision()` Gets the precision as defined in RFC-1305 encoded as an 8-bit signed integer (seconds to the nearest power of two).
    `TimeStamp` | `getReceiveTimeStamp()` Gets the `receive` timestamp as defined in RFC-1305.
    `int` | `getReferenceId()` Gets the reference id as defined in RFC-1305, which is a 32-bit integer whose value is dependent on several criteria.
    `String` | `getReferenceIdString()` Gets the reference id string.
    `TimeStamp` | `getReferenceTimeStamp()` Gets the reference time as defined in RFC-1305.
    `int` | `getRootDelay()` Gets the root delay as defined in RFC-1305, which is the total roundtrip delay to the primary reference source, in seconds.
    `double` | `getRootDelayInMillisDouble()` Gets the root delay as defined in RFC-1305 in milliseconds, which is the total roundtrip delay to the primary reference source, in seconds.
    `int` | `getRootDispersion()` Gets the root dispersion as defined in RFC-1305.
    `long` | `getRootDispersionInMillis()` Gets the root dispersion (as defined in RFC-1305) in milliseconds.
    `double` | `getRootDispersionInMillisDouble()` Gets the root dispersion (as defined in RFC-1305) in milliseconds as double precision value.
    `int` | `getStratum()` Gets the Stratum as defined in RFC-1305, which indicates the stratum level of the local clock, with values defined as follows: 0=unspecified, 1=primary ref clock, and all others a secondary reference (via NTP).
    `TimeStamp` | `getTransmitTimeStamp()` Gets the `transmit` timestamp as defined in RFC-1305.
    `String` | `getType()` Gets the type of time packet.
    `int` | `getVersion()` Gets the NTP version number as defined in RFC-1305.
    `int` | `hashCode()` Computes a hash code for this object.
    `void` | `setDatagramPacket(DatagramPacket srcDp)` Sets the contents of this object from source datagram packet.
    `void` | `setLeapIndicator(int li)` Sets leap indicator as defined in RFC-1305.
    `void` | `setMode(int mode)` Sets mode as defined in RFC-1305.
    `void` | `setOriginateTimeStamp(TimeStamp ts)` Sets originate timestamp given NTP TimeStamp object.
    `void` | `setPoll(int poll)` Sets poll interval as defined in RFC-1305.
    `void` | `setPrecision(int precision)` Sets precision as defined in RFC-1305.
    `void` | `setReceiveTimeStamp(TimeStamp ts)` Sets receive timestamp given NTP TimeStamp object.
    `void` | `setReferenceId(int refId)` Sets reference clock identifier field with 32-bit unsigned integer value.
    `void` | `setReferenceTime(TimeStamp ts)` Sets Reference time with NTP timestamp.
    `void` | `setRootDelay(int delay)` Sets root delay as defined in RFC-1305.
    `void` | `setRootDispersion(int dispersion)` Sets root dispersion as defined in RFC-1305.
    `void` | `setStratum(int stratum)` Sets stratum level as defined in RFC-1305.
    `void` | `setTransmitTime(TimeStamp ts)` Sets transmit time with NTP timestamp.
    `void` | `setVersion(int version)` Sets NTP version as defined in RFC-1305.
    `String` | `toString()` Returns details of NTP packet as a string.
    `protected static int` | `ui(byte b)` Convert byte to unsigned integer.
    `protected static long` | `ul(byte b)` Convert byte to unsigned long.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### NtpV3Impl

          public NtpV3Impl()

      Creates a new instance of NtpV3Impl

  - ### Method Detail

    - #### equals

          public boolean equals(Object obj)

      Compares this object against the specified object. The result is `true` if and only if the argument is not `null` and is a `NtpV3Impl` object that contains the same values as this object.
      Overrides:
        `equals` in class `Object`
      Parameters:
        `obj` - the object to compare with.
      Returns:
        `true` if the objects are the same; `false` otherwise.

    - #### getDatagramPacket

          public DatagramPacket getDatagramPacket()

      Gets the datagram packet with the NTP details already filled in.
      Specified by:
        `getDatagramPacket` in interface `NtpV3Packet`
      Returns:
        a datagram packet.

    - #### getLeapIndicator

          public int getLeapIndicator()

      Gets leap indicator as defined in RFC-1305 which is a two-bit code: 0=no warning 1=last minute has 61 seconds 2=last minute has 59 seconds 3=alarm condition (clock not synchronized)
      Specified by:
        `getLeapIndicator` in interface `NtpV3Packet`
      Returns:
        leap indicator as defined in RFC-1305.

    - #### getMode

          public int getMode()

      Gets the mode as defined in RFC-1305 which is a 3-bit integer whose value is indicated by the MODE_xxx parameters.
      Specified by:
        `getMode` in interface `NtpV3Packet`
      Returns:
        mode as defined in RFC-1305.

    - #### getModeName

          public String getModeName()

      Gets the human-readable name of message mode type as described in RFC 1305.
      Specified by:
        `getModeName` in interface `NtpV3Packet`
      Returns:
        mode name as string.

    - #### getOriginateTimeStamp

          public TimeStamp getOriginateTimeStamp()

      Gets the `originate` time as defined in RFC-1305.
      Specified by:
        `getOriginateTimeStamp` in interface `NtpV3Packet`
      Returns:
        the `originate` time. Never returns null.

    - #### getPoll

          public int getPoll()

      Gets the poll interval as defined in RFC-1305, which is an eight-bit signed integer indicating the maximum interval between successive messages, in seconds to the nearest power of two (e.g. value of six indicates an interval of 64 seconds). The values that can appear in this field range from NTP_MINPOLL to NTP_MAXPOLL inclusive.
      Specified by:
        `getPoll` in interface `NtpV3Packet`
      Returns:
        poll interval as defined in RFC-1305.

    - #### getPrecision

          public int getPrecision()

      Gets the precision as defined in RFC-1305 encoded as an 8-bit signed integer (seconds to the nearest power of two). Values normally range from -6 to -20.
      Specified by:
        `getPrecision` in interface `NtpV3Packet`
      Returns:
        precision as defined in RFC-1305.

    - #### getReceiveTimeStamp

          public TimeStamp getReceiveTimeStamp()

      Gets the `receive` timestamp as defined in RFC-1305.
      Specified by:
        `getReceiveTimeStamp` in interface `NtpV3Packet`
      Returns:
        the `receive` time. Never returns null.

    - #### getReferenceId

          public int getReferenceId()

      Gets the reference id as defined in RFC-1305, which is a 32-bit integer whose value is dependent on several criteria.
      Specified by:
        `getReferenceId` in interface `NtpV3Packet`
      Returns:
        the reference id as defined in RFC-1305.

    - #### getReferenceIdString

          public String getReferenceIdString()

      Gets the reference id string. String cannot be null but value is dependent on the version of the NTP spec supported and stratum level. Value can be an empty string, clock type string, IP address, or a hexadecimal string.
      Specified by:
        `getReferenceIdString` in interface `NtpV3Packet`
      Returns:
        the reference id string.

    - #### getReferenceTimeStamp

          public TimeStamp getReferenceTimeStamp()

      Gets the reference time as defined in RFC-1305.
      Specified by:
        `getReferenceTimeStamp` in interface `NtpV3Packet`
      Returns:
        the reference time as `TimeStamp` object. Never returns null.

    - #### getRootDelay

          public int getRootDelay()

      Gets the root delay as defined in RFC-1305, which is the total roundtrip delay to the primary reference source, in seconds. Values can take positive and negative values, depending on clock precision and skew.
      Specified by:
        `getRootDelay` in interface `NtpV3Packet`
      Returns:
        root delay as defined in RFC-1305.

    - #### getRootDelayInMillisDouble

          public double getRootDelayInMillisDouble()

      Gets the root delay as defined in RFC-1305 in milliseconds, which is the total roundtrip delay to the primary reference source, in seconds. Values can take positive and negative values, depending on clock precision and skew.
      Specified by:
        `getRootDelayInMillisDouble` in interface `NtpV3Packet`
      Returns:
        root delay in milliseconds

    - #### getRootDispersion

          public int getRootDispersion()

      Gets the root dispersion as defined in RFC-1305.
      Specified by:
        `getRootDispersion` in interface `NtpV3Packet`
      Returns:
        root dispersion.

    - #### getRootDispersionInMillis

          public long getRootDispersionInMillis()

      Gets the root dispersion (as defined in RFC-1305) in milliseconds.
      Specified by:
        `getRootDispersionInMillis` in interface `NtpV3Packet`
      Returns:
        root dispersion in milliseconds

    - #### getRootDispersionInMillisDouble

          public double getRootDispersionInMillisDouble()

      Gets the root dispersion (as defined in RFC-1305) in milliseconds as double precision value.
      Specified by:
        `getRootDispersionInMillisDouble` in interface `NtpV3Packet`
      Returns:
        root dispersion in milliseconds

    - #### getStratum

          public int getStratum()

      Gets the Stratum as defined in RFC-1305, which indicates the stratum level of the local clock, with values defined as follows: 0=unspecified, 1=primary ref clock, and all others a secondary reference (via NTP).
      Specified by:
        `getStratum` in interface `NtpV3Packet`
      Returns:
        Stratum level as defined in RFC-1305.

    - #### getTransmitTimeStamp

          public TimeStamp getTransmitTimeStamp()

      Gets the `transmit` timestamp as defined in RFC-1305.
      Specified by:
        `getTransmitTimeStamp` in interface `NtpV3Packet`
      Returns:
        the `transmit` timestamp as defined in RFC-1305. Never returns a null object.

    - #### getType

          public String getType()

      Gets the type of time packet. The values (e.g. NTP, TIME, ICMP, ...) correspond to the protocol used to obtain the timing information.
      Specified by:
        `getType` in interface `NtpV3Packet`
      Returns:
        packet type string identifier which in this case is "NTP".

    - #### getVersion

          public int getVersion()

      Gets the NTP version number as defined in RFC-1305.
      Specified by:
        `getVersion` in interface `NtpV3Packet`
      Returns:
        NTP version number.

    - #### hashCode

          public int hashCode()

      Computes a hash code for this object. The result is the exclusive OR of the values of this object stored as a byte array.
      Overrides:
        `hashCode` in class `Object`
      Returns:
        a hash code value for this object.

    - #### setDatagramPacket

          public void setDatagramPacket(DatagramPacket srcDp)

      Sets the contents of this object from source datagram packet.
      Specified by:
        `setDatagramPacket` in interface `NtpV3Packet`
      Parameters:
        `srcDp` - source DatagramPacket to copy contents from, never null.
      Throws:
        `IllegalArgumentException` - if srcDp is null or byte length is less than minimum length of 48 bytes

    - #### setLeapIndicator

          public void setLeapIndicator(int li)

      Sets leap indicator as defined in RFC-1305.
      Specified by:
        `setLeapIndicator` in interface `NtpV3Packet`
      Parameters:
        `li` - leap indicator.

    - #### setMode

          public void setMode(int mode)

      Sets mode as defined in RFC-1305.
      Specified by:
        `setMode` in interface `NtpV3Packet`
      Parameters:
        `mode` - the mode to set

    - #### setOriginateTimeStamp

          public void setOriginateTimeStamp(TimeStamp ts)

      Sets originate timestamp given NTP TimeStamp object. If `ts` is null then zero time is used.
      Specified by:
        `setOriginateTimeStamp` in interface `NtpV3Packet`
      Parameters:
        `ts` - NTP timestamp

    - #### setPoll

          public void setPoll(int poll)

      Sets poll interval as defined in RFC-1305.
      Specified by:
        `setPoll` in interface `NtpV3Packet`
      Parameters:
        `poll` - poll interval.

    - #### setPrecision

          public void setPrecision(int precision)

      Sets precision as defined in RFC-1305.
      Specified by:
        `setPrecision` in interface `NtpV3Packet`
      Parameters:
        `precision` - the precision to set

    - #### setReceiveTimeStamp

          public void setReceiveTimeStamp(TimeStamp ts)

      Sets receive timestamp given NTP TimeStamp object. If `ts` is null then zero time is used.
      Specified by:
        `setReceiveTimeStamp` in interface `NtpV3Packet`
      Parameters:
        `ts` - timestamp

    - #### setReferenceId

          public void setReferenceId(int refId)

      Sets reference clock identifier field with 32-bit unsigned integer value. See RFC-1305 for description.
      Specified by:
        `setReferenceId` in interface `NtpV3Packet`
      Parameters:
        `refId` - reference clock identifier.

    - #### setReferenceTime

          public void setReferenceTime(TimeStamp ts)

      Sets Reference time with NTP timestamp. If `ts` is null then zero time is used.
      Specified by:
        `setReferenceTime` in interface `NtpV3Packet`
      Parameters:
        `ts` - NTP timestamp

    - #### setRootDelay

          public void setRootDelay(int delay)

      Sets root delay as defined in RFC-1305.
      Specified by:
        `setRootDelay` in interface `NtpV3Packet`
      Parameters:
        `delay` - root delay

    - #### setRootDispersion

          public void setRootDispersion(int dispersion)

      Sets root dispersion as defined in RFC-1305.
      Specified by:
        `setRootDispersion` in interface `NtpV3Packet`
      Parameters:
        `dispersion` - root dispersion

    - #### setStratum

          public void setStratum(int stratum)

      Sets stratum level as defined in RFC-1305.
      Specified by:
        `setStratum` in interface `NtpV3Packet`
      Parameters:
        `stratum` - stratum level.

    - #### setTransmitTime

          public void setTransmitTime(TimeStamp ts)

      Sets transmit time with NTP timestamp. If `ts` is null then zero time is used.
      Specified by:
        `setTransmitTime` in interface `NtpV3Packet`
      Parameters:
        `ts` - NTP timestamp

    - #### setVersion

          public void setVersion(int version)

      Sets NTP version as defined in RFC-1305.
      Specified by:
        `setVersion` in interface `NtpV3Packet`
      Parameters:
        `version` - NTP version.

    - #### toString

          public String toString()

      Returns details of NTP packet as a string.
      Overrides:
        `toString` in class `Object`
      Returns:
        details of NTP packet as a string.

    - #### ui

          protected static final int ui(byte b)

      Convert byte to unsigned integer. Java only has signed types, so we have to do more work to get unsigned ops.
      Parameters:
        `b` - input byte
      Returns:
        unsigned int value of byte

    - #### ul

          protected static final long ul(byte b)

      Convert byte to unsigned long. Java only has signed types, so we have to do more work to get unsigned ops
      Parameters:
        `b` - input byte
      Returns:
        unsigned long value of byte