org.apache.commons.net.ntp

Class NtpV3Impl

Implemented Interfaces:
NtpV3Packet

public class NtpV3Impl
extends java.lang.Object
implements NtpV3Packet

Implementation of NtpV3Packet with methods converting Java objects to/from the Network Time Protocol (NTP) data message header format described in RFC-1305.

Constructor Summary

NtpV3Impl()
Creates a new instance of NtpV3Impl

Method Summary

@Override
String toString()
Returns details of NTP packet as a string.
DatagramPacket
getDatagramPacket()
Returns the datagram packet with the NTP details already filled in.
int
getLeapIndicator()
Returns 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()
Returns mode as defined in RFC-1305 which is a 3-bit integer whose value is indicated by the MODE_xxx parameters.
String
getModeName()
Return human-readable name of message mode type as described in RFC 1305.
TimeStamp
getOriginateTimeStamp()
Returns the originate time as defined in RFC-1305.
int
getPoll()
Returns 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()
Returns precision as defined in RFC-1305 encoded as an 8-bit signed integer (seconds to nearest power of two).
TimeStamp
getReceiveTimeStamp()
Returns receive timestamp as defined in RFC-1305.
int
getReferenceId()
Returns the reference id as defined in RFC-1305, which is a 32-bit integer whose value is dependent on several criteria.
String
getReferenceIdString()
Returns the reference id string.
TimeStamp
getReferenceTimeStamp()
Returns the reference time as defined in RFC-1305.
int
getRootDelay()
Return root delay as defined in RFC-1305, which is the total roundtrip delay to the primary reference source, in seconds.
double
getRootDelayInMillisDouble()
Return root delay as defined in RFC-1305 in milliseconds, which is the total roundtrip delay to the primary reference source, in seconds.
int
getRootDispersion()
Returns root dispersion as defined in RFC-1305.
long
getRootDispersionInMillis()
Returns root dispersion (as defined in RFC-1305) in milliseconds.
double
getRootDispersionInMillisDouble()
Returns root dispersion (as defined in RFC-1305) in milliseconds as double precision value.
int
getStratum()
Returns 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()
Returns the transmit timestamp as defined in RFC-1305.
String
getType()
Return type of time packet.
int
getVersion()
Returns NTP version number as defined in RFC-1305.
void
setDatagramPacket(DatagramPacket srcDp)
Set the contents of this object from source datagram packet.
void
setLeapIndicator(int li)
Set leap indicator as defined in RFC-1305.
void
setMode(int mode)
Set mode as defined in RFC-1305.
void
setOriginateTimeStamp(TimeStamp ts)
Set originate timestamp given NTP TimeStamp object.
void
setPoll(int poll)
Set poll interval as defined in RFC-1305.
void
setPrecision(int precision)
Set precision as defined in RFC-1305.
void
setReceiveTimeStamp(TimeStamp ts)
Set receive timestamp given NTP TimeStamp object.
void
setReferenceId(int refId)
Set reference clock identifier field with 32-bit unsigned integer value.
void
setReferenceTime(TimeStamp ts)
Set Reference time with NTP timestamp.
void
setStratum(int stratum)
Set stratum level as defined in RFC-1305.
void
setTransmitTime(TimeStamp ts)
Set transmit time with NTP timestamp.
void
setVersion(int version)
Set NTP version as defined in RFC-1305.
protected static int
ui(byte b)
Convert byte to unsigned integer.
protected static long
ul(byte b)
Convert byte to unsigned long.

Constructor Details

NtpV3Impl

public NtpV3Impl()
Creates a new instance of NtpV3Impl

Method Details

String toString

public @Override String toString()
Returns details of NTP packet as a string.
Returns:
details of NTP packet as a string.

getDatagramPacket

public DatagramPacket getDatagramPacket()
Returns the datagram packet with the NTP details already filled in.
Returns:
a datagram packet.

getLeapIndicator

public int getLeapIndicator()
Returns 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)
Returns:
leap indicator as defined in RFC-1305.

getMode

public int getMode()
Returns mode as defined in RFC-1305 which is a 3-bit integer whose value is indicated by the MODE_xxx parameters.
Returns:
mode as defined in RFC-1305.

getModeName

public String getModeName()
Return human-readable name of message mode type as described in RFC 1305.
Returns:
mode name as string.

getOriginateTimeStamp

public TimeStamp getOriginateTimeStamp()
Returns the originate time as defined in RFC-1305.
Returns:
the originate time. Never returns null.

getPoll

public int getPoll()
Returns 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.
Returns:
poll interval as defined in RFC-1305.

getPrecision

public int getPrecision()
Returns precision as defined in RFC-1305 encoded as an 8-bit signed integer (seconds to nearest power of two). Values normally range from -6 to -20.
Returns:
precision as defined in RFC-1305.

getReceiveTimeStamp

public TimeStamp getReceiveTimeStamp()
Returns receive timestamp as defined in RFC-1305.
Returns:
the receive time. Never returns null.

getReferenceId

public int getReferenceId()
Returns the reference id as defined in RFC-1305, which is a 32-bit integer whose value is dependent on several criteria.
Returns:
the reference id as defined in RFC-1305.

getReferenceIdString

public String getReferenceIdString()
Returns 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 hex string.
Returns:
the reference id string.

getReferenceTimeStamp

public TimeStamp getReferenceTimeStamp()
Returns the reference time as defined in RFC-1305.
Returns:
the reference time as TimeStamp object. Never returns null.

getRootDelay

public int getRootDelay()
Return 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.
Returns:
root delay as defined in RFC-1305.

getRootDelayInMillisDouble

public double getRootDelayInMillisDouble()
Return 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.
Returns:
root delay in milliseconds

getRootDispersion

public int getRootDispersion()
Returns root dispersion as defined in RFC-1305.
Returns:
root dispersion.

getRootDispersionInMillis

public long getRootDispersionInMillis()
Returns root dispersion (as defined in RFC-1305) in milliseconds.
Returns:
root dispersion in milliseconds

getRootDispersionInMillisDouble

public double getRootDispersionInMillisDouble()
Returns root dispersion (as defined in RFC-1305) in milliseconds as double precision value.
Returns:
root dispersion in milliseconds

getStratum

public int getStratum()
Returns 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).
Returns:
Stratum level as defined in RFC-1305.

getTransmitTimeStamp

public TimeStamp getTransmitTimeStamp()
Returns the transmit timestamp as defined in RFC-1305.
Returns:
the transmit timestamp as defined in RFC-1305. Never returns a null object.

getType

public String getType()
Return type of time packet. The values (e.g. NTP, TIME, ICMP, ...) correspond to the protocol used to obtain the timing information.
Returns:
packet type string identifier which in this case is "NTP".

getVersion

public int getVersion()
Returns NTP version number as defined in RFC-1305.
Returns:
NTP version number.

setDatagramPacket

public void setDatagramPacket(DatagramPacket srcDp)
Set the contents of this object from source datagram packet.
Parameters:
srcDp - source DatagramPacket to copy contents from.

setLeapIndicator

public void setLeapIndicator(int li)
Set leap indicator as defined in RFC-1305.
Parameters:
li - leap indicator.

setMode

public void setMode(int mode)
Set mode as defined in RFC-1305.
Parameters:
mode -

setOriginateTimeStamp

public void setOriginateTimeStamp(TimeStamp ts)
Set originate timestamp given NTP TimeStamp object. If ts is null then zero time is used.
Parameters:
ts - NTP timestamp

setPoll

public void setPoll(int poll)
Set poll interval as defined in RFC-1305.
Parameters:
poll - poll interval.

setPrecision

public void setPrecision(int precision)
Set precision as defined in RFC-1305.
Parameters:
precision -

setReceiveTimeStamp

public void setReceiveTimeStamp(TimeStamp ts)
Set receive timestamp given NTP TimeStamp object. If ts is null then zero time is used.
Parameters:
ts - timestamp

setReferenceId

public void setReferenceId(int refId)
Set reference clock identifier field with 32-bit unsigned integer value. See RFC-1305 for description.
Parameters:
refId - reference clock identifier.

setReferenceTime

public void setReferenceTime(TimeStamp ts)
Set Reference time with NTP timestamp. If ts is null then zero time is used.
Parameters:
ts - NTP timestamp

setStratum

public void setStratum(int stratum)
Set stratum level as defined in RFC-1305.
Parameters:
stratum - stratum level.

setTransmitTime

public void setTransmitTime(TimeStamp ts)
Set transmit time with NTP timestamp. If ts is null then zero time is used.
Parameters:
ts - NTP timestamp

setVersion

public void setVersion(int version)
Set NTP version as defined in RFC-1305.
Parameters:
version - NTP version.

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 -
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 -
Returns:
unsigned long value of byte