diff --git a/java/org/contikios/cooja/LogScriptEngine.java b/java/org/contikios/cooja/LogScriptEngine.java index dd4ce7fbe..8b8ab762d 100644 --- a/java/org/contikios/cooja/LogScriptEngine.java +++ b/java/org/contikios/cooja/LogScriptEngine.java @@ -108,9 +108,6 @@ public void removedLogOutput(LogOutputEvent ev) { private Thread scriptThread = null; /* Script thread */ private final Simulation simulation; - private long timeout; - private long startTime; - private long startRealTime; private final JTextArea textArea; protected LogScriptEngine(Simulation simulation, int logNumber, JTextArea logTextArea) { @@ -196,10 +193,7 @@ protected void closeLog() { * Deactivate script */ public void deactivateScript() { - timeoutProgressEvent.remove(); - engine.put("SHUTDOWN", true); - try { if (semaphoreScript != null) { semaphoreScript.release(100); @@ -233,7 +227,7 @@ public void deactivateScript() { * Does not alter internal engine state that is difficult to undo. */ public CompiledScript compileScript(String code) throws ScriptException { ScriptParser parser = new ScriptParser(code); - timeout = parser.getTimeoutTime(); + var timeout = parser.getTimeoutTime(); if (timeout < 0) { timeout = DEFAULT_TIMEOUT; } @@ -288,26 +282,9 @@ public boolean activateScript(final CompiledScript script) { deactivateScript(); return false; } - startRealTime = System.currentTimeMillis(); - startTime = simulation.getSimulationTime(); - simulation.invokeSimulationThread(() -> - simulation.scheduleEvent(timeoutProgressEvent, startTime + Math.max(1000, timeout / 20))); return true; } - private final TimeEvent timeoutProgressEvent = new TimeEvent() { - @Override - public void execute(long t) { - simulation.scheduleEvent(this, t + Math.max(1000, timeout / 20)); - - double progress = 1.0*(t - startTime)/timeout; - long realDuration = System.currentTimeMillis()-startRealTime; - double estimatedLeft = 1.0*realDuration/progress - realDuration; - if (estimatedLeft == 0) estimatedLeft = 1; - logger.info(String.format("%2.0f%% completed, %2.1f sec remaining", 100*progress, estimatedLeft/1000)); - } - }; - private final ScriptLog scriptLog = new ScriptLog() { @Override public void log(String msg) { diff --git a/java/org/contikios/cooja/Simulation.java b/java/org/contikios/cooja/Simulation.java index 4eed0ad21..eb558d0ac 100644 --- a/java/org/contikios/cooja/Simulation.java +++ b/java/org/contikios/cooja/Simulation.java @@ -132,6 +132,18 @@ public record MoteRelation(Mote source, Mote dest, Color color) {} private final EventTriggers moteRelationsTriggers = new EventTriggers<>(); private final SimConfig cfg; + private final TimeEvent progressEvent = new TimeEvent() { + @Override + public void execute(long t) { + scheduleEvent(this, t + Math.max(1000, timeout / 20)); + double progress = 1.0 * (t - lastStartSimulationTime) / timeout; + long realDuration = System.currentTimeMillis() - lastStartRealTime; + double estimatedLeft = 1.0 * realDuration / progress - realDuration; + if (estimatedLeft == 0) estimatedLeft = 1; + logger.info(String.format("%2.0f%% completed, %2.1f sec remaining", 100 * progress, estimatedLeft / 1000)); + } + }; + private final TimeEvent delayEvent = new TimeEvent() { @Override public void execute(long t) { @@ -538,8 +550,11 @@ public void removeScriptEngine(LogScriptEngine engine) { /** Set the timeout for the simulation. There is no timeout by default. */ public void setTimeout(long timeout) { - this.timeout = timeout; - logger.info("Simulation timeout in " + (timeout / MILLISECOND) + " ms"); + invokeSimulationThread(() -> { + this.timeout = timeout; + logger.info("Simulation timeout in " + (timeout / MILLISECOND) + " ms"); + scheduleEvent(progressEvent, currentSimulationTime + Math.max(1000, timeout / 20)); + }); } /**