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

add more log statements for debugging. fix error in TraCI unsignedByte handling

see https://sam-dev.cs.hm.edu/rover/rover-main/issues/15
parent fc9050b5
......@@ -59,7 +59,7 @@ public class ClientHandler implements Runnable {
while (cmd != null) {
TraCIPacket response = cmdExecutor.execute(cmd);
logger.debugf("send packet with %d byte", response.size());
logger.debugf("send packet [%d byte]", response.size());
traCISocket.sendExact(response);
cmd = traCIPacketBuffer.nextCommand();
......
......@@ -34,10 +34,10 @@ public class Manager {
logger.infof("Start Server(%s) with Loglevel: %s", VadereServer.currentVersion.getVersionString(), logger.getLevel().toString());
AbstractVadereServer server;
if (ns.getBoolean("singleClient")) {
server = new VadereSingleClientServer(serverSocket, Paths.get(ns.getString("output-dir")), ns.getBoolean("guiMode"));
server = new VadereSingleClientServer(serverSocket, Paths.get(ns.getString("output-dir")), ns.getBoolean("guiMode"), ns.getBoolean("trace"));
} else {
ExecutorService pool = Executors.newFixedThreadPool(ns.getInt("clientNum"));
server = new VadereServer(serverSocket, pool, Paths.get(ns.getString("output-dir")), ns.getBoolean("guiMode"));
server = new VadereServer(serverSocket, pool, Paths.get(ns.getString("output-dir")), ns.getBoolean("guiMode"), ns.getBoolean("trace"));
}
server.run();
......@@ -99,6 +99,15 @@ public class Manager {
.dest("clientNum")
.help("Set number of clients to manager. Important: Each client has a separate simulation. No communication between clients");
parser.addArgument("--trace")
.required(false)
.action(Arguments.storeTrue())
.setDefault(false)
.type(Boolean.class)
.dest("trace")
.help("Activate additional TRACE information in low level components. Ensure correct --loglevel setting to see additional information");
parser.addArgument("--single-client")
.required(false)
.action(Arguments.storeTrue())
......
......@@ -161,10 +161,10 @@ public class RemoteManager implements RunnableFinishedListener {
public void startSimulation() {
if (currentSimulationRun == null)
throw new IllegalStateException("RemoteScenarioRun object must not be null");
throw new TraCIExceptionInternal("RemoteScenarioRun object must not be null");
if (currentSimulationThread != null && currentSimulationThread.isAlive())
throw new IllegalStateException("A simulation is already running. Stop current simulation before starting new one.");
throw new TraCIExceptionInternal("A simulation is already running. Stop current simulation before starting new one.");
simulationFinished = false;
currentSimulationThread = new Thread(currentSimulationRun);
......
......@@ -38,4 +38,8 @@ public class TraCIException extends RuntimeException {
+ "not supported in API: " + cmd.getTraCICmd().toString());
}
public String getMessageForClient(){
return getMessage();
}
}
package org.vadere.manager;
/**
* Use this Exception if the message produces has no mean for a TraCI client.
* The stacktrace is printed but the client gets a static response.
*/
public class TraCIExceptionInternal extends TraCIException {
private final static String CLIENT_MESSAGE = "Internal error occurred in TraCI server. See server logs for details";
public TraCIExceptionInternal(String message) {
super(message);
}
public TraCIExceptionInternal(String message, Object... arg) {
super(message, arg);
}
public TraCIExceptionInternal(String message, Throwable cause, Object... arg) {
super(message, cause, arg);
}
public TraCIExceptionInternal(String message, Throwable cause) {
super(message, cause);
}
@Override
public String getMessageForClient() {
return CLIENT_MESSAGE;
}
}
......@@ -3,6 +3,7 @@ package org.vadere.manager;
import org.vadere.manager.traci.reader.TraCIPacketBuffer;
import org.vadere.manager.traci.response.TraCIResponse;
import org.vadere.manager.traci.writer.TraCIPacket;
import org.vadere.util.logging.Logger;
import java.io.Closeable;
import java.io.DataInputStream;
......@@ -16,20 +17,29 @@ import java.nio.ByteBuffer;
*/
public class TraCISocket implements Closeable {
private static Logger logger = Logger.getLogger(TraCISocket.class);
private final static int TRACI_LEN_LENGTH = 4;
private final Socket socket;
private final DataOutputStream outStream;
private final DataInputStream inStream;
private final boolean tracePackets;
private String host;
private int port;
public TraCISocket(Socket socket) throws IOException {
public TraCISocket(Socket socket, boolean tracePackets) throws IOException{
this.socket = socket;
this.host = this.socket.getInetAddress().toString();
this.port = this.socket.getPort();
this.outStream = new DataOutputStream(socket.getOutputStream());
this.inStream = new DataInputStream(socket.getInputStream());
this.tracePackets = tracePackets;
if (this.tracePackets)
logger.infof("TraCISocket is in TRACE-MODE. Ensure the correct Loglevel to see all Information.");
}
public TraCISocket(Socket socket) throws IOException {
this(socket, false);
}
public int getPort() {
......@@ -55,6 +65,8 @@ public class TraCISocket implements Closeable {
}
public void sendExact(final TraCIPacket packet) throws IOException {
if (tracePackets)
logger.tracef("send packet [%d byte]: %s", packet.size(), packet.asHexString());
send(packet.send());
}
......
......@@ -17,10 +17,12 @@ public abstract class AbstractVadereServer implements Runnable {
protected final ServerSocket serverSocket;
protected final Path baseDir;
protected final boolean guiSupport;
protected final boolean trace;
public AbstractVadereServer(ServerSocket serverSocket, Path baseDir, boolean guiSupport) {
public AbstractVadereServer(ServerSocket serverSocket, Path baseDir, boolean guiSupport, boolean trace) {
this.serverSocket = serverSocket;
this.baseDir = baseDir;
this.guiSupport = guiSupport;
this.trace = trace;
}
}
......@@ -15,8 +15,8 @@ public class VadereServer extends AbstractVadereServer {
private final ExecutorService handlerPool;
public VadereServer(ServerSocket serverSocket, ExecutorService handlerPool, Path baseDir, boolean guiSupport) {
super(serverSocket, baseDir, guiSupport);
public VadereServer(ServerSocket serverSocket, ExecutorService handlerPool, Path baseDir, boolean guiSupport, boolean trace) {
super(serverSocket, baseDir, guiSupport, trace);
this.handlerPool = handlerPool;
}
......@@ -31,7 +31,7 @@ public class VadereServer extends AbstractVadereServer {
while (true) {
Socket clientSocket = serverSocket.accept();
handlerPool.execute(new ClientHandler(serverSocket, new TraCISocket(clientSocket), baseDir, guiSupport));
handlerPool.execute(new ClientHandler(serverSocket, new TraCISocket(clientSocket, trace), baseDir, guiSupport));
}
} catch (IOException e) {
e.printStackTrace();
......
......@@ -15,8 +15,8 @@ import java.nio.file.Path;
public class VadereSingleClientServer extends AbstractVadereServer {
public VadereSingleClientServer(ServerSocket serverSocket, Path baseDir, boolean guiSupport) {
super(serverSocket, baseDir, guiSupport);
public VadereSingleClientServer(ServerSocket serverSocket, Path baseDir, boolean guiSupport, boolean trace) {
super(serverSocket, baseDir, guiSupport, trace);
}
@Override
......@@ -24,7 +24,7 @@ public class VadereSingleClientServer extends AbstractVadereServer {
try {
logger.infof("listening on port %d... (gui-mode: %s) Single Simulation", serverSocket.getLocalPort(), Boolean.toString(guiSupport));
Socket clientSocket = serverSocket.accept();
Thread t = new Thread(new ClientHandler(serverSocket, new TraCISocket(clientSocket), baseDir, guiSupport));
Thread t = new Thread(new ClientHandler(serverSocket, new TraCISocket(clientSocket, trace), baseDir, guiSupport));
t.start();
t.join();
......
......@@ -113,4 +113,8 @@ public enum TraCICmd {
return String.format("TraCICmd{%s: id=0x%02X, type=%s}", name(), id, type);
}
public String logShort(){
return String.format("{%s:0x%02X%s}", name(), id, type);
}
}
package org.vadere.manager.traci;
import org.vadere.manager.TraCIException;
public enum TraCIVersion {
V20_0_1(20, 0, 1),
V20_0_2(20, 0, 2); // allow cache transfer
......@@ -18,7 +20,7 @@ public enum TraCIVersion {
public static TraCIVersion valueOf(int ordinalId) {
if (ordinalId < 0 || ordinalId >= values().length)
throw new IllegalArgumentException("given ordinalId is outside of this Enum.");
throw new TraCIException("given ordinalId is outside of this Enum.");
return values()[ordinalId];
}
......
package org.vadere.manager.traci.commandHandler;
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;
......@@ -45,10 +46,27 @@ public class CommandExecutor {
public TraCIPacket execute(TraCICommand cmd) {
TraCICmdHandler handler = cmdMap.get(cmd.getTraCICmd().id);
if (handler == null) {
logger.errorf("No CommandHandler found for command: %02X", cmd.getTraCICmd().id);
logger.errorf("No CommandHandler found for command: %s", cmd.getTraCICmd().logShort());
return TraCIPacket.create().add_Err_StatusResponse(cmd.getTraCICmd().id, "ID not found.");
}
TraCIPacket response;
try {
logger.debugf("execute cmd: %s", cmd.getTraCICmd().logShort());
cmd = handler.handel(cmd, remoteManager);
}catch (TraCIException e){
logger.errorf("Error handling cmd: %s", cmd.getTraCICmd().logShort());
e.printStackTrace();
return TraCIPacket.createErr(cmd.getTraCICmd().id, e.getMessageForClient());
}
try {
logger.debugf("build response for: %s", cmd.getTraCICmd().logShort());
response = cmd.buildResponsePacket();
} catch (TraCIException e){
logger.errorf("error building response for: %s", cmd.getTraCICmd().logShort());
e.printStackTrace();
return TraCIPacket.createErr(cmd.getTraCICmd().id, e.getMessageForClient());
}
return handler.handel(cmd, remoteManager).buildResponsePacket();
return response;
}
}
......@@ -4,6 +4,7 @@ package org.vadere.manager.traci.commandHandler;
import org.vadere.manager.RemoteManager;
import org.vadere.manager.Subscription;
import org.vadere.manager.server.VadereServer;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIVersion;
import org.vadere.manager.traci.commandHandler.annotation.ControlHandler;
import org.vadere.manager.traci.commandHandler.annotation.ControlHandlers;
......@@ -74,15 +75,16 @@ public class ControlCommandHandler extends CommandHandler<ControlVar> {
public TraCICommand process_simStep(TraCICommand rawCmd, RemoteManager remoteManager) {
TraCISimStepCommand cmd = (TraCISimStepCommand) rawCmd;
logger.debugf("Simulate to: %f", cmd.getTargetTime());
// remoteManager.nextStep(cmd.getTargetTime());
logger.debugf("%s: Simulate until=%f", TraCICmd.SIM_STEP.name(), cmd.getTargetTime());
if (!remoteManager.nextStep(cmd.getTargetTime())) {
//simulation finished;
cmd.setResponse(TraCISimTimeResponse.simEndReached());
return cmd;
}
// execute all
logger.debug("execute subscriptions");
logger.debugf("%s: execute %d subscriptions",
TraCICmd.SIM_STEP.name(),
remoteManager.getSubscriptions().size());
remoteManager.getSubscriptions().forEach(sub -> sub.executeSubscription(remoteManager));
// remove subscriptions no longer valid
......
......@@ -172,7 +172,12 @@ public class PersonCommandHandler extends CommandHandler<PersonVar> {
.stream()
.map(p -> Integer.toString(p.getId()))
.collect(Collectors.toList());
logger.debugf("time: %f ID's: %s", state.getSimTimeInSec(), Arrays.toString(data.toArray(String[]::new)));
logger.debugf("%s.%s: t=%f pedIds(#%d)=%s",
TraCICmd.GET_PERSON_VALUE.logShort(),
PersonVar.ID_LIST.logShort(),
state.getSimTimeInSec(),
data.size(),
Arrays.toString(data.toArray(String[]::new)));
cmd.setResponse(responseOK(PersonVar.ID_LIST.type, data));
});
return cmd;
......@@ -233,7 +238,9 @@ public class PersonCommandHandler extends CommandHandler<PersonVar> {
if (checkIfPedestrianExists(ped, cmd)) {
cmd.setResponse(responseOK(PersonVar.POS_2D.type, ped.getPosition()));
logger.debugf("time: %f Pedestrian: %s Position: %s",
logger.tracef("%s.%s: t=%f pedId=%s pos2d=%s",
TraCICmd.GET_PERSON_VALUE.logShort(),
PersonVar.POS_2D.logShort(),
state.getSimTimeInSec(),
cmd.getElementIdentifier(),
ped.getPosition().toString());
......
......@@ -64,4 +64,8 @@ public enum PersonVar {
", type=" + type +
'}';
}
public String logShort(){
return String.format("{%s:0x%02X}", name(), id);
}
}
package org.vadere.manager.traci.commands;
import org.vadere.manager.TraCIException;
import org.vadere.manager.TraCIExceptionInternal;
import org.vadere.manager.server.VadereServer;
import org.vadere.manager.traci.CmdType;
import org.vadere.manager.traci.TraCICmd;
......@@ -55,7 +56,7 @@ public abstract class TraCICommand {
case CONTEXT_SUB:
throw new TraCIException("Subscription not implement. Command: 0x%02X", cmd.id);
default:
throw new IllegalStateException("Should not be reached. All CmdType enums are tested in switch statement");
throw new TraCIExceptionInternal("Should not be reached. All CmdType enums are tested in switch statement");
}
}
......@@ -77,7 +78,7 @@ public abstract class TraCICommand {
case LOAD:
return new TraCILoadCommand(cmdBuffer);
default:
throw new IllegalStateException(String.format("Should not be reached. Only TraCI control commands expected: %0X", cmd.id));
throw new TraCIExceptionInternal(String.format("Should not be reached. Only TraCI control commands expected: 0x%02X", cmd.id));
}
}
......
......@@ -35,6 +35,11 @@ public class TraCICloseCommand extends TraCICommand {
this.response = response;
}
@Override
public String toString() {
return String.format("[ %s | %s ]", traCICmd.toString(), response.toString());
}
@Override
public TraCIPacket buildResponsePacket() {
if (NOK_response != null)
......
package org.vadere.manager.traci.reader;
import org.vadere.manager.TraCIExceptionInternal;
import java.nio.ByteBuffer;
/**
......@@ -38,7 +40,7 @@ public class TraCICommandBuffer extends TraCIByteBuffer {
public int readCmdIdentifier() {
if (cmdIdentifierRead)
throw new IllegalStateException("TraCI Command Identifier already consumed. readCmdIdentifier() must only be called once. Something went wrong in the TraCI message handling.");
throw new TraCIExceptionInternal("TraCI Command Identifier already consumed. readCmdIdentifier() must only be called once. Something went wrong in the TraCI message handling.");
cmdIdentifierRead = true;
return readUnsignedByte();
......
package org.vadere.manager.traci.sumo;
import org.vadere.manager.TraCIException;
public enum LightPhase {
RED(0x01),
......@@ -20,7 +22,7 @@ public enum LightPhase {
if (value.id == id)
return value;
}
throw new IllegalArgumentException("No LightPhase for traCICmd: " + id);
throw new TraCIException("No LightPhase for traCICmd: " + id);
}
}
package org.vadere.manager.traci.writer;
import org.apache.commons.codec.binary.Hex;
import org.vadere.manager.TraCIException;
import org.vadere.manager.TraCIExceptionInternal;
import org.vadere.manager.traci.TraCICmd;
import org.vadere.manager.traci.TraCIDataType;
import org.vadere.manager.traci.commands.TraCICommand;
......@@ -12,6 +14,7 @@ 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.util.logging.Logger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
......@@ -20,7 +23,8 @@ import java.util.List;
/**
* //todo comment
*/
public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter{
private static Logger logger = Logger.getLogger(TraCIPacket.class);
// private TraCIWriter writer;
private boolean emptyLengthField;
......@@ -31,6 +35,10 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
return new TraCIPacket().addEmptyLengthField();
}
public static TraCIPacket createErr(int cmdIdentifier, String description) {
return new TraCIPacket().addEmptyLengthField().add_Err_StatusResponse(cmdIdentifier, description);
}
public static TraCIPacket create(int packetSize) {
TraCIPacket packet = new TraCIPacket();
packet.writeInt(packetSize);
......@@ -65,7 +73,7 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
private void throwIfFinalized() {
if (finalized)
throw new TraCIException("Cannot change finalized TraCIPacket");
throw new TraCIExceptionInternal("Cannot change finalized TraCIPacket");
}
private TraCIPacket() {
......@@ -77,7 +85,7 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
private TraCIPacket addEmptyLengthField() {
if (emptyLengthField)
throw new IllegalStateException("Should only be called at most once.");
throw new TraCIExceptionInternal("Should only be called at most once.");
writeInt(-1);
emptyLengthField = true;
return this;
......@@ -131,6 +139,7 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
}
public TraCIPacket wrapGetResponse(TraCIGetResponse res) {
// logger.tracef("wrap GetResponse: %s", res.toString());
addStatusResponse(res.getStatusResponse());
if (!res.getStatusResponse().getResponse().equals(TraCIStatusResponse.OK))
......@@ -216,7 +225,7 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
public void addCommandWithoutLen(byte[] buffer) {
if (buffer.length > 255) {
if (buffer.length > 254) {
writeUnsignedByte(0);
writeInt(buffer.length + 5); // 1 + 4 length field
writeBytes(buffer);
......@@ -267,4 +276,8 @@ public class TraCIPacket extends ByteArrayOutputStreamTraCIWriter {
return this;
}
public String asHexString(){
return Hex.encodeHexString(this.data.toByteArray());
}
}
Supports Markdown
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