TraCITestClient.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 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 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #include "TraCITestClient.h"
00025 
00026 #include <vector>
00027 #include <iostream>
00028 #include <iomanip>
00029 #include <fstream>
00030 #include <sstream>
00031 #include <ctime>
00032 #include <cstdlib>
00033 
00034 #include <traci-server/TraCIConstants.h>
00035 #include <utils/common/SUMOTime.h>
00036 
00037 #define BUILD_TCPIP
00038 #include <foreign/tcpip/storage.h>
00039 #include <foreign/tcpip/socket.h>
00040 
00041 
00042 // ===========================================================================
00043 // used namespaces
00044 // ===========================================================================
00045 using namespace tcpip;
00046 using namespace testclient;
00047 
00048 
00049 // ===========================================================================
00050 // method definitions
00051 // ===========================================================================
00052 
00053 TraCITestClient::TraCITestClient(std::string outputFileName)
00054         :socket(NULL),
00055         outputFileName(outputFileName),
00056         answerLog("") {
00057     answerLog.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
00058     answerLog.setf(std::ios::showpoint); // print decimal point
00059     answerLog << std::setprecision(2);
00060 }
00061 
00062 
00063 TraCITestClient::~TraCITestClient() {
00064     writeResult();
00065 }
00066 
00067 
00068 void
00069 TraCITestClient::writeResult() {
00070     time_t seconds;
00071     tm* locTime;
00072 
00073     std::ofstream outFile(outputFileName.c_str());
00074     if (!outFile) {
00075         std::cerr << "Unable to write result file" << std::endl;
00076     }
00077     time(&seconds);
00078     locTime = localtime(&seconds);
00079     outFile << "TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
00080     outFile << answerLog.str();
00081     outFile.close();
00082 }
00083 
00084 
00085 void
00086 TraCITestClient::errorMsg(std::stringstream& msg) {
00087     std::cerr << msg.str() << std::endl;
00088     answerLog << "----" << std::endl << msg.str() << std::endl;
00089 }
00090 
00091 
00092 bool
00093 TraCITestClient::connect(int port, std::string host) {
00094     std::stringstream msg;
00095     socket = new tcpip::Socket(host, port);
00096 
00097     //socket->set_blocking(true);
00098 
00099     try {
00100         socket->connect();
00101     } catch (SocketException &e) {
00102         msg << "#Error while connecting: " << e.what();
00103         errorMsg(msg);
00104         return false;
00105     }
00106 
00107     return true;
00108 }
00109 
00110 
00111 bool
00112 TraCITestClient::close() {
00113     if (socket != NULL) {
00114         socket->close();
00115     }
00116     return true;
00117 }
00118 
00119 
00120 bool
00121 TraCITestClient::run(std::string fileName, int port, std::string host) {
00122     std::ifstream defFile;
00123     std::string fileContentStr;
00124     std::stringstream fileContent;
00125     std::string lineCommand;
00126     std::stringstream msg;
00127     int repNo = 1;
00128     bool commentRead = false;
00129 
00130     if (!connect(port, host)) {
00131         return false;
00132     }
00133 
00134     // read definition file and trigger commands according to it
00135     defFile.open(fileName.c_str());
00136 
00137     if (!defFile) {
00138         msg << "Can not open definition file " << fileName << std::endl;
00139         errorMsg(msg);
00140         return false;
00141     }
00142 
00143     while (defFile >> lineCommand) {
00144         repNo = 1;
00145         if (lineCommand.compare("%") == 0) {
00146             // a comment was read
00147             commentRead = 1 - commentRead;
00148             continue;
00149         }
00150         if (commentRead) {
00151             // wait until end of comment is reached
00152             continue;
00153         }
00154         if (lineCommand.compare("repeat") == 0) {
00155             defFile >> repNo;
00156             defFile >> lineCommand;
00157         }
00158         if (lineCommand.compare("simstep") == 0) {
00159             // read parameter for command simulation step and trigger command
00160             std::string time;
00161             int posFormat;
00162 
00163             defFile >> time;
00164             defFile >> posFormat;
00165             for (int i=0; i < repNo; i++) {
00166                 commandSimulationStep(string2time(time), posFormat);
00167             }
00168         } else if (lineCommand.compare("simstep2") == 0) {
00169             // read parameter for command simulation step and trigger command
00170             std::string time;
00171             defFile >> time;
00172             for (int i=0; i < repNo; i++) {
00173                 commandSimulationStep2(string2time(time));
00174             }
00175         } else if (lineCommand.compare("simstep_repeat") == 0) {
00176             // read parameter for command simulation step and trigger command
00177             // repeat the command with increasing target times
00178             std::string timeStr, time_lastStr, incrementStr;
00179             int posFormat;
00180 
00181             defFile >> timeStr;
00182             SUMOTime time(string2time(timeStr));
00183             defFile >> time_lastStr;
00184             SUMOTime time_last(string2time(time_lastStr));
00185             defFile >> incrementStr;
00186             SUMOTime increment(string2time(incrementStr));
00187             defFile >> posFormat;
00188             for (int i=0; i < repNo; i++) {
00189                 while (time <= time_last) {
00190                     commandSimulationStep(time, posFormat);
00191                     time += increment;
00192                 }
00193             }
00194         } else if (lineCommand.compare("setmaxspeed") == 0) {
00195             // trigger command SetMaximumSpeed
00196             int nodeId;
00197             float maxSpeed;
00198 
00199             defFile >> nodeId;
00200             defFile >> maxSpeed;
00201             for (int i=0; i < repNo; i++) {
00202                 commandSetMaximumSpeed(nodeId, maxSpeed);
00203             }
00204         } else if (lineCommand.compare("stopnode2d") == 0) {
00205             // trigger command StopNode with 2d position
00206             int nodeId;
00207             testclient::Position2D pos;
00208             float radius;
00209             std::string waitTime;
00210 
00211             defFile >> nodeId;
00212             defFile >> pos.x;
00213             defFile >> pos.y;
00214             defFile >> radius;
00215             defFile >> waitTime;
00216             commandStopNode(nodeId, pos, radius, string2time(waitTime));
00217         } else if (lineCommand.compare("stopnode3d") == 0) {
00218             // trigger command StopNode with 3d position
00219             int nodeId;
00220             testclient::Position3D pos;
00221             float radius;
00222             std::string waitTime;
00223 
00224             defFile >> nodeId;
00225             defFile >> pos.x;
00226             defFile >> pos.y;
00227             defFile >> pos.z;
00228             defFile >> radius;
00229             defFile >> waitTime;
00230             commandStopNode(nodeId, pos, radius, string2time(waitTime));
00231         } else if (lineCommand.compare("stopnode_roadpos") == 0) {
00232             // trigger command StopNode with road map position
00233             int nodeId;
00234             testclient::PositionRoadMap pos;
00235             float radius;
00236             std::string waitTime;
00237 
00238             defFile >> nodeId;
00239             defFile >> pos.roadId;
00240             defFile >> pos.pos;
00241             defFile >> pos.laneId;
00242             defFile >> radius;
00243             defFile >> waitTime;
00244             commandStopNode(nodeId, pos, radius, string2time(waitTime));
00245         } else if (lineCommand.compare("slowdown") == 0) {
00246             // trigger command slowDown
00247             int nodeId;
00248             float speed;
00249             std::string timeInterval;
00250 
00251             defFile >> nodeId;
00252             defFile >> speed;
00253             defFile >> timeInterval;
00254             commandSlowDown(nodeId, speed, string2time(timeInterval));
00255         } else if (lineCommand.compare("changelane") == 0) {
00256             // trigger command ChangeLane
00257             int nodeId;
00258             int laneId;
00259             std::string fixTime;
00260 
00261             defFile >> nodeId;
00262             defFile >> laneId;
00263             defFile >> fixTime;
00264             commandChangeLane(nodeId, laneId, string2time(fixTime));
00265         } else if (lineCommand.compare("changetarget") == 0) {
00266             // trigger command ChangeTarget
00267             int nodeId;
00268             std::string roadId;
00269 
00270             defFile >> nodeId;
00271             defFile >> roadId;
00272             commandChangeTarget(nodeId, roadId);
00273         } else if (lineCommand.compare("changeroute") == 0) {
00274             // trigger command ChangeRoute
00275             int nodeId;
00276             std::string roadId;
00277             double travelTime;
00278 
00279             defFile >> nodeId;
00280             defFile >> roadId;
00281             defFile >> travelTime;
00282             commandChangeRoute(nodeId, roadId, travelTime);
00283         } else if (lineCommand.compare("posconversion2d") == 0) {
00284             // trigger command PositionConversion for a 2d position
00285             testclient::Position2D pos;
00286             int destFormat;
00287 
00288             defFile >> pos.x;
00289             defFile >> pos.y;
00290             defFile >> destFormat;
00291             commandPositionConversion(pos, destFormat);
00292         } else if (lineCommand.compare("posconversion3d") == 0) {
00293             // trigger command PositionConversion for a 3d position
00294             testclient::Position3D pos;
00295             int destFormat;
00296 
00297             defFile >> pos.x;
00298             defFile >> pos.y;
00299             defFile >> pos.z;
00300             defFile >> destFormat;
00301             commandPositionConversion(pos, destFormat);
00302         } else if (lineCommand.compare("posconversion_roadpos") == 0) {
00303             // trigger command PositionConversion for a road map position
00304             testclient::PositionRoadMap pos;
00305             int destFormat;
00306 
00307             defFile >> pos.roadId;
00308             defFile >> pos.pos;
00309             defFile >> pos.laneId;
00310             defFile >> destFormat;
00311             commandPositionConversion(pos, destFormat);
00312         } else if (lineCommand.compare("distancerequest_3d_3d") == 0) {
00313             // trigger command DistanceRequest for 2 3D positions
00314             testclient::Position3D pos1;
00315             testclient::Position3D pos2;
00316             int flag;
00317 
00318             defFile >> pos1.x;
00319             defFile >> pos1.y;
00320             defFile >> pos1.z;
00321             defFile >> pos2.x;
00322             defFile >> pos2.y;
00323             defFile >> pos2.z;
00324             defFile >> flag;
00325             commandDistanceRequest(pos1, pos2, flag);
00326         } else if (lineCommand.compare("distancerequest_3d_roadpos") == 0) {
00327             // trigger command DistanceRequest for 3D and road map position
00328             testclient::Position3D pos1;
00329             testclient::PositionRoadMap pos2;
00330             int flag;
00331 
00332             defFile >> pos1.x;
00333             defFile >> pos1.y;
00334             defFile >> pos1.z;
00335             defFile >> pos2.roadId;
00336             defFile >> pos2.pos;
00337             defFile >> pos2.laneId;
00338             defFile >> flag;
00339             commandDistanceRequest(pos1, pos2, flag);
00340         } else if (lineCommand.compare("distancerequest_roadpos_3d") == 0) {
00341             // trigger command DistanceRequest for road map and 3D position
00342             testclient::PositionRoadMap pos1;
00343             testclient::Position3D pos2;
00344             int flag;
00345 
00346             defFile >> pos1.roadId;
00347             defFile >> pos1.pos;
00348             defFile >> pos1.laneId;
00349             defFile >> pos2.x;
00350             defFile >> pos2.y;
00351             defFile >> pos2.z;
00352             defFile >> flag;
00353             commandDistanceRequest(pos1, pos2, flag);
00354         } else if (lineCommand.compare("distancerequest_roadpos_roadpos") == 0) {
00355             // trigger command DistanceRequest for 2 road map positions
00356             testclient::PositionRoadMap pos1;
00357             testclient::PositionRoadMap pos2;
00358             int flag;
00359 
00360             defFile >> pos1.roadId;
00361             defFile >> pos1.pos;
00362             defFile >> pos1.laneId;
00363             defFile >> pos2.roadId;
00364             defFile >> pos2.pos;
00365             defFile >> pos2.laneId;
00366             defFile >> flag;
00367             commandDistanceRequest(pos1, pos2, flag);
00368         } else if (lineCommand.compare("scenario_novalue") == 0) {
00369             // trigger command Scenario, not using field "value"
00370             int flag;
00371             int domain;
00372             int domainId;
00373             int variable;
00374             int valueDataType;
00375 
00376             defFile >> flag;
00377             defFile >> domain;
00378             defFile >> domainId;
00379             defFile >> variable;
00380             defFile >> valueDataType;
00381             commandScenario(flag, domain, domainId, variable, valueDataType);
00382         } else if (lineCommand.compare("scenario_string") == 0) {
00383             // trigger command Scenario, giving a std::string value
00384             int flag;
00385             int domain;
00386             int domainId;
00387             int variable;
00388             std::string stringVal;
00389 
00390             defFile >> flag;
00391             defFile >> domain;
00392             defFile >> domainId;
00393             defFile >> variable;
00394             defFile >> stringVal;
00395             commandScenario(flag, domain, domainId, variable, stringVal);
00396         } else if (lineCommand.compare("scenario_pos3d") == 0) {
00397             // trigger command Scenario, giving a 3d position value
00398             int flag;
00399             int domain;
00400             int domainId;
00401             int variable;
00402             testclient::Position3D pos3dVal;
00403 
00404             defFile >> flag;
00405             defFile >> domain;
00406             defFile >> domainId;
00407             defFile >> variable;
00408             defFile >> pos3dVal.x;
00409             defFile >> pos3dVal.y;
00410             defFile >> pos3dVal.z;
00411             commandScenario(flag, domain, domainId, variable, pos3dVal);
00412         } else if (lineCommand.compare("scenario_roadpos") == 0) {
00413             // trigger command Scenario, giving a road map position value
00414             int flag;
00415             int domain;
00416             int domainId;
00417             int variable;
00418             testclient::PositionRoadMap roadPosVal;
00419 
00420             defFile >> flag;
00421             defFile >> domain;
00422             defFile >> domainId;
00423             defFile >> variable;
00424             defFile >> roadPosVal.roadId;
00425             defFile >> roadPosVal.pos;
00426             defFile >> roadPosVal.laneId;
00427             commandScenario(flag, domain, domainId, variable, roadPosVal);
00428         } else if (lineCommand.compare("gettlstatus") == 0) {
00429             // trigger command GetTrafficLightStatus
00430             int tlId;
00431             std::string intervalStart, intervalEnd;
00432 
00433             defFile >> tlId;
00434             defFile >> intervalStart;
00435             defFile >> intervalEnd;
00436             commandGetTLStatus(tlId, string2time(intervalStart), string2time(intervalEnd));
00437         } else if (lineCommand.compare("getvariable") == 0) {
00438             // trigger command GetXXXVariable
00439             int domID, varID;
00440             std::string objID;
00441             defFile >> domID >> varID >> objID;
00442             commandGetVariable(domID, varID, objID);
00443         } else if (lineCommand.compare("getvariable_plus") == 0) {
00444             // trigger command GetXXXVariable
00445             int domID, varID;
00446             std::string objID;
00447             defFile >> domID >> varID >> objID;
00448             commandGetVariablePlus(domID, varID, objID, defFile);
00449         } else if (lineCommand.compare("subscribevariable") == 0) {
00450             // trigger command SubscribeXXXVariable
00451             int domID, varNo;
00452             std::string beginTime, endTime;
00453             std::string objID;
00454             defFile >> domID >> objID >> beginTime >> endTime >> varNo;
00455             commandSubscribeVariable(domID, objID, string2time(beginTime), string2time(endTime), varNo, defFile);
00456         }  else if (lineCommand.compare("setvalue") == 0) {
00457             // trigger command SetXXXValue
00458             int domID, varID;
00459             std::string objID;
00460             defFile >> domID >> varID >> objID;
00461             commandSetValue(domID, varID, objID, defFile);
00462         } else {
00463             msg << "Error in definition file: " << lineCommand
00464             << " is not a valid command";
00465             errorMsg(msg);
00466             commandClose();
00467             close();
00468             return false;
00469         }
00470     }
00471     defFile.close();
00472     commandClose();
00473     close();
00474     return true;
00475 }
00476 
00477 
00478 bool
00479 TraCITestClient::reportResultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId) {
00480     int cmdLength;
00481     int cmdId;
00482     int resultType;
00483     int cmdStart;
00484     std::string msg;
00485 
00486     try {
00487         cmdStart = inMsg.position();
00488         cmdLength = inMsg.readUnsignedByte();
00489         cmdId = inMsg.readUnsignedByte();
00490         if (cmdId != command && !ignoreCommandId) {
00491             answerLog << "#Error: received status response to command: " << cmdId
00492             << " but expected: " << command << std::endl;
00493             return false;
00494         }
00495         resultType = inMsg.readUnsignedByte();
00496         msg = inMsg.readString();
00497     } catch (std::invalid_argument e) {
00498         answerLog << "#Error: an exception was thrown while reading result state message" << std::endl;
00499         return false;
00500     }
00501     switch (resultType) {
00502     case RTYPE_ERR:
00503         answerLog << ".. Answered with error to command (" << cmdId << "), [description: " << msg << "]" << std::endl;
00504         return false;
00505     case RTYPE_NOTIMPLEMENTED:
00506         answerLog << ".. Sent command is not implemented (" << cmdId << "), [description: " << msg << "]" << std::endl;
00507         return false;
00508     case RTYPE_OK:
00509         answerLog << ".. Command acknowledged (" << cmdId << "), [description: " << msg << "]" << std::endl;
00510         break;
00511     default:
00512         answerLog << ".. Answered with unknown result code(" << resultType << ") to command(" << cmdId
00513         << "), [description: " << msg << "]" << std::endl;
00514         return false;
00515     }
00516     if ((cmdStart + cmdLength) != inMsg.position()) {
00517         answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
00518         return false;
00519     }
00520 
00521     return true;
00522 }
00523 
00524 
00525 void
00526 TraCITestClient::commandSimulationStep(SUMOTime time, int posFormat) {
00527     tcpip::Storage outMsg;
00528     tcpip::Storage inMsg;
00529     std::stringstream msg;
00530 
00531     if (socket == NULL) {
00532         msg << "#Error while sending command: no connection to server";
00533         errorMsg(msg);
00534         return;
00535     }
00536 
00537     // command length
00538     outMsg.writeUnsignedByte(1 + 1 + 8 + 1);
00539     // command id
00540     outMsg.writeUnsignedByte(CMD_SIMSTEP);
00541     // simulation time
00542     outMsg.writeInt(time);
00543     // position result format
00544     outMsg.writeUnsignedByte(posFormat);
00545 
00546     // send request message
00547     try {
00548         socket->sendExact(outMsg);
00549     } catch (SocketException &e) {
00550         msg << "Error while sending command: " << e.what();
00551         errorMsg(msg);
00552         return;
00553     }
00554 
00555     answerLog << std::endl << "-> Command sent: <SimulationStep>:" << std::endl << "  TargetTime=" << time2string(time)
00556     << " PosFormat=" << posFormat << std::endl;
00557 
00558     // receive answer message
00559     try {
00560         socket->receiveExact(inMsg);
00561     } catch (SocketException &e) {
00562         msg << "Error while receiving command: " << e.what();
00563         errorMsg(msg);
00564         return;
00565     }
00566 
00567     // validate result state
00568     if (!reportResultState(inMsg, CMD_SIMSTEP)) {
00569         return;
00570     }
00571 
00572     // validate answer message
00573     validateSimulationStep(inMsg);
00574 }
00575 
00576 
00577 void
00578 TraCITestClient::commandSimulationStep2(SUMOTime time) {
00579     tcpip::Storage outMsg;
00580     tcpip::Storage inMsg;
00581     std::stringstream msg;
00582 
00583     if (socket == NULL) {
00584         msg << "#Error while sending command: no connection to server";
00585         errorMsg(msg);
00586         return;
00587     }
00588 
00589     // command length
00590     outMsg.writeUnsignedByte(1 + 1 + 8);
00591     // command id
00592     outMsg.writeUnsignedByte(CMD_SIMSTEP2);
00593     outMsg.writeInt(time);
00594     // send request message
00595     try {
00596         socket->sendExact(outMsg);
00597     } catch (SocketException &e) {
00598         msg << "Error while sending command: " << e.what();
00599         errorMsg(msg);
00600         return;
00601     }
00602     answerLog << std::endl << "-> Command sent: <SimulationStep2>:" << std::endl;
00603     // receive answer message
00604     try {
00605         socket->receiveExact(inMsg);
00606     } catch (SocketException &e) {
00607         msg << "Error while receiving command: " << e.what();
00608         errorMsg(msg);
00609         return;
00610     }
00611     // validate result state
00612     if (!reportResultState(inMsg, CMD_SIMSTEP2)) {
00613         return;
00614     }
00615     // validate answer message
00616     validateSimulationStep2(inMsg);
00617 }
00618 
00619 
00620 void
00621 TraCITestClient::commandSetMaximumSpeed(int nodeId, float speed) {
00622     tcpip::Storage outMsg;
00623     tcpip::Storage inMsg;
00624     std::stringstream msg;
00625 
00626     if (socket == NULL) {
00627         msg << "#Error while sending command: no connection to server" ;
00628         errorMsg(msg);
00629         return;
00630     }
00631 
00632     // command length
00633     outMsg.writeUnsignedByte(1 + 1 + 4 + 4);
00634     // command id
00635     outMsg.writeUnsignedByte(CMD_SETMAXSPEED);
00636     // node id
00637     outMsg.writeInt(nodeId);
00638     // max speed
00639     outMsg.writeFloat(speed);
00640 
00641     // send request message
00642     try {
00643         socket->sendExact(outMsg);
00644     } catch (SocketException &e) {
00645         msg << "Error while sending command: " << e.what();
00646         errorMsg(msg);
00647         return;
00648     }
00649 
00650     answerLog << std::endl << "-> Command sent: <SetMaximumSpeed>:" << std::endl << "  NodeId=" << nodeId
00651     << " MaxSpeed=" << speed << std::endl;
00652 
00653     // receive answer message
00654     try {
00655         socket->receiveExact(inMsg);
00656     } catch (SocketException &e) {
00657         msg << "Error while receiving command: " << e.what();
00658         errorMsg(msg);
00659         return;
00660     }
00661 
00662     // validate result state
00663     if (!reportResultState(inMsg, CMD_SETMAXSPEED)) {
00664         return;
00665     }
00666 }
00667 
00668 
00669 void
00670 TraCITestClient::commandStopNode(int nodeId, testclient::Position2D pos, float radius, SUMOTime waitTime) {
00671     commandStopNode(nodeId, &pos, NULL, NULL, radius, waitTime);
00672 }
00673 
00674 
00675 void
00676 TraCITestClient::commandStopNode(int nodeId, testclient::Position3D pos, float radius, SUMOTime waitTime) {
00677     commandStopNode(nodeId, NULL, &pos, NULL, radius, waitTime);
00678 }
00679 
00680 
00681 void
00682 TraCITestClient::commandStopNode(int nodeId, testclient::PositionRoadMap pos, float radius, SUMOTime waitTime) {
00683     commandStopNode(nodeId, NULL, NULL, &pos, radius, waitTime);
00684 }
00685 
00686 
00687 void
00688 TraCITestClient::commandStopNode(int nodeId, testclient::Position2D* pos2D,
00689                                  testclient::Position3D* pos3D,
00690                                  testclient::PositionRoadMap* posRoad,
00691                                  float radius, SUMOTime waitTime) {
00692     tcpip::Storage outMsg;
00693     tcpip::Storage inMsg;
00694     tcpip::Storage tempMsg;
00695     std::stringstream msg;
00696 
00697     if (socket == NULL) {
00698         msg << "#Error while sending command: no connection to server" ;
00699         errorMsg(msg);
00700         return;
00701     }
00702 
00703     // command id
00704     tempMsg.writeUnsignedByte(CMD_STOP);
00705     // node id
00706     tempMsg.writeInt(nodeId);
00707     // position
00708     if (pos2D != NULL) {
00709         tempMsg.writeUnsignedByte(POSITION_2D);
00710         tempMsg.writeFloat(pos2D->x);
00711         tempMsg.writeFloat(pos2D->y);
00712     } else if (pos3D != NULL) {
00713         tempMsg.writeUnsignedByte(POSITION_3D);
00714         tempMsg.writeFloat(pos3D->x);
00715         tempMsg.writeFloat(pos3D->y);
00716         tempMsg.writeFloat(pos3D->z);
00717     } else if (posRoad != NULL) {
00718         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
00719         tempMsg.writeString(posRoad->roadId);
00720         tempMsg.writeFloat(posRoad->pos);
00721         tempMsg.writeUnsignedByte(posRoad->laneId);
00722     } else {
00723         std::cerr << "Error in method commandStopNode: position is NULL" << std::endl;
00724         return;
00725     }
00726     // radius
00727     tempMsg.writeFloat(radius);
00728     // waittime
00729     tempMsg.writeInt(waitTime);
00730     // command length
00731     outMsg.writeUnsignedByte(1 + (int) tempMsg.size());
00732     outMsg.writeStorage(tempMsg);
00733 
00734     // send request message
00735     try {
00736         socket->sendExact(outMsg);
00737     } catch (SocketException &e) {
00738         msg << "Error while sending command: " << e.what();
00739         errorMsg(msg);
00740         return;
00741     }
00742 
00743     answerLog << std::endl << "-> Command sent: <StopNode>:" << std::endl << "  NodeId=" << nodeId;
00744     if (pos2D != NULL) {
00745         answerLog << " Position-2D: x=" << pos2D->x << " y=" << pos2D->y ;
00746     } else if (pos3D != NULL) {
00747         answerLog << " Position-3D: x=" << pos3D->x << " y=" << pos3D->y << " z=" << pos3D->z;
00748     } else if (posRoad != NULL) {
00749         answerLog << " Position-RoadMap: roadId=" << posRoad->roadId << " pos=" << posRoad->pos << " laneId=" << (int)posRoad->laneId ;
00750     }
00751     answerLog << " radius=" << radius << " waitTime=" << time2string(waitTime) << std::endl;
00752 
00753     // receive answer message
00754     try {
00755         socket->receiveExact(inMsg);
00756     } catch (SocketException &e) {
00757         msg << "Error while receiving command: " << e.what();
00758         errorMsg(msg);
00759         return;
00760     }
00761 
00762     // validate result state
00763     if (!reportResultState(inMsg, CMD_STOP)) {
00764         return;
00765     }
00766 
00767     // validate answer message
00768     validateStopNode(inMsg);
00769 }
00770 
00771 
00772 void
00773 TraCITestClient::commandChangeLane(int nodeId, int laneId, SUMOTime fixTime) {
00774     tcpip::Storage outMsg;
00775     tcpip::Storage inMsg;
00776     std::stringstream msg;
00777 
00778     if (socket == NULL) {
00779         msg << "#Error while sending command: no connection to server" ;
00780         errorMsg(msg);
00781         return;
00782     }
00783 
00784     // command length
00785     outMsg.writeUnsignedByte(1 + 1 + 4 + 1 + 4);
00786     // command id
00787     outMsg.writeUnsignedByte(CMD_CHANGELANE);
00788     // node id
00789     outMsg.writeInt(nodeId);
00790     // lane id
00791     outMsg.writeUnsignedByte(laneId);
00792     // fix time
00793     outMsg.writeInt(fixTime);
00794 
00795     // send request message
00796     try {
00797         socket->sendExact(outMsg);
00798     } catch (SocketException &e) {
00799         msg << "Error while sending command: " << e.what();
00800         errorMsg(msg);
00801         return;
00802     }
00803 
00804     answerLog << std::endl << "-> Command sent: <ChangeLane>:" << std::endl << "  NodeId=" << nodeId
00805     << " LaneId=" << laneId << " fixTime=" << time2string(fixTime) << std::endl;
00806 
00807     // receive answer message
00808     try {
00809         socket->receiveExact(inMsg);
00810     } catch (SocketException &e) {
00811         msg << "Error while receiving command: " << e.what();
00812         errorMsg(msg);
00813         return;
00814     }
00815 
00816     // validate result state
00817     if (!reportResultState(inMsg, CMD_CHANGELANE)) {
00818         return;
00819     }
00820 }
00821 
00822 
00823 void
00824 TraCITestClient::commandSlowDown(int nodeId, float minSpeed, SUMOTime timeInterval) {
00825     tcpip::Storage outMsg;
00826     tcpip::Storage inMsg;
00827     std::stringstream msg;
00828 
00829     if (socket == NULL) {
00830         msg << "#Error while sending command: no connection to server" ;
00831         errorMsg(msg);
00832         return;
00833     }
00834 
00835     // command length
00836     outMsg.writeUnsignedByte(1 + 1 + 4 + 4 + 4);
00837     // command id
00838     outMsg.writeUnsignedByte(CMD_SLOWDOWN);
00839     // node id
00840     outMsg.writeInt(nodeId);
00841     // min speed
00842     outMsg.writeFloat(minSpeed);
00843     // time interval
00844     outMsg.writeInt(timeInterval);
00845 
00846     // send request message
00847     try {
00848         socket->sendExact(outMsg);
00849     } catch (SocketException &e) {
00850         msg << "Error while sending command: " << e.what();
00851         errorMsg(msg);
00852         return;
00853     }
00854 
00855     answerLog << std::endl << "-> Command sent: <SlowDown>:" << std::endl << "  NodeId=" << nodeId
00856     << " MinSpeed=" << minSpeed << " timeInterval" << time2string(timeInterval) << std::endl;
00857 
00858     // receive answer message
00859     try {
00860         socket->receiveExact(inMsg);
00861     } catch (SocketException &e) {
00862         msg << "Error while receiving command: " << e.what();
00863         errorMsg(msg);
00864         return;
00865     }
00866 
00867     // validate result state
00868     if (!reportResultState(inMsg, CMD_SLOWDOWN)) {
00869         return;
00870     }
00871 }
00872 
00873 
00874 void
00875 TraCITestClient::commandChangeRoute(int nodeId, std::string roadId, double travelTime) {
00876     tcpip::Storage outMsg;
00877     tcpip::Storage inMsg;
00878     std::stringstream msg;
00879 
00880     if (socket == NULL) {
00881         msg << "#Error while sending command: no connection to server" ;
00882         errorMsg(msg);
00883         return;
00884     }
00885 
00886     // command length
00887     outMsg.writeUnsignedByte(1 + 1 + 4 + (4+(int) roadId.length()) + 8);
00888     // command id
00889     outMsg.writeUnsignedByte(CMD_CHANGEROUTE);
00890     // node id
00891     outMsg.writeInt(nodeId);
00892     // road id
00893     outMsg.writeString(roadId);
00894     // travel time
00895     outMsg.writeDouble(travelTime);
00896 
00897     answerLog << std::endl << "-> Command sent: <ChangeRoute>:" << std::endl << "  NodeId=" << nodeId
00898     << " RoadId=" << roadId << " travelTime=" << travelTime << std::endl;
00899 
00900     // send request message
00901     try {
00902         socket->sendExact(outMsg);
00903     } catch (SocketException &e) {
00904         msg << "Error while sending command: " << e.what();
00905         errorMsg(msg);
00906         return;
00907     }
00908 
00909     // receive answer message
00910     try {
00911         socket->receiveExact(inMsg);
00912     } catch (SocketException &e) {
00913         msg << "Error while receiving command: " << e.what();
00914         errorMsg(msg);
00915         return;
00916     }
00917 
00918     // validate result state
00919     if (!reportResultState(inMsg, CMD_CHANGEROUTE)) {
00920         return;
00921     }
00922 }
00923 
00924 
00925 void
00926 TraCITestClient::commandChangeTarget(int nodeId, std::string roadId) {
00927     tcpip::Storage outMsg;
00928     tcpip::Storage inMsg;
00929     std::stringstream msg;
00930 
00931     if (socket == NULL) {
00932         msg << "#Error while sending command: no connection to server" ;
00933         errorMsg(msg);
00934         return;
00935     }
00936 
00937     // command length
00938     outMsg.writeUnsignedByte(1 + 1 + 4 + (4+(int) roadId.length()));
00939     // command id
00940     outMsg.writeUnsignedByte(CMD_CHANGETARGET);
00941     // node id
00942     outMsg.writeInt(nodeId);
00943     // road id
00944     outMsg.writeString(roadId);
00945 
00946     // send request message
00947     try {
00948         socket->sendExact(outMsg);
00949     } catch (SocketException &e) {
00950         msg << "Error while sending command: " << e.what();
00951         errorMsg(msg);
00952         return;
00953     }
00954 
00955     answerLog << std::endl << "-> Command sent: <ChangeTarget>:" << std::endl << "  NodeId=" << nodeId
00956     << " RoadId=" << roadId << std::endl;
00957 
00958     // receive answer message
00959     try {
00960         socket->receiveExact(inMsg);
00961     } catch (SocketException &e) {
00962         msg << "Error while receiving command: " << e.what();
00963         errorMsg(msg);
00964         return;
00965     }
00966 
00967     // validate result state
00968     if (!reportResultState(inMsg, CMD_CHANGETARGET)) {
00969         return;
00970     }
00971 }
00972 
00973 
00974 void
00975 TraCITestClient::commandPositionConversion(testclient::Position2D pos, int posId) {
00976     commandPositionConversion(&pos, NULL, NULL, posId);
00977 }
00978 
00979 
00980 void
00981 TraCITestClient::commandPositionConversion(testclient::Position3D pos, int posId) {
00982     commandPositionConversion(NULL, &pos, NULL, posId);
00983 }
00984 
00985 
00986 void
00987 TraCITestClient::commandPositionConversion(testclient::PositionRoadMap pos, int posId) {
00988     commandPositionConversion(NULL, NULL, &pos, posId);
00989 }
00990 
00991 
00992 void
00993 TraCITestClient::commandPositionConversion(testclient::Position2D* pos2D,
00994         testclient::Position3D* pos3D,
00995         testclient::PositionRoadMap* posRoad,
00996         int posId) {
00997     tcpip::Storage outMsg;
00998     tcpip::Storage inMsg;
00999     tcpip::Storage tempMsg;
01000     std::stringstream msg;
01001 
01002     if (socket == NULL) {
01003         msg << "#Error while sending command: no connection to server" ;
01004         errorMsg(msg);
01005         return;
01006     }
01007 
01008     // command id
01009     tempMsg.writeUnsignedByte(CMD_POSITIONCONVERSION);
01010     // position
01011     if (pos2D != NULL) {
01012         tempMsg.writeUnsignedByte(POSITION_2D);
01013         tempMsg.writeFloat(pos2D->x);
01014         tempMsg.writeFloat(pos2D->y);
01015     } else if (pos3D != NULL) {
01016         tempMsg.writeUnsignedByte(POSITION_3D);
01017         tempMsg.writeFloat(pos3D->x);
01018         tempMsg.writeFloat(pos3D->y);
01019         tempMsg.writeFloat(pos3D->z);
01020     } else if (posRoad != NULL) {
01021         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
01022         tempMsg.writeString(posRoad->roadId);
01023         tempMsg.writeFloat(posRoad->pos);
01024         tempMsg.writeUnsignedByte(posRoad->laneId);
01025     } else {
01026         std::cerr << "Error in method commandPositionConversion: position is NULL" << std::endl;
01027         return;
01028     }
01029     // destination position id
01030     tempMsg.writeUnsignedByte(posId);
01031     // command length
01032     outMsg.writeUnsignedByte(1 + (int) tempMsg.size());
01033     outMsg.writeStorage(tempMsg);
01034 
01035     // send request message
01036     try {
01037         socket->sendExact(outMsg);
01038     } catch (SocketException &e) {
01039         msg << "Error while sending command: " << e.what();
01040         errorMsg(msg);
01041         return;
01042     }
01043 
01044     answerLog << std::endl << "-> Command sent: <PositionConversion>:" << std::endl;
01045     if (pos2D != NULL) {
01046         answerLog << " DestPosition-2D: x=" << pos2D->x << " y=" << pos2D->y ;
01047     } else if (pos3D != NULL) {
01048         answerLog << " DestPosition-3D: x=" << pos3D->x << " y=" << pos3D->y << " z=" << pos3D->z;
01049     } else if (posRoad != NULL) {
01050         answerLog << " DestPosition-RoadMap: roadId=" << posRoad->roadId << " pos=" << posRoad->pos << " laneId=" << (int)posRoad->laneId ;
01051     }
01052     answerLog << " posId=" << posId << std::endl;
01053 
01054     // receive answer message
01055     try {
01056         socket->receiveExact(inMsg);
01057     } catch (SocketException &e) {
01058         msg << "Error while receiving command: " << e.what();
01059         errorMsg(msg);
01060         return;
01061     }
01062 
01063     // validate result state
01064     if (!reportResultState(inMsg, CMD_POSITIONCONVERSION)) {
01065         return;
01066     }
01067 
01068     // validate answer message
01069     validatePositionConversion(inMsg);
01070 }
01071 
01072 
01073 void
01074 TraCITestClient::commandDistanceRequest(testclient::Position2D pos1, testclient::Position2D pos2, int flag) {
01075     commandDistanceRequest(&pos1, NULL, NULL, &pos2, NULL, NULL, flag);
01076 }
01077 
01078 
01079 void
01080 TraCITestClient::commandDistanceRequest(testclient::Position2D pos1, testclient::Position3D pos2, int flag) {
01081     commandDistanceRequest(&pos1, NULL, NULL, NULL, &pos2, NULL, flag);
01082 }
01083 
01084 
01085 void
01086 TraCITestClient::commandDistanceRequest(testclient::Position3D pos1, testclient::Position3D pos2, int flag) {
01087     commandDistanceRequest(NULL, &pos1, NULL, NULL, &pos2, NULL, flag);
01088 }
01089 
01090 
01091 void
01092 TraCITestClient::commandDistanceRequest(testclient::Position3D pos1, testclient::Position2D pos2, int flag) {
01093     commandDistanceRequest(NULL, &pos1, NULL, &pos2, NULL, NULL, flag);
01094 }
01095 
01096 
01097 void
01098 TraCITestClient::commandDistanceRequest(testclient::PositionRoadMap pos1, testclient::Position2D pos2, int flag) {
01099     commandDistanceRequest(NULL, NULL, &pos1, &pos2, NULL, NULL, flag);
01100 }
01101 
01102 
01103 void
01104 TraCITestClient::commandDistanceRequest(testclient::PositionRoadMap pos1, testclient::Position3D pos2, int flag) {
01105     commandDistanceRequest(NULL, NULL, &pos1, NULL, &pos2, NULL, flag);
01106 }
01107 
01108 
01109 void
01110 TraCITestClient::commandDistanceRequest(testclient::PositionRoadMap pos1, testclient::PositionRoadMap pos2, int flag) {
01111     commandDistanceRequest(NULL, NULL, &pos1, NULL, NULL, &pos2, flag);
01112 }
01113 
01114 
01115 void
01116 TraCITestClient::commandDistanceRequest(testclient::Position2D pos1, testclient::PositionRoadMap pos2, int flag) {
01117     commandDistanceRequest(&pos1, NULL, NULL, NULL, NULL, &pos2, flag);
01118 }
01119 
01120 
01121 void
01122 TraCITestClient::commandDistanceRequest(testclient::Position3D pos1, testclient::PositionRoadMap pos2, int flag) {
01123     commandDistanceRequest(NULL, &pos1, NULL, NULL, NULL, &pos2, flag);
01124 }
01125 
01126 
01127 
01128 void
01129 TraCITestClient::commandDistanceRequest(testclient::Position2D* pos1_2D,
01130                                         testclient::Position3D* pos1_3D,
01131                                         testclient::PositionRoadMap* pos1_Road,
01132                                         testclient::Position2D* pos2_2D,
01133                                         testclient::Position3D* pos2_3D,
01134                                         testclient::PositionRoadMap* pos2_Road,
01135                                         int flag) {
01136     tcpip::Storage outMsg;
01137     tcpip::Storage inMsg;
01138     tcpip::Storage tempMsg;
01139     std::stringstream msg;
01140 
01141     if (socket == NULL) {
01142         msg << "#Error while sending command: no connection to server" ;
01143         errorMsg(msg);
01144         return;
01145     }
01146 
01147     // command id
01148     tempMsg.writeUnsignedByte(CMD_DISTANCEREQUEST);
01149     // position1
01150     if (pos1_2D != NULL) {
01151         tempMsg.writeUnsignedByte(POSITION_2D);
01152         tempMsg.writeFloat(pos1_2D->x);
01153         tempMsg.writeFloat(pos1_2D->y);
01154     } else if (pos1_3D != NULL) {
01155         tempMsg.writeUnsignedByte(POSITION_3D);
01156         tempMsg.writeFloat(pos1_3D->x);
01157         tempMsg.writeFloat(pos1_3D->y);
01158         tempMsg.writeFloat(pos1_3D->z);
01159     } else if (pos1_Road != NULL) {
01160         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
01161         tempMsg.writeString(pos1_Road->roadId);
01162         tempMsg.writeFloat(pos1_Road->pos);
01163         tempMsg.writeUnsignedByte(pos1_Road->laneId);
01164     } else {
01165         std::cerr << "Error in method commandDistanceRequest: position1 is NULL" << std::endl;
01166         return;
01167     }
01168     // position2
01169     if (pos2_2D != NULL) {
01170         tempMsg.writeUnsignedByte(POSITION_2D);
01171         tempMsg.writeFloat(pos2_2D->x);
01172         tempMsg.writeFloat(pos2_2D->y);
01173     } else if (pos2_3D != NULL) {
01174         tempMsg.writeUnsignedByte(POSITION_3D);
01175         tempMsg.writeFloat(pos2_3D->x);
01176         tempMsg.writeFloat(pos2_3D->y);
01177         tempMsg.writeFloat(pos2_3D->z);
01178     } else if (pos2_Road != NULL) {
01179         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
01180         tempMsg.writeString(pos2_Road->roadId);
01181         tempMsg.writeFloat(pos2_Road->pos);
01182         tempMsg.writeUnsignedByte(pos2_Road->laneId);
01183     } else {
01184         std::cerr << "Error in method commandDistanceRequest: position2 is NULL" << std::endl;
01185         return;
01186     }
01187     // flag
01188     tempMsg.writeUnsignedByte(flag);
01189     // command length
01190     outMsg.writeUnsignedByte(1 + (int) tempMsg.size());
01191     outMsg.writeStorage(tempMsg);
01192 
01193     // send request message
01194     try {
01195         socket->sendExact(outMsg);
01196     } catch (SocketException &e) {
01197         msg << "Error while sending command: " << e.what();
01198         errorMsg(msg);
01199         return;
01200     }
01201 
01202     answerLog << std::endl << "-> Command sent: <DistanceRequest>:" << std::endl;
01203     if (pos1_2D != NULL) {
01204         answerLog << " FirstPosition-2D: x=" << pos1_2D->x << " y=" << pos1_2D->y ;
01205     } else if (pos1_3D != NULL) {
01206         answerLog << " FirstPosition-3D: x=" << pos1_3D->x << " y=" << pos1_3D->y << " z=" << pos1_3D->z;
01207     } else if (pos1_Road != NULL) {
01208         answerLog << " FirstPosition-RoadMap: roadId=" << pos1_Road->roadId << " pos=" << pos1_Road->pos << " laneId=" << (int)pos1_Road->laneId ;
01209     }
01210     if (pos2_2D != NULL) {
01211         answerLog << " SecondPosition-2D: x=" << pos2_2D->x << " y=" << pos2_2D->y ;
01212     } else if (pos2_3D != NULL) {
01213         answerLog << " SecondPosition-3D: x=" << pos2_3D->x << " y=" << pos2_3D->y << " z=" << pos2_3D->z;
01214     } else if (pos2_Road != NULL) {
01215         answerLog << " SecondPosition-RoadMap: roadId=" << pos2_Road->roadId << " pos=" << pos2_Road->pos << " laneId=" << (int)pos2_Road->laneId ;
01216     }
01217     answerLog << " Flag=" << flag << std::endl;
01218 
01219     // receive answer message
01220     try {
01221         socket->receiveExact(inMsg);
01222     } catch (SocketException &e) {
01223         msg << "Error while receiving command: " << e.what();
01224         errorMsg(msg);
01225         return;
01226     }
01227 
01228     // validate result state
01229     if (!reportResultState(inMsg, CMD_DISTANCEREQUEST)) {
01230         return;
01231     }
01232 
01233     // validate answer message
01234     validateDistanceRequest(inMsg);
01235 }
01236 
01237 
01238 void
01239 TraCITestClient::commandGetTLStatus(int tlId, SUMOTime intervalStart, SUMOTime intervalEnd) {
01240     tcpip::Storage outMsg;
01241     tcpip::Storage inMsg;
01242     std::stringstream msg;
01243 
01244     if (socket == NULL) {
01245         msg << "#Error while sending command: no connection to server" ;
01246         errorMsg(msg);
01247         return;
01248     }
01249 
01250     // command length
01251     outMsg.writeUnsignedByte(1 + 1 + 4 + 4 + 4);
01252     // command id
01253     outMsg.writeUnsignedByte(CMD_GETTLSTATUS);
01254     // tl id
01255     outMsg.writeInt(tlId);
01256     // interval start
01257     outMsg.writeInt(intervalStart);
01258     // interval end
01259     outMsg.writeInt(intervalEnd);
01260 
01261     // send request message
01262     try {
01263         socket->sendExact(outMsg);
01264     } catch (SocketException &e) {
01265         msg << "Error while sending command: " << e.what();
01266         errorMsg(msg);
01267         return;
01268     }
01269 
01270     answerLog << std::endl << "-> Command sent: <GetTLStatus>:" << std::endl
01271     << "  TLId=" << tlId << " IntervalStart=" << time2string(intervalStart)
01272     << " IntervalEnd=" << time2string(intervalEnd) << std::endl;
01273 
01274     // receive answer message
01275     try {
01276         socket->receiveExact(inMsg);
01277     } catch (SocketException &e) {
01278         msg << "Error while receiving command: " << e.what();
01279         errorMsg(msg);
01280         return;
01281     }
01282 
01283     // validate result state
01284     if (!reportResultState(inMsg, CMD_GETTLSTATUS)) {
01285         return;
01286     }
01287 
01288     // validate response
01289     validateGetTLStatus(inMsg);
01290 }
01291 
01292 
01293 
01294 void
01295 TraCITestClient::commandGetVariable(int domID, int varID, const std::string &objID) {
01296     tcpip::Storage outMsg, inMsg;
01297     std::stringstream msg;
01298     if (socket == NULL) {
01299         msg << "#Error while sending command: no connection to server" ;
01300         errorMsg(msg);
01301         return;
01302     }
01303     // command length
01304     outMsg.writeUnsignedByte(1 + 1 + 1 + 4 + (int) objID.length());
01305     // command id
01306     outMsg.writeUnsignedByte(domID);
01307     // variable id
01308     outMsg.writeUnsignedByte(varID);
01309     // object id
01310     outMsg.writeString(objID);
01311 
01312     // send request message
01313     try {
01314         socket->sendExact(outMsg);
01315     } catch (SocketException &e) {
01316         msg << "Error while sending command: " << e.what();
01317         errorMsg(msg);
01318         return;
01319     }
01320     answerLog << std::endl << "-> Command sent: <GetVariable>:" << std::endl
01321     << "  domID=" << domID << " varID=" << varID
01322     << " objID=" << objID << std::endl;
01323 
01324     // receive answer message
01325     try {
01326         socket->receiveExact(inMsg);
01327         if (!reportResultState(inMsg, domID)) {
01328             return;
01329         }
01330     } catch (SocketException &e) {
01331         msg << "Error while receiving command: " << e.what();
01332         errorMsg(msg);
01333         return;
01334     }
01335     // validate result state
01336     try {
01337         int respStart = inMsg.position();
01338         int extLength = inMsg.readUnsignedByte();
01339         int respLength = inMsg.readInt();
01340         int cmdId = inMsg.readUnsignedByte();
01341         if (cmdId != (domID+0x10)) {
01342             answerLog << "#Error: received response with command id: " << cmdId
01343             << "but expected: " << (int)(domID+0x10) << std::endl;
01344             return;
01345         }
01346         answerLog << "  CommandID=" << cmdId;
01347         answerLog << "  VariableID=" << inMsg.readUnsignedByte();
01348         answerLog << "  ObjectID=" << inMsg.readString();
01349         int valueDataType = inMsg.readUnsignedByte();
01350         answerLog << " valueDataType=" << valueDataType;
01351         readAndReportTypeDependent(inMsg, valueDataType);
01352     } catch (SocketException &e) {
01353         msg << "Error while receiving command: " << e.what();
01354         errorMsg(msg);
01355         return;
01356     }
01357 }
01358 
01359 
01360 void
01361 TraCITestClient::commandGetVariablePlus(int domID, int varID, const std::string &objID, std::ifstream &defFile) {
01362     std::stringstream msg;
01363     if (socket == NULL) {
01364         msg << "#Error while sending command: no connection to server" ;
01365         errorMsg(msg);
01366         return;
01367     }
01368     tcpip::Storage outMsg, inMsg, tmp;
01369     int dataLength = setValueTypeDependant(tmp, defFile, msg);
01370     std::string msgS = msg.str();
01371     if (msgS!="") {
01372         errorMsg(msg);
01373     }
01374     // command length (domID, varID, objID, dataType, data)
01375     outMsg.writeUnsignedByte(1 + 1 + 1 + 4 + (int) objID.length() + dataLength);
01376     // command id
01377     outMsg.writeUnsignedByte(domID);
01378     // variable id
01379     outMsg.writeUnsignedByte(varID);
01380     // object id
01381     outMsg.writeString(objID);
01382     // data type
01383     outMsg.writeStorage(tmp);
01384     // send request message
01385     try {
01386         socket->sendExact(outMsg);
01387     } catch (SocketException &e) {
01388         msg << "Error while sending command: " << e.what();
01389         errorMsg(msg);
01390         return;
01391     }
01392     answerLog << std::endl << "-> Command sent: <GetVariable>:" << std::endl
01393     << "  domID=" << domID << " varID=" << varID
01394     << " objID=" << objID << std::endl;
01395 
01396     // receive answer message
01397     try {
01398         socket->receiveExact(inMsg);
01399         if (!reportResultState(inMsg, domID)) {
01400             return;
01401         }
01402     } catch (SocketException &e) {
01403         msg << "Error while receiving command: " << e.what();
01404         errorMsg(msg);
01405         return;
01406     }
01407     // validate result state
01408     try {
01409         int respStart = inMsg.position();
01410         int extLength = inMsg.readUnsignedByte();
01411         int respLength = inMsg.readInt();
01412         int cmdId = inMsg.readUnsignedByte();
01413         if (cmdId != (domID+0x10)) {
01414             answerLog << "#Error: received response with command id: " << cmdId
01415             << "but expected: " << (int)(domID+0x10) << std::endl;
01416             return;
01417         }
01418         answerLog << "  CommandID=" << cmdId;
01419         answerLog << "  VariableID=" << inMsg.readUnsignedByte();
01420         answerLog << "  ObjectID=" << inMsg.readString();
01421         int valueDataType = inMsg.readUnsignedByte();
01422         answerLog << " valueDataType=" << valueDataType;
01423         readAndReportTypeDependent(inMsg, valueDataType);
01424     } catch (SocketException &e) {
01425         msg << "Error while receiving command: " << e.what();
01426         errorMsg(msg);
01427         return;
01428     }
01429 }
01430 
01431 
01432 void
01433 TraCITestClient::commandSubscribeVariable(int domID, const std::string &objID, int beginTime, int endTime, int varNo, std::ifstream &defFile) {
01434     std::stringstream msg;
01435     if (socket == NULL) {
01436         msg << "#Error while sending command: no connection to server" ;
01437         errorMsg(msg);
01438         return;
01439     }
01440     tcpip::Storage outMsg, inMsg, tmp;
01441     std::string msgS = msg.str();
01442     if (msgS!="") {
01443         errorMsg(msg);
01444     }
01445     // command length (domID, beginTime, endTime, objID, varNo, <vars>)
01446     outMsg.writeUnsignedByte(0);
01447     outMsg.writeInt(/*1 + 4 +*/ 5 + 1 + 4 + 4 + 4 + (int) objID.length() + 1 + varNo);
01448     // command id
01449     outMsg.writeUnsignedByte(domID);
01450     // time
01451     outMsg.writeInt(beginTime);
01452     outMsg.writeInt(endTime);
01453     // object id
01454     outMsg.writeString(objID);
01455     // command id
01456     outMsg.writeUnsignedByte(varNo);
01457     for (int i=0; i<varNo; ++i) {
01458         int var;
01459         defFile >> var;
01460         // variable id
01461         outMsg.writeUnsignedByte(var);
01462     }
01463     // send request message
01464     try {
01465         socket->sendExact(outMsg);
01466     } catch (SocketException &e) {
01467         msg << "Error while sending command: " << e.what();
01468         errorMsg(msg);
01469         return;
01470     }
01471     answerLog << std::endl << "-> Command sent: <SubscribeVariable>:" << std::endl
01472     << "  domID=" << domID << " objID=" << objID << " with " << varNo << " variables" << std::endl;
01473 
01474     // receive answer message
01475     try {
01476         socket->receiveExact(inMsg);
01477         if (!reportResultState(inMsg, domID)) {
01478             return;
01479         }
01480     } catch (SocketException &e) {
01481         msg << "Error while receiving command: " << e.what();
01482         errorMsg(msg);
01483         return;
01484     }
01485     // validate result state
01486     try {
01487         validateSubscription(inMsg);
01488     } catch (SocketException &e) {
01489         msg << "Error while receiving command: " << e.what();
01490         errorMsg(msg);
01491         return;
01492     }
01493 }
01494 
01495 
01496 
01497 int
01498 TraCITestClient::setValueTypeDependant(tcpip::Storage &into, std::ifstream &defFile, std::stringstream &msg) {
01499     std::string dataTypeS, valueS;
01500     defFile >> dataTypeS >> valueS;
01501     if (dataTypeS=="<int>") {
01502         into.writeUnsignedByte(TYPE_INTEGER);
01503         into.writeInt(atoi(valueS.c_str()));
01504         return 4 + 1;
01505     } else if (dataTypeS=="<byte>") {
01506         into.writeUnsignedByte(TYPE_BYTE);
01507         into.writeByte(atoi(valueS.c_str()));
01508         return 1 + 1;
01509     }  else if (dataTypeS=="<ubyte>") {
01510         into.writeUnsignedByte(TYPE_UBYTE);
01511         into.writeByte(atoi(valueS.c_str()));
01512         return 1 + 1;
01513     } else if (dataTypeS=="<float>") {
01514         into.writeUnsignedByte(TYPE_FLOAT);
01515         into.writeFloat(atof(valueS.c_str()));
01516         return 4 + 1;
01517     } else if (dataTypeS=="<string>") {
01518         into.writeUnsignedByte(TYPE_STRING);
01519         into.writeString(valueS);
01520         return 4 + 1 + (int) valueS.length();
01521     } else if (dataTypeS=="<string*>") {
01522         std::vector<std::string> slValue;
01523         int number = atoi(valueS.c_str());
01524         int length = 1 + 4;
01525         for (int i=0; i<number; ++i) {
01526             std::string tmp;
01527             defFile >> tmp;
01528             slValue.push_back(tmp);
01529             length += 4 + tmp.length();
01530         }
01531         into.writeUnsignedByte(TYPE_STRINGLIST);
01532         into.writeStringList(slValue);
01533         return length;
01534     } else if (dataTypeS=="<compound>") {
01535         into.writeUnsignedByte(TYPE_COMPOUND);
01536         int number = atoi(valueS.c_str());
01537         into.writeInt(number);
01538         int length = 1 + 4;
01539         for (int i=0; i<number; ++i) {
01540             length += setValueTypeDependant(into, defFile, msg);
01541         }
01542         return length;
01543     } else if (dataTypeS=="<color>") {
01544         into.writeUnsignedByte(TYPE_COLOR);
01545         into.writeUnsignedByte(atoi(valueS.c_str()));
01546         for (int i=0; i<3; ++i) {
01547             defFile >> valueS;
01548             into.writeUnsignedByte(atoi(valueS.c_str()));
01549         }
01550         return 1 + 4;
01551     } else if (dataTypeS=="<position2D>") {
01552         into.writeUnsignedByte(TYPE_POSITION2D);
01553         into.writeFloat(atof(valueS.c_str()));
01554         defFile >> valueS;
01555         into.writeFloat(atof(valueS.c_str()));
01556         return 1 + 8;
01557     } else if (dataTypeS=="<shape>") {
01558         into.writeUnsignedByte(TYPE_POLYGON);
01559         int number = atoi(valueS.c_str());
01560         into.writeUnsignedByte(number);
01561         int length = 1 + 1;
01562         for (int i=0; i<number; ++i) {
01563             std::string x, y;
01564             defFile >> x >> y;
01565             into.writeFloat(atof(x.c_str()));
01566             into.writeFloat(atof(y.c_str()));
01567             length += 8;
01568         }
01569         return length;
01570     }
01571     msg << "## Unknown data type: " << dataTypeS;
01572     return 0;
01573 }
01574 
01575 void
01576 TraCITestClient::commandSetValue(int domID, int varID, const std::string &objID, std::ifstream &defFile) {
01577     std::stringstream msg;
01578     if (socket == NULL) {
01579         msg << "#Error while sending command: no connection to server" ;
01580         errorMsg(msg);
01581         return;
01582     }
01583     tcpip::Storage outMsg, inMsg, tmp;
01584     int dataLength = setValueTypeDependant(tmp, defFile, msg);
01585     std::string msgS = msg.str();
01586     if (msgS!="") {
01587         errorMsg(msg);
01588     }
01589     // command length (domID, varID, objID, dataType, data)
01590     outMsg.writeUnsignedByte(1 + 1 + 1 + 4 + (int) objID.length() + dataLength);
01591     // command id
01592     outMsg.writeUnsignedByte(domID);
01593     // variable id
01594     outMsg.writeUnsignedByte(varID);
01595     // object id
01596     outMsg.writeString(objID);
01597     // data type
01598     outMsg.writeStorage(tmp);
01599     // send request message
01600     try {
01601         socket->sendExact(outMsg);
01602     } catch (SocketException &e) {
01603         msg << "Error while sending command: " << e.what();
01604         errorMsg(msg);
01605         return;
01606     }
01607     answerLog << std::endl << "-> Command sent: <SetValue>:" << std::endl
01608     << "  domID=" << domID << " varID=" << varID
01609     << " objID=" << objID << std::endl;
01610 
01611     // receive answer message
01612     try {
01613         socket->receiveExact(inMsg);
01614         if (!reportResultState(inMsg, domID)) {
01615             return;
01616         }
01617     } catch (SocketException &e) {
01618         msg << "Error while receiving command: " << e.what();
01619         errorMsg(msg);
01620         return;
01621     }
01622 }
01623 
01624 
01625 
01626 
01627 void
01628 TraCITestClient::commandClose() {
01629     tcpip::Storage outMsg;
01630     tcpip::Storage inMsg;
01631     std::stringstream msg;
01632 
01633     if (socket == NULL) {
01634         msg << "#Error while sending command: no connection to server" ;
01635         errorMsg(msg);
01636         return;
01637     }
01638 
01639     // command length
01640     outMsg.writeUnsignedByte(1 + 1);
01641     // command id
01642     outMsg.writeUnsignedByte(CMD_CLOSE);
01643 
01644     // send request message
01645     try {
01646         socket->sendExact(outMsg);
01647     } catch (SocketException &e) {
01648         msg << "Error while sending command: " << e.what();
01649         errorMsg(msg);
01650         return;
01651     }
01652 
01653     answerLog << std::endl << "-> Command sent: <Close>:" << std::endl;
01654 
01655     // receive answer message
01656     try {
01657         socket->receiveExact(inMsg);
01658     } catch (SocketException &e) {
01659         msg << "Error while receiving command: " << e.what();
01660         errorMsg(msg);
01661         return;
01662     }
01663 
01664     // validate result state
01665     if (!reportResultState(inMsg, CMD_CLOSE)) {
01666         return;
01667     }
01668 }
01669 
01670 
01671 void
01672 TraCITestClient::commandScenario(int flag, int domain, int domainId, int variable, int valueDataType) {
01673     commandScenario(flag, domain, domainId, variable, valueDataType, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
01674                     NULL, NULL, NULL, NULL);
01675 }
01676 
01677 
01678 void
01679 TraCITestClient::commandScenario(int flag, int domain, int domainId, int variable, std::string stringVal) {
01680     commandScenario(flag, domain, domainId, variable, 0, NULL, NULL, NULL, NULL, NULL, &stringVal,
01681                     NULL, NULL, NULL, NULL, NULL);
01682 }
01683 
01684 
01685 void
01686 TraCITestClient::commandScenario(int flag, int domain, int domainId, int variable, testclient::Position3D pos3dVal) {
01687     commandScenario(flag, domain, domainId, variable, 0, NULL, NULL, NULL, NULL, NULL, NULL,
01688                     &pos3dVal, NULL, NULL, NULL, NULL);
01689 }
01690 
01691 
01692 void
01693 TraCITestClient::commandScenario(int flag, int domain, int domainId, int variable, testclient::PositionRoadMap roadPosVal) {
01694     commandScenario(flag, domain, domainId, variable, 0, NULL, NULL, NULL, NULL, NULL, NULL,
01695                     NULL, &roadPosVal, NULL, NULL, NULL);
01696 }
01697 
01698 
01699 void
01700 TraCITestClient::commandScenario(int flag, int domain, int domainId, int variable, int valueDataType,
01701                                  int* intVal, int* byteVal, int* ubyteVal,
01702                                  float* floatVal, double* doubleVal, std::string* stringVal,
01703                                  testclient::Position3D* pos3dVal, testclient::PositionRoadMap* roadPosVal,
01704                                  testclient::BoundingBox* boxVal, testclient::Polygon* polyVal,
01705                                  testclient::TLPhaseList* tlphaseVal) {
01706     tcpip::Storage outMsg;
01707     tcpip::Storage inMsg;
01708     tcpip::Storage tempMsg;
01709     std::stringstream valueString;
01710     std::stringstream msg;
01711 
01712     if (socket == NULL) {
01713         msg << "#Error while sending command: no connection to server" ;
01714         errorMsg(msg);
01715         return;
01716     }
01717 
01718     // command id
01719     tempMsg.writeUnsignedByte(CMD_SCENARIO);
01720     // flag
01721     tempMsg.writeUnsignedByte(flag);
01722     // domain
01723     tempMsg.writeUnsignedByte(domain);
01724     // domain id
01725     tempMsg.writeInt(domainId);
01726     // variable
01727     tempMsg.writeUnsignedByte(variable);
01728     // value
01729     if (intVal != NULL) {
01730         tempMsg.writeUnsignedByte(TYPE_INTEGER);
01731         tempMsg.writeInt(*intVal);
01732         valueString << " IntegerValue=" << *intVal;
01733     } else if (byteVal != NULL) {
01734         tempMsg.writeUnsignedByte(TYPE_BYTE);
01735         tempMsg.writeByte(*byteVal);
01736         valueString << " ByteValue=" << *byteVal;
01737     } else if (ubyteVal != NULL) {
01738         tempMsg.writeUnsignedByte(TYPE_UBYTE);
01739         tempMsg.writeUnsignedByte(*ubyteVal);
01740         valueString << " UByteValue=" << *ubyteVal;
01741     } else if (floatVal != NULL) {
01742         tempMsg.writeUnsignedByte(TYPE_FLOAT);
01743         tempMsg.writeFloat(*floatVal);
01744         valueString << " FloatValue=" << *floatVal;
01745     } else if (doubleVal != NULL) {
01746         tempMsg.writeUnsignedByte(TYPE_DOUBLE);
01747         tempMsg.writeDouble(*doubleVal);
01748         valueString << " DoubleValue=" << *doubleVal;
01749     } else if (stringVal != NULL) {
01750         tempMsg.writeUnsignedByte(TYPE_STRING);
01751         tempMsg.writeString(*stringVal);
01752         valueString << " StringValue=" << *stringVal;
01753     } else if (pos3dVal != NULL) {
01754         tempMsg.writeUnsignedByte(POSITION_3D);
01755         tempMsg.writeFloat(pos3dVal->x);
01756         tempMsg.writeFloat(pos3dVal->y);
01757         tempMsg.writeFloat(pos3dVal->z);
01758         valueString << std::endl << " Position3DValue: x="
01759         << pos3dVal->x << " y=" << pos3dVal->y << " z=" << pos3dVal->z;
01760     } else if (roadPosVal != NULL) {
01761         tempMsg.writeUnsignedByte(POSITION_ROADMAP);
01762         tempMsg.writeString(roadPosVal->roadId);
01763         tempMsg.writeFloat(roadPosVal->pos);
01764         tempMsg.writeUnsignedByte(roadPosVal->laneId);
01765         valueString << std::endl << " RoadMapPositionValue: roadId=" << roadPosVal->roadId
01766         << " pos=" << roadPosVal->pos << " laneId=" << (int)roadPosVal->laneId;
01767     } else if (boxVal != NULL) {
01768         tempMsg.writeUnsignedByte(TYPE_BOUNDINGBOX);
01769         tempMsg.writeFloat(boxVal->lowerLeft.x);
01770         tempMsg.writeFloat(boxVal->lowerLeft.y);
01771         tempMsg.writeFloat(boxVal->upperRight.x);
01772         tempMsg.writeFloat(boxVal->lowerLeft.y);
01773         valueString << std::endl << " BoundaryBoxValue: lowerleft x=" << boxVal->lowerLeft.x << " y=" << boxVal->lowerLeft.y
01774         << "upperRight x=" << boxVal->upperRight.x << " y=" << boxVal->upperRight.y;
01775     } else if (polyVal != NULL) {
01776         tempMsg.writeUnsignedByte(TYPE_POLYGON);
01777         tempMsg.writeUnsignedByte((int) polyVal->size());
01778         valueString << std::endl << " PolygonValue: " << std::endl;
01779         for (testclient::Polygon::iterator it = polyVal->begin(); it != polyVal->end(); it++) {
01780             tempMsg.writeFloat(it->x);
01781             tempMsg.writeFloat(it->y);
01782             valueString << " (" << it->x << "," << it->y << "), " << std::endl;
01783         }
01784     } else if (tlphaseVal != NULL) {
01785         tempMsg.writeUnsignedByte(TYPE_TLPHASELIST);
01786         tempMsg.writeUnsignedByte((int) tlphaseVal->size());
01787         valueString << std::endl << " TLPhaseListValue: " << std::endl;
01788         for (testclient::TLPhaseList::iterator it = tlphaseVal->begin(); it != tlphaseVal->end(); it++) {
01789             tempMsg.writeString(it->precRoadId);
01790             tempMsg.writeString(it->succRoadId);
01791             tempMsg.writeUnsignedByte(it->phase);
01792             valueString << " (precRoad=" << it->precRoadId << " succRoad=" << it->succRoadId
01793             << " phase=" << it->phase << ")" << std::endl;
01794         }
01795     } else {
01796         // value data type (with out value)
01797         tempMsg.writeUnsignedByte(valueDataType);
01798         valueString << " [no value sent]";
01799     }
01800 
01801     // command length
01802     outMsg.writeUnsignedByte(1 + (int) tempMsg.size());
01803     // whole message
01804     outMsg.writeStorage(tempMsg);
01805 
01806     // send request message
01807     try {
01808         socket->sendExact(outMsg);
01809     } catch (SocketException &e) {
01810         msg << "Error while sending command: " << e.what();
01811         errorMsg(msg);
01812         return;
01813     }
01814 
01815     answerLog << std::endl << "-> Command sent: <Scenario>:" << std::endl << "  flag=" << flag
01816     << " domain=" << domain << " domainId=" << domainId
01817     << " variable=" << variable << " valueDataType=" << valueDataType
01818     << valueString.str() << std::endl;
01819     writeResult();
01820 
01821     // receive answer message
01822     try {
01823         socket->receiveExact(inMsg);
01824     } catch (SocketException &e) {
01825         msg << "Error while receiving command: " << e.what();
01826         errorMsg(msg);
01827         return;
01828     }
01829 
01830     // validate result state
01831     if (!reportResultState(inMsg, CMD_SCENARIO)) {
01832         return;
01833     }
01834 
01835     // validate response
01836     validateScenario(inMsg);
01837 }
01838 
01839 
01840 bool
01841 TraCITestClient::validateSimulationStep(tcpip::Storage &inMsg) {
01842     int cmdId;
01843     int cmdLength;
01844     int nodeId;
01845     SUMOTime targetTime;
01846     int posType;
01847     int cmdStart;
01848     testclient::Position2D pos2D;
01849     testclient::Position3D pos3D;
01850     testclient::PositionRoadMap roadPos;
01851 
01852     while (inMsg.valid_pos()) {
01853         try {
01854             cmdStart = inMsg.position();
01855             cmdLength = inMsg.readUnsignedByte();
01856             cmdId = inMsg.readUnsignedByte();
01857             if (cmdId != CMD_MOVENODE) {
01858                 answerLog << "#Error: received response with command id: " << cmdId
01859                 << "but expected: " << (int)CMD_MOVENODE << std::endl;
01860                 return false;
01861             }
01862             answerLog << ".. Received Response <MoveNode>:" << std::endl;
01863             nodeId = inMsg.readInt();
01864             answerLog << "  nodeId=" << nodeId << " ";
01865             targetTime = inMsg.readInt();
01866             answerLog << "targetTime=" << time2string(targetTime) << " ";
01867             posType = inMsg.readUnsignedByte();
01868             switch (posType) {
01869             case POSITION_2D:
01870                 pos2D.x = inMsg.readFloat();
01871                 pos2D.y = inMsg.readFloat();
01872                 answerLog << "2D-Position: x=" << pos2D.x << " y=" << pos2D.y << std::endl;
01873                 break;
01874             case POSITION_3D:
01875             case POSITION_2_5D:
01876                 if (posType == POSITION_2_5D) {
01877                     answerLog << "2.5D-Position: ";
01878                 } else {
01879                     answerLog << "3D-Position: ";
01880                 }
01881                 pos3D.x = inMsg.readFloat();
01882                 pos3D.y = inMsg.readFloat();
01883                 pos3D.z = inMsg.readFloat();
01884                 answerLog << "x=" << pos3D.x << " y=" << pos3D.y << " z=" << pos3D.z << std::endl;
01885                 break;
01886             case POSITION_ROADMAP:
01887                 roadPos.roadId = inMsg.readString();
01888                 roadPos.pos = inMsg.readFloat();
01889                 roadPos.laneId = inMsg.readUnsignedByte();
01890                 answerLog << "RoadMap-Position: roadId=" << roadPos.roadId << " pos=" << roadPos.pos
01891                 << " laneId=" << (int)roadPos.laneId << std::endl;
01892                 break;
01893             default:
01894                 answerLog << "#Error: received unknown position format" << std::endl;
01895                 return false;
01896             }
01897             if ((cmdStart + cmdLength) != inMsg.position()) {
01898                 answerLog << "#Warning: command at position " << cmdStart << " has wrong length" << std::endl;
01899                 //return false;
01900             }
01901         } catch (std::invalid_argument e) {
01902             answerLog << "#Error while reading message:" << e.what() << std::endl;
01903             return false;
01904         }
01905     }
01906 
01907     return true;
01908 }
01909 
01910 
01911 bool
01912 TraCITestClient::validateSimulationStep2(tcpip::Storage &inMsg) {
01913     try {
01914         int noSubscriptions = inMsg.readInt();
01915         for (int s=0; s<noSubscriptions; ++s) {
01916             /*
01917             if (!reportResultState(inMsg, CMD_SIMSTEP2, true)) {
01918                 return false;
01919             }
01920             */
01921             if (!validateSubscription(inMsg)) {
01922                 return false;
01923             }
01924         }
01925     } catch (std::invalid_argument e) {
01926         answerLog << "#Error while reading message:" << e.what() << std::endl;
01927         return false;
01928     }
01929     return true;
01930 }
01931 
01932 
01933 bool
01934 TraCITestClient::validateSubscription(tcpip::Storage &inMsg) {
01935     try {
01936         int respStart = inMsg.position();
01937         int extLength = inMsg.readUnsignedByte();
01938         int respLength = inMsg.readInt();
01939         int cmdId = inMsg.readUnsignedByte();
01940         if (cmdId<0xe0||cmdId>0xef) {
01941             answerLog << "#Error: received response with command id: " << cmdId << " but expected a subscription response (0xe0-0xef)" << std::endl;
01942             return false;
01943         }
01944         answerLog << "  CommandID=" << cmdId;
01945         answerLog << "  ObjectID=" << inMsg.readString();
01946         unsigned int varNo = inMsg.readUnsignedByte();
01947         answerLog << "  #variables=" << varNo << std::endl;
01948         for (int i=0; i<varNo; ++i) {
01949             answerLog << "      VariableID=" << inMsg.readUnsignedByte();
01950             bool ok = inMsg.readUnsignedByte()==RTYPE_OK;
01951             answerLog << "      ok=" << ok;
01952             int valueDataType = inMsg.readUnsignedByte();
01953             answerLog << " valueDataType=" << valueDataType;
01954             readAndReportTypeDependent(inMsg, valueDataType);
01955         }
01956     } catch (std::invalid_argument e) {
01957         answerLog << "#Error while reading message:" << e.what() << std::endl;
01958         return false;
01959     }
01960     return true;
01961 }
01962 
01963 
01964 bool
01965 TraCITestClient::validateStopNode(tcpip::Storage &inMsg) {
01966     int cmdId;
01967     int cmdLength;
01968     int rNodeId;
01969     float rRadius;
01970     int rPosType;
01971     int cmdStart;
01972     testclient::PositionRoadMap rRoadPos;
01973 
01974     try {
01975         cmdStart = inMsg.position();
01976         cmdLength = inMsg.readUnsignedByte();
01977         // read command id
01978         cmdId = inMsg.readUnsignedByte();
01979         if (cmdId != CMD_STOP) {
01980             answerLog << "#Error: received response with command id: " << cmdId
01981             << "but expected: " << (int)CMD_STOP << std::endl;
01982             return false;
01983         }
01984         answerLog << ".. Received Response <StopNode>:" << std::endl;
01985         // read nodeID
01986         rNodeId = inMsg.readInt();
01987         answerLog << "  nodeId=" << rNodeId << " ";
01988         // read stop position
01989         rPosType = inMsg.readUnsignedByte();
01990         if (rPosType != POSITION_ROADMAP) {
01991             answerLog << "#Error: received position was not in road map format" << std::endl;
01992             return false;
01993         }
01994         rRoadPos.roadId = inMsg.readString();
01995         rRoadPos.pos = inMsg.readFloat();
01996         rRoadPos.laneId = inMsg.readUnsignedByte();
01997         answerLog << "Stop position: roadId=" << rRoadPos.roadId
01998         << " pos=" << rRoadPos.pos << " laneId=" << (int)rRoadPos.laneId;
01999         // read radius
02000         rRadius = inMsg.readFloat();
02001         answerLog << " radius=" << rRadius;
02002         // read wait time
02003         SUMOTime rWaitTime = inMsg.readInt();
02004         answerLog << " wait time=" << time2string(rWaitTime) << std::endl;
02005         // check command length
02006         if ((cmdStart + cmdLength) != inMsg.position()) {
02007             answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
02008             return false;
02009         }
02010     } catch (std::invalid_argument e) {
02011         answerLog << "#Error while reading message:" << e.what() << std::endl;
02012         return false;
02013     }
02014 
02015     return true;
02016 }
02017 
02018 
02019 bool
02020 TraCITestClient::validatePositionConversion(tcpip::Storage &inMsg) {
02021     int cmdId;
02022     int cmdLength;
02023     int posType;
02024     int reqPosType;
02025     int cmdStart;
02026     testclient::PositionRoadMap roadPos;
02027     testclient::Position2D pos2D;
02028     testclient::Position3D pos3D;
02029 
02030     try {
02031         cmdStart = inMsg.position();
02032         cmdLength = inMsg.readUnsignedByte();
02033         // read command id
02034         cmdId = inMsg.readUnsignedByte();
02035         if (cmdId != CMD_POSITIONCONVERSION) {
02036             answerLog << "#Error: received response with command id: " << cmdId
02037             << "but expected: " << (int)CMD_POSITIONCONVERSION << std::endl;
02038             return false;
02039         }
02040         answerLog << ".. Received Response <PositionConversion>:" << std::endl;
02041         // read converted position
02042         posType = inMsg.readUnsignedByte();
02043         switch (posType) {
02044         case POSITION_2D:
02045             pos2D.x = inMsg.readFloat();
02046             pos2D.y = inMsg.readFloat();
02047             answerLog << "2D-Position: x=" << pos2D.x << " y=" << pos2D.y << std::endl;
02048             break;
02049         case POSITION_3D:
02050         case POSITION_2_5D:
02051             if (posType == POSITION_2_5D) {
02052                 answerLog << "2.5D-Position: ";
02053             } else {
02054                 answerLog << "3D-Position: ";
02055             }
02056             pos3D.x = inMsg.readFloat();
02057             pos3D.y = inMsg.readFloat();
02058             pos3D.z = inMsg.readFloat();
02059             answerLog << "x=" << pos3D.x << " y=" << pos3D.y << " z=" << pos3D.z << std::endl;
02060             break;
02061         case POSITION_ROADMAP:
02062             roadPos.roadId = inMsg.readString();
02063             roadPos.pos = inMsg.readFloat();
02064             roadPos.laneId = inMsg.readUnsignedByte();
02065             answerLog << "RoadMap-Position: roadId=" << roadPos.roadId << " pos=" << roadPos.pos
02066             << " laneId=" << (int)roadPos.laneId << std::endl;
02067             break;
02068         default:
02069             answerLog << "#Error: received unknown position format" << std::endl;
02070             return false;
02071         }
02072         // read requested position type
02073         reqPosType = inMsg.readUnsignedByte();
02074         if (reqPosType != posType) {
02075             answerLog << "#Warning: requested position type (" << reqPosType
02076             << ") and received position type (" << posType << ") do not match" << std::endl;
02077         }
02078         // check command length
02079         if ((cmdStart + cmdLength) != inMsg.position()) {
02080             answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
02081             return false;
02082         }
02083     } catch (std::invalid_argument e) {
02084         answerLog << "#Error while reading message:" << e.what() << std::endl;
02085         return false;
02086     }
02087 
02088     return true;
02089 }
02090 
02091 
02092 bool
02093 TraCITestClient::validateDistanceRequest(tcpip::Storage& inMsg) {
02094     int cmdId;
02095     int cmdLength;
02096     int flag;
02097     int cmdStart;
02098     float distance;
02099     /*testclient::PositionRoadMap roadPos;
02100     testclient::Position2D pos2D;
02101     testclient::Position3D pos3D;*/
02102 
02103     try {
02104         cmdStart = inMsg.position();
02105         cmdLength = inMsg.readUnsignedByte();
02106         // read command id
02107         cmdId = inMsg.readUnsignedByte();
02108         if (cmdId != CMD_DISTANCEREQUEST) {
02109             answerLog << "#Error: received response with command id: " << cmdId
02110             << "but expected: " << (int)CMD_DISTANCEREQUEST << std::endl;
02111             return false;
02112         }
02113         answerLog << ".. Received Response <DistanceRequest>:" << std::endl;
02114         // read flag
02115         flag = inMsg.readUnsignedByte();
02116         answerLog << " flag=" << flag;
02117         // read computed distance
02118         distance = inMsg.readFloat();
02119         answerLog << " distance=" << distance << std::endl;
02121         //posType = inMsg.readUnsignedByte();
02122         //switch (posType) {
02123         //case POSITION_2D:
02124         //  pos2D.x = inMsg.readFloat();
02125         //  pos2D.y = inMsg.readFloat();
02126         //  answerLog << "2D-Position: x=" << pos2D.x << " y=" << pos2D.y;
02127         //  break;
02128         //case POSITION_3D:
02129         //case POSITION_2_5D:
02130         //  if (posType == POSITION_2_5D) {
02131         //      answerLog << "2.5D-Position: ";
02132         //  } else {
02133         //      answerLog << "3D-Position: ";
02134         //  }
02135         //  pos3D.x = inMsg.readFloat();
02136         //  pos3D.y = inMsg.readFloat();
02137         //  pos3D.z = inMsg.readFloat();
02138         //  answerLog << "x=" << pos3D.x << " y=" << pos3D.y << " z=" << pos3D.z;
02139         //  break;
02140         //case POSITION_ROADMAP:
02141         //  roadPos.roadId = inMsg.readString();
02142         //  roadPos.pos = inMsg.readFloat();
02143         //  roadPos.laneId = inMsg.readUnsignedByte();
02144         //  answerLog << "RoadMap-Position: roadId=" << roadPos.roadId << " pos=" << roadPos.pos
02145         //      << " laneId=" << (int)roadPos.laneId;
02146         //  break;
02147         //default:
02148         //  answerLog << "#Error: received unknown position format: " << posType << std::endl;
02149         //  return false;
02150         //}
02151         // check command length
02152         if ((cmdStart + cmdLength) != inMsg.position()) {
02153             answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
02154             return false;
02155         }
02156     } catch (std::invalid_argument e) {
02157         answerLog << "#Error while reading message:" << e.what() << std::endl;
02158         return false;
02159     }
02160 
02161     return true;
02162 }
02163 
02164 
02165 bool
02166 TraCITestClient::validateScenario(tcpip::Storage &inMsg) {
02167     int cmdId;
02168     int cmdLength;
02169     int flag;
02170     int domain;
02171     int domainId;
02172     int variable;
02173     int valueDataType;
02174     int cmdStart;
02175     testclient::PositionRoadMap rRoadPos;
02176 
02177     try {
02178         cmdStart = inMsg.position();
02179         cmdLength = inMsg.readUnsignedByte();
02180         // read command id
02181         cmdId = inMsg.readUnsignedByte();
02182         if (cmdId != CMD_SCENARIO) {
02183             answerLog << "#Error: received response with command id: " << cmdId
02184             << "but expected: " << (int)CMD_SCENARIO << std::endl;
02185             return false;
02186         }
02187         answerLog << ".. Received Response <Scenario>:" << std::endl;
02188         // read flag
02189         flag = inMsg.readUnsignedByte();
02190         answerLog << "  flag=" << flag << " ";
02191         // read domain
02192         domain = inMsg.readUnsignedByte();
02193         answerLog << " domain=" << domain;
02194         // read domain id
02195         domainId = inMsg.readInt();
02196         answerLog << " domainId=" << domainId;
02197         // read variable
02198         variable = inMsg.readUnsignedByte();
02199         answerLog << " variable=" << variable;
02200         // value data type
02201         valueDataType = inMsg.readUnsignedByte();
02202         answerLog << " valueDataType=" << valueDataType;
02203         // read value
02204         if (!readAndReportTypeDependent(inMsg, valueDataType)) {
02205             return false;
02206         }
02207         // check command length
02208         if ((cmdStart + cmdLength) != inMsg.position()) {
02209             answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
02210             return false;
02211         }
02212     } catch (std::invalid_argument e) {
02213         answerLog << "#Error while reading message:" << e.what() << std::endl;
02214         return false;
02215     }
02216     return true;
02217 }
02218 
02219 bool
02220 TraCITestClient::readAndReportTypeDependent(tcpip::Storage &inMsg, int valueDataType) {
02221     if (valueDataType == TYPE_UBYTE) {
02222         int ubyte = inMsg.readUnsignedByte();
02223         answerLog << " Unsigned Byte Value: " << ubyte << std::endl;
02224     } else if (valueDataType == TYPE_BYTE) {
02225         int byte = inMsg.readByte();
02226         answerLog << " Byte value: " << byte << std::endl;
02227     } else if (valueDataType == TYPE_INTEGER) {
02228         int integer = inMsg.readInt();
02229         answerLog << " Int value: " << integer << std::endl;
02230     } else if (valueDataType == TYPE_FLOAT) {
02231         float floatv = inMsg.readFloat();
02232         if (floatv<0.1&&floatv>0) {
02233             answerLog.setf(std::ios::scientific, std::ios::floatfield);
02234         }
02235         answerLog << " float value: " << floatv << std::endl;
02236         answerLog.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
02237         answerLog.setf(std::ios::showpoint); // print decimal point
02238         answerLog << std::setprecision(2);
02239     } else if (valueDataType == TYPE_DOUBLE) {
02240         double doublev = inMsg.readDouble();
02241         answerLog << " Double value: " << doublev << std::endl;
02242     } else if (valueDataType == TYPE_BOUNDINGBOX) {
02243         testclient::BoundingBox box;
02244         box.lowerLeft.x = inMsg.readFloat();
02245         box.lowerLeft.y = inMsg.readFloat();
02246         box.upperRight.x = inMsg.readFloat();
02247         box.upperRight.y = inMsg.readFloat();
02248         answerLog << " BoundaryBoxValue: lowerLeft x="<< box.lowerLeft.x
02249         << " y=" << box.lowerLeft.y << " upperRight x=" << box.upperRight.x
02250         << " y=" << box.upperRight.y << std::endl;
02251     } else if (valueDataType == TYPE_POLYGON) {
02252         int length = inMsg.readUnsignedByte();
02253         answerLog << " PolygonValue: ";
02254         for (int i=0; i < length; i++) {
02255             float x = inMsg.readFloat();
02256             float y = inMsg.readFloat();
02257             answerLog << "(" << x << "," << y << ") ";
02258         }
02259         answerLog << std::endl;
02260     } else if (valueDataType == POSITION_3D) {
02261         float x = inMsg.readFloat();
02262         float y = inMsg.readFloat();
02263         float z = inMsg.readFloat();
02264         answerLog << " Position3DValue: " << std::endl;
02265         answerLog << " x: " << x << " y: " << y
02266         << " z: " << z << std::endl;
02267     } else if (valueDataType == POSITION_ROADMAP) {
02268         std::string roadId = inMsg.readString();
02269         float pos = inMsg.readFloat();
02270         int laneId = inMsg.readUnsignedByte();
02271         answerLog << " RoadMapPositionValue: roadId=" << roadId
02272         << " pos=" << pos
02273         << " laneId=" << laneId << std::endl;
02274     } else if (valueDataType == TYPE_TLPHASELIST) {
02275         int length = inMsg.readUnsignedByte();
02276         answerLog << " TLPhaseListValue: length=" << length << std::endl;
02277         for (int i=0; i< length; i++) {
02278             std::string pred = inMsg.readString();
02279             std::string succ = inMsg.readString();
02280             int phase = inMsg.readUnsignedByte();
02281             answerLog << " precRoad=" << pred << " succRoad=" << succ
02282             << " phase=";
02283             switch (phase) {
02284             case TLPHASE_RED:
02285                 answerLog << "red" << std::endl;
02286                 break;
02287             case TLPHASE_YELLOW:
02288                 answerLog << "yellow" << std::endl;
02289                 break;
02290             case TLPHASE_GREEN:
02291                 answerLog << "green" << std::endl;
02292                 break;
02293             default:
02294                 answerLog << "#Error: unknown phase value" << (int)phase << std::endl;
02295                 return false;
02296             }
02297         }
02298     } else if (valueDataType == TYPE_STRING) {
02299         std::string s = inMsg.readString();
02300         answerLog << " string value: " << s << std::endl;
02301     } else if (valueDataType == TYPE_STRINGLIST) {
02302         std::vector<std::string> s = inMsg.readStringList();
02303         answerLog << " string list value: [ " << std::endl;
02304         for (std::vector<std::string>::iterator i=s.begin(); i!=s.end(); ++i) {
02305             if (i!=s.begin()) {
02306                 answerLog << ", ";
02307             }
02308             answerLog << '"' << *i << '"';
02309         }
02310         answerLog << " ]" << std::endl;
02311     } else if (valueDataType == TYPE_COMPOUND) {
02312         int no = inMsg.readInt();
02313         answerLog << " compound value with " << no << " members: [ " << std::endl;
02314         for (int i=0; i<no; ++i) {
02315             int currentValueDataType = inMsg.readUnsignedByte();
02316             answerLog << " valueDataType=" << currentValueDataType;
02317             readAndReportTypeDependent(inMsg, currentValueDataType);
02318         }
02319         answerLog << " ]" << std::endl;
02320     } else if (valueDataType == TYPE_POSITION2D) {
02321         float xv = inMsg.readFloat();
02322         float yv = inMsg.readFloat();
02323         answerLog << " position value: (" << xv << "," << yv << ")" << std::endl;
02324     } else if (valueDataType == TYPE_COLOR) {
02325         int r = inMsg.readUnsignedByte();
02326         int g = inMsg.readUnsignedByte();
02327         int b = inMsg.readUnsignedByte();
02328         int a = inMsg.readUnsignedByte();
02329         answerLog << " color value: (" << r << "," << g << "," << b << "," << a << ")" << std::endl;
02330     } else {
02331         answerLog << "#Error: unknown valueDataType!" << std::endl;
02332         return false;
02333     }
02334     return true;
02335 }
02336 
02337 
02338 bool
02339 TraCITestClient::validateGetTLStatus(tcpip::Storage &inMsg) {
02340     int cmdId;
02341     int cmdLength;
02342     int cmdStart;
02343     std::string precEdge;
02344     std::string succEdge;
02345     float posOnEdge;
02346     int newPhase;
02347 
02348     while (inMsg.valid_pos()) {
02349         try {
02350             cmdStart = inMsg.position();
02351             cmdLength = inMsg.readUnsignedByte();
02352             // read command id
02353             cmdId = inMsg.readUnsignedByte();
02354             if (cmdId != CMD_TLSWITCH) {
02355                 answerLog << "#Error: received response with command id: " << cmdId
02356                 << "but expected: " << (int)CMD_TLSWITCH << std::endl;
02357                 return false;
02358             }
02359             answerLog << ".. Received Response <TrafficLightSwitch>:" << std::endl;
02360             // read switch time
02361             SUMOTime switchTime = inMsg.readInt();
02362             answerLog << "  SwitchTime=" << time2string(switchTime);
02363             // read preceeding edge id
02364             precEdge = inMsg.readString();
02365             answerLog << " PrecEdge=" << precEdge;
02366             // read position on preceeding edge
02367             posOnEdge = inMsg.readFloat();
02368             answerLog << " PosOnPrecEdge=" << posOnEdge;
02369             // read succeeding edge id
02370             succEdge = inMsg.readString();
02371             answerLog << " SuccEdge=" << succEdge;
02372             // read new phase
02373             newPhase = inMsg.readUnsignedByte();
02374             answerLog << " NewPhase=" << newPhase;
02375             // read yellow time
02376             SUMOTime yellowTime = inMsg.readInt();
02377             answerLog << " YellowTime=" << time2string(yellowTime) << std::endl;
02378             // check command length
02379             if ((cmdStart + cmdLength) != inMsg.position()) {
02380                 answerLog << "#Error: command at position " << cmdStart << " has wrong length" << std::endl;
02381                 return false;
02382             }
02383         } catch (std::invalid_argument e) {
02384             answerLog << "#Error while reading message:" << e.what() << std::endl;
02385             return false;
02386         }
02387     }
02388 
02389     return true;
02390 }

Generated on Wed May 5 00:06:37 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6