SUMO - Simulation of Urban MObility

TraCITestClient.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 /* =========================================================================
00020  * included modules
00021  * ======================================================================= */
00022 #ifdef _MSC_VER
00023 #include <windows_config.h>
00024 #else
00025 #include <config.h>
00026 #endif
00027 
00028 #include <vector>
00029 #include <iostream>
00030 #include <iomanip>
00031 #include <fstream>
00032 #include <sstream>
00033 #include <ctime>
00034 #include <cstdlib>
00035 
00036 #define BUILD_TCPIP
00037 #include <foreign/tcpip/storage.h>
00038 #include <foreign/tcpip/socket.h>
00039 
00040 #include <traci-server/TraCIConstants.h>
00041 #include <utils/common/SUMOTime.h>
00042 #include "TraCITestClient.h"
00043 
00044 #ifdef CHECK_MEMORY_LEAKS
00045 #include <foreign/nvwa/debug_new.h>
00046 #endif // CHECK_MEMORY_LEAKS
00047 
00048 
00049 // ===========================================================================
00050 // used namespaces
00051 // ===========================================================================
00052 using namespace testclient;
00053 
00054 
00055 // ===========================================================================
00056 // method definitions
00057 // ===========================================================================
00058 
00059 TraCITestClient::TraCITestClient(std::string outputFileName)
00060     :socket(NULL),
00061      outputFileName(outputFileName),
00062      answerLog("") {
00063     answerLog.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
00064     answerLog.setf(std::ios::showpoint); // print decimal point
00065     answerLog << std::setprecision(2);
00066 }
00067 
00068 
00069 TraCITestClient::~TraCITestClient() {
00070     writeResult();
00071 }
00072 
00073 
00074 void
00075 TraCITestClient::writeResult() {
00076     time_t seconds;
00077     tm* locTime;
00078 
00079     std::ofstream outFile(outputFileName.c_str());
00080     if (!outFile) {
00081         std::cerr << "Unable to write result file" << std::endl;
00082     }
00083     time(&seconds);
00084     locTime = localtime(&seconds);
00085     outFile << "TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
00086     outFile << answerLog.str();
00087     outFile.close();
00088 }
00089 
00090 
00091 void
00092 TraCITestClient::errorMsg(std::stringstream& msg) {
00093     std::cerr << msg.str() << std::endl;
00094     answerLog << "----" << std::endl << msg.str() << std::endl;
00095 }
00096 
00097 
00098 bool
00099 TraCITestClient::connect(int port, std::string host) {
00100     std::stringstream msg;
00101     socket = new tcpip::Socket(host, port);
00102 
00103     //socket->set_blocking(true);
00104 
00105     try {
00106         socket->connect();
00107     } catch (tcpip::SocketException& e) {
00108         msg << "#Error while connecting: " << e.what();
00109         errorMsg(msg);
00110         return false;
00111     }
00112 
00113     return true;
00114 }
00115 
00116 
00117 bool
00118 TraCITestClient::close() {
00119     if (socket != NULL) {
00120         socket->close();
00121     }
00122     return true;
00123 }
00124 
00125 
00126 bool
00127 TraCITestClient::run(std::string fileName, int port, std::string host) {
00128     std::ifstream defFile;
00129     std::string fileContentStr;
00130     std::stringstream fileContent;
00131     std::string lineCommand;
00132     std::stringstream msg;
00133     int repNo = 1;
00134     bool commentRead = false;
00135 
00136     if (!connect(port, host)) {
00137         return false;
00138     }
00139 
00140     // read definition file and trigger commands according to it
00141     defFile.open(fileName.c_str());
00142 
00143     if (!defFile) {
00144         msg << "Can not open definition file " << fileName << std::endl;
00145         errorMsg(msg);
00146         return false;
00147     }
00148     defFile.unsetf(std::ios::dec);
00149 
00150     while (defFile >> lineCommand) {
00151         repNo = 1;
00152         if (lineCommand.compare("%") == 0) {
00153             // a comment was read
00154             commentRead = !commentRead;
00155             continue;
00156         }
00157         if (commentRead) {
00158             // wait until end of comment is reached
00159             continue;
00160         }
00161         if (lineCommand.compare("repeat") == 0) {
00162             defFile >> repNo;
00163             defFile >> lineCommand;
00164         }
00165         if (lineCommand.compare("simstep2") == 0) {
00166             // read parameter for command simulation step and trigger command
00167             std::string time;
00168             defFile >> time;
00169             for (int i=0; i < repNo; i++) {
00170                 commandSimulationStep2(string2time(time));
00171             }
00172         } else if (lineCommand.compare("posconversion2d") == 0) {
00173             // trigger command PositionConversion for a 2d position
00174             testclient::Position pos;
00175             int destFormat;
00176 
00177             defFile >> pos.x;
00178             defFile >> pos.y;
00179             defFile >> destFormat;
00180             commandPositionConversion(pos, destFormat);
00181         } else if (lineCommand.compare("posconversion3d") == 0) {
00182             // trigger command PositionConversion for a 3d position
00183             testclient::Position3D pos;
00184             int destFormat;
00185 
00186             defFile >> pos.x;
00187             defFile >> pos.y;
00188             defFile >> pos.z;
00189             defFile >> destFormat;
00190             commandPositionConversion(pos, destFormat);
00191         } else if (lineCommand.compare("posconversion_roadpos") == 0) {
00192             // trigger command PositionConversion for a road map position
00193             testclient::PositionRoadMap pos;
00194             int destFormat;
00195 
00196             defFile >> pos.roadId;
00197             defFile >> pos.pos;
00198             defFile >> pos.laneId;
00199             defFile >> destFormat;
00200             commandPositionConversion(pos, destFormat);
00201         } else if (lineCommand.compare("distancerequest_3d_3d") == 0) {
00202             // trigger command DistanceRequest for 2 3D positions
00203             testclient::Position3D pos1;
00204             testclient::Position3D pos2;
00205             int flag;
00206 
00207             defFile >> pos1.x;
00208             defFile >> pos1.y;
00209             defFile >> pos1.z;
00210             defFile >> pos2.x;
00211             defFile >> pos2.y;
00212             defFile >> pos2.z;
00213             defFile >> flag;
00214             commandDistanceRequest(pos1, pos2, flag);
00215         } else if (lineCommand.compare("distancerequest_3d_roadpos") == 0) {
00216             // trigger command DistanceRequest for 3D and road map position
00217             testclient::Position3D pos1;
00218             testclient::PositionRoadMap pos2;
00219             int flag;
00220 
00221             defFile >> pos1.x;
00222             defFile >> pos1.y;
00223             defFile >> pos1.z;
00224             defFile >> pos2.roadId;
00225             defFile >> pos2.pos;
00226             defFile >> pos2.laneId;
00227             defFile >> flag;
00228             commandDistanceRequest(pos1, pos2, flag);
00229         } else if (lineCommand.compare("distancerequest_roadpos_3d") == 0) {
00230             // trigger command DistanceRequest for road map and 3D position
00231             testclient::PositionRoadMap pos1;
00232             testclient::Position3D pos2;
00233             int flag;
00234 
00235             defFile >> pos1.roadId;
00236             defFile >> pos1.pos;
00237             defFile >> pos1.laneId;
00238             defFile >> pos2.x;
00239             defFile >> pos2.y;
00240             defFile >> pos2.z;
00241             defFile >> flag;
00242             commandDistanceRequest(pos1, pos2, flag);
00243         } else if (lineCommand.compare("distancerequest_roadpos_roadpos") == 0) {
00244             // trigger command DistanceRequest for 2 road map positions
00245             testclient::PositionRoadMap pos1;
00246             testclient::PositionRoadMap pos2;
00247             int flag;
00248 
00249             defFile >> pos1.roadId;
00250             defFile >> pos1.pos;
00251             defFile >> pos1.laneId;
00252             defFile >> pos2.roadId;
00253             defFile >> pos2.pos;
00254             defFile >> pos2.laneId;
00255             defFile >> flag;
00256             commandDistanceRequest(pos1, pos2, flag);
00257         } else if (lineCommand.compare("getvariable") == 0) {
00258             // trigger command GetXXXVariable
00259             int domID, varID;
00260             std::string objID;
00261             defFile >> domID >> varID >> objID;
00262             commandGetVariable(domID, varID, objID);
00263         } else if (lineCommand.compare("getvariable_plus") == 0) {
00264             // trigger command GetXXXVariable with one parameter
00265             int domID, varID;
00266             std::string objID;
00267             defFile >> domID >> varID >> objID;
00268             commandGetVariablePlus(domID, varID, objID, defFile);
00269         } else if (lineCommand.compare("subscribevariable") == 0) {
00270             // trigger command SubscribeXXXVariable
00271             int domID, varNo;
00272             std::string beginTime, endTime;
00273             std::string objID;
00274             defFile >> domID >> objID >> beginTime >> endTime >> varNo;
00275             commandSubscribeVariable(domID, objID, string2time(beginTime), string2time(endTime), varNo, defFile);
00276         }  else if (lineCommand.compare("setvalue") == 0) {
00277             // trigger command SetXXXValue
00278             int domID, varID;
00279             std::string objID;
00280             defFile >> domID >> varID >> objID;
00281             commandSetValue(domID, varID, objID, defFile);
00282         } else {
00283             msg << "Error in definition file: " << lineCommand
00284                 << " is not a valid command";
00285             errorMsg(msg);
00286             commandClose();
00287             close();
00288             return false;
00289         }
00290     }
00291     defFile.close();
00292     commandClose();
00293     close();
00294     return true;
00295 }
00296 
00297 
00298 bool
00299 TraCITestClient::reportResultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId) {
00300     int cmdLength;
00301     int cmdId;
00302     int resultType;
00303     int cmdStart;
00304     std::string msg;
00305 
00306     try {
00307         cmdStart = inMsg.position();
00308         cmdLength = inMsg.readUnsignedByte();
00309         cmdId = inMsg.readUnsignedByte();
00310         if (cmdId != command && !ignoreCommandId) {
00311             answerLog << "#Error: received status response to command: " << cmdId
00312                       << " but expected: " << command << std::endl;
00313             return false;
00314         }
00315         resultType = inMsg.readUnsignedByte();
00316         msg = inMsg.readString();
00317     } catch (std::invalid_argument&) {
00318         answerLog << "#Error: an exception was thrown while reading result state message" << std::endl;
00319         return false;
00320     }
00321     switch (resultType) {
00322     case RTYPE_ERR:
00323         answerLog << ".. Answered with error to command (" << cmdId << "), [description: " << msg << "]" << std::endl;
00324         return false;
00325     case RTYPE_NOTIMPLEMENTED:
00326         answerLog << ".. Sent command is not implemented (" << cmdId << "), [description: " << msg << "]" << std::endl;
00327         return false;
00328     case RTYPE_OK:
00329         answerLog << ".. Command acknowledged (" << cmdId << "), [description: " << msg << "]" << std::endl;
00330         break;
00331     default:
00332         answerLog << ".. Answered with unknown result code(" << resultType << ") to command(" << cmdId
00333                   << "), [description: " << msg << "]" << std::endl;
00334         return false;
00335     }
00336     if ((cmdStart + cmdLength) != inMsg.position()) {
00337         answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
00338         return false;
00339     }
00340 
00341     return true;
00342 }
00343 
00344 
00345 void
00346 TraCITestClient::commandSimulationStep2(SUMOTime time) {
00347     tcpip::Storage outMsg;
00348     tcpip::Storage inMsg;
00349     std::stringstream msg;
00350 
00351     if (socket == NULL) {
00352         msg << "#Error while sending command: no connection to server";
00353         errorMsg(msg);
00354         return;
00355     }
00356 
00357     // command length
00358     outMsg.writeUnsignedByte(1 + 1 + 4);
00359     // command id
00360     outMsg.writeUnsignedByte(CMD_SIMSTEP2);
00361     outMsg.writeInt(time);
00362     // send request message
00363     try {
00364         socket->sendExact(outMsg);
00365     } catch (tcpip::SocketException& e) {
00366         msg << "Error while sending command: " << e.what();
00367         errorMsg(msg);
00368         return;
00369     }
00370     answerLog << std::endl << "-> Command sent: <SimulationStep2>:" << std::endl;
00371     // receive answer message
00372     try {
00373         socket->receiveExact(inMsg);
00374     } catch (tcpip::SocketException& e) {
00375         msg << "Error while receiving command: " << e.what();
00376         errorMsg(msg);
00377         return;
00378     }
00379     // validate result state
00380     if (!reportResultState(inMsg, CMD_SIMSTEP2)) {
00381         return;
00382     }
00383     // validate answer message
00384     validateSimulationStep2(inMsg);
00385 }
00386 
00387 
00388 void
00389 TraCITestClient::commandPositionConversion(testclient::Position pos, int posId) {
00390     commandPositionConversion(&pos, NULL, NULL, posId);
00391 }
00392 
00393 
00394 void
00395 TraCITestClient::commandPositionConversion(testclient::Position3D pos, int posId) {
00396     commandPositionConversion(NULL, &pos, NULL, posId);
00397 }
00398 
00399 
00400 void
00401 TraCITestClient::commandPositionConversion(testclient::PositionRoadMap pos, int posId) {
00402     commandPositionConversion(NULL, NULL, &pos, posId);
00403 }
00404 
00405 
00406 void
00407 TraCITestClient::commandPositionConversion(testclient::Position* pos2D,
00408         testclient::Position3D* pos3D,
00409         testclient::PositionRoadMap* posRoad,
00410         int posId) {
00411     tcpip::Storage outMsg;
00412     tcpip::Storage inMsg;
00413     tcpip::Storage tempMsg;
00414     std::stringstream msg;
00415 
00416     if (socket == NULL) {
00417         msg << "#Error while sending command: no connection to server" ;
00418         errorMsg(msg);
00419         return;
00420     }
00421 
00422     // command id
00423     tempMsg.writeUnsignedByte(CMD_POSITIONCONVERSION);
00424     // position
00425     if (pos2D != NULL) {
00426         tempMsg.writeUnsignedByte(POSITION_2D);
00427         tempMsg.writeDouble(pos2D->x);
00428         tempMsg.writeDouble(pos2D->y);
00429     } else if (pos3D != NULL) {
00430         tempMsg.writeUnsignedByte(POSITION_3D);
00431         tempMsg.writeDouble(pos3D->x);
00432         tempMsg.writeDouble(pos3D->y);
00433         tempMsg.writeDouble(pos3D->z);
00434     } else if (posRoad != NULL) {
00435         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
00436         tempMsg.writeString(posRoad->roadId);
00437         tempMsg.writeDouble(posRoad->pos);
00438         tempMsg.writeUnsignedByte(posRoad->laneId);
00439     } else {
00440         std::cerr << "Error in method commandPositionConversion: position is NULL" << std::endl;
00441         return;
00442     }
00443     // destination position id
00444     tempMsg.writeUnsignedByte(posId);
00445     // command length
00446     outMsg.writeUnsignedByte(1 + (int) tempMsg.size());
00447     outMsg.writeStorage(tempMsg);
00448 
00449     // send request message
00450     try {
00451         socket->sendExact(outMsg);
00452     } catch (tcpip::SocketException& e) {
00453         msg << "Error while sending command: " << e.what();
00454         errorMsg(msg);
00455         return;
00456     }
00457 
00458     answerLog << std::endl << "-> Command sent: <PositionConversion>:" << std::endl;
00459     if (pos2D != NULL) {
00460         answerLog << " DestPosition-2D: x=" << pos2D->x << " y=" << pos2D->y ;
00461     } else if (pos3D != NULL) {
00462         answerLog << " DestPosition-3D: x=" << pos3D->x << " y=" << pos3D->y << " z=" << pos3D->z;
00463     } else if (posRoad != NULL) {
00464         answerLog << " DestPosition-RoadMap: roadId=" << posRoad->roadId << " pos=" << posRoad->pos << " laneId=" << (int)posRoad->laneId ;
00465     }
00466     answerLog << " posId=" << posId << std::endl;
00467 
00468     // receive answer message
00469     try {
00470         socket->receiveExact(inMsg);
00471     } catch (tcpip::SocketException& e) {
00472         msg << "Error while receiving command: " << e.what();
00473         errorMsg(msg);
00474         return;
00475     }
00476 
00477     // validate result state
00478     if (!reportResultState(inMsg, CMD_POSITIONCONVERSION)) {
00479         return;
00480     }
00481 
00482     // validate answer message
00483     validatePositionConversion(inMsg);
00484 }
00485 
00486 
00487 void
00488 TraCITestClient::commandDistanceRequest(testclient::Position pos1, testclient::Position pos2, int flag) {
00489     commandDistanceRequest(&pos1, NULL, NULL, &pos2, NULL, NULL, flag);
00490 }
00491 
00492 
00493 void
00494 TraCITestClient::commandDistanceRequest(testclient::Position pos1, testclient::Position3D pos2, int flag) {
00495     commandDistanceRequest(&pos1, NULL, NULL, NULL, &pos2, NULL, flag);
00496 }
00497 
00498 
00499 void
00500 TraCITestClient::commandDistanceRequest(testclient::Position3D pos1, testclient::Position3D pos2, int flag) {
00501     commandDistanceRequest(NULL, &pos1, NULL, NULL, &pos2, NULL, flag);
00502 }
00503 
00504 
00505 void
00506 TraCITestClient::commandDistanceRequest(testclient::Position3D pos1, testclient::Position pos2, int flag) {
00507     commandDistanceRequest(NULL, &pos1, NULL, &pos2, NULL, NULL, flag);
00508 }
00509 
00510 
00511 void
00512 TraCITestClient::commandDistanceRequest(testclient::PositionRoadMap pos1, testclient::Position pos2, int flag) {
00513     commandDistanceRequest(NULL, NULL, &pos1, &pos2, NULL, NULL, flag);
00514 }
00515 
00516 
00517 void
00518 TraCITestClient::commandDistanceRequest(testclient::PositionRoadMap pos1, testclient::Position3D pos2, int flag) {
00519     commandDistanceRequest(NULL, NULL, &pos1, NULL, &pos2, NULL, flag);
00520 }
00521 
00522 
00523 void
00524 TraCITestClient::commandDistanceRequest(testclient::PositionRoadMap pos1, testclient::PositionRoadMap pos2, int flag) {
00525     commandDistanceRequest(NULL, NULL, &pos1, NULL, NULL, &pos2, flag);
00526 }
00527 
00528 
00529 void
00530 TraCITestClient::commandDistanceRequest(testclient::Position pos1, testclient::PositionRoadMap pos2, int flag) {
00531     commandDistanceRequest(&pos1, NULL, NULL, NULL, NULL, &pos2, flag);
00532 }
00533 
00534 
00535 void
00536 TraCITestClient::commandDistanceRequest(testclient::Position3D pos1, testclient::PositionRoadMap pos2, int flag) {
00537     commandDistanceRequest(NULL, &pos1, NULL, NULL, NULL, &pos2, flag);
00538 }
00539 
00540 
00541 
00542 void
00543 TraCITestClient::commandDistanceRequest(testclient::Position* pos1_2D,
00544                                         testclient::Position3D* pos1_3D,
00545                                         testclient::PositionRoadMap* pos1_Road,
00546                                         testclient::Position* pos2_2D,
00547                                         testclient::Position3D* pos2_3D,
00548                                         testclient::PositionRoadMap* pos2_Road,
00549                                         int flag) {
00550     tcpip::Storage outMsg;
00551     tcpip::Storage inMsg;
00552     tcpip::Storage tempMsg;
00553     std::stringstream msg;
00554 
00555     if (socket == NULL) {
00556         msg << "#Error while sending command: no connection to server" ;
00557         errorMsg(msg);
00558         return;
00559     }
00560 
00561     // command id
00562     tempMsg.writeUnsignedByte(CMD_DISTANCEREQUEST);
00563     // position1
00564     if (pos1_2D != NULL) {
00565         tempMsg.writeUnsignedByte(POSITION_2D);
00566         tempMsg.writeDouble(pos1_2D->x);
00567         tempMsg.writeDouble(pos1_2D->y);
00568     } else if (pos1_3D != NULL) {
00569         tempMsg.writeUnsignedByte(POSITION_3D);
00570         tempMsg.writeDouble(pos1_3D->x);
00571         tempMsg.writeDouble(pos1_3D->y);
00572         tempMsg.writeDouble(pos1_3D->z);
00573     } else if (pos1_Road != NULL) {
00574         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
00575         tempMsg.writeString(pos1_Road->roadId);
00576         tempMsg.writeDouble(pos1_Road->pos);
00577         tempMsg.writeUnsignedByte(pos1_Road->laneId);
00578     } else {
00579         std::cerr << "Error in method commandDistanceRequest: position1 is NULL" << std::endl;
00580         return;
00581     }
00582     // position2
00583     if (pos2_2D != NULL) {
00584         tempMsg.writeUnsignedByte(POSITION_2D);
00585         tempMsg.writeDouble(pos2_2D->x);
00586         tempMsg.writeDouble(pos2_2D->y);
00587     } else if (pos2_3D != NULL) {
00588         tempMsg.writeUnsignedByte(POSITION_3D);
00589         tempMsg.writeDouble(pos2_3D->x);
00590         tempMsg.writeDouble(pos2_3D->y);
00591         tempMsg.writeDouble(pos2_3D->z);
00592     } else if (pos2_Road != NULL) {
00593         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
00594         tempMsg.writeString(pos2_Road->roadId);
00595         tempMsg.writeDouble(pos2_Road->pos);
00596         tempMsg.writeUnsignedByte(pos2_Road->laneId);
00597     } else {
00598         std::cerr << "Error in method commandDistanceRequest: position2 is NULL" << std::endl;
00599         return;
00600     }
00601     // flag
00602     tempMsg.writeUnsignedByte(flag);
00603     // command length
00604     outMsg.writeUnsignedByte(1 + (int) tempMsg.size());
00605     outMsg.writeStorage(tempMsg);
00606 
00607     // send request message
00608     try {
00609         socket->sendExact(outMsg);
00610     } catch (tcpip::SocketException& e) {
00611         msg << "Error while sending command: " << e.what();
00612         errorMsg(msg);
00613         return;
00614     }
00615 
00616     answerLog << std::endl << "-> Command sent: <DistanceRequest>:" << std::endl;
00617     if (pos1_2D != NULL) {
00618         answerLog << " FirstPosition-2D: x=" << pos1_2D->x << " y=" << pos1_2D->y ;
00619     } else if (pos1_3D != NULL) {
00620         answerLog << " FirstPosition-3D: x=" << pos1_3D->x << " y=" << pos1_3D->y << " z=" << pos1_3D->z;
00621     } else if (pos1_Road != NULL) {
00622         answerLog << " FirstPosition-RoadMap: roadId=" << pos1_Road->roadId << " pos=" << pos1_Road->pos << " laneId=" << (int)pos1_Road->laneId ;
00623     }
00624     if (pos2_2D != NULL) {
00625         answerLog << " SecondPosition-2D: x=" << pos2_2D->x << " y=" << pos2_2D->y ;
00626     } else if (pos2_3D != NULL) {
00627         answerLog << " SecondPosition-3D: x=" << pos2_3D->x << " y=" << pos2_3D->y << " z=" << pos2_3D->z;
00628     } else if (pos2_Road != NULL) {
00629         answerLog << " SecondPosition-RoadMap: roadId=" << pos2_Road->roadId << " pos=" << pos2_Road->pos << " laneId=" << (int)pos2_Road->laneId ;
00630     }
00631     answerLog << " Flag=" << flag << std::endl;
00632 
00633     // receive answer message
00634     try {
00635         socket->receiveExact(inMsg);
00636     } catch (tcpip::SocketException& e) {
00637         msg << "Error while receiving command: " << e.what();
00638         errorMsg(msg);
00639         return;
00640     }
00641 
00642     // validate result state
00643     if (!reportResultState(inMsg, CMD_DISTANCEREQUEST)) {
00644         return;
00645     }
00646 
00647     // validate answer message
00648     validateDistanceRequest(inMsg);
00649 }
00650 
00651 
00652 void
00653 TraCITestClient::commandGetVariable(int domID, int varID, const std::string& objID) {
00654     tcpip::Storage outMsg, inMsg;
00655     std::stringstream msg;
00656     if (socket == NULL) {
00657         msg << "#Error while sending command: no connection to server" ;
00658         errorMsg(msg);
00659         return;
00660     }
00661     // command length
00662     outMsg.writeUnsignedByte(1 + 1 + 1 + 4 + (int) objID.length());
00663     // command id
00664     outMsg.writeUnsignedByte(domID);
00665     // variable id
00666     outMsg.writeUnsignedByte(varID);
00667     // object id
00668     outMsg.writeString(objID);
00669 
00670     // send request message
00671     try {
00672         socket->sendExact(outMsg);
00673     } catch (tcpip::SocketException& e) {
00674         msg << "Error while sending command: " << e.what();
00675         errorMsg(msg);
00676         return;
00677     }
00678     answerLog << std::endl << "-> Command sent: <GetVariable>:" << std::endl
00679               << "  domID=" << domID << " varID=" << varID
00680               << " objID=" << objID << std::endl;
00681 
00682     // receive answer message
00683     try {
00684         socket->receiveExact(inMsg);
00685         if (!reportResultState(inMsg, domID)) {
00686             return;
00687         }
00688     } catch (tcpip::SocketException& e) {
00689         msg << "Error while receiving command: " << e.what();
00690         errorMsg(msg);
00691         return;
00692     }
00693     // validate result state
00694     try {
00695         int respStart = inMsg.position();
00696         int length = inMsg.readUnsignedByte();
00697         if (length==0) {
00698             length = inMsg.readInt();
00699         }
00700         int cmdId = inMsg.readUnsignedByte();
00701         if (cmdId != (domID+0x10)) {
00702             answerLog << "#Error: received response with command id: " << cmdId
00703                       << "but expected: " << (int)(domID+0x10) << std::endl;
00704             return;
00705         }
00706         answerLog << "  CommandID=" << cmdId;
00707         answerLog << "  VariableID=" << inMsg.readUnsignedByte();
00708         answerLog << "  ObjectID=" << inMsg.readString();
00709         int valueDataType = inMsg.readUnsignedByte();
00710         answerLog << " valueDataType=" << valueDataType;
00711         readAndReportTypeDependent(inMsg, valueDataType);
00712     } catch (tcpip::SocketException& e) {
00713         msg << "Error while receiving command: " << e.what();
00714         errorMsg(msg);
00715         return;
00716     }
00717 }
00718 
00719 
00720 void
00721 TraCITestClient::commandGetVariablePlus(int domID, int varID, const std::string& objID, std::ifstream& defFile) {
00722     std::stringstream msg;
00723     if (socket == NULL) {
00724         msg << "#Error while sending command: no connection to server" ;
00725         errorMsg(msg);
00726         return;
00727     }
00728     tcpip::Storage outMsg, inMsg, tmp;
00729     const int dataLength = setValueTypeDependant(tmp, defFile, msg);
00730     std::string msgS = msg.str();
00731     if (msgS!="") {
00732         errorMsg(msg);
00733     }
00734     // command length (domID, varID, objID, dataType, data)
00735     outMsg.writeUnsignedByte(1 + 1 + 1 + 4 + (int) objID.length() + dataLength);
00736     // command id
00737     outMsg.writeUnsignedByte(domID);
00738     // variable id
00739     outMsg.writeUnsignedByte(varID);
00740     // object id
00741     outMsg.writeString(objID);
00742     // data type
00743     outMsg.writeStorage(tmp);
00744     // send request message
00745     try {
00746         socket->sendExact(outMsg);
00747     } catch (tcpip::SocketException& e) {
00748         msg << "Error while sending command: " << e.what();
00749         errorMsg(msg);
00750         return;
00751     }
00752     answerLog << std::endl << "-> Command sent: <GetVariable>:" << std::endl
00753               << "  domID=" << domID << " varID=" << varID
00754               << " objID=" << objID << std::endl;
00755 
00756     // receive answer message
00757     try {
00758         socket->receiveExact(inMsg);
00759         if (!reportResultState(inMsg, domID)) {
00760             return;
00761         }
00762     } catch (tcpip::SocketException& e) {
00763         msg << "Error while receiving command: " << e.what();
00764         errorMsg(msg);
00765         return;
00766     }
00767     // validate result state
00768     try {
00769         int respStart = inMsg.position();
00770         int length = inMsg.readUnsignedByte();
00771         if (length==0) {
00772             length = inMsg.readInt();
00773         }
00774         int cmdId = inMsg.readUnsignedByte();
00775         if (cmdId != (domID+0x10)) {
00776             answerLog << "#Error: received response with command id: " << cmdId
00777                       << "but expected: " << (int)(domID+0x10) << std::endl;
00778             return;
00779         }
00780         answerLog << "  CommandID=" << cmdId;
00781         answerLog << "  VariableID=" << inMsg.readUnsignedByte();
00782         answerLog << "  ObjectID=" << inMsg.readString();
00783         int valueDataType = inMsg.readUnsignedByte();
00784         answerLog << " valueDataType=" << valueDataType;
00785         readAndReportTypeDependent(inMsg, valueDataType);
00786     } catch (tcpip::SocketException& e) {
00787         msg << "Error while receiving command: " << e.what();
00788         errorMsg(msg);
00789         return;
00790     }
00791 }
00792 
00793 
00794 void
00795 TraCITestClient::commandSubscribeVariable(int domID, const std::string& objID, int beginTime, int endTime, int varNo, std::ifstream& defFile) {
00796     std::stringstream msg;
00797     if (socket == NULL) {
00798         msg << "#Error while sending command: no connection to server" ;
00799         errorMsg(msg);
00800         return;
00801     }
00802     tcpip::Storage outMsg, inMsg, tmp;
00803     std::string msgS = msg.str();
00804     if (msgS!="") {
00805         errorMsg(msg);
00806     }
00807     // command length (domID, beginTime, endTime, objID, varNo, <vars>)
00808     outMsg.writeUnsignedByte(0);
00809     outMsg.writeInt(/*1 + 4 +*/ 5 + 1 + 4 + 4 + 4 + (int) objID.length() + 1 + varNo);
00810     // command id
00811     outMsg.writeUnsignedByte(domID);
00812     // time
00813     outMsg.writeInt(beginTime);
00814     outMsg.writeInt(endTime);
00815     // object id
00816     outMsg.writeString(objID);
00817     // command id
00818     outMsg.writeUnsignedByte(varNo);
00819     for (int i=0; i<varNo; ++i) {
00820         int var;
00821         defFile >> var;
00822         // variable id
00823         outMsg.writeUnsignedByte(var);
00824     }
00825     // send request message
00826     try {
00827         socket->sendExact(outMsg);
00828     } catch (tcpip::SocketException& e) {
00829         msg << "Error while sending command: " << e.what();
00830         errorMsg(msg);
00831         return;
00832     }
00833     answerLog << std::endl << "-> Command sent: <SubscribeVariable>:" << std::endl
00834               << "  domID=" << domID << " objID=" << objID << " with " << varNo << " variables" << std::endl;
00835 
00836     // receive answer message
00837     try {
00838         socket->receiveExact(inMsg);
00839         if (!reportResultState(inMsg, domID)) {
00840             return;
00841         }
00842     } catch (tcpip::SocketException& e) {
00843         msg << "Error while receiving command: " << e.what();
00844         errorMsg(msg);
00845         return;
00846     }
00847     // validate result state
00848     try {
00849         validateSubscription(inMsg);
00850     } catch (tcpip::SocketException& e) {
00851         msg << "Error while receiving command: " << e.what();
00852         errorMsg(msg);
00853         return;
00854     }
00855 }
00856 
00857 
00858 
00859 int
00860 TraCITestClient::setValueTypeDependant(tcpip::Storage& into, std::ifstream& defFile, std::stringstream& msg) {
00861     std::string dataTypeS, valueS;
00862     defFile >> dataTypeS;
00863     if (dataTypeS=="<airDist>") {
00864         into.writeUnsignedByte(REQUEST_AIRDIST);
00865         return 1;
00866     } else if (dataTypeS=="<drivingDist>") {
00867         into.writeUnsignedByte(REQUEST_DRIVINGDIST);
00868         return 1;
00869     }
00870     defFile >> valueS;
00871     if (dataTypeS=="<int>") {
00872         into.writeUnsignedByte(TYPE_INTEGER);
00873         into.writeInt(atoi(valueS.c_str()));
00874         return 4 + 1;
00875     } else if (dataTypeS=="<byte>") {
00876         into.writeUnsignedByte(TYPE_BYTE);
00877         into.writeByte(atoi(valueS.c_str()));
00878         return 1 + 1;
00879     }  else if (dataTypeS=="<ubyte>") {
00880         into.writeUnsignedByte(TYPE_UBYTE);
00881         into.writeByte(atoi(valueS.c_str()));
00882         return 1 + 1;
00883     } else if (dataTypeS=="<float>") {
00884         into.writeUnsignedByte(TYPE_FLOAT);
00885         into.writeFloat(float(atof(valueS.c_str())));
00886         return 4 + 1;
00887     } else if (dataTypeS=="<double>") {
00888         into.writeUnsignedByte(TYPE_DOUBLE);
00889         into.writeDouble(atof(valueS.c_str()));
00890         return 8 + 1;
00891     } else if (dataTypeS=="<string>") {
00892         into.writeUnsignedByte(TYPE_STRING);
00893         into.writeString(valueS);
00894         return 4 + 1 + (int) valueS.length();
00895     } else if (dataTypeS=="<string*>") {
00896         std::vector<std::string> slValue;
00897         int number = atoi(valueS.c_str());
00898         int length = 1 + 4;
00899         for (int i=0; i<number; ++i) {
00900             std::string tmp;
00901             defFile >> tmp;
00902             slValue.push_back(tmp);
00903             length += 4 + int(tmp.length());
00904         }
00905         into.writeUnsignedByte(TYPE_STRINGLIST);
00906         into.writeStringList(slValue);
00907         return length;
00908     } else if (dataTypeS=="<compound>") {
00909         into.writeUnsignedByte(TYPE_COMPOUND);
00910         int number = atoi(valueS.c_str());
00911         into.writeInt(number);
00912         int length = 1 + 4;
00913         for (int i=0; i<number; ++i) {
00914             length += setValueTypeDependant(into, defFile, msg);
00915         }
00916         return length;
00917     } else if (dataTypeS=="<color>") {
00918         into.writeUnsignedByte(TYPE_COLOR);
00919         into.writeUnsignedByte(atoi(valueS.c_str()));
00920         for (int i=0; i<3; ++i) {
00921             defFile >> valueS;
00922             into.writeUnsignedByte(atoi(valueS.c_str()));
00923         }
00924         return 1 + 4;
00925     } else if (dataTypeS=="<position2D>") {
00926         into.writeUnsignedByte(POSITION_2D);
00927         into.writeDouble(atof(valueS.c_str()));
00928         defFile >> valueS;
00929         into.writeDouble(atof(valueS.c_str()));
00930         return 1 + 8 + 8;
00931     } else if (dataTypeS=="<position3D>") {
00932         into.writeUnsignedByte(POSITION_3D);
00933         into.writeDouble(atof(valueS.c_str()));
00934         defFile >> valueS;
00935         into.writeDouble(atof(valueS.c_str()));
00936         defFile >> valueS;
00937         into.writeDouble(atof(valueS.c_str()));
00938         return 1 + 8 + 8 + 8;
00939     } else if (dataTypeS=="<positionRoadmap>") {
00940         into.writeUnsignedByte(POSITION_ROADMAP);
00941         into.writeString(valueS);
00942         int length = 1 + 8 + (int) valueS.length();
00943         defFile >> valueS;
00944         into.writeDouble(atof(valueS.c_str()));
00945         defFile >> valueS;
00946         into.writeUnsignedByte(atoi(valueS.c_str()));
00947         return length + 4 + 1;
00948     } else if (dataTypeS=="<shape>") {
00949         into.writeUnsignedByte(TYPE_POLYGON);
00950         int number = atoi(valueS.c_str());
00951         into.writeUnsignedByte(number);
00952         int length = 1 + 1;
00953         for (int i=0; i<number; ++i) {
00954             std::string x, y;
00955             defFile >> x >> y;
00956             into.writeDouble(atof(x.c_str()));
00957             into.writeDouble(atof(y.c_str()));
00958             length += 8 + 8;
00959         }
00960         return length;
00961     }
00962     msg << "## Unknown data type: " << dataTypeS;
00963     return 0;
00964 }
00965 
00966 void
00967 TraCITestClient::commandSetValue(int domID, int varID, const std::string& objID, std::ifstream& defFile) {
00968     std::stringstream msg;
00969     if (socket == NULL) {
00970         msg << "#Error while sending command: no connection to server" ;
00971         errorMsg(msg);
00972         return;
00973     }
00974     tcpip::Storage outMsg, inMsg, tmp;
00975     int dataLength = setValueTypeDependant(tmp, defFile, msg);
00976     std::string msgS = msg.str();
00977     if (msgS!="") {
00978         errorMsg(msg);
00979     }
00980     // command length (domID, varID, objID, dataType, data)
00981     outMsg.writeUnsignedByte(1 + 1 + 1 + 4 + (int) objID.length() + dataLength);
00982     // command id
00983     outMsg.writeUnsignedByte(domID);
00984     // variable id
00985     outMsg.writeUnsignedByte(varID);
00986     // object id
00987     outMsg.writeString(objID);
00988     // data type
00989     outMsg.writeStorage(tmp);
00990     // send request message
00991     try {
00992         socket->sendExact(outMsg);
00993     } catch (tcpip::SocketException& e) {
00994         msg << "Error while sending command: " << e.what();
00995         errorMsg(msg);
00996         return;
00997     }
00998     answerLog << std::endl << "-> Command sent: <SetValue>:" << std::endl
00999               << "  domID=" << domID << " varID=" << varID
01000               << " objID=" << objID << std::endl;
01001 
01002     // receive answer message
01003     try {
01004         socket->receiveExact(inMsg);
01005         if (!reportResultState(inMsg, domID)) {
01006             return;
01007         }
01008     } catch (tcpip::SocketException& e) {
01009         msg << "Error while receiving command: " << e.what();
01010         errorMsg(msg);
01011         return;
01012     }
01013 }
01014 
01015 
01016 
01017 
01018 void
01019 TraCITestClient::commandClose() {
01020     tcpip::Storage outMsg;
01021     tcpip::Storage inMsg;
01022     std::stringstream msg;
01023 
01024     if (socket == NULL) {
01025         msg << "#Error while sending command: no connection to server" ;
01026         errorMsg(msg);
01027         return;
01028     }
01029 
01030     // command length
01031     outMsg.writeUnsignedByte(1 + 1);
01032     // command id
01033     outMsg.writeUnsignedByte(CMD_CLOSE);
01034 
01035     // send request message
01036     try {
01037         socket->sendExact(outMsg);
01038     } catch (tcpip::SocketException& e) {
01039         msg << "Error while sending command: " << e.what();
01040         errorMsg(msg);
01041         return;
01042     }
01043 
01044     answerLog << std::endl << "-> Command sent: <Close>:" << std::endl;
01045 
01046     // receive answer message
01047     try {
01048         socket->receiveExact(inMsg);
01049     } catch (tcpip::SocketException& e) {
01050         msg << "Error while receiving command: " << e.what();
01051         errorMsg(msg);
01052         return;
01053     }
01054 
01055     // validate result state
01056     if (!reportResultState(inMsg, CMD_CLOSE)) {
01057         return;
01058     }
01059 }
01060 
01061 
01062 bool
01063 TraCITestClient::validateSimulationStep2(tcpip::Storage& inMsg) {
01064     try {
01065         int noSubscriptions = inMsg.readInt();
01066         for (int s=0; s<noSubscriptions; ++s) {
01067             /*
01068             if (!reportResultState(inMsg, CMD_SIMSTEP2, true)) {
01069                 return false;
01070             }
01071             */
01072             if (!validateSubscription(inMsg)) {
01073                 return false;
01074             }
01075         }
01076     } catch (std::invalid_argument& e) {
01077         answerLog << "#Error while reading message:" << e.what() << std::endl;
01078         return false;
01079     }
01080     return true;
01081 }
01082 
01083 
01084 bool
01085 TraCITestClient::validateSubscription(tcpip::Storage& inMsg) {
01086     try {
01087         int respStart = inMsg.position();
01088         int length = inMsg.readUnsignedByte();
01089         if (length==0) {
01090             length = inMsg.readInt();
01091         }
01092         int cmdId = inMsg.readUnsignedByte();
01093         if (cmdId<0xe0||cmdId>0xef) {
01094             answerLog << "#Error: received response with command id: " << cmdId << " but expected a subscription response (0xe0-0xef)" << std::endl;
01095             return false;
01096         }
01097         answerLog << "  CommandID=" << cmdId;
01098         answerLog << "  ObjectID=" << inMsg.readString();
01099         unsigned int varNo = inMsg.readUnsignedByte();
01100         answerLog << "  #variables=" << varNo << std::endl;
01101         for (unsigned int i=0; i<varNo; ++i) {
01102             answerLog << "      VariableID=" << inMsg.readUnsignedByte();
01103             bool ok = inMsg.readUnsignedByte()==RTYPE_OK;
01104             answerLog << "      ok=" << ok;
01105             int valueDataType = inMsg.readUnsignedByte();
01106             answerLog << " valueDataType=" << valueDataType;
01107             readAndReportTypeDependent(inMsg, valueDataType);
01108         }
01109     } catch (std::invalid_argument& e) {
01110         answerLog << "#Error while reading message:" << e.what() << std::endl;
01111         return false;
01112     }
01113     return true;
01114 }
01115 
01116 
01117 bool
01118 TraCITestClient::validatePositionConversion(tcpip::Storage& inMsg) {
01119     int cmdId;
01120     int cmdLength;
01121     int posType;
01122     int reqPosType;
01123     int cmdStart;
01124     testclient::PositionRoadMap roadPos;
01125     testclient::Position pos2D;
01126     testclient::Position3D pos3D;
01127 
01128     try {
01129         cmdStart = inMsg.position();
01130         cmdLength = inMsg.readUnsignedByte();
01131         // read command id
01132         cmdId = inMsg.readUnsignedByte();
01133         if (cmdId != CMD_POSITIONCONVERSION) {
01134             answerLog << "#Error: received response with command id: " << cmdId
01135                       << "but expected: " << (int)CMD_POSITIONCONVERSION << std::endl;
01136             return false;
01137         }
01138         answerLog << ".. Received Response <PositionConversion>:" << std::endl;
01139         // read converted position
01140         posType = inMsg.readUnsignedByte();
01141         switch (posType) {
01142         case POSITION_2D:
01143             pos2D.x = inMsg.readDouble();
01144             pos2D.y = inMsg.readDouble();
01145             answerLog << "2D-Position: x=" << pos2D.x << " y=" << pos2D.y << std::endl;
01146             break;
01147         case POSITION_3D:
01148             answerLog << "3D-Position: ";
01149             pos3D.x = inMsg.readDouble();
01150             pos3D.y = inMsg.readDouble();
01151             pos3D.z = inMsg.readDouble();
01152             answerLog << "x=" << pos3D.x << " y=" << pos3D.y << " z=" << pos3D.z << std::endl;
01153             break;
01154         case POSITION_ROADMAP:
01155             roadPos.roadId = inMsg.readString();
01156             roadPos.pos = inMsg.readDouble();
01157             roadPos.laneId = inMsg.readUnsignedByte();
01158             answerLog << "RoadMap-Position: roadId=" << roadPos.roadId << " pos=" << roadPos.pos
01159                       << " laneId=" << (int)roadPos.laneId << std::endl;
01160             break;
01161         default:
01162             answerLog << "#Error: received unknown position format" << std::endl;
01163             return false;
01164         }
01165         // read requested position type
01166         reqPosType = inMsg.readUnsignedByte();
01167         if (reqPosType != posType) {
01168             answerLog << "#Warning: requested position type (" << reqPosType
01169                       << ") and received position type (" << posType << ") do not match" << std::endl;
01170         }
01171         // check command length
01172         if ((cmdStart + cmdLength) != inMsg.position()) {
01173             answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
01174             return false;
01175         }
01176     } catch (std::invalid_argument& e) {
01177         answerLog << "#Error while reading message:" << e.what() << std::endl;
01178         return false;
01179     }
01180 
01181     return true;
01182 }
01183 
01184 
01185 bool
01186 TraCITestClient::validateDistanceRequest(tcpip::Storage& inMsg) {
01187     int cmdId;
01188     int cmdLength;
01189     int flag;
01190     int cmdStart;
01191     SUMOReal distance;
01192     /*testclient::PositionRoadMap roadPos;
01193     testclient::Position pos2D;
01194     testclient::Position3D pos3D;*/
01195 
01196     try {
01197         cmdStart = inMsg.position();
01198         cmdLength = inMsg.readUnsignedByte();
01199         // read command id
01200         cmdId = inMsg.readUnsignedByte();
01201         if (cmdId != CMD_DISTANCEREQUEST) {
01202             answerLog << "#Error: received response with command id: " << cmdId
01203                       << "but expected: " << (int)CMD_DISTANCEREQUEST << std::endl;
01204             return false;
01205         }
01206         answerLog << ".. Received Response <DistanceRequest>:" << std::endl;
01207         // read flag
01208         flag = inMsg.readUnsignedByte();
01209         answerLog << " flag=" << flag;
01210         // read computed distance
01211         distance = inMsg.readDouble();
01212         answerLog << " distance=" << distance << std::endl;
01214         //posType = inMsg.readUnsignedByte();
01215         //switch (posType) {
01216         //case POSITION_2D:
01217         //  pos2D.x = inMsg.readDouble();
01218         //  pos2D.y = inMsg.readDouble();
01219         //  answerLog << "2D-Position: x=" << pos2D.x << " y=" << pos2D.y;
01220         //  break;
01221         //case POSITION_3D:
01222         //  answerLog << "3D-Position: ";
01223         //  pos3D.x = inMsg.readDouble();
01224         //  pos3D.y = inMsg.readDouble();
01225         //  pos3D.z = inMsg.readDouble();
01226         //  answerLog << "x=" << pos3D.x << " y=" << pos3D.y << " z=" << pos3D.z;
01227         //  break;
01228         //case POSITION_ROADMAP:
01229         //  roadPos.roadId = inMsg.readString();
01230         //  roadPos.pos = inMsg.readDouble();
01231         //  roadPos.laneId = inMsg.readUnsignedByte();
01232         //  answerLog << "RoadMap-Position: roadId=" << roadPos.roadId << " pos=" << roadPos.pos
01233         //      << " laneId=" << (int)roadPos.laneId;
01234         //  break;
01235         //default:
01236         //  answerLog << "#Error: received unknown position format: " << posType << std::endl;
01237         //  return false;
01238         //}
01239         // check command length
01240         if ((cmdStart + cmdLength) != inMsg.position()) {
01241             answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
01242             return false;
01243         }
01244     } catch (std::invalid_argument& e) {
01245         answerLog << "#Error while reading message:" << e.what() << std::endl;
01246         return false;
01247     }
01248 
01249     return true;
01250 }
01251 
01252 
01253 bool
01254 TraCITestClient::readAndReportTypeDependent(tcpip::Storage& inMsg, int valueDataType) {
01255     if (valueDataType == TYPE_UBYTE) {
01256         int ubyte = inMsg.readUnsignedByte();
01257         answerLog << " Unsigned Byte Value: " << ubyte << std::endl;
01258     } else if (valueDataType == TYPE_BYTE) {
01259         int byte = inMsg.readByte();
01260         answerLog << " Byte value: " << byte << std::endl;
01261     } else if (valueDataType == TYPE_INTEGER) {
01262         int integer = inMsg.readInt();
01263         answerLog << " Int value: " << integer << std::endl;
01264     } else if (valueDataType == TYPE_FLOAT) {
01265         float floatv = inMsg.readFloat();
01266         if (floatv<0.1&&floatv>0) {
01267             answerLog.setf(std::ios::scientific, std::ios::floatfield);
01268         }
01269         answerLog << " float value: " << floatv << std::endl;
01270         answerLog.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
01271         answerLog.setf(std::ios::showpoint); // print decimal point
01272         answerLog << std::setprecision(2);
01273     } else if (valueDataType == TYPE_DOUBLE) {
01274         double doublev = inMsg.readDouble();
01275         answerLog << " Double value: " << doublev << std::endl;
01276     } else if (valueDataType == TYPE_BOUNDINGBOX) {
01277         testclient::BoundingBox box;
01278         box.lowerLeft.x = inMsg.readDouble();
01279         box.lowerLeft.y = inMsg.readDouble();
01280         box.upperRight.x = inMsg.readDouble();
01281         box.upperRight.y = inMsg.readDouble();
01282         answerLog << " BoundaryBoxValue: lowerLeft x="<< box.lowerLeft.x
01283                   << " y=" << box.lowerLeft.y << " upperRight x=" << box.upperRight.x
01284                   << " y=" << box.upperRight.y << std::endl;
01285     } else if (valueDataType == TYPE_POLYGON) {
01286         int length = inMsg.readUnsignedByte();
01287         answerLog << " PolygonValue: ";
01288         for (int i=0; i < length; i++) {
01289             SUMOReal x = inMsg.readDouble();
01290             SUMOReal y = inMsg.readDouble();
01291             answerLog << "(" << x << "," << y << ") ";
01292         }
01293         answerLog << std::endl;
01294     } else if (valueDataType == POSITION_3D) {
01295         SUMOReal x = inMsg.readDouble();
01296         SUMOReal y = inMsg.readDouble();
01297         SUMOReal z = inMsg.readDouble();
01298         answerLog << " Position3DValue: " << std::endl;
01299         answerLog << " x: " << x << " y: " << y
01300                   << " z: " << z << std::endl;
01301     } else if (valueDataType == POSITION_ROADMAP) {
01302         std::string roadId = inMsg.readString();
01303         SUMOReal pos = inMsg.readDouble();
01304         int laneId = inMsg.readUnsignedByte();
01305         answerLog << " RoadMapPositionValue: roadId=" << roadId
01306                   << " pos=" << pos
01307                   << " laneId=" << laneId << std::endl;
01308     } else if (valueDataType == TYPE_TLPHASELIST) {
01309         int length = inMsg.readUnsignedByte();
01310         answerLog << " TLPhaseListValue: length=" << length << std::endl;
01311         for (int i=0; i< length; i++) {
01312             std::string pred = inMsg.readString();
01313             std::string succ = inMsg.readString();
01314             int phase = inMsg.readUnsignedByte();
01315             answerLog << " precRoad=" << pred << " succRoad=" << succ
01316                       << " phase=";
01317             switch (phase) {
01318             case TLPHASE_RED:
01319                 answerLog << "red" << std::endl;
01320                 break;
01321             case TLPHASE_YELLOW:
01322                 answerLog << "yellow" << std::endl;
01323                 break;
01324             case TLPHASE_GREEN:
01325                 answerLog << "green" << std::endl;
01326                 break;
01327             default:
01328                 answerLog << "#Error: unknown phase value" << (int)phase << std::endl;
01329                 return false;
01330             }
01331         }
01332     } else if (valueDataType == TYPE_STRING) {
01333         std::string s = inMsg.readString();
01334         answerLog << " string value: " << s << std::endl;
01335     } else if (valueDataType == TYPE_STRINGLIST) {
01336         std::vector<std::string> s = inMsg.readStringList();
01337         answerLog << " string list value: [ " << std::endl;
01338         for (std::vector<std::string>::iterator i=s.begin(); i!=s.end(); ++i) {
01339             if (i!=s.begin()) {
01340                 answerLog << ", ";
01341             }
01342             answerLog << '"' << *i << '"';
01343         }
01344         answerLog << " ]" << std::endl;
01345     } else if (valueDataType == TYPE_COMPOUND) {
01346         int no = inMsg.readInt();
01347         answerLog << " compound value with " << no << " members: [ " << std::endl;
01348         for (int i=0; i<no; ++i) {
01349             int currentValueDataType = inMsg.readUnsignedByte();
01350             answerLog << " valueDataType=" << currentValueDataType;
01351             readAndReportTypeDependent(inMsg, currentValueDataType);
01352         }
01353         answerLog << " ]" << std::endl;
01354     } else if (valueDataType == POSITION_2D) {
01355         SUMOReal xv = inMsg.readDouble();
01356         SUMOReal yv = inMsg.readDouble();
01357         answerLog << " position value: (" << xv << "," << yv << ")" << std::endl;
01358     } else if (valueDataType == TYPE_COLOR) {
01359         int r = inMsg.readUnsignedByte();
01360         int g = inMsg.readUnsignedByte();
01361         int b = inMsg.readUnsignedByte();
01362         int a = inMsg.readUnsignedByte();
01363         answerLog << " color value: (" << r << "," << g << "," << b << "," << a << ")" << std::endl;
01364     } else {
01365         answerLog << "#Error: unknown valueDataType!" << std::endl;
01366         return false;
01367     }
01368     return true;
01369 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines