57 : outputFileName(outputFileName), answerLog(
"") {
58 answerLog.setf(std::ios::fixed , std::ios::floatfield);
71 std::ifstream defFile;
72 std::string fileContentStr;
73 std::stringstream fileContent;
74 std::string lineCommand;
75 std::stringstream msg;
77 bool commentRead =
false;
83 std::stringstream msg;
84 msg <<
"#Error while connecting: " << e.
what();
90 defFile.open(fileName.c_str());
92 msg <<
"Can not open definition file " << fileName << std::endl;
96 defFile.unsetf(std::ios::dec);
98 while (defFile >> lineCommand) {
100 if (lineCommand.compare(
"%") == 0) {
102 commentRead = !commentRead;
109 if (lineCommand.compare(
"repeat") == 0) {
111 defFile >> lineCommand;
113 if (lineCommand.compare(
"simstep2") == 0) {
117 for (
int i = 0; i < repNo; i++) {
120 }
else if (lineCommand.compare(
"getvariable") == 0) {
124 defFile >> domID >> varID >> objID;
126 }
else if (lineCommand.compare(
"getvariable_plus") == 0) {
130 defFile >> domID >> varID >> objID;
131 std::stringstream msg;
134 std::string msgS = msg.str();
139 }
else if (lineCommand.compare(
"subscribevariable") == 0) {
142 std::string beginTime, endTime;
144 defFile >> domID >> objID >> beginTime >> endTime >> varNo;
146 }
else if (lineCommand.compare(
"subscribecontext") == 0) {
148 int domID, varNo, domain;
150 std::string beginTime, endTime;
152 defFile >> domID >> objID >> beginTime >> endTime >> domain >> range >> varNo;
154 }
else if (lineCommand.compare(
"setvalue") == 0) {
158 defFile >> domID >> varID >> objID;
160 }
else if (lineCommand.compare(
"testAPI") == 0) {
164 msg <<
"Error in definition file: " << lineCommand <<
" is not a valid command";
182 answerLog << std::endl <<
"-> Command sent: <SimulationStep2>:" << std::endl;
185 std::string acknowledgement;
187 answerLog << acknowledgement << std::endl;
198 answerLog << std::endl <<
"-> Command sent: <Close>:" << std::endl;
201 std::string acknowledgement;
203 answerLog << acknowledgement << std::endl;
213 answerLog << std::endl <<
"-> Command sent: <GetVariable>:" << std::endl
214 <<
" domID=" << domID <<
" varID=" << varID
215 <<
" objID=" << objID << std::endl;
218 std::string acknowledgement;
220 answerLog << acknowledgement << std::endl;
230 answerLog <<
" CommandID=" << (domID + 0x10) <<
" VariableID=" << variableID <<
" ObjectID=" << objectID;
232 answerLog <<
" valueDataType=" << valueDataType;
235 std::stringstream msg;
236 msg <<
"Error while receiving command: " << e.
what();
245 std::stringstream msg;
248 std::string msgS = msg.str();
253 answerLog << std::endl <<
"-> Command sent: <SetValue>:" << std::endl
254 <<
" domID=" << domID <<
" varID=" << varID
255 <<
" objID=" << objID << std::endl;
257 std::string acknowledgement;
259 answerLog << acknowledgement << std::endl;
268 std::vector<int> vars;
269 for (
int i = 0; i < varNo; ++i) {
276 answerLog << std::endl <<
"-> Command sent: <SubscribeVariable>:" << std::endl
277 <<
" domID=" << domID <<
" objID=" << objID <<
" with " << varNo <<
" variables" << std::endl;
280 std::string acknowledgement;
282 answerLog << acknowledgement << std::endl;
292 int domain,
double range,
int varNo, std::ifstream& defFile) {
293 std::vector<int> vars;
294 for (
int i = 0; i < varNo; ++i) {
301 answerLog << std::endl <<
"-> Command sent: <SubscribeContext>:" << std::endl
302 <<
" domID=" << domID <<
" objID=" << objID <<
" domain=" << domain <<
" range=" << range
303 <<
" with " << varNo <<
" variables" << std::endl;
306 std::string acknowledgement;
308 answerLog << acknowledgement << std::endl;
323 std::cerr <<
"Unable to write result file" << std::endl;
326 locTime = localtime(&seconds);
327 outFile <<
"TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
335 std::cerr << msg.str() << std::endl;
336 answerLog <<
"----" << std::endl << msg.str() << std::endl;
347 int noSubscriptions = inMsg.
readInt();
348 for (
int s = 0; s < noSubscriptions; ++s) {
353 }
catch (std::invalid_argument& e) {
354 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
373 answerLog <<
" #variables=" << varNo << std::endl;
374 for (
int i = 0; i < varNo; ++i) {
379 answerLog <<
" valueDataType=" << valueDataType;
387 answerLog <<
" #variables=" << varNo << std::endl;
389 answerLog <<
" #objects=" << objNo << std::endl;
390 for (
int j = 0; j < objNo; ++j) {
392 for (
int i = 0; i < varNo; ++i) {
397 answerLog <<
" valueDataType=" << valueDataType;
402 answerLog <<
"#Error: received response with command id: " << cmdId <<
" but expected a subscription response (0xe0-0xef / 0x90-0x9f)" << std::endl;
405 }
catch (std::invalid_argument& e) {
406 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
421 std::string dataTypeS;
422 defFile >> dataTypeS;
423 if (dataTypeS ==
"<airDist>") {
426 }
else if (dataTypeS ==
"<drivingDist>") {
429 }
else if (dataTypeS ==
"<objSubscription>") {
430 int beginTime, endTime, numVars;
431 defFile >> beginTime >> endTime >> numVars;
435 for (
int i = 0; i < numVars; ++i) {
440 return 4 + 4 + 4 + numVars;
444 if (dataTypeS ==
"<int>") {
449 }
else if (dataTypeS ==
"<byte>") {
454 }
else if (dataTypeS ==
"<ubyte>") {
459 }
else if (dataTypeS ==
"<float>") {
464 }
else if (dataTypeS ==
"<double>") {
469 }
else if (dataTypeS ==
"<string>") {
472 if (valueS ==
"\"\"") {
477 return 4 + 1 + (int) valueS.length();
478 }
else if (dataTypeS ==
"<string*>") {
479 std::vector<std::string> slValue;
482 for (
int i = 0; i < valI; ++i) {
485 slValue.push_back(tmp);
486 length += 4 + int(tmp.length());
491 }
else if (dataTypeS ==
"<compound>") {
496 for (
int i = 0; i < valI; ++i) {
500 }
else if (dataTypeS ==
"<color>") {
504 for (
int i = 0; i < 3; ++i) {
509 }
else if (dataTypeS ==
"<position2D>") {
516 }
else if (dataTypeS ==
"<position3D>") {
524 return 1 + 8 + 8 + 8;
525 }
else if (dataTypeS ==
"<positionRoadmap>") {
530 int length = 1 + 8 + (int) valueS.length();
535 return length + 4 + 1;
536 }
else if (dataTypeS ==
"<shape>") {
541 for (
int i = 0; i < valI; ++i) {
550 msg <<
"## Unknown data type: " << dataTypeS;
559 answerLog <<
" Unsigned Byte Value: " << ubyte << std::endl;
562 answerLog <<
" Byte value: " << byte << std::endl;
565 answerLog <<
" Int value: " << integer << std::endl;
568 if (floatv < 0.1 && floatv > 0) {
569 answerLog.setf(std::ios::scientific, std::ios::floatfield);
571 answerLog <<
" float value: " << floatv << std::endl;
572 answerLog.setf(std::ios::fixed , std::ios::floatfield);
577 answerLog <<
" Double value: " << doublev << std::endl;
583 answerLog <<
" BoundaryBoxValue: lowerLeft x=" << lowerLeftX
584 <<
" y=" << lowerLeftY <<
" upperRight x=" << upperRightX
585 <<
" y=" << upperRightY << std::endl;
589 for (
int i = 0; i < length; i++) {
592 answerLog <<
"(" << x <<
"," << y <<
") ";
599 answerLog <<
" Position3DValue: " << std::endl;
601 <<
" z: " << z << std::endl;
606 answerLog <<
" RoadMapPositionValue: roadId=" << roadId
608 <<
" laneId=" << laneId << std::endl;
611 answerLog <<
" TLPhaseListValue: length=" << length << std::endl;
612 for (
int i = 0; i < length; i++) {
616 answerLog <<
" precRoad=" << pred <<
" succRoad=" << succ
629 answerLog <<
"#Error: unknown phase value" << (int)phase << std::endl;
635 answerLog <<
" string value: " << s << std::endl;
638 answerLog <<
" string list value: [ " << std::endl;
639 for (std::vector<std::string>::iterator i = s.begin(); i != s.end(); ++i) {
640 if (i != s.begin()) {
648 answerLog <<
" compound value with " << no <<
" members: [ " << std::endl;
649 for (
int i = 0; i < no; ++i) {
651 answerLog <<
" valueDataType=" << currentValueDataType;
658 answerLog <<
" position value: (" << xv <<
"," << yv <<
")" << std::endl;
664 answerLog <<
" color value: (" << r <<
"," << g <<
"," << b <<
"," << a <<
")" << std::endl;
666 answerLog <<
"#Error: unknown valueDataType!" << std::endl;
678 const std::string edgeID =
"e_m0";
686 std::vector<std::string> edges;
687 edges.push_back(
"e_u1");
703 answerLog <<
" vehicle:\n";
705 std::vector<std::string> via;
706 via.push_back(
"e_shape1");
725 answerLog <<
" getColor: r=" << (int)col2.
r <<
" g=" << (
int)col2.
g <<
" b=" << (int)col2.
b <<
" a=" << (
int)col2.
a <<
"\n";
726 answerLog <<
" getNextTLS:\n";
728 for (
int i = 0; i < (int)result.size(); ++i) {
729 const VehicleScope::NextTLSData& d = result[i];
730 answerLog <<
" tls=" << d.id <<
" tlIndex=" << d.tlIndex <<
" dist=" << d.dist <<
" state=" << d.state <<
"\n";
732 answerLog <<
" moveToXY, simStep:\n";
739 answerLog <<
" add:\n";
746 answerLog <<
" remove:\n";
750 answerLog <<
" inductionloop:\n";
752 answerLog <<
" getVehicleData:\n";
754 for (
int i = 0; i < (int)result2.size(); ++i) {
759 answerLog <<
" simulation:\n";
761 answerLog <<
" subscribe to road and pos of vehicle '1':\n";
762 std::vector<int> vars;
767 answerLog <<
" subscription results:\n";
771 answerLog <<
" subscribe to vehicles around edge 'e_u1':\n";
772 std::vector<int> vars2;
776 answerLog <<
" context subscription results:\n";
778 for (SubscribedValues::iterator it = result4.begin(); it != result4.end(); ++it) {
779 answerLog <<
" vehicle=" << it->first <<
" pos=" << it->second[
VAR_LANEPOSITION].scalar <<
"\n";
782 answerLog <<
" person:\n";
802 std::vector<std::string> walkEdges;
803 walkEdges.push_back(
"e_u1");
804 walkEdges.push_back(
"e_shape1");
816 answerLog <<
" trafficlights:\n";
824 answerLog <<
" controlledLinks:\n";
825 for (
int i = 0; i < (int)links.size(); ++i) {
826 answerLog <<
" index=" << i <<
" from=" << links[i].from <<
" via=" << links[i].via <<
" to=" << links[i].to <<
"\n";
828 answerLog <<
" load:\n";
829 std::vector<std::string> args;
830 args.push_back(
"-n");
831 args.push_back(
"net.net.xml");
832 args.push_back(
"-r");
833 args.push_back(
"input_routes.rou.xml");
834 args.push_back(
"--no-step-log");
841 answerLog <<
" gui:\n";
843 answerLog <<
" setScheme: \n";
845 answerLog <<
" getScheme: " <<
gui.
getSchema(
"View #0") <<
"\n";
846 answerLog <<
" take screenshot: \n";
849 answerLog <<
" no support for gui commands\n";
EdgeScope edge
Scope for interaction with edges.
std::vector< std::string > getIDList() const
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_CONTEXT
std::vector< std::string > getIDList() const
void send_commandSetValue(int domID, int varID, const std::string &objID, tcpip::Storage &content) const
Sends a SetVariable request.
void remove(const std::string &vehicleID, char reason=REMOVE_VAPORIZED) const
void setMinGapLat(const std::string &typeID, double minGapLat) const
int getNextSwitch(const std::string &tlsID) const
std::vector< NextTLSData > getNextTLS(const std::string &vehID) const
void readAndReportTypeDependent(tcpip::Storage &inMsg, int valueDataType)
Reads a value of the given type from the given storage and reports it.
void screenshot(const std::string &viewID, const std::string &filename) const
void setEffort(const std::string &edgeID, double effort, SUMOTime begin=0, SUMOTime end=SUMOTime_MAX) const
void setWidth(const std::string &typeID, double width) const
void appendDrivingStage(const std::string &personID, const std::string &toEdge, const std::string &lines, const std::string &stopID="")
#define REQUEST_DRIVINGDIST
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0) const
Validates the result state of a command.
void connect(const std::string &host, int port)
Connects to the specified SUMO server.
void setLength(const std::string &personID, double length) const
double length
Length of the vehicle.
void changeTarget(const std::string &vehicleID, const std::string &edgeID) const
void adaptTraveltime(const std::string &edgeID, double time, double begin=0, double end=SUMOTime_MAX/1000.0) const
#define CMD_GET_VEHICLE_VARIABLE
SUMOTime getCurrentTime() const
std::stringstream answerLog
Stream containing the log.
void setVia(const std::string &vehicleID, const std::vector< std::string > &via) const
virtual std::vector< std::string > readStringList()
#define RESPONSE_SUBSCRIBE_GUI_VARIABLE
void commandSubscribeObjectVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeVariable command.
void testAPI()
call all API methods once
std::map< int, TraCIValue > TraCIValues
{object->{variable->value}}
int setValueTypeDependant(tcpip::Storage &into, std::ifstream &defFile, std::stringstream &msg)
Parses the next value type / value pair from the stream and inserts it into the storage.
double leaveTime
Leave-time of the vehicle in [s].
TraCIColor getColor(const std::string &vehicleID) const
double getWaitingTime(const std::string &vehicleID) const
void send_commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *add=0) const
Sends a GetVariable request.
virtual double readDouble()
PersonScope person
Scope for interaction with persons.
double getMaxSpeedLat(const std::string &typeID) const
void setMaxSpeedLat(const std::string &typeID, double speed) const
double getMaxSpeed(const std::string &vehicleID) const
void setLine(const std::string &vehicleID, const std::string &line) const
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
std::string getRoadID(const std::string &vehicleID) const
void appendWaitingStage(const std::string &personID, double duration, const std::string &description="waiting", const std::string &stopID="")
void load(const std::vector< std::string > &args)
Let sumo load a simulation using the given command line like options.
double getSpeed(const std::string &personID) const
void closeSocket()
Closes the connection.
void simulationStep(SUMOTime time=0)
Advances by one step (or up to the given time)
std::string getNextEdge(const std::string &personID) const
std::string outputFileName
The name of the file to write the results log into.
virtual void writeUnsignedByte(int)
std::string getRedYellowGreenState(const std::string &tlsID) const
TrafficLightScope trafficlights
Scope for interaction with traffic lights.
int getPhase(const std::string &tlsID) const
std::string getLine(const std::string &vehicleID) const
void removeStage(const std::string &personID, int nextStageIndex) const
InductionLoopScope inductionloop
Scope for interaction with inductive loops.
virtual void writeInt(int)
std::vector< std::string > getEdges(const std::string &vehicleID) const
virtual int readUnsignedByte()
std::vector< TraCIVehicleData > getVehicleData(const std::string &loopID) const
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP.
const SubscribedValues & getSubscriptionResults() const
void setColor(const std::string &vehicleID, const TraCIColor &c) const
void commandSetValue(int domID, int varID, const std::string &objID, std::ifstream &defFile)
Sends and validates a SetVariable command.
void moveToXY(const std::string &vehicleID, const std::string &edgeID, const int lane, const double x, const double y, const double angle, const int keepRoute) const
std::string id
The id of the vehicle.
std::string getShapeClass(const std::string &vehicleID) const
std::string getTypeID(const std::string &personID) const
std::vector< std::string > getIDList() const
void setHeight(const std::string &personID, double height) const
std::vector< std::string > getVia(const std::string &vehicleID) const
std::map< std::string, TraCIValues > SubscribedValues
void writeResult()
Writes the results file.
void send_commandSubscribeObjectVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, const std::vector< int > &vars) const
Sends a SubscribeVariable request.
virtual void writeByte(int)
RouteScope route
Scope for interaction with routes.
void send_commandSimulationStep(SUMOTime time) const
Sends a SimulationStep command.
virtual const char * what() const
double getLateralLanePosition(const std::string &vehicleID) const
bool run(std::string fileName, int port, std::string host="localhost")
Runs a test.
virtual void writeStringList(const std::vector< std::string > &s)
void setLateralAlignment(const std::string &typeID, const std::string &latAlignment) const
SUMOTime string2time(const std::string &r)
void setColor(const std::string &personID, const TraCIColor &c) const
void add(const std::string &personID, const std::string &edgeID, double pos, double depart=DEPARTFLAG_NOW, const std::string typeID="DEFAULT_PEDTYPE")
double getWaitingTime(const std::string &personID) const
std::string getRoadID(const std::string &personID) const
virtual std::string readString()
void add(const std::string &routeID, const std::vector< std::string > &edges) const
std::string getProgram(const std::string &tlsID) const
#define CMD_SUBSCRIBE_EDGE_CONTEXT
void setWidth(const std::string &personID, double width) const
void setSpeed(const std::string &personID, double speed) const
void setShapeClass(const std::string &vehicleID, const std::string &clazz) const
std::string getVehicle(const std::string &personID) const
std::vector< TraCILink > getControlledLinks(const std::string &tlsID) const
TraCITestClient(std::string outputFileName="testclient_result.out")
Constructor.
virtual void writeFloat(float)
const SubscribedContextValues & getContextSubscriptionResults() const
double getEffort(const std::string &edgeID, SUMOTime time) const
int getRemainingStages(const std::string &personID) const
double getSlope(const std::string &vehicleID) const
void errorMsg(std::stringstream &msg)
Writes an error message.
#define RESPONSE_SUBSCRIBE_GUI_CONTEXT
void commandSubscribeContextVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, double range, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeContext command.
SimulationScope simulation
Scope for interaction with the simulation.
virtual void writeString(const std::string &s)
void setMaxSpeed(const std::string &vehicleID, double speed) const
VehicleScope vehicle
Scope for interaction with vehicles.
#define CMD_SUBSCRIBE_VEHICLE_VARIABLE
std::string typeID
Type of the vehicle in.
double getHeight(const std::string &typeID) const
std::vector< std::string > getControlledLanes(const std::string &tlsID) const
virtual float readFloat()
void commandClose()
Sends and validates a Close command.
void send_commandClose() const
Sends a Close command.
int check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
Validates the result state of a command.
int getSpeedMode(const std::string &vehicleID) const
void setSchema(const std::string &viewID, const std::string &schemeName) const
std::vector< std::string > getIDList() const
virtual void writeDouble(double)
double getTraveltime(const std::string &edgeID) const
GUIScope gui
Scope for interaction with the gui.
void setMinGap(const std::string &personID, double minGap) const
void subscribe(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, const std::vector< int > &vars) const
void subscribeContext(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, double range, const std::vector< int > &vars) const
std::string getLateralAlignment(const std::string &typeID) const
void appendWalkingStage(const std::string &personID, const std::vector< std::string > &edges, double arrivalPos, double duration=-1, double speed=-1, const std::string &stopID="")
std::vector< std::string > getIDList() const
void setHeight(const std::string &typeID, double height) const
std::string getLaneID(const std::string &vehicleID) const
int getStage(const std::string &personID, int nextStageIndex=0) const
double getLanePosition(const std::string &vehicleID) const
void commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *addData=0)
Sends and validates a GetVariable command.
void removeStages(const std::string &personID) const
void add(const std::string &vehicleID, const std::string &routeID, const std::string &typeID="DEFAULT_VEHTYPE", std::string depart="-1", const std::string &departLane="first", const std::string &departPos="base", const std::string &departSpeed="0", const std::string &arrivalLane="current", const std::string &arrivalPos="max", const std::string &arrivalSpeed="current", const std::string &fromTaz="", const std::string &toTaz="", const std::string &line="", int personCapacity=0, int personNumber=0) const
void commandSimulationStep(SUMOTime time)
Sends and validates a simulation step command.
double getAdaptedTraveltime(const std::string &edgeID, double time) const
double entryTime
Entry-time of the vehicle in [s].
void setType(const std::string &personID, const std::string &typeID) const
void send_commandSubscribeObjectContext(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, double range, const std::vector< int > &vars) const
Sends a SubscribeContext request.
std::vector< std::string > getEdges(const std::string &personID, int nextStageIndex=0) const
double getWidth(const std::string &typeID) const
double getMinGapLat(const std::string &typeID) const
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=gPrecision)
~TraCITestClient()
Destructor.
std::vector< std::string > getIDList() const
std::vector< std::string > getIDList() const
std::string getSchema(const std::string &viewID=DEFAULT_VIEW) const
VehicleTypeScope vehicletype
Scope for interaction with vehicle types.