Commit 80b09f17 authored by Christina Maria Mayr's avatar Christina Maria Mayr
Browse files

Merge branch 'close_thread' into 'master'

[Remote] Fix bug in remote simulation run

See merge request !158
parents abf2779a 40a99905
Pipeline #527897 passed with stages
in 151 minutes and 34 seconds
......@@ -53,10 +53,13 @@ public class RemoteScenarioRun extends ScenarioRun implements RemoteRunListener
return true;
}
synchronized public SimThreadState getCurrentSimThreadState(){
synchronized public SimThreadState getCurrentSimThreadState(){
if (simulation == null){
return SimThreadState.INIT;
}
else if (simulation.getThreadState() == null){
return SimThreadState.INIT;
}
return simulation.getThreadState();
}
......
......@@ -57,6 +57,18 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
TraCICloseCommand cmd = (TraCICloseCommand) rawCmd;
remoteManager.setClientCloseCommandReceived(true);
if (remoteManager.getCurrentSimThreadState().equals(SimThreadState.MAIN_LOOP)) {
logger.info("Current simulation run in main loop. Stop simulation.");
remoteManager.getRemoteSimulationRun().setIsRunSimulation(false); // is_running = false in Simulation!
logger.info("Wake up simulation thread.");
remoteManager.getRemoteSimulationRun().notifySimulationThread(); // wake up simulation thread
logger.info("Wait for simulation end.");
remoteManager.waitForSimulationEnd(); // wait for simulation thread
logger.info("Wait for simulation end finished.");
}
if (remoteManager.stopSimulationIfRunning())
cmd.getResponse().getStatusResponse().setDescription("Stop simulation waiting for client close EOF");
......@@ -70,6 +82,7 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
remoteManager.getSubscriptions().forEach(sub -> sub.executeSubscription(remoteManager));
// get responses
TraCIGetStateResponse response = new TraCIGetStateResponse(
new StatusResponse(cmd.getTraCICmd(), TraCIStatusResponse.OK, ""));
......@@ -89,6 +102,7 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
if (!remoteManager.nextStep(cmd.getTargetTime())) {
// Simulation finished. Wait until simulation thread finishes with post loop before
// telling traci client
// TODO: check if this is reachable
if (remoteManager.getCurrentSimThreadState().equals(SimThreadState.MAIN_LOOP)){
remoteManager.waitForSimulationEnd();
}
......@@ -123,10 +137,14 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
// Simulation finished. Wait until simulation thread finishes with post loop before
// telling traci client
if (remoteManager.getCurrentSimThreadState().equals(SimThreadState.MAIN_LOOP)){
remoteManager.waitForSimulationEnd();
logger.errorf("Main loop not finished.");
}
cmd.setResponse(TraCISimTimeResponse.simEndReached());
logger.info("Sent simEndReached command to client.");
remoteManager.notifySimulationThread();
logger.info("Notified simulation thread");
}
logger.debug("process_simStep done.");
......
......@@ -17,6 +17,7 @@ import org.vadere.manager.traci.response.TraCIGetVersionResponse;
import org.vadere.manager.traci.response.TraCIResponse;
import org.vadere.manager.traci.response.TraCISimTimeResponse;
import org.vadere.manager.traci.response.TraCIStatusResponse;
import org.vadere.simulator.control.simulation.SimThreadState;
import java.util.HashMap;
......@@ -31,8 +32,7 @@ public class ControlCommandHandlerTest extends CommandHandlerTest {
private ControlCommandHandler ctrCmdHandler = ControlCommandHandler.instance;
@Test
public void process_close() {
private void process_close_(SimThreadState simThreadState) {
TraCICmd traciCmd = TraCICmd.CLOSE;
CmdType cmdType = traciCmd.type;
......@@ -40,6 +40,7 @@ public class ControlCommandHandlerTest extends CommandHandlerTest {
TraCICloseCommand rawCmd = (TraCICloseCommand) getFirstCommand(TraCICloseCommand.build());
RemoteManager rm = mock(RemoteManager.class, Mockito.RETURNS_DEEP_STUBS);
doNothing().when(rm).setClientCloseCommandReceived(true);
when(rm.getCurrentSimThreadState()).thenReturn(simThreadState);
TraCICommand cmd = ctrCmdHandler.process_close(rawCmd, rm);
testTraCICommand(cmd, traciCmd, cmdType);
......@@ -50,6 +51,23 @@ public class ControlCommandHandlerTest extends CommandHandlerTest {
TraCIResponse res = closeCommand.getResponse();
}
@Test
public void process_close(){
process_close_(SimThreadState.INIT);
}
@Test
public void process_close_mainLoop(){
process_close_(SimThreadState.MAIN_LOOP);
}
@Test
public void process_close_postLoop(){
process_close_(SimThreadState.POST_LOOP);
}
@Test
public void process_simStep() {
......
......@@ -267,7 +267,12 @@ public class ScenarioRun implements Runnable {
return simulation != null && simulation.isRunning();
}
public void setIsRunSimulation(boolean running) {
if (simulation != null) {
simulation.setIsRunSimulation(running);
}
}
public boolean isScenarioInSingleStepMode(){
return simulation != null && simulation.isSingleStepMode();
......
......@@ -333,8 +333,8 @@ public class Simulation implements ControllerProvider{
// check reached next simTime (-1 simulate one step)
// round to long to ensure correct trap.
boolean timeReached = Math.round(simTimeInSec) >= Math.round(simulateUntilInSec);
if (timeReached || simulateUntilInSec == -1){
logger.debugf("Simulated until: %.4f", simTimeInSec);
if (timeReached && isRunSimulation){
logger.debugf("Synchronized reached at: %.4f. Wait for traci commands.", simTimeInSec);
waitForTraci();
}
}
......@@ -620,4 +620,8 @@ public class Simulation implements ControllerProvider{
return processorManager;
}
public void setIsRunSimulation(boolean running){
isRunSimulation = running;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment