diff --git a/zond/src/main/java/asys/zond/PingMonitor.java b/zond/src/main/java/asys/zond/PingMonitor.java new file mode 100644 index 0000000..944bcec --- /dev/null +++ b/zond/src/main/java/asys/zond/PingMonitor.java @@ -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; + } +} diff --git a/zond/src/main/resources/zond.properties b/zond/src/main/resources/zond.properties index 924f988..ce34e16 100644 --- a/zond/src/main/resources/zond.properties +++ b/zond/src/main/resources/zond.properties @@ -2,4 +2,5 @@ serverId = SpigotServer0 host = 127.0.0.1 port = 8779 passcode = testpassphrase -bridge.port = 8710 \ No newline at end of file +bridge.port = 8710 +pingmonitor.delay = 2100 \ No newline at end of file