Commit 0e1b2bff authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add GetStateCommand

parent 3a97ebf9
Pipeline #433824 passed with stages
in 121 minutes and 5 seconds
......@@ -13,6 +13,12 @@ import org.vadere.util.logging.Logger;
import java.util.Arrays;
/**
* Wrapper around a given TraCIValueSubscriptionCommand to execute the
* subscription
* todo: merge multiple subscription so no unnecessary duplicates are send send
*/
public class Subscription {
private static Logger logger = Logger.getLogger(Subscription.class);
......@@ -30,6 +36,13 @@ public class Subscription {
}
public void executeSubscription(RemoteManager remoteManager) {
// todo check if subscription is still valid.
// markForRemoval();
// if (markedForRemoval){
// return;
// }
TraCISubscriptionResponse subResponse = new TraCISubscriptionResponse(
new StatusResponse(valueSubscriptionCommand.getTraCICmd(), TraCIStatusResponse.OK, ""),
responseIdentifier, valueSubscriptionCommand.getElementIdentifier(), valueSubscriptionCommand.getNumberOfVariables());
......@@ -62,6 +75,12 @@ public class Subscription {
}
}
public void markIfOld(double simTime){
if (this.valueSubscriptionCommand.getEndTime() >= simTime){
markForRemoval();
}
}
public void markForRemoval() {
this.markedForRemoval = true;
}
......
......@@ -17,6 +17,7 @@ public enum TraCICmd {
CLOSE(0x7F, CmdType.CTRL, ControlVar::asString),
LOAD(0x01, CmdType.CTRL, ControlVar::asString),
SET_ORDER(0x03, CmdType.CTRL, ControlVar::asString),
GET_STATE(0x04, CmdType.CTRL, ControlVar::asString),
SEND_FILE(0x75, CmdType.CTRL, ControlVar::asString),
// Value Retrieval
GET_INDUCTION_LOOP(0xa0, CmdType.VALUE_GET),
......
package org.vadere.manager.traci.commandHandler;
import com.sun.jdi.InvocationException;
import org.vadere.manager.RemoteManager;
import org.vadere.manager.TraCIException;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.TraCISetCommand;
......@@ -11,7 +8,6 @@ import org.vadere.manager.traci.writer.TraCIPacket;
import org.vadere.util.logging.Logger;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
/**
* Dispatcher for {@link TraCICommand}s.
......@@ -29,6 +25,7 @@ public class CommandExecutor {
cmdMap.put(TraCICmd.GET_VERSION.id, ControlCommandHandler.instance::process_getVersion);
cmdMap.put(TraCICmd.LOAD.id, ControlCommandHandler.instance::process_load);
cmdMap.put(TraCICmd.SIM_STEP.id, ControlCommandHandler.instance::process_simStep);
cmdMap.put(TraCICmd.GET_STATE.id, ControlCommandHandler.instance::process_getState);
cmdMap.put(TraCICmd.CLOSE.id, ControlCommandHandler.instance::process_close);
cmdMap.put(TraCICmd.SEND_FILE.id, ControlCommandHandler.instance::process_load_file);
cmdMap.put(TraCICmd.GET_PERSON_VALUE.id, PersonCommandHandler.instance::processGet);
......
......@@ -6,7 +6,6 @@ import org.vadere.manager.Subscription;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.TraCIGetCommand;
import org.vadere.manager.traci.commands.TraCIValueSubscriptionCommand;
import org.vadere.manager.traci.response.StatusResponse;
import org.vadere.manager.traci.response.TraCIGetResponse;
......@@ -17,9 +16,7 @@ import org.vadere.util.logging.Logger;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
......@@ -123,13 +120,8 @@ public abstract class CommandHandler<VAR extends Enum> {
TraCICmd apiCmdResponse) {
TraCIValueSubscriptionCommand cmd = (TraCIValueSubscriptionCommand) rawCmd;
List<TraCIGetCommand> getCommands = new ArrayList<>();
cmd.buildGetCommands(getCommand);
cmd.getVariables().forEach(var -> {
getCommands.add(new TraCIGetCommand(getCommand, var, cmd.getElementIdentifier()));
});
cmd.setGetCommands(getCommands);
// add correct Get-Handler and the subscription command to the remoteManager
// after each SIM_STEP command the remoteMangers knows how to gather
// the subscribed variables. It is the responsibility of the
......
......@@ -10,15 +10,8 @@ import org.vadere.manager.traci.commandHandler.annotation.ControlHandler;
import org.vadere.manager.traci.commandHandler.annotation.ControlHandlers;
import org.vadere.manager.traci.commandHandler.variables.ControlVar;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.control.TraCICloseCommand;
import org.vadere.manager.traci.commands.control.TraCIGetVersionCommand;
import org.vadere.manager.traci.commands.control.TraCISendFileCommand;
import org.vadere.manager.traci.commands.control.TraCISendFileCommandV20_0_1;
import org.vadere.manager.traci.commands.control.TraCISimStepCommand;
import org.vadere.manager.traci.response.StatusResponse;
import org.vadere.manager.traci.response.TraCIGetVersionResponse;
import org.vadere.manager.traci.response.TraCISimTimeResponse;
import org.vadere.manager.traci.response.TraCIStatusResponse;
import org.vadere.manager.traci.commands.control.*;
import org.vadere.manager.traci.response.*;
import org.vadere.util.logging.Logger;
import java.lang.reflect.Method;
......@@ -71,6 +64,23 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
return cmd;
}
public TraCICommand process_getState(TraCICommand rawCmd, RemoteManager remoteManager){
TraCIGetStateCommand cmd = (TraCIGetStateCommand) rawCmd;
remoteManager.getSubscriptions().forEach(sub -> sub.executeSubscription(remoteManager));
// get responses
TraCIGetStateResponse response = new TraCIGetStateResponse(
new StatusResponse(cmd.getTraCICmd(), TraCIStatusResponse.OK, ""));
remoteManager.getSubscriptions().forEach(sub -> {
response.addSubscriptionResponse(sub.getValueSubscriptionCommand().getResponse());
});
cmd.setResponse(response);
return cmd;
}
public TraCICommand process_simStep(TraCICommand rawCmd, RemoteManager remoteManager) {
TraCISimStepCommand cmd = (TraCISimStepCommand) rawCmd;
......@@ -84,9 +94,7 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
logger.debugf("%s: execute %d subscriptions",
TraCICmd.SIM_STEP.name(),
remoteManager.getSubscriptions().size());
remoteManager.getSubscriptions().forEach(sub -> {
sub.executeSubscription(remoteManager);
});
remoteManager.getSubscriptions().forEach(sub -> sub.executeSubscription(remoteManager));
// remove subscriptions no longer valid
remoteManager.getSubscriptions().removeIf(Subscription::isMarkedForRemoval);
......
......@@ -265,12 +265,18 @@ public class SimulationCommandHandler extends CommandHandler<SimulationVar> {
ReferenceCoordinateSystem coord =
state.getScenarioStore().getTopography()
.getAttributes().getReferenceCoordinateSystem();
coord.initialize();
CompoundObject o = CoordRef.asCompoundObject(
coord.getEpsgCode(),
coord.getTranslation()
);
cmd.setResponse(responseOK(SimulationVar.COORD_REF.type, o));
if (coord != null){
coord.initialize();
CompoundObject o = CoordRef.asCompoundObject(
coord.getEpsgCode(),
coord.getTranslation()
);
cmd.setResponse(responseOK(SimulationVar.COORD_REF.type, o));
} else {
cmd.setResponse(responseERR("Conversion not supported. Is ReferenceCoordinateSystem correctly set in Vadere?",
TraCICmd.GET_SIMULATION_VALUE, TraCICmd.RESPONSE_GET_SIMULATION_VALUE));
}
});
return cmd;
}
......
......@@ -6,12 +6,7 @@ import org.vadere.manager.server.VadereServer;
import org.vadere.manager.traci.CmdType;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIVersion;
import org.vadere.manager.traci.commands.control.TraCICloseCommand;
import org.vadere.manager.traci.commands.control.TraCIGetVersionCommand;
import org.vadere.manager.traci.commands.control.TraCILoadCommand;
import org.vadere.manager.traci.commands.control.TraCISendFileCommand;
import org.vadere.manager.traci.commands.control.TraCISendFileCommandV20_0_1;
import org.vadere.manager.traci.commands.control.TraCISimStepCommand;
import org.vadere.manager.traci.commands.control.*;
import org.vadere.manager.traci.reader.TraCICommandBuffer;
import org.vadere.manager.traci.writer.TraCIPacket;
......@@ -72,6 +67,8 @@ public abstract class TraCICommand {
return new TraCIGetVersionCommand();
case SIM_STEP:
return new TraCISimStepCommand(cmdBuffer);
case GET_STATE:
return new TraCIGetStateCommand(cmdBuffer);
case CLOSE:
return new TraCICloseCommand();
case SEND_FILE:
......
......@@ -8,6 +8,11 @@ import org.vadere.manager.traci.writer.TraCIPacket;
import java.util.ArrayList;
import java.util.List;
/**
* Subscription for {@link #variables} of the domain {@link #traCICmd} and {@link #elementIdentifier}
* {@link #elementIdentifier} '-1' is used for domain global values (i.e. idList) otherwise
* the {@link #elementIdentifier} is the id of the element (i.e. pedestrian)
*/
public class TraCIValueSubscriptionCommand extends TraCICommand {
private double beginTime;
......@@ -40,6 +45,12 @@ public class TraCIValueSubscriptionCommand extends TraCICommand {
getCommands = new ArrayList<>();
}
public void buildGetCommands(TraCICmd getCmdId){
for(int varId: variables){
getCommands.add(new TraCIGetCommand(getCmdId, varId, elementIdentifier));
}
}
@Override
public TraCIPacket buildResponsePacket() {
return TraCIPacket.create().wrapValueSubscriptionCommand(response);
......@@ -49,34 +60,18 @@ public class TraCIValueSubscriptionCommand extends TraCICommand {
return beginTime;
}
public void setBeginTime(double beginTime) {
this.beginTime = beginTime;
}
public double getEndTime() {
return endTime;
}
public void setEndTime(double endTime) {
this.endTime = endTime;
}
public String getElementIdentifier() {
return elementIdentifier;
}
public void setElementIdentifier(String elementIdentifier) {
this.elementIdentifier = elementIdentifier;
}
public int getNumberOfVariables() {
return numberOfVariables;
}
public void setNumberOfVariables(int numberOfVariables) {
this.numberOfVariables = numberOfVariables;
}
public List<Integer> getVariables() {
return variables;
}
......@@ -100,8 +95,4 @@ public class TraCIValueSubscriptionCommand extends TraCICommand {
public List<TraCIGetCommand> getGetCommands() {
return getCommands;
}
public void setGetCommands(List<TraCIGetCommand> getCommands) {
this.getCommands = getCommands;
}
}
package org.vadere.manager.traci.commands.control;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.reader.TraCICommandBuffer;
import org.vadere.manager.traci.response.TraCIGetStateResponse;
import org.vadere.manager.traci.writer.TraCIPacket;
public class TraCIGetStateCommand extends TraCICommand {
private double targetTime;
private TraCIGetStateResponse response;
public TraCIGetStateCommand(TraCICommandBuffer cmdBuffer) {
super(TraCICmd.GET_STATE);
this.targetTime = cmdBuffer.readDouble();
}
public static TraCIPacket build(double targetTime) {
TraCIPacket packet = TraCIPacket.create(14); // 4
packet.writeUnsignedByte(10) // 1
.writeUnsignedByte(TraCICmd.GET_STATE.id) // 1
.writeDouble(targetTime); // 8
return packet;
}
public double getTargetTime() {
return targetTime;
}
public void setTargetTime(double targetTime) {
this.targetTime = targetTime;
}
public TraCIGetStateResponse getResponse() {
return response;
}
public void setResponse(TraCIGetStateResponse response) {
this.response = response;
}
@Override
public TraCIPacket buildResponsePacket() {
if (NOK_response != null)
return NOK_response;
else
return TraCIPacket.create().wrapGetStateCommand(response); // TODO
}
}
package org.vadere.manager.traci.response;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.reader.TraCICommandBuffer;
import java.util.ArrayList;
import java.util.List;
/**
* Response object for {@link org.vadere.manager.traci.commands.control.TraCIGetStateCommand}
* command. It includes all subscriptions previously added by each client.
*
* See {@link TraCIResponse} for static factory methods used to create objects from byte[]
*/
public class TraCIGetStateResponse extends TraCIResponse {
// private int numberOfSubscriptions;
private List<TraCISubscriptionResponse> subscriptionResponses;
public TraCIGetStateResponse(StatusResponse statusResponse, TraCICommandBuffer buffer) {
this(statusResponse);
int numberOfSubscriptions = buffer.readInt();
for (int i = 0; i < numberOfSubscriptions; i++) {
int zeroByte = buffer.readUnsignedByte();
int length = buffer.readInt();
TraCICmd responseIdentifier = TraCICmd.fromId(buffer.readUnsignedByte());
subscriptionResponses.add(new TraCISubscriptionResponse(statusResponse, responseIdentifier, buffer));
}
}
public TraCIGetStateResponse(StatusResponse statusResponse) {
super(statusResponse, TraCICmd.GET_STATE);
subscriptionResponses = new ArrayList<>();
}
public int getNumberOfSubscriptions() {
return subscriptionResponses.size();
}
public List<TraCISubscriptionResponse> getSubscriptionResponses() {
return subscriptionResponses;
}
public void setSubscriptionResponses(List<TraCISubscriptionResponse> subscriptionResponses) {
this.subscriptionResponses = subscriptionResponses;
}
public void addSubscriptionResponse(TraCISubscriptionResponse response) {
this.subscriptionResponses.add(response);
}
}
......@@ -7,12 +7,7 @@ import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commands.TraCICommand;
import org.vadere.manager.traci.commands.control.TraCIGetVersionCommand;
import org.vadere.manager.traci.reader.TraCIPacketBuffer;
import org.vadere.manager.traci.response.StatusResponse;
import org.vadere.manager.traci.response.TraCIGetResponse;
import org.vadere.manager.traci.response.TraCIGetVersionResponse;
import org.vadere.manager.traci.response.TraCISimTimeResponse;
import org.vadere.manager.traci.response.TraCIStatusResponse;
import org.vadere.manager.traci.response.TraCISubscriptionResponse;
import org.vadere.manager.traci.response.*;
import org.vadere.util.logging.Logger;
import java.nio.ByteBuffer;
......@@ -198,6 +193,23 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
return this;
}
public TraCIPacket wrapGetStateCommand(TraCIGetStateResponse res) {
addStatusResponse(res.getStatusResponse());
if (!res.getStatusResponse().getResponse().equals(TraCIStatusResponse.OK))
return this; // ERR or NOT_IMPLEMENTED --> only StatusResponse
// not length field! Directly add number of subscription responses.
writeInt(res.getNumberOfSubscriptions());
// add each SubscriptionsResponse as its own command (with length and responseID)
res.getSubscriptionResponses().forEach(this::wrapSubscription);
return this;
}
public TraCIPacket wrapSimTimeStepCommand(TraCISimTimeResponse res) {
addStatusResponse(res.getStatusResponse());
......
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