Commit aec314dd authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Added serialization for RoadNetwork to enable faster startups

parent 5540da20
package de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
......@@ -14,10 +20,12 @@ import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.contro
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.SerializableRoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.BreathFirstSearchRoutingAlgorithm;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.RoutingAlgorithm;
import de.tudresden.sumo.cmd.Edge;
import de.tudresden.sumo.cmd.Junction;
import de.tudresden.sumo.cmd.Lane;
import de.tudresden.sumo.cmd.Simulation;
......@@ -32,6 +40,7 @@ import de.tudresden.ws.container.SumoStringList;
import it.polito.appeal.traci.SumoTraciConnection;
public class TraciSimulationController implements VehicleController, SimulationSetupExtractor {
private static final File TEMP_FILE = new File(new File(System.getProperty("java.io.tmpdir")), "road_network.tmp");
private static final Map<String, TraciSimulationController> CONTROLLER = new HashMap<>();
......@@ -526,6 +535,42 @@ public class TraciSimulationController implements VehicleController, SimulationS
public void obtainRoadNetwork() {
if (_roadNetwork == null) {
if (TEMP_FILE.exists()) {
try {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(TEMP_FILE));
Object readObject = inputStream.readObject();
if (readObject instanceof SerializableRoadNetwork) {
SerializableRoadNetwork serializedRoadNetwork = (SerializableRoadNetwork) readObject;
_roadNetwork = new RoadNetwork(serializedRoadNetwork);
}
inputStream.close();
} catch (IOException | ClassNotFoundException e) {
//Nothing to do
}
}
if (_roadNetwork != null) {
SumoCommand edgeIDCommand = Edge.getIDList();
SumoStringList edgeIDStringList = (SumoStringList) requestObject(edgeIDCommand);
if (_roadNetwork.getAvailableEdges().size() == edgeIDStringList.size()) {
boolean matching = true;
for (String edgeID : edgeIDStringList) {
if (_roadNetwork.getEdge(edgeID) == null) {
matching = false;
}
}
if (matching) {
return;
}
}
}
SumoCommand laneIDCommand = Lane.getIDList();
Map<String, RoadNetworkEdge> roadNetwork = new HashMap<>();
......@@ -571,6 +616,18 @@ public class TraciSimulationController implements VehicleController, SimulationS
}
_roadNetwork = new RoadNetwork(roadNetwork);
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(TEMP_FILE));
outputStream.writeObject(new SerializableRoadNetwork(_roadNetwork));
outputStream.flush();
outputStream.close();
} catch (IOException e) {
//Nothing to do
e.printStackTrace();
}
}
}
......@@ -601,7 +658,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
public double getVehicleSpeed(String pVehicleID) {
SumoCommand speedCommand = Vehicle.getSpeed(pVehicleID);
Object object = requestObject(speedCommand);
System.out.println(object.getClass());
return (Double)object;
}
......
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