Commit 9a0b120b authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

implement setSimConfig command (0x73) in api (0xCB)

parent 2a9d2b68
......@@ -2,6 +2,7 @@ package org.vadere.manager;
import org.vadere.gui.onlinevisualization.OnlineVisualization;
import org.vadere.manager.traci.commandHandler.StateAccessHandler;
import org.vadere.manager.traci.compound.object.SimulationCfg;
import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.simulator.entrypoints.ScenarioFactory;
import org.vadere.simulator.projects.RunnableFinishedListener;
......@@ -13,6 +14,7 @@ import org.vadere.util.logging.Logger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -35,17 +37,19 @@ public class RemoteManager implements RunnableFinishedListener {
private Thread currentSimulationThread;
private boolean simulationFinished;
private boolean clientCloseCommandReceived;
private Path baseDir;
private Path defaultOutputdir; // defined by command line parameter. May be overwritten by simCfg
private boolean guiSupport;
private SimulationCfg simCfg; // received from traci client.
private List<Subscription> subscriptions;
public RemoteManager(Path baseDir, boolean guiSupport) {
this.baseDir = baseDir;
public RemoteManager(Path defaultOutputdir, boolean guiSupport) {
this.defaultOutputdir = defaultOutputdir;
this.guiSupport = guiSupport;
this.subscriptions = new ArrayList<>();
this.clientCloseCommandReceived = false;
this.simCfg = null;
}
public void loadScenario(String scenarioString, Map<String, ByteArrayInputStream> cacheData) {
......@@ -54,23 +58,35 @@ public class RemoteManager implements RunnableFinishedListener {
ScenarioCache scenarioCache;
Path scenarioPath;
Path outputDir;
if (simCfg != null) {
outputDir = Paths.get(simCfg.outputPath());
logger.infof("received output directory from traci client '%s'", simCfg.outputPath());
} else {
outputDir = defaultOutputdir;
}
try {
scenario = ScenarioFactory.createScenarioWithScenarioJson(scenarioString);
scenarioPath = baseDir.resolve(IOUtils.SCENARIO_DIR).resolve(scenario.getName() + IOUtils.SCENARIO_FILE_EXTENSION);
scenarioPath = defaultOutputdir.resolve(IOUtils.SCENARIO_DIR).resolve(scenario.getName()
+ IOUtils.SCENARIO_FILE_EXTENSION);
scenarioCache = buildScenarioCache(scenario, cacheData);
} catch (IOException e) {
throw new TraCIException("Cannot create Scenario from given file.");
}
currentSimulationRun = new RemoteScenarioRun(scenario, baseDir, this, scenarioPath, scenarioCache);
if (simCfg != null) {
scenario.getAttributesSimulation().setFixedSeed(simCfg.getSeed());
scenario.getAttributesSimulation().setUseFixedSeed(true);
logger.infof("received seed from traci client '%s'", Long.toString(simCfg.getSeed()));
}
currentSimulationRun = new RemoteScenarioRun(scenario, outputDir, this, scenarioPath, scenarioCache);
}
public void loadScenario(String scenarioString) {
loadScenario(scenarioString, null);
}
private ScenarioCache buildScenarioCache(final Scenario scenario, Map<String, ByteArrayInputStream> cacheData) {
ScenarioCache scenarioCache = ScenarioCache.load(scenario, baseDir);
ScenarioCache scenarioCache = ScenarioCache.load(scenario, defaultOutputdir);
if (scenarioCache.isEmpty()) {
if (cacheData != null) {
logger.warnf("received cache data but given Scenario has cache deactivated. Received cache will be ignored");
......@@ -165,4 +181,12 @@ public class RemoteManager implements RunnableFinishedListener {
logger.infof("Start Scenario %s with remote control...", currentSimulationRun.getScenario().getName());
currentSimulationThread.start();
}
public SimulationCfg getSimCfg() {
return simCfg;
}
public void setSimCfg(SimulationCfg simCfg) {
this.simCfg = simCfg;
}
}
......@@ -463,6 +463,11 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
System.out.println(res.toString());
}
@Override
public void simulationapi_setSimConfig(String[] args) throws IOException {
System.out.println("not implemented");
}
// polygonapi
@Override
public void polygonapi_getTopographyBounds(String[] args) throws IOException {
......
......@@ -3,6 +3,7 @@ package org.vadere.manager.traci.commandHandler;
import org.vadere.annotation.traci.client.TraCIApi;
import org.vadere.manager.RemoteManager;
import org.vadere.manager.TraCICommandCreationException;
import org.vadere.manager.TraCIException;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commandHandler.annotation.SimulationHandler;
......@@ -12,11 +13,14 @@ import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.TraCIGetCommand;
import org.vadere.manager.traci.commands.TraCISetCommand;
import org.vadere.manager.traci.commands.get.TraCIGetCacheHashCommand;
import org.vadere.manager.traci.compound.CompoundObject;
import org.vadere.manager.traci.compound.object.SimulationCfg;
import org.vadere.manager.traci.response.TraCIGetResponse;
import org.vadere.simulator.entrypoints.ScenarioFactory;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.utils.cache.ScenarioCache;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.logging.Logger;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
......@@ -39,6 +43,7 @@ import java.util.ArrayList;
)
public class SimulationCommandHandler extends CommandHandler<SimulationVar> {
private static Logger logger = Logger.getLogger(SimulationCommandHandler.class);
public static SimulationCommandHandler instance;
static {
......@@ -92,7 +97,8 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar> {
return cmd;
}
@SimulationHandler(cmd = TraCICmd.GET_SIMULATION_VALUE, var = SimulationVar.CURR_SIM_TIME, name = "getTime", ignoreElementId = true)
@SimulationHandler(cmd = TraCICmd.GET_SIMULATION_VALUE, var = SimulationVar.CURR_SIM_TIME,
name = "getTime", ignoreElementId = true)
public TraCICommand process_getSimTime(TraCIGetCommand cmd, RemoteManager remoteManager, SimulationVar traCIVar) {
remoteManager.accessState((manager, state) -> {
......@@ -104,7 +110,23 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar> {
return cmd;
}
@SimulationHandler(cmd = TraCICmd.GET_SIMULATION_VALUE, var = SimulationVar.CACHE_HASH, name = "getHash", dataTypeStr = "String", ignoreElementId = true)
@SimulationHandler(cmd = TraCICmd.SET_SIMULATION_STATE, var = SimulationVar.SIM_CONFIG,
name = "setSimConfig", ignoreElementId = true)
public TraCICommand process_setSimConfig(TraCISetCommand cmd, RemoteManager remoteManager) {
try {
SimulationCfg cfg = new SimulationCfg((CompoundObject) cmd.getVariableValue());
remoteManager.setSimCfg(cfg);
cmd.setOK();
} catch (TraCIException ex) {
logger.errorf("cannot parse setSimConfig object. Err: %s", ex.getMessage());
cmd.setErr(String.format("cannot parse setSimConfig object. Err: %s", ex.getMessage()));
}
return cmd;
}
@SimulationHandler(cmd = TraCICmd.GET_SIMULATION_VALUE, var = SimulationVar.CACHE_HASH,
name = "getHash", dataTypeStr = "String", ignoreElementId = true)
public TraCICommand process_getCacheHash(TraCIGetCommand rawCmd, RemoteManager remoteManager) {
try {
......@@ -185,7 +207,8 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar> {
TraCISetCommand cmd = (TraCISetCommand) rawCmd;
SimulationVar var = SimulationVar.fromId(cmd.getVariableId());
switch (var) {
case CACHE_HASH:
case SIM_CONFIG:
return process_setSimConfig(cmd, remoteManager);
default:
return process_NotImplemented(cmd, remoteManager);
}
......
......@@ -19,6 +19,7 @@ public enum SimulationVar {
//
NETWORK_BOUNDING_BOX_2D(0x7c, TraCIDataType.POLYGON),
CACHE_HASH(0x7d, TraCIDataType.STRING),
SIM_CONFIG(0x7e, TraCIDataType.COMPOUND_OBJECT),
;
public int id;
......
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