forked from KNEMC/KLALB
隧道链接改成标准URI格式
This commit is contained in:
@@ -20,7 +20,8 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.kne.cloud.clock.HighAccuracyClock;
|
||||
import org.kne.cloud.clock.NTPTimestamps;
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.MultiProtocolSocketAddress;
|
||||
import org.kne.cloud.network.ThreadTool;
|
||||
import org.kne.io.KNEChannels;
|
||||
import org.kne.math.Long128;
|
||||
|
||||
@@ -32,7 +33,7 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
private static final long EPHEMERAL_TIMEOUT = 60000000000L;
|
||||
|
||||
public static class NTPPeer {
|
||||
private MultipurposeSocketAddress address;
|
||||
private MultiProtocolSocketAddress address;
|
||||
private boolean isEphemeral;// ephemeral
|
||||
private volatile long ephemeralUpdateTime = System.nanoTime();
|
||||
private volatile long pollInterval = DEFAULT_POLL_INTERVAL;
|
||||
@@ -70,11 +71,11 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
this.pollInterval = pollInterval;
|
||||
}
|
||||
|
||||
public NTPPeer(MultipurposeSocketAddress address, int requestMode) {
|
||||
public NTPPeer(MultiProtocolSocketAddress address, int requestMode) {
|
||||
this(address, requestMode, false);
|
||||
}
|
||||
|
||||
protected NTPPeer(MultipurposeSocketAddress address, int requestMode, boolean isEphemeral) {
|
||||
protected NTPPeer(MultiProtocolSocketAddress address, int requestMode, boolean isEphemeral) {
|
||||
super();
|
||||
checkRequestMode(requestMode);
|
||||
this.address = address;
|
||||
@@ -90,10 +91,10 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
}
|
||||
|
||||
public NTPPeer(String hostport, int requestMode) {
|
||||
this(new MultipurposeSocketAddress(hostport), requestMode);
|
||||
this(new MultiProtocolSocketAddress(hostport), requestMode);
|
||||
}
|
||||
|
||||
public MultipurposeSocketAddress getAddress() {
|
||||
public MultiProtocolSocketAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
@@ -153,17 +154,28 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
|
||||
private NTPContext context;
|
||||
private DatagramSocket dgs;
|
||||
private MultipurposeSocketAddress bind;
|
||||
private MultiProtocolSocketAddress bind;
|
||||
|
||||
public NTPv4Protocol(NTPContext context) throws UnknownHostException, IOException {
|
||||
this(context, new MultipurposeSocketAddress("UDP", "::0", NTP_DEFAULT_PORT));
|
||||
this(context, new MultiProtocolSocketAddress("UDP", "::0", NTP_DEFAULT_PORT));
|
||||
}
|
||||
|
||||
public NTPv4Protocol(NTPContext context, MultipurposeSocketAddress bind) throws UnknownHostException, IOException {
|
||||
public NTPv4Protocol(NTPContext context, MultiProtocolSocketAddress bind) throws UnknownHostException, IOException {
|
||||
this.bind = bind;
|
||||
this.context = context;
|
||||
dgs = bind.listenDatagramSocket();
|
||||
context.registerIO(this);
|
||||
Thread t4= ThreadTool.makeVThread("NTPv4 Packet Cleaner",()->{
|
||||
while(isClosed()){
|
||||
removeTimeoutTimestamp();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
t4.start();
|
||||
Thread tr = new Thread(recv);
|
||||
tr.setName("NTPv4 Receive Thread");
|
||||
tr.start();
|
||||
@@ -219,7 +231,6 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
private void putOriginTimestamp(NTPv4Packet nv4, NTPPeer peer) {
|
||||
checkLock.lock();
|
||||
try {
|
||||
removeTimeoutTimestamp();
|
||||
checkList.add(new CheckListItem(nv4.getTransmitTimestamp128(), peer));
|
||||
} finally {
|
||||
checkLock.unlock();
|
||||
@@ -227,13 +238,18 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
}
|
||||
|
||||
private void removeTimeoutTimestamp() {
|
||||
for (Iterator<CheckListItem> iterator = checkList.iterator(); iterator.hasNext();) {
|
||||
CheckListItem bigInteger = (CheckListItem) iterator.next();
|
||||
if (bigInteger.checkTimeout()) {
|
||||
if (showPacket)
|
||||
System.out.println("timeout:" + bigInteger.timeStamp);
|
||||
iterator.remove();
|
||||
checkLock.lock();
|
||||
try {
|
||||
for (Iterator<CheckListItem> iterator = checkList.iterator(); iterator.hasNext(); ) {
|
||||
CheckListItem bigInteger = (CheckListItem) iterator.next();
|
||||
if (bigInteger.checkTimeout()) {
|
||||
if (showPacket)
|
||||
System.out.println("timeout:" + bigInteger.timeStamp);
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}finally{
|
||||
checkLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +282,7 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
return null;
|
||||
}
|
||||
|
||||
NTPPeer findPeer(MultipurposeSocketAddress addr) {
|
||||
NTPPeer findPeer(MultiProtocolSocketAddress addr) {
|
||||
Object[] objs = peers.toArray();
|
||||
for (int i = 0; i < objs.length; i++) {
|
||||
NTPPeer np = (NTPPeer) objs[i];
|
||||
@@ -291,7 +307,7 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
while (!dgs.isClosed()) {
|
||||
AtomicReference<InetSocketAddress> isa = new AtomicReference<>();
|
||||
NTPv4Packet nv4 = receiveNTPPacket(isa);
|
||||
MultipurposeSocketAddress mpsafrom = new MultipurposeSocketAddress(bind.getType(), isa.get());
|
||||
MultiProtocolSocketAddress mpsafrom = new MultiProtocolSocketAddress(bind.getProtocol(), isa.get());
|
||||
switch (nv4.getMode()) {
|
||||
case NTPv4Packet.NTP_SYMMETRIC_ACTIVE:
|
||||
NTPv4Packet nv4r = new NTPv4Packet(context.getClock());
|
||||
@@ -393,7 +409,7 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
|
||||
HighAccuracyClock hac = new HighAccuracyClock();
|
||||
NTPContext context = new NTPContext(hac);
|
||||
System.out.println(context);
|
||||
NTPv4Protocol nvc = new NTPv4Protocol(context, new MultipurposeSocketAddress("{UDP}0.0.0.0:123"));// 106.55.184.199
|
||||
NTPv4Protocol nvc = new NTPv4Protocol(context, new MultiProtocolSocketAddress("{UDP}0.0.0.0:123"));// 106.55.184.199
|
||||
nvc.getPeers().add(new NTPPeer("{UDP}106.55.184.199:123", NTPv4Packet.NTP_CLIENT));
|
||||
nvc.getPeers().add(new NTPPeer("{UDP}time.windows.com:123", NTPv4Packet.NTP_CLIENT));
|
||||
nvc.getPeers().add(new NTPPeer("{UDP}127.0.0.1:123", NTPv4Packet.NTP_SYMMETRIC_ACTIVE));
|
||||
|
||||
Reference in New Issue
Block a user