Package org.apache.commons.net.ntp
Class NtpV3Impl
- java.lang.Object
-
- org.apache.commons.net.ntp.NtpV3Impl
-
- All Implemented Interfaces:
NtpV3Packet
public class NtpV3Impl extends java.lang.Object implements NtpV3Packet
ImplementsNtpV3Packetto convert Java objects to and from the Network Time Protocol (NTP) data message header format described in RFC-1305.
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]bufprivate java.net.DatagramPacketdpprivate static intLI_INDEXprivate static intLI_SHIFTprivate static intMODE_INDEXprivate static intMODE_SHIFTprivate static intORIGINATE_TIMESTAMP_INDEXprivate static intPOLL_INDEXprivate static intPRECISION_INDEXprivate static intRECEIVE_TIMESTAMP_INDEXprivate static intREFERENCE_ID_INDEXprivate static intREFERENCE_TIMESTAMP_INDEXprivate static intROOT_DELAY_INDEXprivate static intROOT_DISPERSION_INDEXprivate static intSTRATUM_INDEXprivate static intTRANSMIT_TIMESTAMP_INDEXprivate static intVERSION_INDEXprivate static intVERSION_SHIFT-
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 Description NtpV3Impl()Creates a new instance of NtpV3Impl
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)Compares this object against the specified object.java.net.DatagramPacketgetDatagramPacket()Gets the datagram packet with the NTP details already filled in.private intgetInt(int index)Gets 4 bytes as 32-bit int.intgetLeapIndicator()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)private longgetLong(int index)Gets Long value represented by bits starting at specified index.intgetMode()Gets the mode as defined in RFC-1305 which is a 3-bit integer whose value is indicated by the MODE_xxx parameters.java.lang.StringgetModeName()Gets the human-readable name of message mode type as described in RFC 1305.TimeStampgetOriginateTimeStamp()Gets theoriginatetime as defined in RFC-1305.intgetPoll()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.intgetPrecision()Gets the precision as defined in RFC-1305 encoded as an 8-bit signed integer (seconds to the nearest power of two).TimeStampgetReceiveTimeStamp()Gets thereceivetimestamp as defined in RFC-1305.intgetReferenceId()Gets the reference id as defined in RFC-1305, which is a 32-bit integer whose value is dependent on several criteria.java.lang.StringgetReferenceIdString()Gets the reference id string.TimeStampgetReferenceTimeStamp()Gets the reference time as defined in RFC-1305.intgetRootDelay()Gets the root delay as defined in RFC-1305, which is the total roundtrip delay to the primary reference source, in seconds.doublegetRootDelayInMillisDouble()Gets the root delay as defined in RFC-1305 in milliseconds, which is the total roundtrip delay to the primary reference source, in seconds.intgetRootDispersion()Gets the root dispersion as defined in RFC-1305.longgetRootDispersionInMillis()Gets the root dispersion (as defined in RFC-1305) in milliseconds.doublegetRootDispersionInMillisDouble()Gets the root dispersion (as defined in RFC-1305) in milliseconds as double precision value.intgetStratum()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).private TimeStampgetTimestamp(int index)Gets the NTP Timestamp at specified starting index.TimeStampgetTransmitTimeStamp()Gets thetransmittimestamp as defined in RFC-1305.java.lang.StringgetType()Gets the type of time packet.intgetVersion()Gets the NTP version number as defined in RFC-1305.inthashCode()Computes a hash code for this object.private java.lang.StringidAsHex()private java.lang.StringidAsIPAddress()Returns Reference id as dotted IP address.private java.lang.StringidAsString()voidsetDatagramPacket(java.net.DatagramPacket srcDp)Sets the contents of this object from source datagram packet.private voidsetInt(int idx, int value)Sets integer value at index position.voidsetLeapIndicator(int li)Sets leap indicator as defined in RFC-1305.voidsetMode(int mode)Sets mode as defined in RFC-1305.voidsetOriginateTimeStamp(TimeStamp ts)Sets originate timestamp given NTP TimeStamp object.voidsetPoll(int poll)Sets poll interval as defined in RFC-1305.voidsetPrecision(int precision)Sets precision as defined in RFC-1305.voidsetReceiveTimeStamp(TimeStamp ts)Sets receive timestamp given NTP TimeStamp object.voidsetReferenceId(int refId)Sets reference clock identifier field with 32-bit unsigned integer value.voidsetReferenceTime(TimeStamp ts)Sets Reference time with NTP timestamp.voidsetRootDelay(int delay)Sets root delay as defined in RFC-1305.voidsetRootDispersion(int dispersion)Sets root dispersion as defined in RFC-1305.voidsetStratum(int stratum)Sets stratum level as defined in RFC-1305.private voidsetTimestamp(int index, TimeStamp t)Sets the NTP timestamp at the given array index.voidsetTransmitTime(TimeStamp ts)Sets transmit time with NTP timestamp.voidsetVersion(int version)Sets NTP version as defined in RFC-1305.java.lang.StringtoString()Returns details of NTP packet as a string.protected static intui(byte b)Convert byte to unsigned integer.protected static longul(byte b)Convert byte to unsigned long.
-
-
-
Field Detail
-
MODE_INDEX
private static final int MODE_INDEX
- See Also:
- Constant Field Values
-
MODE_SHIFT
private static final int MODE_SHIFT
- See Also:
- Constant Field Values
-
VERSION_INDEX
private static final int VERSION_INDEX
- See Also:
- Constant Field Values
-
VERSION_SHIFT
private static final int VERSION_SHIFT
- See Also:
- Constant Field Values
-
LI_INDEX
private static final int LI_INDEX
- See Also:
- Constant Field Values
-
LI_SHIFT
private static final int LI_SHIFT
- See Also:
- Constant Field Values
-
STRATUM_INDEX
private static final int STRATUM_INDEX
- See Also:
- Constant Field Values
-
POLL_INDEX
private static final int POLL_INDEX
- See Also:
- Constant Field Values
-
PRECISION_INDEX
private static final int PRECISION_INDEX
- See Also:
- Constant Field Values
-
ROOT_DELAY_INDEX
private static final int ROOT_DELAY_INDEX
- See Also:
- Constant Field Values
-
ROOT_DISPERSION_INDEX
private static final int ROOT_DISPERSION_INDEX
- See Also:
- Constant Field Values
-
REFERENCE_ID_INDEX
private static final int REFERENCE_ID_INDEX
- See Also:
- Constant Field Values
-
REFERENCE_TIMESTAMP_INDEX
private static final int REFERENCE_TIMESTAMP_INDEX
- See Also:
- Constant Field Values
-
ORIGINATE_TIMESTAMP_INDEX
private static final int ORIGINATE_TIMESTAMP_INDEX
- See Also:
- Constant Field Values
-
RECEIVE_TIMESTAMP_INDEX
private static final int RECEIVE_TIMESTAMP_INDEX
- See Also:
- Constant Field Values
-
TRANSMIT_TIMESTAMP_INDEX
private static final int TRANSMIT_TIMESTAMP_INDEX
- See Also:
- Constant Field Values
-
buf
private final byte[] buf
-
dp
private volatile java.net.DatagramPacket dp
-
-
Method Detail
-
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
-
equals
public boolean equals(java.lang.Object obj)
Compares this object against the specified object. The result istrueif and only if the argument is notnulland is aNtpV3Implobject that contains the same values as this object.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to compare with.- Returns:
trueif the objects are the same;falseotherwise.- Since:
- 3.4
-
getDatagramPacket
public java.net.DatagramPacket getDatagramPacket()
Gets the datagram packet with the NTP details already filled in.- Specified by:
getDatagramPacketin interfaceNtpV3Packet- Returns:
- a datagram packet.
-
getInt
private int getInt(int index)
Gets 4 bytes as 32-bit int.- Returns:
- 4 bytes as 32-bit int
-
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:
getLeapIndicatorin interfaceNtpV3Packet- Returns:
- leap indicator as defined in RFC-1305.
-
getLong
private long getLong(int index)
Gets Long value represented by bits starting at specified index.- Returns:
- 8 bytes as 64-bit long
-
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:
getModein interfaceNtpV3Packet- Returns:
- mode as defined in RFC-1305.
-
getModeName
public java.lang.String getModeName()
Gets the human-readable name of message mode type as described in RFC 1305.- Specified by:
getModeNamein interfaceNtpV3Packet- Returns:
- mode name as string.
-
getOriginateTimeStamp
public TimeStamp getOriginateTimeStamp()
Gets theoriginatetime as defined in RFC-1305.- Specified by:
getOriginateTimeStampin interfaceNtpV3Packet- Returns:
- the
originatetime. 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:
getPollin interfaceNtpV3Packet- 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:
getPrecisionin interfaceNtpV3Packet- Returns:
- precision as defined in RFC-1305.
-
getReceiveTimeStamp
public TimeStamp getReceiveTimeStamp()
Gets thereceivetimestamp as defined in RFC-1305.- Specified by:
getReceiveTimeStampin interfaceNtpV3Packet- Returns:
- the
receivetime. 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:
getReferenceIdin interfaceNtpV3Packet- Returns:
- the reference id as defined in RFC-1305.
-
getReferenceIdString
public java.lang.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:
getReferenceIdStringin interfaceNtpV3Packet- Returns:
- the reference id string.
-
getReferenceTimeStamp
public TimeStamp getReferenceTimeStamp()
Gets the reference time as defined in RFC-1305.- Specified by:
getReferenceTimeStampin interfaceNtpV3Packet- Returns:
- the reference time as
TimeStampobject. 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:
getRootDelayin interfaceNtpV3Packet- 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:
getRootDelayInMillisDoublein interfaceNtpV3Packet- Returns:
- root delay in milliseconds
-
getRootDispersion
public int getRootDispersion()
Gets the root dispersion as defined in RFC-1305.- Specified by:
getRootDispersionin interfaceNtpV3Packet- Returns:
- root dispersion.
-
getRootDispersionInMillis
public long getRootDispersionInMillis()
Gets the root dispersion (as defined in RFC-1305) in milliseconds.- Specified by:
getRootDispersionInMillisin interfaceNtpV3Packet- 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:
getRootDispersionInMillisDoublein interfaceNtpV3Packet- 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:
getStratumin interfaceNtpV3Packet- Returns:
- Stratum level as defined in RFC-1305.
-
getTimestamp
private TimeStamp getTimestamp(int index)
Gets the NTP Timestamp at specified starting index.- Parameters:
index- index into data array- Returns:
- TimeStamp object for 64 bits starting at index
-
getTransmitTimeStamp
public TimeStamp getTransmitTimeStamp()
Gets thetransmittimestamp as defined in RFC-1305.- Specified by:
getTransmitTimeStampin interfaceNtpV3Packet- Returns:
- the
transmittimestamp as defined in RFC-1305. Never returns a null object.
-
getType
public java.lang.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:
getTypein interfaceNtpV3Packet- 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:
getVersionin interfaceNtpV3Packet- 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:
hashCodein classjava.lang.Object- Returns:
- a hash code value for this object.
- Since:
- 3.4
-
idAsHex
private java.lang.String idAsHex()
-
idAsIPAddress
private java.lang.String idAsIPAddress()
Returns Reference id as dotted IP address.- Returns:
- refId as IP address string.
-
idAsString
private java.lang.String idAsString()
-
setDatagramPacket
public void setDatagramPacket(java.net.DatagramPacket srcDp)
Sets the contents of this object from source datagram packet.- Specified by:
setDatagramPacketin interfaceNtpV3Packet- Parameters:
srcDp- source DatagramPacket to copy contents from, never null.- Throws:
java.lang.IllegalArgumentException- if srcDp is null or byte length is less than minimum length of 48 bytes
-
setInt
private void setInt(int idx, int value)Sets integer value at index position.- Parameters:
idx- index positionvalue- 32-bit int value
-
setLeapIndicator
public void setLeapIndicator(int li)
Sets leap indicator as defined in RFC-1305.- Specified by:
setLeapIndicatorin interfaceNtpV3Packet- Parameters:
li- leap indicator.
-
setMode
public void setMode(int mode)
Sets mode as defined in RFC-1305.- Specified by:
setModein interfaceNtpV3Packet- Parameters:
mode- the mode to set
-
setOriginateTimeStamp
public void setOriginateTimeStamp(TimeStamp ts)
Sets originate timestamp given NTP TimeStamp object. Iftsis null then zero time is used.- Specified by:
setOriginateTimeStampin interfaceNtpV3Packet- Parameters:
ts- NTP timestamp
-
setPoll
public void setPoll(int poll)
Sets poll interval as defined in RFC-1305.- Specified by:
setPollin interfaceNtpV3Packet- Parameters:
poll- poll interval.
-
setPrecision
public void setPrecision(int precision)
Sets precision as defined in RFC-1305.- Specified by:
setPrecisionin interfaceNtpV3Packet- Parameters:
precision- the precision to set- Since:
- 3.4
-
setReceiveTimeStamp
public void setReceiveTimeStamp(TimeStamp ts)
Sets receive timestamp given NTP TimeStamp object. Iftsis null then zero time is used.- Specified by:
setReceiveTimeStampin interfaceNtpV3Packet- 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:
setReferenceIdin interfaceNtpV3Packet- Parameters:
refId- reference clock identifier.
-
setReferenceTime
public void setReferenceTime(TimeStamp ts)
Sets Reference time with NTP timestamp. Iftsis null then zero time is used.- Specified by:
setReferenceTimein interfaceNtpV3Packet- Parameters:
ts- NTP timestamp
-
setRootDelay
public void setRootDelay(int delay)
Sets root delay as defined in RFC-1305.- Specified by:
setRootDelayin interfaceNtpV3Packet- Parameters:
delay- root delay- Since:
- 3.4
-
setRootDispersion
public void setRootDispersion(int dispersion)
Sets root dispersion as defined in RFC-1305.- Specified by:
setRootDispersionin interfaceNtpV3Packet- Parameters:
dispersion- root dispersion- Since:
- 3.4
-
setStratum
public void setStratum(int stratum)
Sets stratum level as defined in RFC-1305.- Specified by:
setStratumin interfaceNtpV3Packet- Parameters:
stratum- stratum level.
-
setTimestamp
private void setTimestamp(int index, TimeStamp t)Sets the NTP timestamp at the given array index.- Parameters:
index- index into the byte array.t- TimeStamp.
-
setTransmitTime
public void setTransmitTime(TimeStamp ts)
Sets transmit time with NTP timestamp. Iftsis null then zero time is used.- Specified by:
setTransmitTimein interfaceNtpV3Packet- Parameters:
ts- NTP timestamp
-
setVersion
public void setVersion(int version)
Sets NTP version as defined in RFC-1305.- Specified by:
setVersionin interfaceNtpV3Packet- Parameters:
version- NTP version.
-
toString
public java.lang.String toString()
Returns details of NTP packet as a string.- Overrides:
toStringin classjava.lang.Object- Returns:
- details of NTP packet as a string.
-
-