![]() |
Eclipse SUMO - Simulation of Urban MObility
|
C++ TraCI client API implementation. More...
#include <Connection.h>
Public Member Functions | |
void | close () |
ends the simulation and closes the connection More... | |
tcpip::Storage & | doCommand (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
libsumo::ContextSubscriptionResults & | getAllContextSubscriptionResults (const int domain) |
libsumo::SubscriptionResults | getAllSubscriptionResults (const int domain) |
int | getByte (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
libsumo::TraCIColor | getCol (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
double | getDouble (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
int | getInt (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
const std::string & | getLabel () |
libsumo::TraCIPositionVector | getPolygon (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
libsumo::TraCIPosition | getPos (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
libsumo::TraCIPosition | getPos3D (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
std::string | getString (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
std::vector< std::string > | getStringVector (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
libsumo::TraCIStage | getTraCIStage (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
int | getUnsignedByte (int command, int var, const std::string &id, tcpip::Storage *add=nullptr) |
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) |
void | setCol (int command, int var, const std::string &id, const libsumo::TraCIColor c) |
void | setDouble (int command, int var, const std::string &id, double value) |
void | setInt (int command, int var, const std::string &id, int value) |
void | setString (int command, int var, const std::string &id, const std::string &value) |
void | setStringVector (int command, int var, const std::string &id, const std::vector< std::string > &value) |
Command sending methods | |
void | simulationStep (double time) |
Sends a SimulationStep command. More... | |
void | send_commandSetOrder (int order) |
Sends a SetOrder command. More... | |
void | createCommand (int cmdID, int varID, const std::string &objID, tcpip::Storage *add=nullptr) const |
Sends a GetVariable / SetVariable request if mySocket is connected. Otherwise writes to myOutput only. More... | |
void | createFilterCommand (int cmdID, int varID, tcpip::Storage *add=nullptr) const |
void | subscribeObjectVariable (int domID, const std::string &objID, double beginTime, double endTime, const std::vector< int > &vars, const libsumo::TraCIResults ¶ms) |
Sends a SubscribeVariable request. More... | |
void | subscribeObjectContext (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 request. More... | |
void | check_resultState (tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0) |
Validates the result state of a command. More... | |
int | check_commandGetResult (tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const |
Validates the result state of a command. More... | |
bool | processGet (int command, int expectedType, bool ignoreCommandId=false) |
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 | |
Connection (const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe) | |
Constructor, connects to the specified SUMO server. More... | |
Connection & | operator= (const Connection &) |
Invalidated assignment operator. More... | |
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. More... | |
const std::string | myLabel |
tcpip::Storage | myOutput |
The reusable output storage. More... | |
FILE *const | myProcessPipe |
tcpip::Socket | mySocket |
The socket. More... | |
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 47 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 45 of file Connection.cpp.
References tcpip::Socket::connect(), and mySocket.
Referenced by connect().
int libtraci::Connection::check_commandGetResult | ( | tcpip::Storage & | inMsg, |
int | command, | ||
int | expectedType = -1 , |
||
bool | ignoreCommandId = false |
||
) | const |
Validates the result state of a command.
Definition at line 329 of file Connection.cpp.
References tcpip::Storage::position(), tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and toString().
Referenced by processGet(), simulationStep(), subscribeObjectContext(), and subscribeObjectVariable().
void libtraci::Connection::check_resultState | ( | tcpip::Storage & | inMsg, |
int | command, | ||
bool | ignoreCommandId = false , |
||
std::string * | acknowledgement = 0 |
||
) |
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 290 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 toString().
Referenced by close(), doCommand(), processGet(), simulationStep(), subscribeObjectContext(), and subscribeObjectVariable().
void libtraci::Connection::close | ( | ) |
ends the simulation and closes the connection
Definition at line 62 of file Connection.cpp.
References check_resultState(), tcpip::Socket::close(), libsumo::CMD_CLOSE, tcpip::Socket::has_client_connection(), myProcessPipe, mySocket, tcpip::Socket::sendExact(), and tcpip::Storage::writeUnsignedByte().
|
inlinestatic |
Definition at line 49 of file Connection.h.
References Connection(), and myConnections.
void libtraci::Connection::createCommand | ( | int | cmdID, |
int | varID, | ||
const std::string & | 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 143 of file Connection.cpp.
References myOutput, tcpip::Storage::reset(), tcpip::Storage::size(), tcpip::Storage::writeInt(), tcpip::Storage::writeStorage(), tcpip::Storage::writeString(), and tcpip::Storage::writeUnsignedByte().
Referenced by doCommand(), getByte(), getCol(), getDouble(), getInt(), getPolygon(), getPos(), getPos3D(), getString(), getStringVector(), getTraCIStage(), and getUnsignedByte().
void libtraci::Connection::createFilterCommand | ( | int | cmdID, |
int | varID, | ||
tcpip::Storage * | add = nullptr |
||
) | const |
Definition at line 172 of file Connection.cpp.
References myOutput, tcpip::Storage::reset(), tcpip::Storage::size(), tcpip::Storage::writeInt(), tcpip::Storage::writeStorage(), and tcpip::Storage::writeUnsignedByte().
tcpip::Storage & libtraci::Connection::doCommand | ( | int | command, |
int | var, | ||
const std::string & | id, | ||
tcpip::Storage * | add = nullptr |
||
) |
Definition at line 366 of file Connection.cpp.
References check_resultState(), createCommand(), tcpip::Socket::has_client_connection(), myInput, myOutput, mySocket, tcpip::Storage::reset(), and tcpip::Socket::sendExact().
Referenced by setCol(), setDouble(), setInt(), setString(), and setStringVector().
|
inlinestatic |
Definition at line 53 of file Connection.h.
References myActive.
Referenced by libtraci::Domain< GET, SET >::getByte(), libtraci::Domain< GET, SET >::getCol(), libtraci::Domain< GET, SET >::getDouble(), 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(), libtraci::Domain< GET, SET >::setCol(), libtraci::Domain< GET, SET >::setDouble(), libtraci::Domain< GET, SET >::setInt(), libtraci::Domain< GET, SET >::setString(), and libtraci::Domain< GET, SET >::setStringVector().
|
inline |
Definition at line 76 of file Connection.h.
References myContextSubscriptionResults.
|
inline |
Definition at line 72 of file Connection.h.
References mySubscriptionResults.
|
inline |
Definition at line 153 of file Connection.h.
References createCommand(), libsumo::INVALID_INT_VALUE, myInput, processGet(), tcpip::Storage::readByte(), and libsumo::TYPE_BYTE.
Referenced by libtraci::Domain< GET, SET >::getByte().
|
inline |
Definition at line 231 of file Connection.h.
References libsumo::TraCIColor::a, libsumo::TraCIColor::b, createCommand(), libsumo::TraCIColor::g, myInput, processGet(), libsumo::TraCIColor::r, tcpip::Storage::readUnsignedByte(), and libsumo::TYPE_COLOR.
Referenced by libtraci::Domain< GET, SET >::getCol().
|
inline |
Definition at line 167 of file Connection.h.
References createCommand(), libsumo::INVALID_DOUBLE_VALUE, myInput, processGet(), tcpip::Storage::readDouble(), and libsumo::TYPE_DOUBLE.
Referenced by libtraci::Domain< GET, SET >::getDouble().
|
inline |
Definition at line 160 of file Connection.h.
References createCommand(), libsumo::INVALID_INT_VALUE, myInput, processGet(), tcpip::Storage::readInt(), and libsumo::TYPE_INTEGER.
Referenced by libtraci::Domain< GET, SET >::getInt().
|
inline |
Definition at line 65 of file Connection.h.
References myLabel.
|
inline |
Definition at line 174 of file Connection.h.
References createCommand(), myInput, processGet(), tcpip::Storage::readDouble(), tcpip::Storage::readInt(), tcpip::Storage::readUnsignedByte(), libsumo::TYPE_POLYGON, libsumo::TraCIPosition::x, libsumo::TraCIPosition::y, and libsumo::TraCIPosition::z.
Referenced by libtraci::Domain< GET, SET >::getPolygon().
|
inline |
Definition at line 192 of file Connection.h.
References createCommand(), myInput, libsumo::POSITION_2D, processGet(), tcpip::Storage::readDouble(), libsumo::TraCIPosition::x, and libsumo::TraCIPosition::y.
Referenced by libtraci::Domain< GET, SET >::getPos().
|
inline |
Definition at line 202 of file Connection.h.
References createCommand(), myInput, libsumo::POSITION_3D, processGet(), tcpip::Storage::readDouble(), libsumo::TraCIPosition::x, libsumo::TraCIPosition::y, and libsumo::TraCIPosition::z.
Referenced by libtraci::Domain< GET, SET >::getPos3D().
|
inline |
Definition at line 212 of file Connection.h.
References createCommand(), myInput, processGet(), tcpip::Storage::readString(), and libsumo::TYPE_STRING.
Referenced by libtraci::Domain< GET, SET >::getString().
|
inline |
Definition at line 219 of file Connection.h.
References createCommand(), myInput, processGet(), tcpip::Storage::readInt(), tcpip::Storage::readString(), and libsumo::TYPE_STRINGLIST.
Referenced by libtraci::Domain< GET, SET >::getStringVector().
|
inline |
Definition at line 243 of file Connection.h.
References libsumo::TraCIStage::arrivalPos, libsumo::TraCIStage::cost, createCommand(), libsumo::TraCIStage::depart, libsumo::TraCIStage::departPos, libsumo::TraCIStage::description, libsumo::TraCIStage::destStop, libsumo::TraCIStage::edges, libsumo::TraCIStage::intended, libsumo::TraCIStage::length, libsumo::TraCIStage::line, myInput, processGet(), tcpip::Storage::readDouble(), tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readStringList(), tcpip::Storage::readUnsignedByte(), libsumo::TraCIStage::travelTime, libsumo::TraCIStage::type, libsumo::TYPE_COMPOUND, and libsumo::TraCIStage::vType.
Referenced by libtraci::Domain< GET, SET >::getTraCIStage().
|
inline |
Definition at line 146 of file Connection.h.
References createCommand(), libsumo::INVALID_INT_VALUE, myInput, processGet(), tcpip::Storage::readUnsignedByte(), and libsumo::TYPE_UBYTE.
Referenced by libtraci::Domain< GET, SET >::getUnsignedByte().
|
inlinestatic |
Definition at line 57 of file Connection.h.
References myActive.
|
private |
Invalidated assignment operator.
bool libtraci::Connection::processGet | ( | int | command, |
int | expectedType, | ||
bool | ignoreCommandId = false |
||
) |
Definition at line 353 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), tcpip::Socket::has_client_connection(), myInput, myOutput, mySocket, tcpip::Storage::reset(), and tcpip::Socket::sendExact().
Referenced by getByte(), getCol(), getDouble(), getInt(), getPolygon(), getPos(), getPos3D(), getString(), getStringVector(), getTraCIStage(), and getUnsignedByte().
void libtraci::Connection::readContextSubscription | ( | int | responseID, |
tcpip::Storage & | inMsg | ||
) |
Definition at line 454 of file Connection.cpp.
References myContextSubscriptionResults, tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and readVariables().
Referenced by simulationStep(), and subscribeObjectContext().
void libtraci::Connection::readVariables | ( | tcpip::Storage & | inMsg, |
const std::string & | objectID, | ||
int | variableCount, | ||
libsumo::SubscriptionResults & | into | ||
) |
Definition at line 378 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_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 446 of file Connection.cpp.
References mySubscriptionResults, tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and readVariables().
Referenced by simulationStep(), and subscribeObjectVariable().
void libtraci::Connection::send_commandSetOrder | ( | int | order | ) |
Sends a SetOrder command.
Definition at line 130 of file Connection.cpp.
References libsumo::CMD_SETORDER, mySocket, tcpip::Socket::sendExact(), tcpip::Storage::writeInt(), and tcpip::Storage::writeUnsignedByte().
|
inline |
Definition at line 319 of file Connection.h.
References libsumo::TraCIColor::a, libsumo::TraCIColor::b, doCommand(), libsumo::TraCIColor::g, libsumo::TraCIColor::r, libsumo::TYPE_COLOR, and tcpip::Storage::writeUnsignedByte().
Referenced by libtraci::Domain< GET, SET >::setCol().
|
inline |
Definition at line 298 of file Connection.h.
References doCommand(), libsumo::TYPE_DOUBLE, tcpip::Storage::writeDouble(), and tcpip::Storage::writeUnsignedByte().
Referenced by libtraci::Domain< GET, SET >::setDouble().
|
inline |
Definition at line 292 of file Connection.h.
References doCommand(), libsumo::TYPE_INTEGER, tcpip::Storage::writeInt(), and tcpip::Storage::writeUnsignedByte().
Referenced by libtraci::Domain< GET, SET >::setInt().
|
inline |
Definition at line 305 of file Connection.h.
References doCommand(), libsumo::TYPE_STRING, tcpip::Storage::writeString(), and tcpip::Storage::writeUnsignedByte().
Referenced by libtraci::Domain< GET, SET >::setString().
|
inline |
Definition at line 312 of file Connection.h.
References doCommand(), libsumo::TYPE_STRINGLIST, tcpip::Storage::writeStringList(), and tcpip::Storage::writeUnsignedByte().
Referenced by libtraci::Domain< GET, SET >::setStringVector().
void libtraci::Connection::simulationStep | ( | double | time | ) |
Sends a SimulationStep command.
Definition at line 101 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), libsumo::CMD_SIMSTEP, myContextSubscriptionResults, 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::subscribeObjectContext | ( | 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 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 |
[in] | range | The range around the obj to investigate |
[in] | vars | The variables to subscribe |
Definition at line 249 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), tcpip::Socket::has_client_connection(), mySocket, readContextSubscription(), tcpip::Socket::sendExact(), tcpip::Storage::size(), tcpip::Storage::writeDouble(), tcpip::Storage::writeInt(), tcpip::Storage::writePacket(), tcpip::Storage::writeString(), and tcpip::Storage::writeUnsignedByte().
void libtraci::Connection::subscribeObjectVariable | ( | int | domID, |
const std::string & | objID, | ||
double | beginTime, | ||
double | endTime, | ||
const std::vector< int > & | vars, | ||
const libsumo::TraCIResults & | params | ||
) |
Sends 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] | vars | The variables to subscribe |
Definition at line 195 of file Connection.cpp.
References check_commandGetResult(), check_resultState(), libsumo::CMD_SUBSCRIBE_INDUCTIONLOOP_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, mySocket, readVariableSubscription(), tcpip::Socket::sendExact(), tcpip::Storage::size(), libsumo::TRACI_ID_LIST, libsumo::VAR_LANEPOSITION, libsumo::VAR_ROAD_ID, tcpip::Storage::writeDouble(), tcpip::Storage::writeInt(), tcpip::Storage::writePacket(), tcpip::Storage::writeString(), and tcpip::Storage::writeUnsignedByte().
|
inlinestatic |
Definition at line 61 of file Connection.h.
References myActive, and myConnections.
|
inlinestaticprivate |
Definition at line 335 of file Connection.h.
Referenced by check_commandGetResult(), check_resultState(), and readVariables().
|
staticprivate |
Definition at line 363 of file Connection.h.
Referenced by getActive(), isActive(), and switchCon().
|
staticprivate |
Definition at line 364 of file Connection.h.
Referenced by connect(), and switchCon().
|
private |
Definition at line 361 of file Connection.h.
Referenced by getAllContextSubscriptionResults(), readContextSubscription(), and simulationStep().
|
mutableprivate |
The reusable input storage.
Definition at line 358 of file Connection.h.
Referenced by doCommand(), getByte(), getCol(), getDouble(), getInt(), getPolygon(), getPos(), getPos3D(), getString(), getStringVector(), getTraCIStage(), getUnsignedByte(), and processGet().
|
private |
Definition at line 351 of file Connection.h.
Referenced by getLabel().
|
mutableprivate |
The reusable output storage.
Definition at line 356 of file Connection.h.
Referenced by createCommand(), createFilterCommand(), doCommand(), and processGet().
|
private |
Definition at line 352 of file Connection.h.
Referenced by close().
|
private |
The socket.
Definition at line 354 of file Connection.h.
Referenced by check_resultState(), close(), Connection(), doCommand(), processGet(), send_commandSetOrder(), simulationStep(), subscribeObjectContext(), and subscribeObjectVariable().
|
private |
Definition at line 360 of file Connection.h.
Referenced by getAllSubscriptionResults(), readVariableSubscription(), and simulationStep().