49 #ifdef CHECK_MEMORY_LEAKS
51 #endif // CHECK_MEMORY_LEAKS
58 : outputFileName(outputFileName), answerLog(
"") {
59 answerLog.setf(std::ios::fixed , std::ios::floatfield);
72 std::ifstream defFile;
73 std::string fileContentStr;
74 std::stringstream fileContent;
75 std::string lineCommand;
76 std::stringstream msg;
78 bool commentRead =
false;
84 std::stringstream msg;
85 msg <<
"#Error while connecting: " << e.
what();
91 defFile.open(fileName.c_str());
93 msg <<
"Can not open definition file " << fileName << std::endl;
97 defFile.unsetf(std::ios::dec);
99 while (defFile >> lineCommand) {
101 if (lineCommand.compare(
"%") == 0) {
103 commentRead = !commentRead;
110 if (lineCommand.compare(
"repeat") == 0) {
112 defFile >> lineCommand;
114 if (lineCommand.compare(
"simstep2") == 0) {
118 for (
int i = 0; i < repNo; i++) {
121 }
else if (lineCommand.compare(
"getvariable") == 0) {
125 defFile >> domID >> varID >> objID;
127 }
else if (lineCommand.compare(
"getvariable_plus") == 0) {
131 defFile >> domID >> varID >> objID;
132 std::stringstream msg;
135 std::string msgS = msg.str();
140 }
else if (lineCommand.compare(
"subscribevariable") == 0) {
143 std::string beginTime, endTime;
145 defFile >> domID >> objID >> beginTime >> endTime >> varNo;
147 }
else if (lineCommand.compare(
"subscribecontext") == 0) {
149 int domID, varNo, domain;
151 std::string beginTime, endTime;
153 defFile >> domID >> objID >> beginTime >> endTime >> domain >> range >> varNo;
155 }
else if (lineCommand.compare(
"setvalue") == 0) {
159 defFile >> domID >> varID >> objID;
162 msg <<
"Error in definition file: " << lineCommand <<
" is not a valid command";
180 answerLog << std::endl <<
"-> Command sent: <SimulationStep2>:" << std::endl;
183 std::string acknowledgement;
185 answerLog << acknowledgement << std::endl;
196 answerLog << std::endl <<
"-> Command sent: <Close>:" << std::endl;
199 std::string acknowledgement;
201 answerLog << acknowledgement << std::endl;
211 answerLog << std::endl <<
"-> Command sent: <GetVariable>:" << std::endl
212 <<
" domID=" << domID <<
" varID=" << varID
213 <<
" objID=" << objID << std::endl;
216 std::string acknowledgement;
218 answerLog << acknowledgement << std::endl;
228 answerLog <<
" CommandID=" << (domID + 0x10) <<
" VariableID=" << variableID <<
" ObjectID=" << objectID;
230 answerLog <<
" valueDataType=" << valueDataType;
233 std::stringstream msg;
234 msg <<
"Error while receiving command: " << e.
what();
243 std::stringstream msg;
246 std::string msgS = msg.str();
251 answerLog << std::endl <<
"-> Command sent: <SetValue>:" << std::endl
252 <<
" domID=" << domID <<
" varID=" << varID
253 <<
" objID=" << objID << std::endl;
255 std::string acknowledgement;
257 answerLog << acknowledgement << std::endl;
266 std::vector<int> vars;
267 for (
int i = 0; i < varNo; ++i) {
274 answerLog << std::endl <<
"-> Command sent: <SubscribeVariable>:" << std::endl
275 <<
" domID=" << domID <<
" objID=" << objID <<
" with " << varNo <<
" variables" << std::endl;
278 std::string acknowledgement;
280 answerLog << acknowledgement << std::endl;
290 int domain,
SUMOReal range,
int varNo, std::ifstream& defFile) {
291 std::vector<int> vars;
292 for (
int i = 0; i < varNo; ++i) {
299 answerLog << std::endl <<
"-> Command sent: <SubscribeContext>:" << std::endl
300 <<
" domID=" << domID <<
" objID=" << objID <<
" domain=" << domain <<
" range=" << range
301 <<
" with " << varNo <<
" variables" << std::endl;
304 std::string acknowledgement;
306 answerLog << acknowledgement << std::endl;
321 std::cerr <<
"Unable to write result file" << std::endl;
324 locTime = localtime(&seconds);
325 outFile <<
"TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
333 std::cerr << msg.str() << std::endl;
334 answerLog <<
"----" << std::endl << msg.str() << std::endl;
345 int noSubscriptions = inMsg.
readInt();
346 for (
int s = 0; s < noSubscriptions; ++s) {
351 }
catch (std::invalid_argument& e) {
352 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
371 answerLog <<
" #variables=" << varNo << std::endl;
372 for (
unsigned int i = 0; i < varNo; ++i) {
377 answerLog <<
" valueDataType=" << valueDataType;
385 answerLog <<
" #variables=" << varNo << std::endl;
386 unsigned int objNo = inMsg.
readInt();
387 answerLog <<
" #objects=" << objNo << std::endl;
388 for (
unsigned int j = 0; j < objNo; ++j) {
390 for (
unsigned int i = 0; i < varNo; ++i) {
395 answerLog <<
" valueDataType=" << valueDataType;
400 answerLog <<
"#Error: received response with command id: " << cmdId <<
" but expected a subscription response (0xe0-0xef / 0x90-0x9f)" << std::endl;
403 }
catch (std::invalid_argument& e) {
404 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
419 std::string dataTypeS;
420 defFile >> dataTypeS;
421 if (dataTypeS ==
"<airDist>") {
424 }
else if (dataTypeS ==
"<drivingDist>") {
427 }
else if (dataTypeS ==
"<objSubscription>") {
428 int beginTime, endTime, numVars;
429 defFile >> beginTime >> endTime >> numVars;
433 for (
int i = 0; i < numVars; ++i) {
438 return 4 + 4 + 4 + numVars;
442 if (dataTypeS ==
"<int>") {
447 }
else if (dataTypeS ==
"<byte>") {
452 }
else if (dataTypeS ==
"<ubyte>") {
457 }
else if (dataTypeS ==
"<float>") {
462 }
else if (dataTypeS ==
"<double>") {
467 }
else if (dataTypeS ==
"<string>") {
470 if (valueS ==
"\"\"") {
475 return 4 + 1 + (
int) valueS.length();
476 }
else if (dataTypeS ==
"<string*>") {
477 std::vector<std::string> slValue;
480 for (
int i = 0; i < valI; ++i) {
483 slValue.push_back(tmp);
484 length += 4 +
int(tmp.length());
489 }
else if (dataTypeS ==
"<compound>") {
494 for (
int i = 0; i < valI; ++i) {
498 }
else if (dataTypeS ==
"<color>") {
502 for (
int i = 0; i < 3; ++i) {
507 }
else if (dataTypeS ==
"<position2D>") {
514 }
else if (dataTypeS ==
"<position3D>") {
522 return 1 + 8 + 8 + 8;
523 }
else if (dataTypeS ==
"<positionRoadmap>") {
528 int length = 1 + 8 + (
int) valueS.length();
533 return length + 4 + 1;
534 }
else if (dataTypeS ==
"<shape>") {
539 for (
int i = 0; i < valI; ++i) {
548 msg <<
"## Unknown data type: " << dataTypeS;
557 answerLog <<
" Unsigned Byte Value: " << ubyte << std::endl;
560 answerLog <<
" Byte value: " << byte << std::endl;
563 answerLog <<
" Int value: " << integer << std::endl;
566 if (floatv < 0.1 && floatv > 0) {
567 answerLog.setf(std::ios::scientific, std::ios::floatfield);
569 answerLog <<
" float value: " << floatv << std::endl;
570 answerLog.setf(std::ios::fixed , std::ios::floatfield);
575 answerLog <<
" Double value: " << doublev << std::endl;
581 answerLog <<
" BoundaryBoxValue: lowerLeft x=" << lowerLeftX
582 <<
" y=" << lowerLeftY <<
" upperRight x=" << upperRightX
583 <<
" y=" << upperRightY << std::endl;
587 for (
int i = 0; i < length; i++) {
590 answerLog <<
"(" << x <<
"," << y <<
") ";
597 answerLog <<
" Position3DValue: " << std::endl;
599 <<
" z: " << z << std::endl;
604 answerLog <<
" RoadMapPositionValue: roadId=" << roadId
606 <<
" laneId=" << laneId << std::endl;
609 answerLog <<
" TLPhaseListValue: length=" << length << std::endl;
610 for (
int i = 0; i < length; i++) {
614 answerLog <<
" precRoad=" << pred <<
" succRoad=" << succ
627 answerLog <<
"#Error: unknown phase value" << (
int)phase << std::endl;
633 answerLog <<
" string value: " << s << std::endl;
636 answerLog <<
" string list value: [ " << std::endl;
637 for (std::vector<std::string>::iterator i = s.begin(); i != s.end(); ++i) {
638 if (i != s.begin()) {
646 answerLog <<
" compound value with " << no <<
" members: [ " << std::endl;
647 for (
int i = 0; i < no; ++i) {
649 answerLog <<
" valueDataType=" << currentValueDataType;
656 answerLog <<
" position value: (" << xv <<
"," << yv <<
")" << std::endl;
662 answerLog <<
" color value: (" << r <<
"," << g <<
"," << b <<
"," << a <<
")" << std::endl;
664 answerLog <<
"#Error: unknown valueDataType!" << std::endl;
#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 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.
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.
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()
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
std::string outputFileName
The name of the file to write the results log into.
virtual void writeUnsignedByte(int)
virtual void writeInt(int)
virtual int readUnsignedByte()
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP2.
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 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()
void commandSubscribeObjectVariable(int domID, const std::string &objID, int beginTime, int endTime, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeVariable command.
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, int beginTime, int endTime, const std::vector< int > &vars) const
Sends a SubscribeVariable request.
virtual void writeString(const std::string &s)
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 commandClose()
Sends and validates a Close command.
virtual void writeDouble(double)
void send_commandSetValue(int domID, int varID, const std::string &objID, tcpip::Storage &content) const
Sends a SetVariable request.
void check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) 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.