Zond: class PingMonitor
This commit is contained in:
45
zond/src/main/java/asys/zond/PingMonitor.java
Normal file
45
zond/src/main/java/asys/zond/PingMonitor.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package asys.zond;
|
||||||
|
|
||||||
|
public class PingMonitor {
|
||||||
|
private static final long STEP_PING = 5000;
|
||||||
|
private final long delayStart;
|
||||||
|
private long lastPingTime;
|
||||||
|
private Thread threadPingMon;
|
||||||
|
|
||||||
|
public PingMonitor(long delayStart) {
|
||||||
|
this.delayStart = delayStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(final Runnable callback) {
|
||||||
|
this.threadPingMon = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
Thread.sleep(delayStart);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
|
long currentTimeMillis = System.currentTimeMillis();
|
||||||
|
if ((currentTimeMillis - lastPingTime) >= (2*STEP_PING)) { // если пропущено два пинга
|
||||||
|
callback.run(); // запускаем код завершения процесса
|
||||||
|
return; // завершаем поток
|
||||||
|
} else {
|
||||||
|
lastPingTime = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(STEP_PING);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastPingTime = 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
threadPingMon.interrupt();
|
||||||
|
threadPingMon = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,4 +2,5 @@ serverId = SpigotServer0
|
|||||||
host = 127.0.0.1
|
host = 127.0.0.1
|
||||||
port = 8779
|
port = 8779
|
||||||
passcode = testpassphrase
|
passcode = testpassphrase
|
||||||
bridge.port = 8710
|
bridge.port = 8710
|
||||||
|
pingmonitor.delay = 2100
|
||||||
Reference in New Issue
Block a user