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 (
unsigned int i = 0; i < varNo; ++i) {
382 answerLog <<
" valueDataType=" << valueDataType;
390 answerLog <<
" #variables=" << varNo << std::endl;
391 unsigned int objNo = inMsg.
readInt();
392 answerLog <<
" #objects=" << objNo << std::endl;
393 for (
unsigned int j = 0; j < objNo; ++j) {
395 for (
unsigned 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;
687 for (
int i = 0; i < (int)result.size(); ++i) {
688 const InductionLoopScope::VehicleData& vd = result[i];
689 answerLog <<
" veh=" << vd.id <<
" length=" << vd.length <<
" entered=" << vd.entryTime <<
" left=" << vd.leaveTime <<
" type=" << vd.typeID <<
"\n";
691 answerLog <<
" simulation:\n";
693 answerLog <<
" gui:\n";
695 answerLog <<
" setScheme: \n";
697 answerLog <<
" getScheme: " <<
gui.
getSchema(
"View #0") <<
"\n";
699 answerLog <<
" no support for gui commands\n";
EdgeScope edge
Scope for interaction with edges.
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_CONTEXT
void readAndReportTypeDependent(tcpip::Storage &inMsg, int valueDataType)
Reads a value of the given type from the given storage and reports it.
#define REQUEST_DRIVINGDIST
void close()
Closes the connection.
void connect(const std::string &host, int port)
Connects to the specified SUMO server.
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
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 send_commandClose() const
Sends a Close command.
unsigned int getIDCount() const
virtual double readDouble()
std::vector< std::string > getIDList() const
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
SUMOTime getCurrentTime() const
std::string outputFileName
The name of the file to write the results log into.
virtual void writeUnsignedByte(int)
std::vector< std::string > getIDList() const
InductionLoopScope inductionloop
Scope for interaction with inductive loops.
virtual void writeInt(int)
virtual int readUnsignedByte()
unsigned int getIDCount() const
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP2.
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.
void commandSetValue(int domID, int varID, const std::string &objID, std::ifstream &defFile)
Sends and validates a SetVariable command.
void send_commandSimulationStep(SUMOTime time) const
Sends a SimulationStep command.
void setSchema(const std::string &viewID, const std::string &schemeName) const
void send_commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *add=0) const
Sends a GetVariable request.
void writeResult()
Writes the results file.
virtual void writeByte(int)
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()
std::string getSchema(const std::string &viewID=DEFAULT_VIEW) const
TraCITestClient(std::string outputFileName="testclient_result.out")
Constructor.
virtual void writeFloat(float)
void errorMsg(std::stringstream &msg)
Writes an error message.
#define RESPONSE_SUBSCRIBE_GUI_CONTEXT
void send_commandSubscribeObjectVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, const std::vector< int > &vars) const
Sends a SubscribeVariable request.
SimulationScope simulation
Scope for interaction with the simulation.
virtual void writeString(const std::string &s)
virtual const char * what() const
VehicleScope vehicle
Scope for interaction with vehicles.
virtual float readFloat()
std::vector< std::string > getIDList() const
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0) const
Validates the result state of a command.
void commandClose()
Sends and validates a Close command.
virtual void writeDouble(double)
GUIScope gui
Scope for interaction with the gui.
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
void send_commandSetValue(int domID, int varID, const std::string &objID, tcpip::Storage &content) const
Sends a SetVariable request.
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.
void check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
std::vector< VehicleData > getVehicleData(const std::string &loopID) const
void commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *addData=0)
Sends and validates a GetVariable command.
void commandSimulationStep(SUMOTime time)
Sends and validates a simulation step command.
~TraCITestClient()
Destructor.