forked from KNEMC/KLALB
优化代码,性能暴涨
This commit is contained in:
@@ -5,55 +5,50 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.BindException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.channels.UnresolvedAddressException;
|
||||
import java.time.Clock;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.UUID;
|
||||
import java.util.Vector;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.management.monitor.Monitor;
|
||||
|
||||
import org.kne.cloud.clock.AdjustedNanoClock;
|
||||
import org.kne.cloud.clock.ExponentialBackoffTimeClock;
|
||||
import org.kne.cloud.clock.ReliabilityBackoffTimeClock;
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
import org.kne.cloud.network.SpeedLimiter;
|
||||
import org.kne.cloud.network.ThreadTool;
|
||||
import org.kne.cloud.network.ipv6.IPv6NetworkLink;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet;
|
||||
import org.kne.cloud.network.ipv6.Inet6AddressGroup;
|
||||
import org.kne.cloud.network.ipv6.Neighbor;
|
||||
import org.kne.cloud.network.monitor.MonitorData;
|
||||
import org.kne.cloud.network.monitor.QueueingMonitorDataImpl;
|
||||
import org.kne.cloud.network.monitor.SpeedAndTrafficAndDelayMonitorDataImpl;
|
||||
import org.kne.cloud.network.monitor.SpeedAndTrafficMonitorDataImpl;
|
||||
import org.kne.debug.TimeDebugger;
|
||||
import org.kne.cloud.network.te.LoadingBalanceEntry;
|
||||
|
||||
public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
public class KLALBRemoteLine implements IPv6NetworkLink,Comparable<KLALBRemoteLine> ,LoadingBalanceEntry<KLALBRemoteLine>{
|
||||
|
||||
private static final boolean debug = true;
|
||||
private static final boolean debug = false;
|
||||
|
||||
private static final boolean showpacket = false;
|
||||
|
||||
private ReliabilityBackoffTimeClock coll = new ReliabilityBackoffTimeClock();
|
||||
private volatile boolean closed = false;
|
||||
|
||||
private volatile Supplier<Inet6Address> localVaddrSupplier;
|
||||
private volatile Supplier<Inet6AddressGroup> localVaddrSupplier;
|
||||
|
||||
private volatile KLALBController klalbController;
|
||||
|
||||
@@ -64,21 +59,21 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
public void setKlalbController(KLALBController klalbController) {
|
||||
this.klalbController = klalbController;
|
||||
if (klalbController != null && remoteVaddr != null) {
|
||||
adjnc = klalbController.getAdjustedClockByVaddr(remoteVaddr);
|
||||
adjnc = klalbController.getAdjustedClockByVaddr(remoteVaddr.getAddress());
|
||||
}
|
||||
}
|
||||
|
||||
public Supplier<Inet6Address> getLocalVaddrSupplier() {
|
||||
public Supplier<Inet6AddressGroup> getLocalVaddrSupplier() {
|
||||
return localVaddrSupplier;
|
||||
}
|
||||
|
||||
public void setLocalVaddrSupplier(Supplier<Inet6Address> localVaddrSupplier) {
|
||||
public void setLocalVaddrSupplier(Supplier<Inet6AddressGroup> localVaddrSupplier) {
|
||||
this.localVaddrSupplier = localVaddrSupplier;
|
||||
}
|
||||
|
||||
private volatile Inet6Address remoteVaddr;
|
||||
private volatile Inet6AddressGroup remoteVaddr;
|
||||
|
||||
public Inet6Address getRemoteVaddr() {
|
||||
public Inet6AddressGroup getRemoteVaddr() {
|
||||
return remoteVaddr;
|
||||
}
|
||||
|
||||
@@ -98,7 +93,7 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
}
|
||||
}
|
||||
|
||||
public SpeedAndTrafficAndDelayMonitorDataImpl getMonitor() {
|
||||
public QueueingMonitorDataImpl getMonitor() {
|
||||
return monitor;
|
||||
}
|
||||
|
||||
@@ -106,7 +101,7 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
return remoteVaddr + "\t" + monitor.toString();
|
||||
}
|
||||
|
||||
private SpeedAndTrafficAndDelayMonitorDataImpl monitor;
|
||||
private QueueingMonitorDataImpl monitor;
|
||||
private volatile KLALBPacketLink kplink;
|
||||
private MultipurposeSocketAddress bindAddress;
|
||||
private MultipurposeSocketAddress socketAddress;
|
||||
@@ -129,56 +124,43 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
return bindAddress;
|
||||
}
|
||||
|
||||
protected static KLALBPacketLink createLink(MultipurposeSocketAddress bindAddress, MultipurposeSocketAddress mpa)
|
||||
throws IOException {
|
||||
if (mpa.isStream()) {
|
||||
if (mpa.supportNIO()) {
|
||||
if (bindAddress != null) {
|
||||
return new StreamChannelKLALBPacketLink(mpa
|
||||
.connectSocketChannel(InetAddress.getByName(bindAddress.getHost()), bindAddress.getPort()));
|
||||
} else {
|
||||
return new StreamChannelKLALBPacketLink(mpa.connectSocketChannel());
|
||||
}
|
||||
} else {
|
||||
if (bindAddress != null) {
|
||||
return new StreamKLALBPacketLink(
|
||||
mpa.connectSocket(InetAddress.getByName(bindAddress.getHost()), bindAddress.getPort()));
|
||||
} else {
|
||||
return new StreamKLALBPacketLink(mpa.connectSocket());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (bindAddress != null) {
|
||||
return new SplitedDatagramKLALBPacketLink(
|
||||
mpa.connectDatagramSocket(InetAddress.getByName(bindAddress.getHost()), bindAddress.getPort()));
|
||||
} else {
|
||||
return new SplitedDatagramKLALBPacketLink(mpa.connectDatagramSocket());
|
||||
}
|
||||
}
|
||||
private Inet6AddressGroup addressGroup;
|
||||
private Inet6AddressGroup peerAddress;
|
||||
@Override
|
||||
public Inet6AddressGroup getAddressGroup() {
|
||||
return addressGroup;
|
||||
}
|
||||
|
||||
public Inet6AddressGroup getPeerAddress() {
|
||||
return peerAddress;
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa) {
|
||||
this(mpa, new SpeedAndTrafficAndDelayMonitorDataImpl());
|
||||
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, Inet6AddressGroup address) {
|
||||
this(mpa,address, new QueueingMonitorDataImpl());
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, SpeedAndTrafficAndDelayMonitorDataImpl monitor) {
|
||||
this(mpa, null, monitor);
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, Inet6AddressGroup address, QueueingMonitorDataImpl monitor) {
|
||||
this(mpa, null,address, monitor);
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(KLALBPacketLink kpl) {
|
||||
this(kpl, new SpeedAndTrafficAndDelayMonitorDataImpl());
|
||||
public KLALBRemoteLine(KLALBPacketLink kpl, Inet6AddressGroup address) {
|
||||
this(kpl,address, new QueueingMonitorDataImpl());
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(KLALBPacketLink kpl, SpeedAndTrafficAndDelayMonitorDataImpl monitor) {
|
||||
public KLALBRemoteLine(KLALBPacketLink kpl, Inet6AddressGroup address,QueueingMonitorDataImpl monitor) {
|
||||
this.kplink = kpl;
|
||||
this.addressGroup=address;
|
||||
this.monitor = monitor;
|
||||
monitor.setName(kpl.toString());
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, MultipurposeSocketAddress bindaddr,
|
||||
SpeedAndTrafficAndDelayMonitorDataImpl monitor) {
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, MultipurposeSocketAddress bindaddr,Inet6AddressGroup address,
|
||||
QueueingMonitorDataImpl monitor) {
|
||||
this.socketAddress = mpa;
|
||||
this.bindAddress = bindaddr;
|
||||
this.addressGroup=address;
|
||||
this.monitor = monitor;
|
||||
if (bindaddr == null) {
|
||||
monitor.setName("→" + mpa.toString());
|
||||
@@ -187,8 +169,20 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
}
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, MultipurposeSocketAddress bindaddr) {
|
||||
this(mpa, bindaddr, new SpeedAndTrafficAndDelayMonitorDataImpl());
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, MultipurposeSocketAddress bindaddr, Inet6AddressGroup address) {
|
||||
this(mpa, bindaddr,address, new QueueingMonitorDataImpl());
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa, MultipurposeSocketAddress bind) {
|
||||
this(mpa, bind, null);
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(MultipurposeSocketAddress mpa) {
|
||||
this(mpa,(Inet6AddressGroup)null);
|
||||
}
|
||||
|
||||
public KLALBRemoteLine(KLALBPacketLink link) {
|
||||
this(link, (Inet6AddressGroup)null);
|
||||
}
|
||||
|
||||
PrintStream pw;
|
||||
@@ -225,13 +219,15 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private SpeedLimiter congress = new SpeedLimiter();
|
||||
|
||||
private static long MIN_SPEED=256*1024L;
|
||||
private SpeedLimiter congress = new SpeedLimiter(MIN_SPEED,10000000L);
|
||||
private long congressSpeed = 0;
|
||||
private double increaceFactor = 1.2;
|
||||
private double loadPercent = 1.01;
|
||||
|
||||
protected void startIO() {
|
||||
ThreadTool.makeVDaemonThreadIfSupport("远程接收线程", () -> {
|
||||
//Thread.currentThread().setPriority(Thread.NORM_PRIORITY+1);
|
||||
while (true) {
|
||||
try {
|
||||
|
||||
@@ -241,13 +237,34 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
close();
|
||||
break;
|
||||
}
|
||||
if(addressGroup==null) {
|
||||
byte[]addr=new byte[16];
|
||||
SecureRandom scr=new SecureRandom();
|
||||
scr.nextBytes(addr);
|
||||
addr[0]=(byte)0x24;
|
||||
addr[1]=(byte) 0x86;
|
||||
addr[2]=0;
|
||||
addr[3]=2;
|
||||
addr[14]=0;
|
||||
addr[15]=1;
|
||||
Inet6Address i6a=(Inet6Address) Inet6Address.getByAddress(addr);
|
||||
addressGroup=new Inet6AddressGroup(i6a, 112);
|
||||
}
|
||||
} else {
|
||||
kplink = createLink(bindAddress, socketAddress);
|
||||
kplink = KLALBUtils. createKLALBPacketLink(bindAddress, socketAddress);
|
||||
}
|
||||
startRecord();
|
||||
kplink.setSoTimeout(10000);
|
||||
//startRecord();
|
||||
kplink.setSoTimeout(20000);
|
||||
Thread t = ThreadTool.makeVDaemonThreadIfSupport("远程发送线程", () -> {
|
||||
tlock = Thread.currentThread();
|
||||
//tlock.setPriority(Thread.NORM_PRIORITY+1);
|
||||
try {
|
||||
writePacketToKPL(new ADDRREQPacket());
|
||||
writePacketToKPL(new ADDRREQPacket());
|
||||
if(addressGroup!=null) {
|
||||
writePacketToKPL(new ADDRPacket(addressGroup));
|
||||
writePacketToKPL(new ADDRPacket(addressGroup));
|
||||
}
|
||||
writePacketToKPL(new VADDRREQPacket());
|
||||
writePacketToKPL(new VADDRREQPacket());
|
||||
writePacketToKPL(new VADDRPacket(localVaddrSupplier.get()));
|
||||
@@ -257,20 +274,22 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
while ((!kplink.isClosed()) && (!closed)) {
|
||||
// TimeDebugger tdb=new TimeDebugger();
|
||||
// tdb.putTime("start");
|
||||
|
||||
boolean flsh = false;
|
||||
if (checkPingTime()) {
|
||||
if(checkBandwidthReportTime()) {
|
||||
sendIPacket(new BWINFPacket(monitor.getOutSpeedAvg2(), monitor.getInSpeedAvg2()));
|
||||
}
|
||||
if (checkPingTimeSleep()) {
|
||||
writePacketToKPL(new PINGPacket(System.nanoTime()));
|
||||
if (remoteVaddr == null)
|
||||
writePacketToKPL(new VADDRREQPacket());
|
||||
monitor.updateSpeedSync();
|
||||
flsh = true;
|
||||
}
|
||||
if(peerAddress==null)
|
||||
writePacketToKPL(new ADDRREQPacket());
|
||||
|
||||
}else {
|
||||
KLALBPacket kpip = IsendDequeList.poll();
|
||||
if (kpip != null) {
|
||||
writePacketToKPL(kpip);
|
||||
flsh = true;
|
||||
}
|
||||
|
||||
}else {
|
||||
|
||||
// tdb.putTime("Isend");
|
||||
|
||||
@@ -278,32 +297,45 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
do {
|
||||
kpp = sendDequeList.poll();
|
||||
} while (kpp != null && kpp.isDisposed());
|
||||
if(kpp==null) {
|
||||
monitor.setQueueingDelay(0);
|
||||
}
|
||||
}
|
||||
// System.out.println(sendDequeList.size());
|
||||
if (kpp != null) {
|
||||
kpp.getDisposeLock().lock();
|
||||
// TimeDebugger tdb=new TimeDebugger();
|
||||
//tdb.putTime("start");
|
||||
KLALBPacket kppt=kpp;
|
||||
kppt.lockAll();
|
||||
try {
|
||||
if (kpp.isDisposed()) {
|
||||
kpp.getDisposeLock().unlock();
|
||||
if (kpp.isSomeDisposed()) {
|
||||
kpp = null;
|
||||
} else {
|
||||
if (congress.checkTransmit(kpp.getLength())) {
|
||||
|
||||
// if (congress.checkTransmit(length)) {
|
||||
|
||||
writePacketToKPL(kpp);
|
||||
if (kpp instanceof DATATPacket) {
|
||||
resetSleepTimer();
|
||||
if (checkPingTime()) {
|
||||
writePacketToKPL(new PINGPacket(System.nanoTime()));
|
||||
}
|
||||
flsh = true;
|
||||
kpp.getDisposeLock().unlock();
|
||||
monitor.setQueueingDelay( System.nanoTime()- kpp.getJoinqueuetime());
|
||||
/*if (kpp instanceof DATATPacket||kpp instanceof IPv6OverKLALBPacket) {
|
||||
resetSleepTimer();
|
||||
}*/
|
||||
kpp.doDisposeAfterSend();
|
||||
kpp = null;
|
||||
}
|
||||
}
|
||||
//}
|
||||
} finally {
|
||||
if(kpp!=null)
|
||||
kpp.getDisposeLock().unlock();
|
||||
kppt.unlockAll();
|
||||
}
|
||||
//tdb.putTime("sendToLink");
|
||||
//tdb.print();
|
||||
}else {
|
||||
LockSupport.parkNanos(1000000);
|
||||
}
|
||||
if (flsh) {
|
||||
}
|
||||
}
|
||||
/*if (flsh) {
|
||||
flushKPL();
|
||||
} else {
|
||||
|
||||
@@ -320,7 +352,7 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
}
|
||||
|
||||
// tdb.putTime("park");
|
||||
}
|
||||
}*/
|
||||
|
||||
// tdb.print();
|
||||
// System.out.println(sendDequeList.isEmpty());
|
||||
@@ -343,26 +375,26 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
while ((!kplink.isClosed()) && (!closed)) {
|
||||
long readStart = System.nanoTime();
|
||||
KLALBPacket kpp = readPacketFromKPL();
|
||||
long readTime = System.nanoTime() - readStart;
|
||||
/*long readTime = System.nanoTime() - readStart;
|
||||
readTime *= 10;
|
||||
if (readTime > stime) {
|
||||
stime = readTime;
|
||||
} else {
|
||||
stime = (stime * 99 + readTime) / 100;
|
||||
stime = (stime * 999 + readTime) / 1000;
|
||||
}
|
||||
if (stime < 2000000000L) {
|
||||
stime = 2000000000L;
|
||||
}
|
||||
}*/
|
||||
|
||||
// System.out.println(readTime);
|
||||
// kplink.setSoTimeout(5000);
|
||||
kplink.setSoTimeout(3000);
|
||||
kplink.setSoTimeout((int) (stime / 1000000));
|
||||
if (kpp == null)
|
||||
if (kpp == null) {
|
||||
break;
|
||||
}
|
||||
switch (kpp.getType()) {
|
||||
case KLALBPacket.PING:
|
||||
sendIPacket(new PONGPacket(((PINGPacket) kpp).getTime() ,kpp.getRcvtime(), System.nanoTime()));
|
||||
sendIPacket(new BWINFPacket(monitor.getOutSpeed(), monitor.getInSpeed()));
|
||||
break;
|
||||
case KLALBPacket.PONG:
|
||||
PONGPacket png = (PONGPacket) kpp;
|
||||
@@ -401,10 +433,10 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
monitor.setOutDelay(DsndDelayFactor);
|
||||
monitor.setInDelay(DrcvDelayFactor);
|
||||
monitor.setRecentPingNanoTime(png.getTimepingsnd());
|
||||
pingInterval = monitor.getOutDelayMin();
|
||||
//pingInterval = monitor.getLatencyAvg()+2000000L;
|
||||
|
||||
double load = 1 - monitor.getOutDelayMin() / (double) monitor.getOutDelay();
|
||||
increaceFactor =Math.max( 2.0 - load*2,1.0);
|
||||
//double load = 1 - monitor.getOutDelayMin() / (double) monitor.getOutDelay();
|
||||
loadPercent =Math.max( 1.2,0.4);
|
||||
}
|
||||
|
||||
LockSupport.unpark(tlock);
|
||||
@@ -416,19 +448,32 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
} else {
|
||||
congressSpeed = (congressSpeed * 999 + bwi.getDownSpeed()) / 1000;
|
||||
}
|
||||
congress.setLimitspeed((long) (congressSpeed * increaceFactor) + 32768);
|
||||
congress.setLimitspeed(Math.max(MIN_SPEED,(long) (congressSpeed * loadPercent)) );
|
||||
// System.out.println(congressSpeed);
|
||||
break;
|
||||
case KLALBPacket.ADDRREQ:
|
||||
if(addressGroup!=null)
|
||||
sendIPacket(new ADDRPacket(addressGroup));
|
||||
break;
|
||||
case KLALBPacket.ADDR:
|
||||
peerAddress = ((ADDRPacket) kpp).getAddr();
|
||||
if(addressGroup==null) {
|
||||
byte[]ab=peerAddress.getAddress().getAddress();
|
||||
ab[15]=2;
|
||||
Inet6AddressGroup ardg2=new Inet6AddressGroup((Inet6Address) InetAddress.getByAddress(ab),peerAddress.getPrefixLength());
|
||||
addressGroup=ardg2;
|
||||
}
|
||||
break;
|
||||
case KLALBPacket.VADDRREQ:
|
||||
sendIPacket(new VADDRPacket(localVaddrSupplier.get()));
|
||||
break;
|
||||
case KLALBPacket.VADDR:
|
||||
Inet6Address vdr = ((VADDRPacket) kpp).getVaddr();
|
||||
Inet6AddressGroup vdr = ((VADDRPacket) kpp).getVaddr();
|
||||
|
||||
remoteVaddr = vdr;
|
||||
sendIPacket(new VADDRACKPacket());
|
||||
if (klalbController != null) {
|
||||
adjnc = klalbController.getAdjustedClockByVaddr(remoteVaddr);
|
||||
adjnc = klalbController.getAdjustedClockByVaddr(remoteVaddr.getAddress());
|
||||
}
|
||||
if (fst) {
|
||||
// coll.resetCoolingTime();
|
||||
@@ -440,11 +485,21 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
break;
|
||||
case KLALBPacket.TEST:
|
||||
break;
|
||||
case KLALBPacket.IPV6OVERKLALB:
|
||||
if(ipv6con!=null) {
|
||||
IPv6OverKLALBPacket ivk=(IPv6OverKLALBPacket) kpp;
|
||||
IPv6Packet iv6= ivk.getIPv6Packet();
|
||||
ivk.putTimePassport("unpacked");
|
||||
ivk.printPassport();
|
||||
iv6.putTimePassport("unpackFromLink");
|
||||
ipv6con.accept(iv6);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
while (rec == null) {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
rec.accept(KLALBRemoteLine.this, kpp);
|
||||
rec.accept( kpp);
|
||||
break;
|
||||
}
|
||||
Thread.yield();
|
||||
@@ -463,17 +518,17 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
KLALBPacket pack;
|
||||
/*KLALBPacket pack;
|
||||
while((pack=sendDequeList.poll())!=null) {
|
||||
try {
|
||||
if(remoteVaddr!=null) {
|
||||
klalbController.sendPacketToAddress(remoteVaddr, pack);
|
||||
klalbController.sendPacketToLinkAddress(remoteVaddr, pack);
|
||||
if(debug)
|
||||
System.out.println("RETRY:"+pack);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
if (closed) {
|
||||
@@ -499,29 +554,56 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
if (showpacket) {
|
||||
if (!(packet instanceof PINGPacket))
|
||||
if (!(packet instanceof PONGPacket))
|
||||
if (!(packet instanceof BWINFPacket))
|
||||
System.out.println("RX:" + packet);
|
||||
}
|
||||
if (packet != null) {
|
||||
monitor.getInTrafficAL().addAndGet(packet.getLength());
|
||||
packet.putTimePassport("received");
|
||||
long length=packet.getLength();
|
||||
monitor.getInTrafficAL().addAndGet(length);
|
||||
monitor.getInPacketCounterAL().incrementAndGet();
|
||||
if (klalbController != null)
|
||||
klalbController.getLinkMonitor().getInTrafficAL().addAndGet(packet.getLength());
|
||||
klalbController.getLinkMonitor().getInTrafficAL().addAndGet(length);
|
||||
klalbController.getLinkMonitor().getInPacketCounterAL().incrementAndGet();
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
private void writePacketToKPL(KLALBPacket packet) throws IOException {
|
||||
monitor.getOutTrafficAL().addAndGet(packet.getLength());
|
||||
long length=packet.getLength();
|
||||
monitor.getOutTrafficAL().addAndGet(length);
|
||||
monitor.getOutPacketCounterAL().incrementAndGet();
|
||||
if (klalbController != null)
|
||||
klalbController.getLinkMonitor().getOutTrafficAL().addAndGet(packet.getLength());
|
||||
klalbController.getLinkMonitor().getOutTrafficAL().addAndGet(length);
|
||||
klalbController.getLinkMonitor().getOutPacketCounterAL().incrementAndGet();
|
||||
kplink.writePacket(packet);
|
||||
packet.putTimePassport("sended");
|
||||
packet.printPassport();
|
||||
|
||||
if (showpacket) {
|
||||
if (!(packet instanceof PINGPacket))
|
||||
if (!(packet instanceof PONGPacket))
|
||||
if (!(packet instanceof BWINFPacket))
|
||||
System.err.println("TX:" + packet);
|
||||
}
|
||||
}
|
||||
|
||||
private void writePacketToKPL(KLALBPacket packet,long length) throws IOException {
|
||||
monitor.getOutTrafficAL().addAndGet(length);
|
||||
monitor.getOutPacketCounterAL().incrementAndGet();
|
||||
if (klalbController != null)
|
||||
klalbController.getLinkMonitor().getOutTrafficAL().addAndGet(length);
|
||||
klalbController.getLinkMonitor().getOutPacketCounterAL().incrementAndGet();
|
||||
kplink.writePacket(packet);
|
||||
|
||||
if (showpacket) {
|
||||
if (!(packet instanceof PINGPacket))
|
||||
if (!(packet instanceof PONGPacket))
|
||||
if (!(packet instanceof BWINFPacket))
|
||||
System.err.println("TX:" + packet);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
closed = true;
|
||||
monitor.setState(MonitorData.OFFLINE);
|
||||
@@ -538,28 +620,48 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
return closed;
|
||||
}
|
||||
|
||||
private volatile long timeTurnSleep = System.nanoTime();
|
||||
|
||||
private volatile long time = System.nanoTime();
|
||||
private volatile long pingInterval = 50000000L;
|
||||
private volatile long pingInterval = 10000000L;
|
||||
private volatile long pingIntervalSleep = 200000000L;
|
||||
|
||||
private void resetSleepTimer() {
|
||||
timeTurnSleep = System.nanoTime();
|
||||
}
|
||||
|
||||
private boolean checkPingTime() {
|
||||
long cu = System.nanoTime();
|
||||
if (cu - time > ((System.nanoTime() - timeTurnSleep > 500000000) ? pingIntervalSleep
|
||||
: Math.min(pingIntervalSleep, pingInterval))) {
|
||||
time = cu;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (cu - time > pingInterval) {
|
||||
time = cu;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private volatile BiConsumer<KLALBRemoteLine, KLALBPacket> rec;
|
||||
private boolean checkPingTimeSleep() {
|
||||
long cu = System.nanoTime();
|
||||
if (cu - time > pingIntervalSleep) {
|
||||
time = cu;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private volatile long bwtime = System.nanoTime();
|
||||
private volatile long bwpingInterval = 10000000L;
|
||||
private volatile long bwpingIntervalSleep = 100000000L;
|
||||
private boolean checkBandwidthReportTime(){
|
||||
long cu = System.nanoTime();
|
||||
if (cu - bwtime > bwpingIntervalSleep) {
|
||||
bwtime = cu;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private volatile Consumer<KLALBPacket> rec;
|
||||
|
||||
private volatile Thread tlock;
|
||||
|
||||
@@ -572,6 +674,7 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
}
|
||||
|
||||
protected void sendPacket(KLALBPacket blk) {
|
||||
blk.markJoinqueuetime();
|
||||
sendDequeList.add(blk);
|
||||
LockSupport.unpark(tlock);
|
||||
}
|
||||
@@ -581,7 +684,7 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
LockSupport.unpark(tlock);
|
||||
}
|
||||
|
||||
public void setPacketReceiver(BiConsumer<KLALBRemoteLine, KLALBPacket> rec) {
|
||||
public void setPacketReceiver(Consumer< KLALBPacket> rec) {
|
||||
this.rec = rec;
|
||||
}
|
||||
|
||||
@@ -641,7 +744,7 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
}
|
||||
}
|
||||
|
||||
private static final TESTPacket testPacket = new TESTPacket();
|
||||
private static final TESTPacket testPacket = new TESTPacket(60000);
|
||||
private volatile boolean pressure = false;
|
||||
private SpeedLimiter pressureSpeed = new SpeedLimiter(32768);
|
||||
|
||||
@@ -671,4 +774,111 @@ public class KLALBRemoteLine implements Comparable<KLALBRemoteLine> {
|
||||
return predictTime;
|
||||
}
|
||||
|
||||
private List<Double>ranks=new ArrayList<Double>();
|
||||
|
||||
private Consumer<IPv6Packet> ipv6con;
|
||||
@Override
|
||||
public void noticeRank(int rank) {
|
||||
while(rank>=ranks.size()) {
|
||||
ranks.add(0.5);
|
||||
}
|
||||
for (int i = 0; i < ranks.size(); i++) {
|
||||
if(i==rank) {
|
||||
ranks.set(i, (ranks.get(i)*99.0+1.0D)/100.0);
|
||||
}else {
|
||||
ranks.set(i, ranks.get(i)*99.0/100.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getWeightAtRank(int rank) {
|
||||
while(rank>=ranks.size()) {
|
||||
ranks.add(0.5);
|
||||
}
|
||||
return ranks.get(rank);
|
||||
}
|
||||
|
||||
public List<Double> getRanks() {
|
||||
|
||||
return ranks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoopBack() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Neighbor> getNeighborsInfo() {
|
||||
List<Neighbor>hs=new ArrayList<>();
|
||||
if(peerAddress!=null) {
|
||||
hs.add(new Neighbor(peerAddress, remoteVaddr, monitor));
|
||||
//System.out.println(peerAddress+" "+addressGroup);
|
||||
}
|
||||
return hs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void sendPacket(IPv6Packet pack, Inet6Address inet6Address) throws IOException {
|
||||
pack.lockAll();
|
||||
try {
|
||||
if(pack.isSomeDisposed()) {
|
||||
return;
|
||||
}
|
||||
if(inet6Address.equals(remoteVaddr.getAddress())||inet6Address.equals(peerAddress.getAddress())) {
|
||||
IPv6OverKLALBPacket kipv6=new IPv6OverKLALBPacket(pack);
|
||||
kipv6.setPriority(pack.getPriority());
|
||||
kipv6.setDisposeAfterSend(true);
|
||||
pack.putTimePassport("packdInLink");
|
||||
pack.printPassport();
|
||||
sendPacketToLink(kipv6);
|
||||
}
|
||||
}finally {
|
||||
pack.unlockAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void sendPacketToLink(KLALBPacket packet) {
|
||||
packet.genseq();
|
||||
//packet.getSendRecord().add(this);
|
||||
sendPacket(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCongress(IPv6Packet iPv6Packet) {
|
||||
int size=getQueue().size();
|
||||
if(size>30) {
|
||||
return true;
|
||||
}else {
|
||||
return !congress.checkTransmit(iPv6Packet.getLength());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return monitor.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUp() {
|
||||
return (!isClosed())&&getMonitor().getState()==MonitorData.ONLINE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSend(IPv6Packet iPv6Packet) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReceiveConsumer(Consumer<IPv6Packet> ipv6con) {
|
||||
this.ipv6con=ipv6con;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user