forked from KNEMC/KLALB
KLALB2.3
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
public interface DelayMonitorData extends MonitorData {
|
||||
public long getOutDelay();
|
||||
public void setOutDelay(long delay);
|
||||
public long getOutJitter();
|
||||
|
||||
public long getInDelay();
|
||||
public void setInDelay(long delay);
|
||||
public long getInJitter();
|
||||
|
||||
public long getLatency();
|
||||
public void setLatency(long latency);
|
||||
public long getTotalJitter();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface MonitorData {
|
||||
public static final int OFFLINE=0;
|
||||
public static final int CONNECTING=1;
|
||||
public static final int ONLINE=2;
|
||||
public Consumer<MonitorData> getChangeListener();
|
||||
public void setChangeListener(Consumer<MonitorData> changeListener);
|
||||
public String getName();
|
||||
public void setName(String name);
|
||||
@Override
|
||||
public String toString();
|
||||
|
||||
public int getState();
|
||||
public void setState(int state);
|
||||
|
||||
public double getReliability();
|
||||
|
||||
public static String parseStateToString(int state) {
|
||||
switch (state) {
|
||||
case OFFLINE:
|
||||
return "offline";
|
||||
case CONNECTING:
|
||||
return "connecting";
|
||||
case ONLINE:
|
||||
return "online";
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown state:"+state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
import static org.kne.cloud.network.klalb.KLALBUtils.bytesUnit;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
public class MonitorDataImpl implements MonitorData {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
private static Timer timer=new Timer("Monitor Update Thread",true);
|
||||
public MonitorDataImpl() {
|
||||
timer.scheduleAtFixedRate(ptt, 0, 100);
|
||||
}
|
||||
public MonitorDataImpl(long period) {
|
||||
timer.scheduleAtFixedRate(ptt, 0, period);
|
||||
}
|
||||
private TimerTask ptt=new TimerTask() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
reliability=(reliability*999+(state==ONLINE?1:0))/1000;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
ptt.cancel();
|
||||
}
|
||||
|
||||
|
||||
public static Timer getTimer() {
|
||||
return timer;
|
||||
}
|
||||
private Consumer<MonitorData>changeListener;
|
||||
|
||||
public Consumer<MonitorData> getChangeListener() {
|
||||
return changeListener;
|
||||
}
|
||||
public void setChangeListener(Consumer<MonitorData> changeListener) {
|
||||
this.changeListener = changeListener;
|
||||
}
|
||||
private volatile int state=0;
|
||||
private double reliability=0.0;
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
private String getDsc() {
|
||||
switch(state) {
|
||||
case OFFLINE:
|
||||
return "○离线";
|
||||
case CONNECTING:
|
||||
return "◐连接中";
|
||||
case ONLINE:
|
||||
return "●在线";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.append(name);
|
||||
sb.append('\n');
|
||||
sb.append(getDsc());
|
||||
sb.append('\t');
|
||||
sb.append((int)(reliability*100));
|
||||
sb.append('%');
|
||||
return sb.toString();
|
||||
}
|
||||
@Override
|
||||
public double getReliability() {
|
||||
return reliability;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
public class NanoTimeSeries {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
public class SpeedAndTrafficAndDelayMonitorDataImpl extends SpeedAndTrafficMonitorDataImpl implements SpeedAndTrafficMonitorData,DelayMonitorData{
|
||||
|
||||
|
||||
@Override
|
||||
protected void createTask() {
|
||||
}
|
||||
public void updateSpeedSync(){
|
||||
tmt.run();
|
||||
}
|
||||
|
||||
private volatile long outDelay;
|
||||
private volatile long outDelayMin=Long.MAX_VALUE;
|
||||
private long outDelayOld;
|
||||
private volatile long outJitter;
|
||||
|
||||
|
||||
private volatile long inDelay;
|
||||
private volatile long inDelayMin=Long.MAX_VALUE;
|
||||
private long inDelayOld;
|
||||
private volatile long inJitter;
|
||||
|
||||
|
||||
private volatile long latency;
|
||||
private volatile long latencyMin=Long.MAX_VALUE;
|
||||
private long latencyOld;
|
||||
private volatile long totalJitter;
|
||||
|
||||
private volatile long outDelayPredicted;
|
||||
private volatile long inDelayPredicted;
|
||||
|
||||
private volatile long recentPingNanoTime;
|
||||
|
||||
public long getRecentPingNanoTime() {
|
||||
return recentPingNanoTime;
|
||||
}
|
||||
public void setRecentPingNanoTime(long recentPingNanoTime) {
|
||||
this.recentPingNanoTime = recentPingNanoTime;
|
||||
}
|
||||
@Override
|
||||
public long getOutDelay() {
|
||||
return outDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOutDelay(long delay) {
|
||||
this.outDelay=delay;
|
||||
if(outDelayMin==Long.MAX_VALUE||delay<=outDelayMin) {
|
||||
outDelayMin=delay;
|
||||
}else{
|
||||
outDelayMin=(outDelayMin*9999+delay)/10000;
|
||||
}
|
||||
this.latency=outDelay+inDelay;
|
||||
if(latencyMin==Long.MAX_VALUE||latency<= latencyMin) {
|
||||
latencyMin=latency;
|
||||
}else {
|
||||
latencyMin=(latencyMin*9999+latency)/10000;
|
||||
}
|
||||
outDelayPredicted=outDelay+(outDelay-outDelayOld);
|
||||
updateOutJitter();
|
||||
updateTotalJitter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getInDelay() {
|
||||
return inDelay;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInDelay(long delay) {
|
||||
this.inDelay=delay;
|
||||
if(inDelayMin==Long.MAX_VALUE||delay<=inDelayMin) {
|
||||
inDelayMin=delay;
|
||||
}else{
|
||||
inDelayMin=(inDelayMin*9999+delay)/10000;
|
||||
}
|
||||
this.latency=outDelay+inDelay;
|
||||
if(latencyMin==Long.MAX_VALUE||latency<= latencyMin) {
|
||||
latencyMin=latency;
|
||||
}else {
|
||||
latencyMin=(latencyMin*9999+latency)/10000;
|
||||
}
|
||||
inDelayPredicted=inDelay+(inDelay-inDelayOld);
|
||||
updateInJitter();
|
||||
updateTotalJitter();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getLatency() {
|
||||
return latency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLatency(long latency) {
|
||||
this.latency=latency;
|
||||
if(latencyMin==Long.MAX_VALUE||latency<= latencyMin) {
|
||||
latencyMin=latency;
|
||||
}else {
|
||||
latencyMin=(latencyMin*9999+latency)/10000;
|
||||
}
|
||||
long tmp= latency>>1;
|
||||
this.inDelay=tmp;
|
||||
if(inDelayMin==Long.MAX_VALUE||tmp<=inDelayMin) {
|
||||
inDelayMin=tmp;
|
||||
}else{
|
||||
inDelayMin=(inDelayMin*9999+tmp)/10000;
|
||||
}
|
||||
this.outDelay=tmp;
|
||||
if(outDelayMin==Long.MAX_VALUE||tmp<=outDelayMin) {
|
||||
outDelayMin=tmp;
|
||||
}else{
|
||||
outDelayMin=(outDelayMin*9999+tmp)/10000;
|
||||
}
|
||||
outDelayPredicted=outDelay+(outDelay-outDelayOld);
|
||||
inDelayPredicted=inDelay+(inDelay-inDelayOld);
|
||||
updateOutJitter();
|
||||
updateInJitter();
|
||||
updateTotalJitter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb=new StringBuilder(super.toString());
|
||||
sb.append(String.format("%.1f", outDelay/1000000.0) ).append("ms").append("\t").append(String.format("%.1f", inDelay/1000000.0) ).append("ms").append("\t").append(String.format("%.1f", outJitter/1000000.0) ).append("ms\t").append(String.format("%.1f", inJitter/1000000.0) ).append("ms\t");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getOutJitter() {
|
||||
return outJitter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getInJitter() {
|
||||
return inJitter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTotalJitter() {
|
||||
return totalJitter;
|
||||
}
|
||||
public long getOutDelayMin() {
|
||||
return outDelayMin;
|
||||
}
|
||||
|
||||
public long getInDelayMin() {
|
||||
return inDelayMin;
|
||||
}
|
||||
|
||||
public long getLatencyMin() {
|
||||
return latencyMin;
|
||||
}
|
||||
|
||||
private void updateOutJitter() {
|
||||
|
||||
long vj=Math.abs( outDelay-outDelayOld);
|
||||
outJitter=(outJitter*9+vj)/10;
|
||||
outDelayOld =outDelay;
|
||||
}
|
||||
private void updateInJitter() {
|
||||
long vj=Math.abs( inDelay-inDelayOld);
|
||||
inJitter=(inJitter*9+vj)/10;
|
||||
inDelayOld =inDelay;
|
||||
}
|
||||
private void updateTotalJitter() {
|
||||
long vj=Math.abs( latency-latencyOld);
|
||||
totalJitter=(totalJitter*9+vj)/10;
|
||||
latencyOld =latency;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public long getOutDelayPredicted() {
|
||||
return outDelayPredicted;
|
||||
}
|
||||
|
||||
public long getInDelayPredicted() {
|
||||
return inDelayPredicted;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public interface SpeedAndTrafficMonitorData extends MonitorData{
|
||||
public long getInTraffic();
|
||||
|
||||
public long getOutTraffic();
|
||||
|
||||
public AtomicLong getInTrafficAL();
|
||||
|
||||
public AtomicLong getOutTrafficAL();
|
||||
|
||||
public long getInSpeed();
|
||||
|
||||
|
||||
public long getOutSpeed();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package org.kne.cloud.network.monitor;
|
||||
|
||||
import static org.kne.cloud.network.klalb.KLALBUtils.bytesUnit;
|
||||
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements SpeedAndTrafficMonitorData {
|
||||
|
||||
private long updatetime=System.nanoTime();
|
||||
private long updatetime1=System.nanoTime();
|
||||
private long updatetime2=System.nanoTime();
|
||||
|
||||
private volatile AtomicLong inTraffic=new AtomicLong();
|
||||
private volatile AtomicLong outTraffic=new AtomicLong();
|
||||
|
||||
|
||||
private long inTrafficOld=0;
|
||||
private long outTrafficOld=0;
|
||||
private long inTrafficOld1=0;
|
||||
private long outTrafficOld1=0;
|
||||
private long inTrafficOld2=0;
|
||||
private long outTrafficOld2=0;
|
||||
|
||||
private volatile long inSpeed;
|
||||
private volatile long outSpeed;
|
||||
|
||||
private volatile long inSpeedAvg;
|
||||
private volatile long outSpeedAvg;
|
||||
private volatile long inSpeedAvg2;
|
||||
private volatile long outSpeedAvg2;
|
||||
|
||||
private volatile long inSpeedMax;
|
||||
private volatile long outSpeedMax;
|
||||
|
||||
public SpeedAndTrafficMonitorDataImpl() {
|
||||
super();
|
||||
createTask();
|
||||
createTask1();
|
||||
createTask2();
|
||||
}
|
||||
|
||||
protected void createTask() {
|
||||
getTimer().scheduleAtFixedRate(tmt, 0, 50);
|
||||
}
|
||||
|
||||
protected void createTask1() {
|
||||
getTimer().scheduleAtFixedRate(tmt1, 0, 1000);
|
||||
}
|
||||
protected void createTask2() {
|
||||
getTimer().scheduleAtFixedRate(tmt2, 0, 5000);
|
||||
}
|
||||
public long getInSpeedAvg2() {
|
||||
return inSpeedAvg2;
|
||||
}
|
||||
|
||||
public long getOutSpeedAvg2() {
|
||||
return outSpeedAvg2;
|
||||
}
|
||||
|
||||
public long getInTraffic() {
|
||||
return inTraffic.get();
|
||||
}
|
||||
|
||||
public long getOutTraffic() {
|
||||
return outTraffic.get();
|
||||
}
|
||||
|
||||
public AtomicLong getInTrafficAL() {
|
||||
return inTraffic;
|
||||
}
|
||||
|
||||
public AtomicLong getOutTrafficAL() {
|
||||
return outTraffic;
|
||||
}
|
||||
|
||||
public long getInSpeed() {
|
||||
return inSpeed;
|
||||
}
|
||||
|
||||
|
||||
public long getOutSpeed() {
|
||||
return outSpeed;
|
||||
}
|
||||
|
||||
public long getInSpeedAvg() {
|
||||
return inSpeedAvg;
|
||||
}
|
||||
|
||||
|
||||
public long getOutSpeedAvg() {
|
||||
return outSpeedAvg;
|
||||
}
|
||||
|
||||
public long getInSpeedMax() {
|
||||
return inSpeedMax;
|
||||
}
|
||||
|
||||
|
||||
public long getOutSpeedMax() {
|
||||
return outSpeedMax;
|
||||
}
|
||||
|
||||
protected TimerTask tmt=new TimerTask() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long d=System.nanoTime()-updatetime;
|
||||
|
||||
if(inTraffic!=null) {
|
||||
long i=inTraffic.get()-inTrafficOld;
|
||||
inTrafficOld =inTraffic.get();
|
||||
inSpeed= i*1000000000/d;
|
||||
}
|
||||
|
||||
if(outTraffic!=null) {
|
||||
long o=outTraffic.get()-outTrafficOld;
|
||||
outTrafficOld =outTraffic.get();
|
||||
outSpeed= o*1000000000/d;
|
||||
}
|
||||
|
||||
if(outSpeed>=outSpeedMax) {
|
||||
outSpeedMax=outSpeed;
|
||||
}else {
|
||||
outSpeedMax=(outSpeedMax*999+outSpeed)/1000;
|
||||
}
|
||||
|
||||
if(inSpeed>=inSpeedMax) {
|
||||
inSpeedMax=inSpeed;
|
||||
}else {
|
||||
inSpeedMax=(inSpeedMax*999+inSpeed)/1000;
|
||||
}
|
||||
|
||||
updatetime=System.nanoTime();
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
protected TimerTask tmt1=new TimerTask() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long d=System.nanoTime()-updatetime1;
|
||||
|
||||
if(inTraffic!=null) {
|
||||
long i=inTraffic.get()-inTrafficOld1;
|
||||
inTrafficOld1 =inTraffic.get();
|
||||
inSpeedAvg= i*1000000000/d;
|
||||
}
|
||||
|
||||
if(outTraffic!=null) {
|
||||
long o=outTraffic.get()-outTrafficOld1;
|
||||
outTrafficOld1 =outTraffic.get();
|
||||
outSpeedAvg= o*1000000000/d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
updatetime1=System.nanoTime();
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
protected TimerTask tmt2=new TimerTask() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long d=System.nanoTime()-updatetime2;
|
||||
|
||||
if(inTraffic!=null) {
|
||||
long i=inTraffic.get()-inTrafficOld2;
|
||||
inTrafficOld2 =inTraffic.get();
|
||||
inSpeedAvg2= i*1000000000/d;
|
||||
}
|
||||
|
||||
if(outTraffic!=null) {
|
||||
long o=outTraffic.get()-outTrafficOld2;
|
||||
outTrafficOld2 =outTraffic.get();
|
||||
outSpeedAvg2= o*1000000000/d;
|
||||
}
|
||||
|
||||
|
||||
updatetime2=System.nanoTime();
|
||||
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb=new StringBuilder(super.toString());
|
||||
sb.append('\t');
|
||||
sb.append(bytesUnit(outTraffic.get())).append("\t").append(bytesUnit(inTraffic.get())).append("\t").append(bytesUnit(outSpeed)).append("/s\t").append(bytesUnit(inSpeed)).append("/s\t");
|
||||
|
||||
/*if(getState()==OFFLINE) {
|
||||
sb.append((coolingTime-(System.currentTimeMillis()-mls))/1000L);
|
||||
sb.append('s');
|
||||
}else {
|
||||
sb.append('/');
|
||||
}*/
|
||||
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
tmt.cancel();
|
||||
tmt1.cancel();
|
||||
tmt2.cancel();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user