49 #ifdef CHECK_MEMORY_LEAKS
51 #endif // CHECK_MEMORY_LEAKS
57 using namespace testclient;
66 outputFileName(outputFileName),
68 answerLog.setf(std::ios::fixed , std::ios::floatfield);
87 std::cerr <<
"Unable to write result file" << std::endl;
90 locTime = localtime(&seconds);
91 outFile <<
"TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
99 std::cerr << msg.str() << std::endl;
100 answerLog <<
"----" << std::endl << msg.str() << std::endl;
106 std::stringstream msg;
114 msg <<
"#Error while connecting: " << e.
what();
134 std::ifstream defFile;
135 std::string fileContentStr;
136 std::stringstream fileContent;
137 std::string lineCommand;
138 std::stringstream msg;
140 bool commentRead =
false;
147 defFile.open(fileName.c_str());
150 msg <<
"Can not open definition file " << fileName << std::endl;
154 defFile.unsetf(std::ios::dec);
156 while (defFile >> lineCommand) {
158 if (lineCommand.compare(
"%") == 0) {
160 commentRead = !commentRead;
167 if (lineCommand.compare(
"repeat") == 0) {
169 defFile >> lineCommand;
171 if (lineCommand.compare(
"simstep2") == 0) {
175 for (
int i = 0; i < repNo; i++) {
178 }
else if (lineCommand.compare(
"posconversion2d") == 0) {
185 defFile >> destFormat;
187 }
else if (lineCommand.compare(
"posconversion3d") == 0) {
195 defFile >> destFormat;
197 }
else if (lineCommand.compare(
"posconversion_roadpos") == 0) {
205 defFile >> destFormat;
207 }
else if (lineCommand.compare(
"distancerequest_3d_3d") == 0) {
221 }
else if (lineCommand.compare(
"distancerequest_3d_roadpos") == 0) {
235 }
else if (lineCommand.compare(
"distancerequest_roadpos_3d") == 0) {
249 }
else if (lineCommand.compare(
"distancerequest_roadpos_roadpos") == 0) {
263 }
else if (lineCommand.compare(
"getvariable") == 0) {
267 defFile >> domID >> varID >> objID;
269 }
else if (lineCommand.compare(
"getvariable_plus") == 0) {
273 defFile >> domID >> varID >> objID;
275 }
else if (lineCommand.compare(
"subscribevariable") == 0) {
278 std::string beginTime, endTime;
280 defFile >> domID >> objID >> beginTime >> endTime >> varNo;
282 }
else if (lineCommand.compare(
"setvalue") == 0) {
286 defFile >> domID >> varID >> objID;
289 msg <<
"Error in definition file: " << lineCommand
290 <<
" is not a valid command";
316 if (cmdId != command && !ignoreCommandId) {
317 answerLog <<
"#Error: received status response to command: " << cmdId
318 <<
" but expected: " << command << std::endl;
323 }
catch (std::invalid_argument&) {
324 answerLog <<
"#Error: an exception was thrown while reading result state message" << std::endl;
327 switch (resultType) {
329 answerLog <<
".. Answered with error to command (" << cmdId <<
"), [description: " << msg <<
"]" << std::endl;
332 answerLog <<
".. Sent command is not implemented (" << cmdId <<
"), [description: " << msg <<
"]" << std::endl;
335 answerLog <<
".. Command acknowledged (" << cmdId <<
"), [description: " << msg <<
"]" << std::endl;
338 answerLog <<
".. Answered with unknown result code(" << resultType <<
") to command(" << cmdId
339 <<
"), [description: " << msg <<
"]" << std::endl;
342 if ((cmdStart + cmdLength) != inMsg.
position()) {
343 answerLog <<
"#Error: command at position " << cmdStart <<
" has wrong length" << std::endl;
355 std::stringstream msg;
358 msg <<
"#Error while sending command: no connection to server";
372 msg <<
"Error while sending command: " << e.
what();
376 answerLog << std::endl <<
"-> Command sent: <SimulationStep2>:" << std::endl;
381 msg <<
"Error while receiving command: " << e.
what();
420 std::stringstream msg;
423 msg <<
"#Error while sending command: no connection to server" ;
435 }
else if (pos3D != NULL) {
440 }
else if (posRoad != NULL) {
446 std::cerr <<
"Error in method commandPositionConversion: position is NULL" << std::endl;
459 msg <<
"Error while sending command: " << e.
what();
464 answerLog << std::endl <<
"-> Command sent: <PositionConversion>:" << std::endl;
466 answerLog <<
" DestPosition-2D: x=" << pos2D->
x <<
" y=" << pos2D->
y ;
467 }
else if (pos3D != NULL) {
468 answerLog <<
" DestPosition-3D: x=" << pos3D->
x <<
" y=" << pos3D->
y <<
" z=" << pos3D->
z;
469 }
else if (posRoad != NULL) {
470 answerLog <<
" DestPosition-RoadMap: roadId=" << posRoad->
roadId <<
" pos=" << posRoad->
pos <<
" laneId=" << (
int)posRoad->
laneId ;
472 answerLog <<
" posId=" << posId << std::endl;
478 msg <<
"Error while receiving command: " << e.
what();
559 std::stringstream msg;
562 msg <<
"#Error while sending command: no connection to server" ;
570 if (pos1_2D != NULL) {
574 }
else if (pos1_3D != NULL) {
579 }
else if (pos1_Road != NULL) {
585 std::cerr <<
"Error in method commandDistanceRequest: position1 is NULL" << std::endl;
589 if (pos2_2D != NULL) {
593 }
else if (pos2_3D != NULL) {
598 }
else if (pos2_Road != NULL) {
604 std::cerr <<
"Error in method commandDistanceRequest: position2 is NULL" << std::endl;
617 msg <<
"Error while sending command: " << e.
what();
622 answerLog << std::endl <<
"-> Command sent: <DistanceRequest>:" << std::endl;
623 if (pos1_2D != NULL) {
624 answerLog <<
" FirstPosition-2D: x=" << pos1_2D->
x <<
" y=" << pos1_2D->
y ;
625 }
else if (pos1_3D != NULL) {
626 answerLog <<
" FirstPosition-3D: x=" << pos1_3D->
x <<
" y=" << pos1_3D->
y <<
" z=" << pos1_3D->
z;
627 }
else if (pos1_Road != NULL) {
628 answerLog <<
" FirstPosition-RoadMap: roadId=" << pos1_Road->
roadId <<
" pos=" << pos1_Road->
pos <<
" laneId=" << (
int)pos1_Road->
laneId ;
630 if (pos2_2D != NULL) {
631 answerLog <<
" SecondPosition-2D: x=" << pos2_2D->
x <<
" y=" << pos2_2D->
y ;
632 }
else if (pos2_3D != NULL) {
633 answerLog <<
" SecondPosition-3D: x=" << pos2_3D->
x <<
" y=" << pos2_3D->
y <<
" z=" << pos2_3D->
z;
634 }
else if (pos2_Road != NULL) {
635 answerLog <<
" SecondPosition-RoadMap: roadId=" << pos2_Road->
roadId <<
" pos=" << pos2_Road->
pos <<
" laneId=" << (
int)pos2_Road->
laneId ;
637 answerLog <<
" Flag=" << flag << std::endl;
643 msg <<
"Error while receiving command: " << e.
what();
661 std::stringstream msg;
663 msg <<
"#Error while sending command: no connection to server" ;
680 msg <<
"Error while sending command: " << e.
what();
684 answerLog << std::endl <<
"-> Command sent: <GetVariable>:" << std::endl
685 <<
" domID=" << domID <<
" varID=" << varID
686 <<
" objID=" << objID << std::endl;
695 msg <<
"Error while receiving command: " << e.
what();
707 if (cmdId != (domID + 0x10)) {
708 answerLog <<
"#Error: received response with command id: " << cmdId
709 <<
"but expected: " << (
int)(domID + 0x10) << std::endl;
716 answerLog <<
" valueDataType=" << valueDataType;
719 msg <<
"Error while receiving command: " << e.
what();
728 std::stringstream msg;
730 msg <<
"#Error while sending command: no connection to server" ;
736 std::string msgS = msg.str();
754 msg <<
"Error while sending command: " << e.
what();
758 answerLog << std::endl <<
"-> Command sent: <GetVariable>:" << std::endl
759 <<
" domID=" << domID <<
" varID=" << varID
760 <<
" objID=" << objID << std::endl;
769 msg <<
"Error while receiving command: " << e.
what();
781 if (cmdId != (domID + 0x10)) {
782 answerLog <<
"#Error: received response with command id: " << cmdId
783 <<
"but expected: " << (
int)(domID + 0x10) << std::endl;
790 answerLog <<
" valueDataType=" << valueDataType;
793 msg <<
"Error while receiving command: " << e.
what();
802 std::stringstream msg;
804 msg <<
"#Error while sending command: no connection to server" ;
809 std::string msgS = msg.str();
815 outMsg.
writeInt( 5 + 1 + 4 + 4 + 4 + (
int) objID.length() + 1 + varNo);
825 for (
int i = 0; i < varNo; ++i) {
835 msg <<
"Error while sending command: " << e.
what();
839 answerLog << std::endl <<
"-> Command sent: <SubscribeVariable>:" << std::endl
840 <<
" domID=" << domID <<
" objID=" << objID <<
" with " << varNo <<
" variables" << std::endl;
849 msg <<
"Error while receiving command: " << e.
what();
857 msg <<
"Error while receiving command: " << e.
what();
867 std::string dataTypeS, valueS;
868 defFile >> dataTypeS;
869 if (dataTypeS ==
"<airDist>") {
872 }
else if (dataTypeS ==
"<drivingDist>") {
877 if (dataTypeS ==
"<int>") {
879 into.
writeInt(atoi(valueS.c_str()));
881 }
else if (dataTypeS ==
"<byte>") {
885 }
else if (dataTypeS ==
"<ubyte>") {
889 }
else if (dataTypeS ==
"<float>") {
893 }
else if (dataTypeS ==
"<double>") {
897 }
else if (dataTypeS ==
"<string>") {
900 return 4 + 1 + (
int) valueS.length();
901 }
else if (dataTypeS ==
"<string*>") {
902 std::vector<std::string> slValue;
903 int number = atoi(valueS.c_str());
905 for (
int i = 0; i < number; ++i) {
908 slValue.push_back(tmp);
909 length += 4 +
int(tmp.length());
914 }
else if (dataTypeS ==
"<compound>") {
916 int number = atoi(valueS.c_str());
919 for (
int i = 0; i < number; ++i) {
923 }
else if (dataTypeS ==
"<color>") {
926 for (
int i = 0; i < 3; ++i) {
931 }
else if (dataTypeS ==
"<position2D>") {
937 }
else if (dataTypeS ==
"<position3D>") {
944 return 1 + 8 + 8 + 8;
945 }
else if (dataTypeS ==
"<positionRoadmap>") {
948 int length = 1 + 8 + (
int) valueS.length();
953 return length + 4 + 1;
954 }
else if (dataTypeS ==
"<shape>") {
956 int number = atoi(valueS.c_str());
959 for (
int i = 0; i < number; ++i) {
968 msg <<
"## Unknown data type: " << dataTypeS;
974 std::stringstream msg;
976 msg <<
"#Error while sending command: no connection to server" ;
982 std::string msgS = msg.str();
1000 msg <<
"Error while sending command: " << e.
what();
1004 answerLog << std::endl <<
"-> Command sent: <SetValue>:" << std::endl
1005 <<
" domID=" << domID <<
" varID=" << varID
1006 <<
" objID=" << objID << std::endl;
1015 msg <<
"Error while receiving command: " << e.
what();
1028 std::stringstream msg;
1031 msg <<
"#Error while sending command: no connection to server" ;
1045 msg <<
"Error while sending command: " << e.
what();
1050 answerLog << std::endl <<
"-> Command sent: <Close>:" << std::endl;
1056 msg <<
"Error while receiving command: " << e.
what();
1071 int noSubscriptions = inMsg.
readInt();
1072 for (
int s = 0; s < noSubscriptions; ++s) {
1082 }
catch (std::invalid_argument& e) {
1083 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
1099 if (cmdId < 0xe0 || cmdId > 0xef) {
1100 answerLog <<
"#Error: received response with command id: " << cmdId <<
" but expected a subscription response (0xe0-0xef)" << std::endl;
1106 answerLog <<
" #variables=" << varNo << std::endl;
1107 for (
unsigned int i = 0; i < varNo; ++i) {
1112 answerLog <<
" valueDataType=" << valueDataType;
1115 }
catch (std::invalid_argument& e) {
1116 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
1140 answerLog <<
"#Error: received response with command id: " << cmdId
1144 answerLog <<
".. Received Response <PositionConversion>:" << std::endl;
1151 answerLog <<
"2D-Position: x=" << pos2D.
x <<
" y=" << pos2D.
y << std::endl;
1158 answerLog <<
"x=" << pos3D.
x <<
" y=" << pos3D.
y <<
" z=" << pos3D.
z << std::endl;
1165 <<
" laneId=" << (
int)roadPos.
laneId << std::endl;
1168 answerLog <<
"#Error: received unknown position format" << std::endl;
1173 if (reqPosType != posType) {
1174 answerLog <<
"#Warning: requested position type (" << reqPosType
1175 <<
") and received position type (" << posType <<
") do not match" << std::endl;
1178 if ((cmdStart + cmdLength) != inMsg.
position()) {
1179 answerLog <<
"#Error: command at position " << cmdStart <<
" has wrong length" << std::endl;
1182 }
catch (std::invalid_argument& e) {
1183 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
1208 answerLog <<
"#Error: received response with command id: " << cmdId
1212 answerLog <<
".. Received Response <DistanceRequest>:" << std::endl;
1218 answerLog <<
" distance=" << distance << std::endl;
1246 if ((cmdStart + cmdLength) != inMsg.
position()) {
1247 answerLog <<
"#Error: command at position " << cmdStart <<
" has wrong length" << std::endl;
1250 }
catch (std::invalid_argument& e) {
1251 answerLog <<
"#Error while reading message:" << e.what() << std::endl;
1263 answerLog <<
" Unsigned Byte Value: " << ubyte << std::endl;
1264 }
else if (valueDataType ==
TYPE_BYTE) {
1266 answerLog <<
" Byte value: " << byte << std::endl;
1268 int integer = inMsg.
readInt();
1269 answerLog <<
" Int value: " << integer << std::endl;
1272 if (floatv < 0.1 && floatv > 0) {
1273 answerLog.setf(std::ios::scientific, std::ios::floatfield);
1275 answerLog <<
" float value: " << floatv << std::endl;
1276 answerLog.setf(std::ios::fixed , std::ios::floatfield);
1281 answerLog <<
" Double value: " << doublev << std::endl;
1294 for (
int i = 0; i < length; i++) {
1297 answerLog <<
"(" << x <<
"," << y <<
") ";
1304 answerLog <<
" Position3DValue: " << std::endl;
1306 <<
" z: " << z << std::endl;
1311 answerLog <<
" RoadMapPositionValue: roadId=" << roadId
1313 <<
" laneId=" << laneId << std::endl;
1316 answerLog <<
" TLPhaseListValue: length=" << length << std::endl;
1317 for (
int i = 0; i < length; i++) {
1321 answerLog <<
" precRoad=" << pred <<
" succRoad=" << succ
1334 answerLog <<
"#Error: unknown phase value" << (
int)phase << std::endl;
1340 answerLog <<
" string value: " << s << std::endl;
1343 answerLog <<
" string list value: [ " << std::endl;
1344 for (std::vector<std::string>::iterator i = s.begin(); i != s.end(); ++i) {
1345 if (i != s.begin()) {
1353 answerLog <<
" compound value with " << no <<
" members: [ " << std::endl;
1354 for (
int i = 0; i < no; ++i) {
1356 answerLog <<
" valueDataType=" << currentValueDataType;
1363 answerLog <<
" position value: (" << xv <<
"," << yv <<
")" << std::endl;
1369 answerLog <<
" color value: (" << r <<
"," << g <<
"," << b <<
"," << a <<
")" << std::endl;
1371 answerLog <<
"#Error: unknown valueDataType!" << std::endl;