NtpV3Impl1
湛骏祥
2023-12-01
package org.apache.commons.net.ntp;
import java.net.DatagramPacket;
// Referenced classes of package org.apache.commons.net.ntp:
// TimeStamp, NtpV3Packet, NtpUtils
public class NtpV3Impl
implements NtpV3Packet
{
public NtpV3Impl()
{
buf = new byte[48];
}
public int getMode()
{
return ui(buf[0]) >> 0 & 7;
}
public String getModeName()
{
return NtpUtils.getModeName(getMode());
}
public void setMode(int mode)
{
buf[0] = (byte)(buf[0] & 248 | mode & 7);
}
public int getLeapIndicator()
{
return ui(buf[0]) >> 6 & 3;
}
public void setLeapIndicator(int li)
{
buf[0] = (byte)(buf[0] & 63 | (li & 3) << 6);
}
public int getPoll()
{
return buf[2];
}
public void setPoll(int poll)
{
buf[2] = (byte)(poll & 255);
}
public int getPrecision()
{
return buf[3];
}
public void setPrecision(int precision)
{
buf[3] = (byte)(precision & 255);
}
public int getVersion()
{
return ui(buf[0]) >> 3 & 7;
}
public void setVersion(int version)
{
buf[0] = (byte)(buf[0] & 199 | (version & 7) << 3);
}
public int getStratum()
{
return ui(buf[1]);
}
public void setStratum(int stratum)
{
buf[1] = (byte)(stratum & 255);
}
public int getRootDelay()
{
return getInt(4);
}
public double getRootDelayInMillisDouble()
{
double l = getRootDelay();
return l / 65.536000000000001D;
}
public int getRootDispersion()
{
return getInt(8);
}
public long getRootDispersionInMillis()
{
long l = getRootDispersion();
return (l * 1000L) / 65536L;
}
public double getRootDispersionInMillisDouble()
{
double l = getRootDispersion();
return l / 65.536000000000001D;
}
public void setReferenceId(int refId)
{
for(int i = 3; i >= 0; i--)
{
buf[12 + i] = (byte)(refId & 255);
refId >>>= 8;
}
}
public int getReferenceId()
{
return getInt(12);
}
public String getReferenceIdString()
{
int version = getVersion();
int stratum = getStratum();
if(version == 3 || version == 4)
{
if(stratum == 0 || stratum == 1)
return idAsString();
if(version == 4)
return idAsHex();
}
if(stratum >= 2)
return idAsIPAddress();
else
return idAsHex();
}
private String idAsIPAddress()
{
return ui(buf[12]) + "." + ui(buf[13]) + "." + ui(buf[14]) + "." + ui(buf[15]);
}