隧道链接改成标准URI格式
This commit is contained in:
@@ -18,7 +18,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
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.ntp.NTPv4Protocol.NTPPeer;
|
||||
import org.kne.math.Long128;
|
||||
|
||||
@@ -110,13 +110,13 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
return clock;
|
||||
}
|
||||
|
||||
private ConcurrentHashMap<MultipurposeSocketAddress, List<NTPv4Packet>> recvmap = new ConcurrentHashMap<MultipurposeSocketAddress, List<NTPv4Packet>>();
|
||||
private ConcurrentHashMap<MultiProtocolSocketAddress, List<NTPv4Packet>> recvmap = new ConcurrentHashMap<MultiProtocolSocketAddress, List<NTPv4Packet>>();
|
||||
|
||||
protected void clearPackets() {
|
||||
recvmap.clear();
|
||||
}
|
||||
|
||||
protected void putPacket(NTPv4Packet nv4, MultipurposeSocketAddress inetSocketAddress) {
|
||||
protected void putPacket(NTPv4Packet nv4, MultiProtocolSocketAddress inetSocketAddress) {
|
||||
checkIP();
|
||||
List<NTPv4Packet> newv = new Vector<NTPv4Packet>();
|
||||
List<NTPv4Packet> oldv = recvmap.putIfAbsent(inetSocketAddress, newv);
|
||||
@@ -133,10 +133,10 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
}
|
||||
|
||||
private void checkIP() {
|
||||
Set<Entry<MultipurposeSocketAddress, List<NTPv4Packet>>> ens = recvmap.entrySet();
|
||||
for (Iterator<Entry<MultipurposeSocketAddress, List<NTPv4Packet>>> iterator = ens.iterator(); iterator
|
||||
Set<Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>> ens = recvmap.entrySet();
|
||||
for (Iterator<Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>> iterator = ens.iterator(); iterator
|
||||
.hasNext();) {
|
||||
Entry<MultipurposeSocketAddress, List<NTPv4Packet>> entry = (Entry<MultipurposeSocketAddress, List<NTPv4Packet>>) iterator
|
||||
Entry<MultiProtocolSocketAddress, List<NTPv4Packet>> entry = (Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>) iterator
|
||||
.next();
|
||||
AtomicBoolean ab = new AtomicBoolean(false);
|
||||
ios.forEach((x) -> {
|
||||
@@ -153,7 +153,7 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
}
|
||||
|
||||
private class PeerInfo implements Comparable<PeerInfo> {
|
||||
private MultipurposeSocketAddress address;
|
||||
private MultiProtocolSocketAddress address;
|
||||
private int leapIndicator = 3;
|
||||
private int stratum = 16;
|
||||
private int referenceIdentifier;
|
||||
@@ -265,10 +265,10 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
|
||||
private List<PeerInfo> mergeResponses() {
|
||||
List<PeerInfo> peerInfo = new ArrayList<PeerInfo>();
|
||||
Set<Entry<MultipurposeSocketAddress, List<NTPv4Packet>>> ens = recvmap.entrySet();
|
||||
for (Iterator<Entry<MultipurposeSocketAddress, List<NTPv4Packet>>> iterator = ens.iterator(); iterator
|
||||
Set<Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>> ens = recvmap.entrySet();
|
||||
for (Iterator<Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>> iterator = ens.iterator(); iterator
|
||||
.hasNext();) {
|
||||
Entry<MultipurposeSocketAddress, List<NTPv4Packet>> entry = (Entry<MultipurposeSocketAddress, List<NTPv4Packet>>) iterator
|
||||
Entry<MultiProtocolSocketAddress, List<NTPv4Packet>> entry = (Entry<MultiProtocolSocketAddress, List<NTPv4Packet>>) iterator
|
||||
.next();
|
||||
|
||||
List<NTPv4Packet> newv = entry.getValue();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.kne.cloud.network.ntp;
|
||||
|
||||
import org.kne.cloud.network.UDPSocketType;
|
||||
|
||||
public class NTPSocketType extends UDPSocketType {
|
||||
private static final NTPSocketType INSTANCE = new NTPSocketType();
|
||||
|
||||
private NTPSocketType() {
|
||||
super();
|
||||
}
|
||||
|
||||
public static NTPSocketType getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDefaultPort() {
|
||||
return 123;
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
@@ -4,8 +4,7 @@ import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.kne.cloud.clock.HighAccuracyClock;
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.ntp.NTPv4Protocol.NTPPeer;
|
||||
import org.kne.cloud.network.MultiProtocolSocketAddress;
|
||||
|
||||
public class TestNTP1 {
|
||||
public static void main(String[] args) throws UnknownHostException, IOException {
|
||||
@@ -13,7 +12,7 @@ public class TestNTP1 {
|
||||
NTPContext context = new NTPContext(hac);
|
||||
context.syncToSystem();
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.kne.cloud.clock.HighAccuracyClock;
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.MultiProtocolSocketAddress;
|
||||
import org.kne.cloud.network.ntp.NTPv4Protocol.NTPPeer;
|
||||
|
||||
public class TestNTP2 {
|
||||
@@ -13,7 +13,7 @@ public class TestNTP2 {
|
||||
NTPContext context = new NTPContext(hac);
|
||||
context.syncToSystem();
|
||||
System.out.println(context);
|
||||
NTPv4Protocol nvc = new NTPv4Protocol(context, new MultipurposeSocketAddress("{UDP}0.0.0.0:0"));// 106.55.184.199
|
||||
NTPv4Protocol nvc = new NTPv4Protocol(context, new MultiProtocolSocketAddress("{UDP}0.0.0.0:0"));// 106.55.184.199
|
||||
nvc.getPeers().add(new NTPPeer("{UDP}127.0.0.1:123", NTPv4Packet.NTP_SYMMETRIC_ACTIVE));
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user