Archived
0

Zond: class PingMonitor

This commit is contained in:
2017-07-21 16:09:19 +03:00
parent 2a7599ec61
commit 2a1aad2f9f
2 changed files with 47 additions and 1 deletions

View 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;
}
}

View File

@@ -2,4 +2,5 @@ serverId = SpigotServer0
host = 127.0.0.1
port = 8779
passcode = testpassphrase
bridge.port = 8710
bridge.port = 8710
pingmonitor.delay = 2100