Commit 63e8dc76 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add getCash TraCI Method.

This allows to calculate the hash value of a given scenario to find
the correct cache version.
parent 6e908aeb
Pipeline #177555 passed with stages
in 127 minutes and 13 seconds
......@@ -160,25 +160,37 @@ public class ClientAnnotationProcessor extends AbstractProcessor {
}
protected void writeGET(PrintWriter writer, ApiHandler apiHandler){
if (apiHandler.ignoreElementId){
writer.append("\tpublic TraCIGetResponse ").append(apiHandler.name).append("() throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCIGetCommand.build(").append(apiHandler.cmd).append(", ").append(apiHandler.varId).append(", \"-1\");").println();
}
else {
writer.append("\tpublic TraCIGetResponse ").append(apiHandler.name).append("(String elementID) throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCIGetCommand.build(").append(apiHandler.cmd).append(", ").append(apiHandler.varId).append(", elementID);").println();
if (apiHandler.dataTypeStr.isEmpty()){
// standard GET command without additional data
if (apiHandler.ignoreElementId){
writer.append("\tpublic TraCIResponse ").append(apiHandler.name).append("() throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCIGetCommand.build(").append(apiHandler.cmd).append(", ").append(apiHandler.varId).append(", \"-1\");").println();
}
else {
writer.append("\tpublic TraCIResponse ").append(apiHandler.name).append("(String elementID) throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCIGetCommand.build(").append(apiHandler.cmd).append(", ").append(apiHandler.varId).append(", elementID);").println();
}
} else {
// extended GET command which accepts any kind of data based on the standard traci data types
if (apiHandler.ignoreElementId){
writer.append("\tpublic TraCIResponse ").append(apiHandler.name).append("(").append(apiHandler.dataTypeStr).append(" data) throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCIGetCommand.build(").append(apiHandler.cmd).append(", \"-1\" , ").append(apiHandler.varId).append(", ").append(apiHandler.varType).append(", data);").println();
} else {
writer.append("\tpublic TraCIResponse ").append(apiHandler.name).append("(String elementId, ").append(apiHandler.dataTypeStr).append(" data) throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCIGetCommand.build(").append(apiHandler.cmd).append(", elementId, ").append(apiHandler.varId).append(", ").append(apiHandler.varType).append(", data);").println();
}
}
writer.append("\n\t\tsocket.sendExact(p);\n").println();
writer.append("\t\treturn (TraCIGetResponse) socket.receiveResponse();").println();
writer.append("\t\treturn socket.receiveResponse();").println();
writer.append("\t}").println();
writer.println();
}
protected void writeSET(PrintWriter writer, ApiHandler apiHandler){
writer.append("\tpublic TraCIResponse ").append(apiHandler.name).append("(String elementId, ").append(apiHandler.dataTypeStr).append(" data) throws IOException {").println();
writer.append("\t\tTraCIPacket p = TraCISetCommand.build(")
.append(apiHandler.cmd).append(", elementId, ").append(apiHandler.varId).append(", ").append(apiHandler.varType).append(", data);").println();
writer.append("\t\tTraCIPacket p = TraCISetCommand.build(").append(apiHandler.cmd).append(", elementId, ").append(apiHandler.varId).append(", ").append(apiHandler.varType).append(", data);").println();
writer.append("\n\t\tsocket.sendExact(p);\n").println();
writer.append("\t\treturn socket.receiveResponse();").println();
......
package org.vadere.manager;
public class TraCICommandCreationException extends TraCIException {
public TraCICommandCreationException(String message) {
super(message);
}
public TraCICommandCreationException(String message, Object... arg) {
super(message, arg);
}
public TraCICommandCreationException(String message, Throwable cause, Object... arg) {
super(message, cause, arg);
}
public TraCICommandCreationException(String message, Throwable cause) {
super(message, cause);
}
}
......@@ -22,7 +22,6 @@ import java.net.Socket;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
public class TestClient extends org.vadere.manager.client.AbstractTestClient implements Runnable{
......@@ -176,7 +175,6 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
System.out.println(cmd.toString());
}
void sendFile(String[] args) throws IOException {
String filePath;
......@@ -215,22 +213,31 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
System.out.println(cmd.toString());
}
private void printGet(TraCIResponse res){
if (res.isErr()){
System.out.println(res.toString());
} else {
System.out.println(res.getStatusResponse().toString());
System.out.println("--> " + ((TraCIGetResponse)res).getResponseData());
}
}
@Override
public void personapi_getIDList(String[] args) throws IOException {
TraCIGetResponse res = personapi.getIDList();
System.out.println(res.getResponseData());
TraCIResponse res = personapi.getIDList();
printGet(res);
}
@Override
public void personapi_getNextFreeId(String[] args) throws IOException {
TraCIGetResponse res = personapi.getNextFreeId();
System.out.println(res.getResponseData());
TraCIResponse res = personapi.getNextFreeId();
printGet(res);
}
@Override
public void personapi_getIDCount(String[] args) throws IOException {
TraCIGetResponse res = personapi.getIDCount();
System.out.println(res.getResponseData());
TraCIResponse res = personapi.getIDCount();
printGet(res);
}
@Override
......@@ -242,7 +249,7 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
String elementIdentifier = args[1];
try {
TraCIGetResponse res = personapi.getSpeed(elementIdentifier);
TraCIGetResponse res = (TraCIGetResponse)personapi.getSpeed(elementIdentifier);
double p = (double) res.getResponseData();
System.out.println(p);
} catch (ClassCastException e){
......@@ -271,7 +278,7 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
return;
}
String elementIdentifier = args[1];
TraCIGetResponse res = personapi.getPosition2D(elementIdentifier);
TraCIGetResponse res = (TraCIGetResponse)personapi.getPosition2D(elementIdentifier);
VPoint p = (VPoint) res.getResponseData();
System.out.println(p.toString());
}
......@@ -298,8 +305,8 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
@Override
public void personapi_getPosition2DList(String[] args) throws IOException {
TraCIGetResponse res = personapi.getPosition2DList();
System.out.println(((Map<String, VPoint>) res.getResponseData()).toString());
TraCIResponse res = personapi.getPosition2DList();
printGet(res);
}
@Override
......@@ -335,7 +342,7 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
}
String elementIdentifier = args[1];
TraCIGetResponse res = personapi.getTargetList(elementIdentifier);
TraCIGetResponse res = (TraCIGetResponse)personapi.getTargetList(elementIdentifier);
ArrayList<String> targets = (ArrayList<String>) res.getResponseData();
System.out.println(elementIdentifier + ": " + Arrays.toString(targets.toArray()));
}
......@@ -377,4 +384,26 @@ public class TestClient extends org.vadere.manager.client.AbstractTestClient imp
System.out.println(res.toString());
}
@Override
public void simulationapi_getHash(String[] args) throws IOException {
String data;
try{
data = IOUtils.readTextFile(Paths.get(basePath, defaultScenario).toString());
} catch (IOException e){
System.out.println("File not found: " + Paths.get(basePath, defaultScenario).toString());
return;
}
TraCIResponse cmd = simulationapi.getHash(data);
System.out.println(cmd.toString());
}
@Override
public void simulationapi_getTime(String[] args) throws IOException {
TraCIResponse res = simulationapi.getTime();
System.out.println(res.toString());
}
}
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.traci.TraCICmd;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commandHandler.annotation.SimulationHandler;
......@@ -8,10 +10,15 @@ import org.vadere.manager.traci.commandHandler.annotation.SimulationHandlers;
import org.vadere.manager.traci.commandHandler.variables.SimulationVar;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.TraCIGetCommand;
import org.vadere.manager.traci.commands.get.TraCIGetCacheHashCommand;
import org.vadere.manager.traci.respons.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 java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
......@@ -20,6 +27,14 @@ import java.util.ArrayList;
/**
* Handel GET/SET/SUB {@link org.vadere.manager.traci.commands.TraCICommand}s for the Simulation API
*/
@TraCIApi(
name = "SimulationAPI",
nameShort = "sim",
singleAnnotation = SimulationHandler.class,
multipleAnnotation = SimulationHandlers.class,
cmdEnum = TraCICmd.class,
varEnum = SimulationVar.class
)
public class SimulationCommandHandler extends CommandHandler<SimulationVar>{
public static SimulationCommandHandler instance;
......@@ -55,6 +70,10 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar>{
return responseOK(responseDataType, responseData, TraCICmd.GET_SIMULATION_VALUE, TraCICmd.RESPONSE_GET_SIMULATION_VALUE);
}
public TraCIGetResponse responseERR(SimulationVar var, String err){
return responseERR("["+var.toString()+"] "+ err, TraCICmd.GET_SIMULATION_VALUE, TraCICmd.RESPONSE_GET_SIMULATION_VALUE);
}
public TraCICommand process_getNetworkBound(TraCIGetCommand cmd, RemoteManager remoteManager, SimulationVar traCIVar){
remoteManager.accessState((manager, state) -> {
......@@ -71,17 +90,40 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar>{
return cmd;
}
@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) -> {
// BigDecimal to ensure correct comparison in omentpp
BigDecimal time = BigDecimal.valueOf(state.getSimTimeInSec());
cmd.setResponse(responseOK(traCIVar.type, time.setScale(1, RoundingMode.HALF_UP).doubleValue()));
cmd.setResponse(responseOK(SimulationVar.CURR_SIM_TIME.type, time.setScale(1, RoundingMode.HALF_UP).doubleValue()));
});
return cmd;
}
@SimulationHandler(cmd = TraCICmd.GET_SIMULATION_VALUE, var = SimulationVar.CACHE_HASH, name = "getHash", dataTypeStr = "String", ignoreElementId = true)
public TraCICommand process_get_cache_hash(TraCIGetCommand rawCmd, RemoteManager remoteManager){
try{
TraCIGetCacheHashCommand cmd = TraCIGetCacheHashCommand.create(rawCmd);
if (cmd.getFile().length() > 0){
try {
Scenario scenario = ScenarioFactory.createScenarioWithScenarioJson(cmd.getFile());
String hash = ScenarioCache.getHash(scenario);
cmd.setResponse(responseOK(SimulationVar.CACHE_HASH.type, hash));
} catch (IOException e) {
cmd.setResponse(responseERR(SimulationVar.CACHE_HASH,"cannot read scenario"));
}
}
return cmd;
} catch (TraCICommandCreationException ee){
rawCmd.setResponse(responseERR(SimulationVar.CACHE_HASH,"Ill formatted TraCIGetCacheHashCommand"));
}
return rawCmd;
}
public TraCICommand process_getVehiclesStartTeleportIDs(TraCIGetCommand cmd, RemoteManager remoteManager, SimulationVar traCIVar){
cmd.setResponse(responseOK(traCIVar.type, new ArrayList<>()));
......@@ -129,7 +171,8 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar>{
return process_getVehiclesStartParkingIDs(cmd, remoteManager, var);
case VEHICLES_STOP_PARKING_IDS:
return process_getVehiclesStopParkingIDs(cmd, remoteManager, var);
case CACHE_HASH:
return process_get_cache_hash(cmd, remoteManager);
default:
return process_NotImplemented(cmd, remoteManager);
}
......
......@@ -25,4 +25,6 @@ public @interface SimulationHandler {
TraCICmd cmd();
SimulationVar var() ;
String name(); // name of client function.
boolean ignoreElementId() default false;
String dataTypeStr() default "";
}
......@@ -17,8 +17,8 @@ public enum SimulationVar {
VEHICLES_START_PARKING_IDS(0x6d, TraCIDataType.STRING_LIST),
VEHICLES_STOP_PARKING_IDS(0x6f, TraCIDataType.STRING_LIST),
//
NETWORK_BOUNDING_BOX_2D(0x7c, TraCIDataType.POLYGON)
NETWORK_BOUNDING_BOX_2D(0x7c, TraCIDataType.POLYGON),
CACHE_HASH(0x7d, TraCIDataType.STRING)
;
public int id;
......
package org.vadere.manager.traci.commands;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commandHandler.variables.PersonVar;
import org.vadere.manager.traci.writer.TraCIPacket;
import org.vadere.manager.traci.reader.TraCICommandBuffer;
......@@ -30,6 +31,7 @@ public class TraCIGetCommand extends TraCICommand {
protected int variableIdentifier;
protected String elementIdentifier;
protected TraCICommandBuffer cmdBuffer;
private TraCIGetResponse response;
......@@ -45,16 +47,25 @@ public class TraCIGetCommand extends TraCICommand {
return packet;
}
public static TraCIPacket build(TraCICmd commandIdentifier, String elementIdentifier, int variableIdentifier, TraCIDataType dataType, Object data){
return TraCIPacket.create()
.wrapCommand(commandIdentifier, elementIdentifier, variableIdentifier,
dataType, data);
}
public TraCIGetCommand(TraCICmd traCICmd, int variableIdentifier, String elementIdentifier) {
super(traCICmd);
this.variableIdentifier = variableIdentifier;
this.elementIdentifier = elementIdentifier;
this.cmdBuffer = TraCICommandBuffer.empty();
}
public TraCIGetCommand(TraCICmd traCICmd, TraCICommandBuffer cmdBuffer) {
super(traCICmd);
variableIdentifier = cmdBuffer.readUnsignedByte();
elementIdentifier = cmdBuffer.readString();
this.variableIdentifier = cmdBuffer.readUnsignedByte();
this.elementIdentifier = cmdBuffer.readString();
this.cmdBuffer = cmdBuffer;
}
public int getVariableIdentifier() {
......@@ -83,6 +94,10 @@ public class TraCIGetCommand extends TraCICommand {
this.response = response;
}
public TraCICommandBuffer getCmdBuffer() {
return cmdBuffer;
}
@Override
public TraCIPacket buildResponsePacket() {
return TraCIPacket.create().wrapGetResponse(response);
......
......@@ -38,7 +38,7 @@ public class TraCISetCommand extends TraCICommand{
public static TraCIPacket build(TraCICmd commandIdentifier, String elementIdentifier, int variableIdentifier, TraCIDataType dataType, Object data){
return TraCIPacket.create()
.wrapSetCommand(commandIdentifier, elementIdentifier, variableIdentifier,
.wrapCommand(commandIdentifier, elementIdentifier, variableIdentifier,
dataType, data);
}
......
......@@ -10,7 +10,7 @@ import java.nio.charset.StandardCharsets;
public class TraCISendFileCommand extends TraCICommand {
private String fileName;
private String file;
private String file; // file content
public static TraCIPacket TraCISendFileCommand(String fileName, String file){
int strLen = file.getBytes(StandardCharsets.US_ASCII).length;
......
package org.vadere.manager.traci.commands.get;
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.variables.SimulationVar;
import org.vadere.manager.traci.commands.TraCIGetCommand;
public class TraCIGetCacheHashCommand extends TraCIGetCommand {
private String file; // file content
public TraCIGetCacheHashCommand(TraCICmd traCICmd, int variableIdentifier, String elementIdentifier) {
super(traCICmd, variableIdentifier, elementIdentifier);
this.file = "";
}
public TraCIGetCacheHashCommand(TraCIGetCommand getCmd) {
super(getCmd.getTraCICmd(), getCmd.getVariableIdentifier(), getCmd.getElementIdentifier());
getCmd.getCmdBuffer().ensureBytes(1);
TraCIDataType dataType = TraCIDataType.fromId(getCmd.getCmdBuffer().readUnsignedByte());
if (!dataType.equals(TraCIDataType.STRING)){
throw new TraCIException("expected String value in TraCIGetCacheHashCommand");
}
this.file = getCmd.getCmdBuffer().readString();
}
static public TraCIGetCacheHashCommand create(TraCIGetCommand getCmd) {
if (getCmd.getTraCICmd().equals(TraCICmd.GET_SIMULATION_VALUE) &&
getCmd.getVariableIdentifier() == SimulationVar.CACHE_HASH.id ){
return new TraCIGetCacheHashCommand(getCmd);
} else {
throw new TraCICommandCreationException("cannot create TraCIGetCacheHashCommand from %s", getCmd.toString());
}
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}
......@@ -64,6 +64,14 @@ public class StatusResponse {
this.description = description;
}
boolean isErr(){
return response.equals(TraCIStatusResponse.ERR);
}
boolean isOk(){
return response.equals(TraCIStatusResponse.OK);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
......@@ -100,6 +100,19 @@ public class TraCIResponse {
this.responseIdentifier = responseIdentifier;
}
public boolean isErr(){
return this.statusResponse.getResponse().equals(TraCIStatusResponse.ERR);
}
public boolean isOK(){
return this.statusResponse.getResponse().equals(TraCIStatusResponse.OK);
}
public boolean isNotImpl(){
return this.statusResponse.getResponse().equals(TraCIStatusResponse.NOT_IMPLEMENTED);
}
@Override
public String toString() {
return "TraCIResponse{" +
......
......@@ -102,8 +102,8 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter{
return new ByteArrayOutputStreamTraCIWriter();
}
public TraCIPacket wrapSetCommand(TraCICmd commandIdentifier, String elementIdentifier,
int variableIdentifier, TraCIDataType dataType, Object data){
public TraCIPacket wrapCommand(TraCICmd commandIdentifier, String elementIdentifier,
int variableIdentifier, TraCIDataType dataType, Object data){
TraCIWriter cmdBuilder = getCmdBuilder();
cmdBuilder.writeUnsignedByte(commandIdentifier.id)
......
......@@ -42,6 +42,19 @@ public class ScenarioCache {
return new ScenarioCache(scenario, cacheParentDir);
}
public static String getHash(final Scenario scenario){
Topography topography = scenario.getTopography();
AttributesFloorField attFF = scenario.getModelAttributes()
.stream()
.filter(a -> a instanceof AttributesFloorField)
.map(a ->(AttributesFloorField)a)
.findFirst().orElse(null);
if(attFF != null) {
return StateJsonConverter.getFloorFieldHash(topography, attFF);
}
return null;
}
private ScenarioCache(){
this.empty = true;
this.scenario = null;
......
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