把NTP从平均数改成中位数,防止极端值干扰
This commit is contained in:
@@ -26,7 +26,7 @@ import org.kne.math.Long128;
|
||||
public class NTPContext implements Closeable, AutoCloseable {
|
||||
private HighAccuracyClock clock;
|
||||
private static final boolean debug = false;
|
||||
private static final int REQUEST_COUNT = 10;
|
||||
private static final int REQUEST_COUNT = 5;
|
||||
|
||||
private Long128 systemFrequencyOffset = NTPTimestamps.nanosToNtp128BitTimeInterval(new Long128(5000));
|
||||
private Long128 localPrecision = NTPTimestamps.nanosToNtp128BitTimeInterval(new Long128(1000));
|
||||
@@ -117,7 +117,7 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
}
|
||||
|
||||
protected void putPacket(NTPv4Packet nv4, MultiProtocolSocketAddress inetSocketAddress) {
|
||||
checkIP();
|
||||
//checkIP();
|
||||
List<NTPv4Packet> newv = new Vector<NTPv4Packet>();
|
||||
List<NTPv4Packet> oldv = recvmap.putIfAbsent(inetSocketAddress, newv);
|
||||
if (oldv == null) {
|
||||
@@ -132,25 +132,6 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkIP() {
|
||||
Set<Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>> ens = recvmap.entrySet();
|
||||
for (Iterator<Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>> iterator = ens.iterator(); iterator
|
||||
.hasNext();) {
|
||||
Entry<MultiProtocolSocketAddress, List<NTPv4Packet>> entry = (Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>) iterator
|
||||
.next();
|
||||
AtomicBoolean ab = new AtomicBoolean(false);
|
||||
ios.forEach((x) -> {
|
||||
if (!ab.get())
|
||||
if (x.findPeer(entry.getKey()) != null) {
|
||||
ab.set(true);
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (!ab.get()) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class PeerInfo implements Comparable<PeerInfo> {
|
||||
private MultiProtocolSocketAddress address;
|
||||
@@ -225,14 +206,12 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
PeerInfo pix = peerInfo.get(0);
|
||||
currentClock = pix;
|
||||
int i;
|
||||
int m = Math.min(peerInfo.size(), 3);
|
||||
Long128 bi = Long128.ZERO;
|
||||
for (i = 0; i < m; i++) {
|
||||
PeerInfo pi = peerInfo.get(i);
|
||||
Long128 adjt = pi.getAdj();
|
||||
bi = bi.add(adjt);
|
||||
List<Long128> adjts = new ArrayList<>();
|
||||
for (i = 0; i < peerInfo.size(); i++) {
|
||||
adjts.add(peerInfo.get(i).getAdj());
|
||||
}
|
||||
Long128 delta = bi.divide(Long128.valueOf(i));
|
||||
Long128 delta = NTPTimestamps. median(adjts);
|
||||
//System.out.println(adjts+":"+delta);
|
||||
avgAdj=(avgAdj*7+delta.abs().longValue())/8;
|
||||
Long128 deltaabs=delta.abs();
|
||||
if (deltaabs.compareTo(adjustThreshold0) > 0&&firstSync.compareAndSet(true, false)) {
|
||||
|
||||
Reference in New Issue
Block a user