49 #ifdef CHECK_MEMORY_LEAKS
51 #endif // CHECK_MEMORY_LEAKS
57 using namespace testclient;
64 : outputFileName(outputFileName), answerLog(
"") {
65 answerLog.setf(std::ios::fixed , std::ios::floatfield);
78 std::ifstream defFile;
79 std::string fileContentStr;
80 std::stringstream fileContent;
81 std::string lineCommand;
82 std::stringstream msg;
84 bool commentRead =
false;
90 std::stringstream msg;
91 msg <<
"#Error while connecting: " << e.
what();
97 defFile.open(fileName.c_str());
99 msg <<
"Can not open definition file " << fileName << std::endl;
103 defFile.unsetf(std::ios::dec);
105 while (defFile >> lineCommand) {
107 if (lineCommand.compare(
"%") == 0) {
109 commentRead = !commentRead;
116 if (lineCommand.compare(
"repeat") == 0) {
118 defFile >> lineCommand;
120 if (lineCommand.compare(
"simstep2") == 0) {
124 for (
int i = 0; i < repNo; i++) {
127 }
else if (lineCommand.compare(
"getvariable") == 0) {
131 defFile >> domID >> varID >> objID;
133 }
else if (lineCommand.compare(
"getvariable_plus") == 0) {
137 defFile >> domID >> varID >> objID;
138 std::stringstream msg;
141 std::string msgS = msg.str();
146 }
else if (lineCommand.compare(
"subscribevariable") == 0) {
149 std::string beginTime, endTime;
151 defFile >> domID >> objID >> beginTime >> endTime >> varNo;
153 }
else if (lineCommand.compare(
"subscribecontext") == 0) {
155 int domID, varNo, domain;
157 std::string beginTime, endTime;
159 defFile >> domID >> objID >> beginTime >> endTime >> domain >> range >> varNo;
161 }
else if (lineCommand.compare(
"setvalue") == 0) {
165 defFile >> domID >> varID >> objID;
168 msg <<
"Error in definition file: " << lineCommand <<
" is not a valid command";
186 answerLog << std::endl <<
"-> Command sent: <SimulationStep2>:" << std::endl;
189 std::string acknowledgement;
191 answerLog << acknowledgement << std::endl;
202 answerLog << std::endl <<
"-> Command sent: <Close>:" << std::endl;
205 std::string acknowledgement;
207 answerLog << acknowledgement << std::endl;
217 answerLog << std::endl <<
"-> Command sent: <GetVariable>:" << std::endl
218 <<
" domID=" << domID <<
" varID=" << varID
219 <<
" objID=" << objID << std::endl;
222 std::string acknowledgement;
224 answerLog << acknowledgement << std::endl;
234 answerLog <<
" CommandID=" << (domID + 0x10) <<
" VariableID=" << variableID <<
" ObjectID=" << objectID;
236 answerLog <<
" valueDataType=" << valueDataType;
239 std::stringstream msg;
240 msg <<
"Error while receiving command: " << e.
what();
249 std::stringstream msg;
252 std::string msgS = msg.str();
257 answerLog << std::endl <<
"-> Command sent: <SetValue>:" << std::endl
258 <<
" domID=" << domID <<
" varID=" << varID
259 <<
" objID=" << objID << std::endl;
261 std::string acknowledgement;
263 answerLog << acknowledgement << std::endl;
272 std::vector<int> vars;
273 for (
int i = 0; i < varNo; ++i) {
280 answerLog << std::endl <<
"-> Command sent: <SubscribeVariable>:" << std::endl
281 <<
" domID=" << domID <<
" objID=" << objID <<
" with " << varNo <<
" variables" << std::endl;
284 std::string acknowledgement;
286 answerLog << acknowledgement << std::endl;
296 int domain,
SUMOReal range,
int varNo, std::ifstream& defFile) {
297 std::vector<int> vars;
298 for (
int i = 0; i < varNo; ++i) {
305 answerLog << std::endl <<
"-> Command sent: <SubscribeContext>:" << std::endl
306 <<
" domID=" << domID <<
" objID=" << objID <<
" domain=" << domain <<
" range=" << range
307 <<
" with " << varNo <<
" variables" << std::endl;
310 std::string acknowledgement;
312 answerLog << acknowledgement << std::endl;
327 std::cerr <<
"Unable to write result file" << std::endl;
330 locTime = localtime(&seconds);
331 outFile <<
"TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
339 std::cerr << msg.str() << std::endl;
340 answerLog <<
"----" << std::endl << msg.str() << std::endl;
351 int noSubscriptions = inMsg.
readInt();
352 for (
int s = 0; s < noSubscriptions; ++s) {
357 }
catch (std::invalid_argument& e) {
358 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
377 answerLog <<
" #variables=" << varNo << std::endl;
378 for (
unsigned int i = 0; i < varNo; ++i) {
383 answerLog <<
" valueDataType=" << valueDataType;
391 answerLog <<
" #variables=" << varNo << std::endl;
392 unsigned int objNo = inMsg.
readInt();
393 answerLog <<
" #objects=" << objNo << std::endl;
394 for (
unsigned int j = 0; j < objNo; ++j) {
396 for (
unsigned int i = 0; i < varNo; ++i) {
401 answerLog <<
" valueDataType=" << valueDataType;
406 answerLog <<
"#Error: received response with command id: " << cmdId <<
" but expected a subscription response (0xe0-0xef / 0x90-0x9f)" << std::endl;
409 }
catch (std::invalid_argument& e) {
410 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
425 std::string dataTypeS, valueS;
426 defFile >> dataTypeS;
427 if (dataTypeS ==
"<airDist>") {
430 }
else if (dataTypeS ==
"<drivingDist>") {
433 }
else if (dataTypeS ==
"<objSubscription>") {
434 int beginTime, endTime, numVars;
435 defFile >> beginTime >> endTime >> numVars;
439 for (
int i = 0; i < numVars; ++i) {
444 return 4 + 4 + 4 + numVars;
447 if (dataTypeS ==
"<int>") {
449 into.
writeInt(atoi(valueS.c_str()));
451 }
else if (dataTypeS ==
"<byte>") {
455 }
else if (dataTypeS ==
"<ubyte>") {
459 }
else if (dataTypeS ==
"<float>") {
463 }
else if (dataTypeS ==
"<double>") {
467 }
else if (dataTypeS ==
"<string>") {
470 return 4 + 1 + (
int) valueS.length();
471 }
else if (dataTypeS ==
"<string*>") {
472 std::vector<std::string> slValue;
473 int number = atoi(valueS.c_str());
475 for (
int i = 0; i < number; ++i) {
478 slValue.push_back(tmp);
479 length += 4 +
int(tmp.length());
484 }
else if (dataTypeS ==
"<compound>") {
485 int number = atoi(valueS.c_str());
489 for (
int i = 0; i < number; ++i) {
493 }
else if (dataTypeS ==
"<color>") {
496 for (
int i = 0; i < 3; ++i) {
501 }
else if (dataTypeS ==
"<position2D>") {
507 }
else if (dataTypeS ==
"<position3D>") {
514 return 1 + 8 + 8 + 8;
515 }
else if (dataTypeS ==
"<positionRoadmap>") {
518 int length = 1 + 8 + (
int) valueS.length();
523 return length + 4 + 1;
524 }
else if (dataTypeS ==
"<shape>") {
526 int number = atoi(valueS.c_str());
529 for (
int i = 0; i < number; ++i) {
538 msg <<
"## Unknown data type: " << dataTypeS;
547 answerLog <<
" Unsigned Byte Value: " << ubyte << std::endl;
550 answerLog <<
" Byte value: " << byte << std::endl;
553 answerLog <<
" Int value: " << integer << std::endl;
556 if (floatv < 0.1 && floatv > 0) {
557 answerLog.setf(std::ios::scientific, std::ios::floatfield);
559 answerLog <<
" float value: " << floatv << std::endl;
560 answerLog.setf(std::ios::fixed , std::ios::floatfield);
565 answerLog <<
" Double value: " << doublev << std::endl;
571 answerLog <<
" BoundaryBoxValue: lowerLeft x=" << lowerLeftX
572 <<
" y=" << lowerLeftY <<
" upperRight x=" << upperRightX
573 <<
" y=" << upperRightY << std::endl;
577 for (
int i = 0; i < length; i++) {
580 answerLog <<
"(" << x <<
"," << y <<
") ";
587 answerLog <<
" Position3DValue: " << std::endl;
589 <<
" z: " << z << std::endl;
594 answerLog <<
" RoadMapPositionValue: roadId=" << roadId
596 <<
" laneId=" << laneId << std::endl;
599 answerLog <<
" TLPhaseListValue: length=" << length << std::endl;
600 for (
int i = 0; i < length; i++) {
604 answerLog <<
" precRoad=" << pred <<
" succRoad=" << succ
617 answerLog <<
"#Error: unknown phase value" << (
int)phase << std::endl;
623 answerLog <<
" string value: " << s << std::endl;
626 answerLog <<
" string list value: [ " << std::endl;
627 for (std::vector<std::string>::iterator i = s.begin(); i != s.end(); ++i) {
628 if (i != s.begin()) {
636 answerLog <<
" compound value with " << no <<
" members: [ " << std::endl;
637 for (
int i = 0; i < no; ++i) {
639 answerLog <<
" valueDataType=" << currentValueDataType;
646 answerLog <<
" position value: (" << xv <<
"," << yv <<
")" << std::endl;
652 answerLog <<
" color value: (" << r <<
"," << g <<
"," << b <<
"," << a <<
")" << std::endl;
654 answerLog <<
"#Error: unknown valueDataType!" << std::endl;
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_CONTEXT
#define REQUEST_DRIVINGDIST
void close()
Closes the connection.
void connect(const std::string &host, int port)
Connects to the specified SUMO server.
void commandClose()
Sends and validates a Close command.
virtual std::vector< std::string > readStringList()
#define RESPONSE_SUBSCRIBE_GUI_VARIABLE
bool run(std::string fileName, int port, std::string host="localhost")
Runs a test.
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.
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP2.
void send_commandClose() const
Sends a Close command.
void send_commandSubscribeObjectContext(int domID, const std::string &objID, int beginTime, int endTime, int domain, SUMOReal range, const std::vector< int > &vars) const
Sends a SubscribeContext request.
virtual double readDouble()
void writeResult()
Writes the results file.
virtual void writeUnsignedByte(int)
virtual void writeInt(int)
virtual int readUnsignedByte()
TraCITestClient(std::string outputFileName="testclient_result.out")
Constructor.
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
void send_commandSimulationStep(SUMOTime time) const
Sends a SimulationStep command.
void send_commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *add=0) const
Sends a GetVariable request.
virtual void writeByte(int)
void commandSubscribeContextVariable(int domID, const std::string &objID, int beginTime, int endTime, int domain, SUMOReal range, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeContext command.
virtual void writeStringList(const std::vector< std::string > &s)
SUMOTime string2time(const std::string &r)
virtual std::string readString()
virtual void writeFloat(float)
#define RESPONSE_SUBSCRIBE_GUI_CONTEXT
void send_commandSubscribeObjectVariable(int domID, const std::string &objID, int beginTime, int endTime, const std::vector< int > &vars) const
Sends a SubscribeVariable request.
virtual void writeString(const std::string &s)
void commandSimulationStep(SUMOTime time)
Sends and validates a simulation step command.
std::string outputFileName
The name of the file to write the results log into.
virtual const char * what() const
virtual float readFloat()
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0) const
Validates the result state of a command.
void commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *addData=0)
Sends and validates a GetVariable command.
virtual void writeDouble(double)
void send_commandSetValue(int domID, int varID, const std::string &objID, tcpip::Storage &content) const
Sends a SetVariable request.
~TraCITestClient()
Destructor.
void check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
void readAndReportTypeDependent(tcpip::Storage &inMsg, int valueDataType)
Reads a value of the given type from the given storage and reports it.
void commandSubscribeObjectVariable(int domID, const std::string &objID, int beginTime, int endTime, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeVariable command.
void errorMsg(std::stringstream &msg)
Writes an error message.
void commandSetValue(int domID, int varID, const std::string &objID, std::ifstream &defFile)
Sends and validates a SetVariable command.
std::stringstream answerLog
Stream containing the log.