51 #ifdef CHECK_MEMORY_LEAKS 53 #endif // CHECK_MEMORY_LEAKS 60 : outputFileName(outputFileName), answerLog(
"") {
61 answerLog.setf(std::ios::fixed , std::ios::floatfield);
74 std::ifstream defFile;
75 std::string fileContentStr;
76 std::stringstream fileContent;
77 std::string lineCommand;
78 std::stringstream msg;
80 bool commentRead =
false;
86 std::stringstream msg;
87 msg <<
"#Error while connecting: " << e.
what();
93 defFile.open(fileName.c_str());
95 msg <<
"Can not open definition file " << fileName << std::endl;
99 defFile.unsetf(std::ios::dec);
101 while (defFile >> lineCommand) {
103 if (lineCommand.compare(
"%") == 0) {
105 commentRead = !commentRead;
112 if (lineCommand.compare(
"repeat") == 0) {
114 defFile >> lineCommand;
116 if (lineCommand.compare(
"simstep2") == 0) {
120 for (
int i = 0; i < repNo; i++) {
123 }
else if (lineCommand.compare(
"getvariable") == 0) {
127 defFile >> domID >> varID >> objID;
129 }
else if (lineCommand.compare(
"getvariable_plus") == 0) {
133 defFile >> domID >> varID >> objID;
134 std::stringstream msg;
137 std::string msgS = msg.str();
142 }
else if (lineCommand.compare(
"subscribevariable") == 0) {
145 std::string beginTime, endTime;
147 defFile >> domID >> objID >> beginTime >> endTime >> varNo;
149 }
else if (lineCommand.compare(
"subscribecontext") == 0) {
151 int domID, varNo, domain;
153 std::string beginTime, endTime;
155 defFile >> domID >> objID >> beginTime >> endTime >> domain >> range >> varNo;
157 }
else if (lineCommand.compare(
"setvalue") == 0) {
161 defFile >> domID >> varID >> objID;
163 }
else if (lineCommand.compare(
"testAPI") == 0) {
167 msg <<
"Error in definition file: " << lineCommand <<
" is not a valid command";
185 answerLog << std::endl <<
"-> Command sent: <SimulationStep2>:" << std::endl;
188 std::string acknowledgement;
190 answerLog << acknowledgement << std::endl;
201 answerLog << std::endl <<
"-> Command sent: <Close>:" << std::endl;
204 std::string acknowledgement;
206 answerLog << acknowledgement << std::endl;
216 answerLog << std::endl <<
"-> Command sent: <GetVariable>:" << std::endl
217 <<
" domID=" << domID <<
" varID=" << varID
218 <<
" objID=" << objID << std::endl;
221 std::string acknowledgement;
223 answerLog << acknowledgement << std::endl;
233 answerLog <<
" CommandID=" << (domID + 0x10) <<
" VariableID=" << variableID <<
" ObjectID=" << objectID;
235 answerLog <<
" valueDataType=" << valueDataType;
238 std::stringstream msg;
239 msg <<
"Error while receiving command: " << e.
what();
248 std::stringstream msg;
251 std::string msgS = msg.str();
256 answerLog << std::endl <<
"-> Command sent: <SetValue>:" << std::endl
257 <<
" domID=" << domID <<
" varID=" << varID
258 <<
" objID=" << objID << std::endl;
260 std::string acknowledgement;
262 answerLog << acknowledgement << std::endl;
271 std::vector<int> vars;
272 for (
int i = 0; i < varNo; ++i) {
279 answerLog << std::endl <<
"-> Command sent: <SubscribeVariable>:" << std::endl
280 <<
" domID=" << domID <<
" objID=" << objID <<
" with " << varNo <<
" variables" << std::endl;
283 std::string acknowledgement;
285 answerLog << acknowledgement << std::endl;
295 int domain,
SUMOReal range,
int varNo, std::ifstream& defFile) {
296 std::vector<int> vars;
297 for (
int i = 0; i < varNo; ++i) {
304 answerLog << std::endl <<
"-> Command sent: <SubscribeContext>:" << std::endl
305 <<
" domID=" << domID <<
" objID=" << objID <<
" domain=" << domain <<
" range=" << range
306 <<
" with " << varNo <<
" variables" << std::endl;
309 std::string acknowledgement;
311 answerLog << acknowledgement << std::endl;
326 std::cerr <<
"Unable to write result file" << std::endl;
329 locTime = localtime(&seconds);
330 outFile <<
"TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
338 std::cerr << msg.str() << std::endl;
339 answerLog <<
"----" << std::endl << msg.str() << std::endl;
350 int noSubscriptions = inMsg.
readInt();
351 for (
int s = 0; s < noSubscriptions; ++s) {
356 }
catch (std::invalid_argument& e) {
357 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
376 answerLog <<
" #variables=" << varNo << std::endl;
377 for (
int i = 0; i < varNo; ++i) {
382 answerLog <<
" valueDataType=" << valueDataType;
390 answerLog <<
" #variables=" << varNo << std::endl;
392 answerLog <<
" #objects=" << objNo << std::endl;
393 for (
int j = 0; j < objNo; ++j) {
395 for (
int i = 0; i < varNo; ++i) {
400 answerLog <<
" valueDataType=" << valueDataType;
405 answerLog <<
"#Error: received response with command id: " << cmdId <<
" but expected a subscription response (0xe0-0xef / 0x90-0x9f)" << std::endl;
408 }
catch (std::invalid_argument& e) {
409 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
424 std::string dataTypeS;
425 defFile >> dataTypeS;
426 if (dataTypeS ==
"<airDist>") {
429 }
else if (dataTypeS ==
"<drivingDist>") {
432 }
else if (dataTypeS ==
"<objSubscription>") {
433 int beginTime, endTime, numVars;
434 defFile >> beginTime >> endTime >> numVars;
438 for (
int i = 0; i < numVars; ++i) {
443 return 4 + 4 + 4 + numVars;
447 if (dataTypeS ==
"<int>") {
452 }
else if (dataTypeS ==
"<byte>") {
457 }
else if (dataTypeS ==
"<ubyte>") {
462 }
else if (dataTypeS ==
"<float>") {
467 }
else if (dataTypeS ==
"<double>") {
472 }
else if (dataTypeS ==
"<string>") {
475 if (valueS ==
"\"\"") {
480 return 4 + 1 + (int) valueS.length();
481 }
else if (dataTypeS ==
"<string*>") {
482 std::vector<std::string> slValue;
485 for (
int i = 0; i < valI; ++i) {
488 slValue.push_back(tmp);
489 length += 4 + int(tmp.length());
494 }
else if (dataTypeS ==
"<compound>") {
499 for (
int i = 0; i < valI; ++i) {
503 }
else if (dataTypeS ==
"<color>") {
507 for (
int i = 0; i < 3; ++i) {
512 }
else if (dataTypeS ==
"<position2D>") {
519 }
else if (dataTypeS ==
"<position3D>") {
527 return 1 + 8 + 8 + 8;
528 }
else if (dataTypeS ==
"<positionRoadmap>") {
533 int length = 1 + 8 + (int) valueS.length();
538 return length + 4 + 1;
539 }
else if (dataTypeS ==
"<shape>") {
544 for (
int i = 0; i < valI; ++i) {
553 msg <<
"## Unknown data type: " << dataTypeS;
562 answerLog <<
" Unsigned Byte Value: " << ubyte << std::endl;
565 answerLog <<
" Byte value: " << byte << std::endl;
568 answerLog <<
" Int value: " << integer << std::endl;
571 if (floatv < 0.1 && floatv > 0) {
572 answerLog.setf(std::ios::scientific, std::ios::floatfield);
574 answerLog <<
" float value: " << floatv << std::endl;
575 answerLog.setf(std::ios::fixed , std::ios::floatfield);
580 answerLog <<
" Double value: " << doublev << std::endl;
586 answerLog <<
" BoundaryBoxValue: lowerLeft x=" << lowerLeftX
587 <<
" y=" << lowerLeftY <<
" upperRight x=" << upperRightX
588 <<
" y=" << upperRightY << std::endl;
592 for (
int i = 0; i < length; i++) {
595 answerLog <<
"(" << x <<
"," << y <<
") ";
602 answerLog <<
" Position3DValue: " << std::endl;
604 <<
" z: " << z << std::endl;
609 answerLog <<
" RoadMapPositionValue: roadId=" << roadId
611 <<
" laneId=" << laneId << std::endl;
614 answerLog <<
" TLPhaseListValue: length=" << length << std::endl;
615 for (
int i = 0; i < length; i++) {
619 answerLog <<
" precRoad=" << pred <<
" succRoad=" << succ
632 answerLog <<
"#Error: unknown phase value" << (int)phase << std::endl;
638 answerLog <<
" string value: " << s << std::endl;
641 answerLog <<
" string list value: [ " << std::endl;
642 for (std::vector<std::string>::iterator i = s.begin(); i != s.end(); ++i) {
643 if (i != s.begin()) {
651 answerLog <<
" compound value with " << no <<
" members: [ " << std::endl;
652 for (
int i = 0; i < no; ++i) {
654 answerLog <<
" valueDataType=" << currentValueDataType;
661 answerLog <<
" position value: (" << xv <<
"," << yv <<
")" << std::endl;
667 answerLog <<
" color value: (" << r <<
"," << g <<
"," << b <<
"," << a <<
")" << std::endl;
669 answerLog <<
"#Error: unknown valueDataType!" << std::endl;
681 const std::string edgeID =
"e_m0";
689 std::vector<std::string> edges;
690 edges.push_back(
"e_u1");
699 answerLog <<
" vehicle:\n";
711 answerLog <<
" getColor: " << col2.
r <<
"r=" << col2.
r <<
" g=" << col2.
g <<
" b=" << col2.
b <<
" a=" << col2.
a <<
"\n";
712 answerLog <<
" getNextTLS:\n";
714 for (
int i = 0; i < (int)result.size(); ++i) {
715 const VehicleScope::NextTLSData& d = result[i];
716 answerLog <<
" tls=" << d.id <<
" tlIndex=" << d.tlIndex <<
" dist=" << d.dist <<
" state=" << d.state <<
"\n";
718 answerLog <<
" moveToXY, simStep:\n";
723 answerLog <<
" add:\n";
728 answerLog <<
" remove:\n";
732 answerLog <<
" inductionloop:\n";
734 answerLog <<
" getVehicleData:\n";
736 for (
int i = 0; i < (int)result2.size(); ++i) {
737 const InductionLoopScope::VehicleData& vd = result2[i];
738 answerLog <<
" veh=" << vd.id <<
" length=" << vd.length <<
" entered=" << vd.entryTime <<
" left=" << vd.leaveTime <<
" type=" << vd.typeID <<
"\n";
741 answerLog <<
" simulation:\n";
743 answerLog <<
" subscribe to road and pos of vehicle '1':\n";
744 std::vector<int> vars;
749 answerLog <<
" subscription results:\n";
753 answerLog <<
" subscribe to vehicles around edge 'e_u1':\n";
754 std::vector<int> vars2;
758 answerLog <<
" context subscription results:\n";
760 for (SubscribedValues::iterator it = result4.begin(); it != result4.end(); ++it) {
761 answerLog <<
" vehicle=" << it->first <<
" pos=" << it->second[
VAR_LANEPOSITION].scalar <<
"\n";
764 answerLog <<
" person:\n";
771 answerLog <<
" gui:\n";
773 answerLog <<
" setScheme: \n";
775 answerLog <<
" getScheme: " <<
gui.
getSchema(
"View #0") <<
"\n";
776 answerLog <<
" take screenshot: \n";
779 answerLog <<
" no support for gui commands\n";
EdgeScope edge
Scope for interaction with edges.
#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.
SubscribedContextValues getContextSubscriptionResults()
void remove(const std::string &vehicleID, char reason=REMOVE_VAPORIZED) 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
#define REQUEST_DRIVINGDIST
void close()
Closes the connection.
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 moveToXY(const std::string &vehicleID, const std::string &edgeID, const int lane, const SUMOReal x, const SUMOReal y, const SUMOReal angle, const int keepRoute) const
#define CMD_GET_VEHICLE_VARIABLE
SUMOTime getCurrentTime() const
std::stringstream answerLog
Stream containing the log.
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}}
std::string getTypeID(const std::string &typeID) const
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.
void setEffort(const std::string &edgeID, SUMOReal effort, SUMOTime begin=0, SUMOTime end=SUMOTime_MAX) const
SUMOReal getWaitingTime(const std::string &typeID) const
void send_commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *add=0) const
Sends a GetVariable request.
virtual double readDouble()
std::string getRoadID(const std::string &typeID) const
PersonScope person
Scope for interaction with persons.
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
std::string getRoadID(const std::string &vehicleID) const
TraCIColor getColor(const std::string &vehicleID) const
SUMOReal getSlope(const std::string &vehicleID) const
void simulationStep(SUMOTime time=0)
Advances by one step (or up to the given time)
std::string outputFileName
The name of the file to write the results log into.
virtual void writeUnsignedByte(int)
InductionLoopScope inductionloop
Scope for interaction with inductive loops.
virtual void writeInt(int)
virtual int readUnsignedByte()
void subscribeContext(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, SUMOReal range, const std::vector< int > &vars) const
SUMOReal getWaitingTime(const std::string &vehicleID) const
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP2.
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.
SUMOReal getTraveltime(const std::string &edgeID) const
std::vector< std::string > getIDList() const
std::map< std::string, TraCIValues > SubscribedValues
std::string getNextEdge(const std::string &typeID) const
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.
std::vector< VehicleData > getVehicleData(const std::string &loopID) const
virtual const char * what() const
bool run(std::string fileName, int port, std::string host="localhost")
Runs a test.
virtual void writeStringList(const std::vector< std::string > &s)
SUMOTime string2time(const std::string &r)
virtual std::string readString()
SUMOReal getWidth(const std::string &typeID) const
void setWidth(const std::string &typeID, SUMOReal width) const
void add(const std::string &routeID, const std::vector< std::string > &edges) const
#define CMD_SUBSCRIBE_EDGE_CONTEXT
TraCITestClient(std::string outputFileName="testclient_result.out")
Constructor.
virtual void writeFloat(float)
void errorMsg(std::stringstream &msg)
Writes an error message.
void send_commandSubscribeObjectContext(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, SUMOReal range, const std::vector< int > &vars) const
Sends a SubscribeContext request.
#define RESPONSE_SUBSCRIBE_GUI_CONTEXT
SimulationScope simulation
Scope for interaction with the simulation.
virtual void writeString(const std::string &s)
SubscribedValues getSubscriptionResults()
VehicleScope vehicle
Scope for interaction with vehicles.
#define CMD_SUBSCRIBE_VEHICLE_VARIABLE
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)
GUIScope gui
Scope for interaction with the gui.
void subscribe(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, const std::vector< int > &vars) const
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
std::vector< std::string > getIDList() const
void commandSubscribeContextVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, SUMOReal range, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeContext command.
std::string getLaneID(const std::string &vehicleID) const
void adaptTraveltime(const std::string &edgeID, SUMOReal time, SUMOTime begin=0, SUMOTime end=SUMOTime_MAX) const
void commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *addData=0)
Sends and validates a GetVariable command.
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
SUMOReal getEffort(const std::string &edgeID, SUMOTime time) const
void commandSimulationStep(SUMOTime time)
Sends and validates a simulation step command.
SUMOReal getHeight(const std::string &typeID) const
SUMOReal getAdaptedTraveltime(const std::string &edgeID, SUMOTime time) const
void setHeight(const std::string &typeID, SUMOReal height) const
~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.