Eclipse SUMO - Simulation of Urban MObility
|
C++ TraCI client API implementation. More...
#include <Connection.h>
Public Member Functions | |
void | addFilter (int var, tcpip::Storage *add=nullptr) |
void | close () |
ends the simulation and closes the connection | |
tcpip::Storage & | doCommand (int command, int var=-1, const std::string &id="", tcpip::Storage *add=nullptr, int expectedType=-1) |
libsumo::ContextSubscriptionResults & | getAllContextSubscriptionResults (const int domain) |
libsumo::SubscriptionResults & | getAllSubscriptionResults (const int domain) |
const std::string & | getLabel () const |
std::mutex & | getMutex () const |
void | readContextSubscription (int responseID, tcpip::Storage &inMsg) |
void | readVariables (tcpip::Storage &inMsg, const std::string &objectID, int variableCount, libsumo::SubscriptionResults &into) |
void | readVariableSubscription (int responseID, tcpip::Storage &inMsg) |
Command sending methods | |
void | simulationStep (double time) |
Sends a SimulationStep command. | |
void | setOrder (int order) |
Sends a SetOrder command. | |
void | createCommand (int cmdID, int varID, const std::string *const objID, tcpip::Storage *add=nullptr) const |
Sends a GetVariable / SetVariable request if mySocket is connected. Otherwise writes to myOutput only. | |
void | subscribe (int domID, const std::string &objID, double beginTime, double endTime, int domain, double range, const std::vector< int > &vars, const libsumo::TraCIResults ¶ms) |
Sends a SubscribeContext or a SubscribeVariable request. | |
Static Public Member Functions | |
static void | connect (const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe) |
static Connection & | getActive () |
static bool | isActive () |
static void | switchCon (const std::string &label) |
Private Member Functions | |
int | check_commandGetResult (tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const |
Validates the result state of a command. | |
void | check_resultState (tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0) |
Validates the result state of a command. | |
Connection (const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe) | |
Constructor, connects to the specified SUMO server. | |
Connection & | operator= (const Connection &) |
Invalidated assignment operator. | |
void | readOutput () |
template<typename T > | |
std::string | toHex (const T i, std::streamsize numDigits=2) |
Static Private Member Functions | |
template<class T > | |
static std::string | toString (const T &t, std::streamsize accuracy=PRECISION) |
Private Attributes | |
std::map< int, libsumo::ContextSubscriptionResults > | myContextSubscriptionResults |
tcpip::Storage | myInput |
The reusable input storage. | |
const std::string | myLabel |
std::mutex | myMutex |
tcpip::Storage | myOutput |
The reusable output storage. | |
FILE *const | myProcessPipe |
std::thread * | myProcessReader |
tcpip::Socket | mySocket |
The socket. | |
std::map< int, libsumo::SubscriptionResults > | mySubscriptionResults |
Static Private Attributes | |
static Connection * | myActive = nullptr |
static std::map< const std::string, Connection * > | myConnections |
C++ TraCI client API implementation.
Definition at line 51 of file Connection.h.
|
private |
Constructor, connects to the specified SUMO server.
[in] | host | The name of the host to connect to |
[in] | port | The port to connect to |
tcpip::SocketException | if the connection fails |
Definition at line 44 of file Connection.cpp.
References tcpip::Socket::close(), close(), tcpip::Socket::connect(), myProcessReader, mySocket, readOutput(), and toString().
void libtraci::Connection::addFilter | ( | int | var, |
tcpip::Storage * | add = nullptr |
||
) |
Definition at line 342 of file Connection.cpp.
References check_resultState(), libsumo::CMD_ADD_SUBSCRIPTION_FILTER, createCommand(), myInput, myMutex, myOutput, mySocket, tcpip::Storage::reset(), and tcpip::Socket::sendExact().
|
private |
Validates the result state of a command.
Definition at line 306 of file Connection.cpp.
References tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and toString().
Referenced by doCommand(), simulationStep(), and subscribe().
|
private |
Validates the result state of a command.
[in] | inMsg | The buffer to read the message from |
[in] | command | The original command id |
[in] | ignoreCommandId | Whether the returning command id shall be validated |
[in] | acknowledgement | Pointer to an existing string into which the acknowledgement message shall be inserted |
Definition at line 267 of file Connection.cpp.
References mySocket, tcpip::Storage::position(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), tcpip::Socket::receiveExact(), libsumo::RTYPE_ERR, libsumo::RTYPE_NOTIMPLEMENTED, libsumo::RTYPE_OK, and toHex().
Referenced by addFilter(), close(), doCommand(), setOrder(), simulationStep(), and subscribe().
void libtraci::Connection::close | ( | ) |
ends the simulation and closes the connection
Definition at line 89 of file Connection.cpp.
References check_resultState(), tcpip::Socket::close(), libsumo::CMD_CLOSE, tcpip::Socket::has_client_connection(), myActive, myConnections, myLabel, myMutex, myProcessPipe, myProcessReader, mySocket, tcpip::Socket::sendExact(), and tcpip::Storage::writeUnsignedByte().
Referenced by Connection().
|
inlinestatic |
Definition at line 53 of file Connection.h.
References myConnections.
void libtraci::Connection::createCommand | ( | int | cmdID, |
int | varID, | ||
const std::string *const | objID, | ||
tcpip::Storage * | add = nullptr |
||
) | const |
Sends a GetVariable / SetVariable request if mySocket is connected. Otherwise writes to myOutput only.
[in] | cmdID | The command and domain of the variable |
[in] | varID | The variable to retrieve |
[in] | objID | The object to retrieve the variable from |
[in] | add | Optional additional parameter |
Definition at line 167 of file Connection.cpp.
References tcpip::Socket::has_client_connection(), myOutput, mySocket, tcpip::Storage::reset(), tcpip::Storage::size(), tcpip::Storage::writeInt(), tcpip::Storage::writeStorage(), tcpip::Storage::writeString(), and tcpip::Storage::writeUnsignedByte().
Referenced by addFilter(), and doCommand().
tcpip::Storage & libtraci::Connection::doCommand | ( | int | command, |
int | var = -1 , |
||
const std::string & | id = "" , |
||
tcpip::Storage * | add = nullptr , |
||
int | expectedType = -1 |
||
) |
Definition at line 329 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), createCommand(), myInput, myOutput, mySocket, tcpip::Storage::reset(), and tcpip::Socket::sendExact().
Referenced by libtraci::Domain< GET, SET >::get(), and libtraci::Domain< GET, SET >::set().
|
inlinestatic |
Definition at line 57 of file Connection.h.
References myActive.
Referenced by libtraci::Domain< GET, SET >::get(), libtraci::Domain< GET, SET >::getByte(), libtraci::Domain< GET, SET >::getCol(), libtraci::Domain< GET, SET >::getDouble(), libtraci::Domain< GET, SET >::getDoubleVector(), libtraci::Domain< GET, SET >::getInt(), libtraci::Domain< GET, SET >::getPolygon(), libtraci::Domain< GET, SET >::getPos(), libtraci::Domain< GET, SET >::getPos3D(), libtraci::Domain< GET, SET >::getString(), libtraci::Domain< GET, SET >::getStringVector(), libtraci::Domain< GET, SET >::getTraCIStage(), libtraci::Domain< GET, SET >::getUnsignedByte(), and libtraci::Domain< GET, SET >::set().
|
inline |
Definition at line 87 of file Connection.h.
References myContextSubscriptionResults.
|
inline |
Definition at line 83 of file Connection.h.
References mySubscriptionResults.
|
inline |
Definition at line 72 of file Connection.h.
References myLabel.
|
inline |
Definition at line 76 of file Connection.h.
References myMutex.
Referenced by libtraci::Domain< GET, SET >::getByte(), libtraci::Domain< GET, SET >::getCol(), libtraci::Domain< GET, SET >::getDouble(), libtraci::Domain< GET, SET >::getDoubleVector(), libtraci::Domain< GET, SET >::getInt(), libtraci::Domain< GET, SET >::getPolygon(), libtraci::Domain< GET, SET >::getPos(), libtraci::Domain< GET, SET >::getPos3D(), libtraci::Domain< GET, SET >::getString(), libtraci::Domain< GET, SET >::getStringVector(), libtraci::Domain< GET, SET >::getTraCIStage(), libtraci::Domain< GET, SET >::getUnsignedByte(), and libtraci::Domain< GET, SET >::set().
|
inlinestatic |
Definition at line 64 of file Connection.h.
References myActive.
|
private |
Invalidated assignment operator.
void libtraci::Connection::readContextSubscription | ( | int | responseID, |
tcpip::Storage & | inMsg | ||
) |
Definition at line 447 of file Connection.cpp.
References myContextSubscriptionResults, tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and readVariables().
Referenced by simulationStep(), and subscribe().
|
private |
Definition at line 68 of file Connection.cpp.
References myProcessPipe.
Referenced by Connection().
void libtraci::Connection::readVariables | ( | tcpip::Storage & | inMsg, |
const std::string & | objectID, | ||
int | variableCount, | ||
libsumo::SubscriptionResults & | into | ||
) |
Definition at line 352 of file Connection.cpp.
References libsumo::POSITION_2D, libsumo::POSITION_3D, tcpip::Storage::readDouble(), tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), libsumo::RTYPE_OK, toString(), libsumo::TYPE_COLOR, libsumo::TYPE_COMPOUND, libsumo::TYPE_DOUBLE, libsumo::TYPE_INTEGER, libsumo::TYPE_STRING, and libsumo::TYPE_STRINGLIST.
Referenced by readContextSubscription(), and readVariableSubscription().
void libtraci::Connection::readVariableSubscription | ( | int | responseID, |
tcpip::Storage & | inMsg | ||
) |
Definition at line 439 of file Connection.cpp.
References mySubscriptionResults, tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and readVariables().
Referenced by simulationStep(), and subscribe().
void libtraci::Connection::setOrder | ( | int | order | ) |
Sends a SetOrder command.
Definition at line 150 of file Connection.cpp.
References check_resultState(), libsumo::CMD_SETORDER, myMutex, mySocket, tcpip::Socket::sendExact(), tcpip::Storage::writeInt(), and tcpip::Storage::writeUnsignedByte().
void libtraci::Connection::simulationStep | ( | double | time | ) |
Sends a SimulationStep command.
Definition at line 121 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), libsumo::CMD_SIMSTEP, myContextSubscriptionResults, myMutex, mySocket, mySubscriptionResults, readContextSubscription(), tcpip::Storage::readInt(), readVariableSubscription(), libsumo::RESPONSE_SUBSCRIBE_BUSSTOP_VARIABLE, libsumo::RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE, libsumo::RESPONSE_SUBSCRIBE_OVERHEADWIRE_VARIABLE, libsumo::RESPONSE_SUBSCRIBE_PARKINGAREA_VARIABLE, tcpip::Socket::sendExact(), tcpip::Storage::writeDouble(), and tcpip::Storage::writeUnsignedByte().
void libtraci::Connection::subscribe | ( | int | domID, |
const std::string & | objID, | ||
double | beginTime, | ||
double | endTime, | ||
int | domain, | ||
double | range, | ||
const std::vector< int > & | vars, | ||
const libsumo::TraCIResults & | params | ||
) |
Sends a SubscribeContext or a SubscribeVariable request.
[in] | domID | The domain of the variable |
[in] | objID | The object to subscribe the variables from |
[in] | beginTime | The begin time step of subscriptions |
[in] | endTime | The end time step of subscriptions |
[in] | domain | The domain of the objects which values shall be returned (-1 means variable subscription) |
[in] | range | The range around the obj to investigate (only meaningful for context subscription) |
[in] | vars | The variables to subscribe |
[in] | params | map of variable ids to parameters if needed |
Definition at line 204 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), libsumo::CMD_SUBSCRIBE_EDGE_VARIABLE, libsumo::CMD_SUBSCRIBE_INDUCTIONLOOP_VARIABLE, libsumo::CMD_SUBSCRIBE_LANE_VARIABLE, libsumo::CMD_SUBSCRIBE_LANEAREA_VARIABLE, libsumo::CMD_SUBSCRIBE_MULTIENTRYEXIT_VARIABLE, libsumo::CMD_SUBSCRIBE_VEHICLE_VARIABLE, tcpip::Socket::has_client_connection(), libsumo::LAST_STEP_VEHICLE_NUMBER, myMutex, mySocket, readContextSubscription(), readVariableSubscription(), tcpip::Socket::sendExact(), tcpip::Storage::size(), libsumo::StorageHelper::toStorage(), libsumo::TRACI_ID_LIST, libsumo::VAR_LANEPOSITION, libsumo::VAR_ROAD_ID, tcpip::Storage::writeDouble(), tcpip::Storage::writeInt(), tcpip::Storage::writeStorage(), tcpip::Storage::writeString(), and tcpip::Storage::writeUnsignedByte().
|
inlinestatic |
Definition at line 68 of file Connection.h.
References myActive, and myConnections.
|
inlineprivate |
Definition at line 159 of file Connection.h.
Referenced by check_resultState().
|
inlinestaticprivate |
Definition at line 150 of file Connection.h.
Referenced by check_commandGetResult(), Connection(), and readVariables().
|
staticprivate |
Definition at line 191 of file Connection.h.
Referenced by close(), getActive(), isActive(), and switchCon().
|
staticprivate |
Definition at line 192 of file Connection.h.
Referenced by close(), connect(), and switchCon().
|
private |
Definition at line 189 of file Connection.h.
Referenced by getAllContextSubscriptionResults(), readContextSubscription(), and simulationStep().
|
mutableprivate |
The reusable input storage.
Definition at line 184 of file Connection.h.
Referenced by addFilter(), and doCommand().
|
private |
Definition at line 176 of file Connection.h.
Referenced by close(), and getLabel().
|
mutableprivate |
Definition at line 186 of file Connection.h.
Referenced by addFilter(), close(), getMutex(), setOrder(), simulationStep(), and subscribe().
|
mutableprivate |
The reusable output storage.
Definition at line 182 of file Connection.h.
Referenced by addFilter(), createCommand(), and doCommand().
|
private |
Definition at line 177 of file Connection.h.
Referenced by close(), and readOutput().
|
private |
Definition at line 178 of file Connection.h.
Referenced by close(), and Connection().
|
private |
The socket.
Definition at line 180 of file Connection.h.
Referenced by addFilter(), check_resultState(), close(), Connection(), createCommand(), doCommand(), setOrder(), simulationStep(), and subscribe().
|
private |
Definition at line 188 of file Connection.h.
Referenced by getAllSubscriptionResults(), readVariableSubscription(), and simulationStep().