Skip to content

Commit

Permalink
Move progress event to Simulation
Browse files Browse the repository at this point in the history
This removes the duplicate progress reports when
using multiple script engines.
  • Loading branch information
pjonsson committed Dec 31, 2022
1 parent 8fadc67 commit fb8d00a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
25 changes: 1 addition & 24 deletions java/org/contikios/cooja/LogScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 17 additions & 2 deletions java/org/contikios/cooja/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public record MoteRelation(Mote source, Mote dest, Color color) {}
private final EventTriggers<AddRemove, MoteRelation> 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) {
Expand Down Expand Up @@ -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));
});
}

/**
Expand Down

0 comments on commit fb8d00a

Please sign in to comment.