Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libtraci::Connection Class Reference

C++ TraCI client API implementation. More...

#include <Connection.h>

Collaboration diagram for libtraci::Connection:
[legend]

Public Member Functions

void addFilter (int var, tcpip::Storage *add=nullptr)
void close ()
 ends the simulation and closes the connection
tcpip::StoragedoCommand (int command, int var=-1, const std::string &id="", tcpip::Storage *add=nullptr, int expectedType=-1)
libsumo::ContextSubscriptionResultsgetAllContextSubscriptionResults (const int domain)
libsumo::SubscriptionResultsgetAllSubscriptionResults (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 &params)
 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 ConnectiongetActive ()
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.
Connectionoperator= (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::ContextSubscriptionResultsmyContextSubscriptionResults
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::SubscriptionResultsmySubscriptionResults

Static Private Attributes

static ConnectionmyActive = nullptr
static std::map< const std::string, Connection * > myConnections

Detailed Description

C++ TraCI client API implementation.

Definition at line 51 of file Connection.h.

Constructor & Destructor Documentation

◆ Connection()

libtraci::Connection::Connection ( const std::string & host,
int port,
int numRetries,
const std::string & label,
FILE *const pipe )
private

Constructor, connects to the specified SUMO server.

Parameters
[in]hostThe name of the host to connect to
[in]portThe port to connect to
Exceptions
tcpip::SocketExceptionif the connection fails

Definition at line 44 of file Connection.cpp.

References close(), myLabel, myProcessPipe, myProcessReader, mySocket, readOutput(), and toString().

Referenced by connect(), getActive(), and operator=().

Here is the caller graph for this function:

Member Function Documentation

◆ addFilter()

void libtraci::Connection::addFilter ( int var,
tcpip::Storage * add = nullptr )

◆ check_commandGetResult()

int libtraci::Connection::check_commandGetResult ( tcpip::Storage & inMsg,
int command,
int expectedType = -1,
bool ignoreCommandId = false ) const
private

Validates the result state of a command.

Returns
The command Id

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().

Here is the caller graph for this function:

◆ check_resultState()

void libtraci::Connection::check_resultState ( tcpip::Storage & inMsg,
int command,
bool ignoreCommandId = false,
std::string * acknowledgement = 0 )
private

Validates the result state of a command.

Parameters
[in]inMsgThe buffer to read the message from
[in]commandThe original command id
[in]ignoreCommandIdWhether the returning command id shall be validated
[in]acknowledgementPointer 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(), libsumo::RTYPE_ERR, libsumo::RTYPE_NOTIMPLEMENTED, libsumo::RTYPE_OK, and toHex().

Referenced by addFilter(), close(), doCommand(), setOrder(), simulationStep(), and subscribe().

Here is the caller graph for this function:

◆ close()

void libtraci::Connection::close ( )

ends the simulation and closes the connection

Definition at line 89 of file Connection.cpp.

References check_resultState(), libsumo::CMD_CLOSE, myActive, myConnections, myLabel, myMutex, myProcessPipe, myProcessReader, mySocket, and tcpip::Storage::writeUnsignedByte().

Referenced by Connection().

Here is the caller graph for this function:

◆ connect()

void libtraci::Connection::connect ( const std::string & host,
int port,
int numRetries,
const std::string & label,
FILE *const pipe )
inlinestatic

Definition at line 53 of file Connection.h.

References Connection(), and myConnections.

◆ createCommand()

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.

Parameters
[in]cmdIDThe command and domain of the variable
[in]varIDThe variable to retrieve
[in]objIDThe object to retrieve the variable from
[in]addOptional additional parameter

Definition at line 167 of file Connection.cpp.

References myOutput, mySocket, and tcpip::Storage::size().

Referenced by addFilter(), and doCommand().

Here is the caller graph for this function:

◆ 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, and mySocket.

Referenced by libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::get(), and libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::set().

Here is the caller graph for this function:

◆ getActive()

Connection & libtraci::Connection::getActive ( )
inlinestatic

Definition at line 57 of file Connection.h.

References Connection(), and myActive.

Referenced by libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::get(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getByte(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getCol(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getDouble(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getDoubleVector(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getInt(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getPolygon(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getPos(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getPos3D(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getString(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getStringVector(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getTraCIStage(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getUnsignedByte(), and libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::set().

Here is the caller graph for this function:

◆ getAllContextSubscriptionResults()

libsumo::ContextSubscriptionResults & libtraci::Connection::getAllContextSubscriptionResults ( const int domain)
inline

Definition at line 87 of file Connection.h.

References myContextSubscriptionResults.

◆ getAllSubscriptionResults()

libsumo::SubscriptionResults & libtraci::Connection::getAllSubscriptionResults ( const int domain)
inline

Definition at line 83 of file Connection.h.

References mySubscriptionResults.

◆ getLabel()

const std::string & libtraci::Connection::getLabel ( ) const
inline

Definition at line 72 of file Connection.h.

References myLabel.

◆ getMutex()

std::mutex & libtraci::Connection::getMutex ( ) const
inline

Definition at line 76 of file Connection.h.

References myMutex.

Referenced by libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getByte(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getCol(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getDouble(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getDoubleVector(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getInt(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getPolygon(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getPos(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getPos3D(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getString(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getStringVector(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getTraCIStage(), libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::getUnsignedByte(), and libtraci::Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE >::set().

Here is the caller graph for this function:

◆ isActive()

bool libtraci::Connection::isActive ( )
inlinestatic

Definition at line 64 of file Connection.h.

References myActive.

◆ operator=()

Connection & libtraci::Connection::operator= ( const Connection & )
private

Invalidated assignment operator.

References Connection().

◆ readContextSubscription()

void libtraci::Connection::readContextSubscription ( int responseID,
tcpip::Storage & inMsg )

Definition at line 550 of file Connection.cpp.

References myContextSubscriptionResults, tcpip::Storage::readInt(), tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and readVariables().

Referenced by simulationStep(), and subscribe().

Here is the caller graph for this function:

◆ readOutput()

void libtraci::Connection::readOutput ( )
private

Definition at line 68 of file Connection.cpp.

References myProcessPipe.

Referenced by Connection().

Here is the caller graph for this function:

◆ readVariables()

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::CMD_CHANGELANE, FALLTHROUGH, libsumo::LAST_STEP_VEHICLE_DATA, n, libsumo::POSITION_2D, libsumo::POSITION_3D, libsumo::StorageHelper::readBestLanesVector(), libsumo::StorageHelper::readCollisionVector(), libsumo::StorageHelper::readConnection(), libsumo::StorageHelper::readConstraintVector(), tcpip::Storage::readDouble(), tcpip::Storage::readDoubleList(), tcpip::Storage::readInt(), libsumo::StorageHelper::readJunctionFoeVector(), libsumo::StorageHelper::readLinkVectorVector(), libsumo::StorageHelper::readLogic(), libsumo::StorageHelper::readPolygon(), libsumo::StorageHelper::readReservation(), libsumo::StorageHelper::readStage(), libsumo::StorageHelper::readStopVector(), tcpip::Storage::readString(), libsumo::StorageHelper::readTLSDataVector(), libsumo::StorageHelper::readTypedInt(), tcpip::Storage::readUnsignedByte(), libsumo::StorageHelper::readVehicleDataVector(), libsumo::RTYPE_OK, libsumo::TL_COMPLETE_DEFINITION_RYG, libsumo::TL_CONSTRAINT, libsumo::TL_CONSTRAINT_BYFOE, libsumo::TL_CONTROLLED_LINKS, toHex(), libsumo::TYPE_COLOR, libsumo::TYPE_COMPOUND, libsumo::TYPE_DOUBLE, libsumo::TYPE_DOUBLELIST, libsumo::TYPE_INTEGER, libsumo::TYPE_POLYGON, libsumo::TYPE_STRING, libsumo::TYPE_STRINGLIST, libsumo::VAR_BEST_LANES, libsumo::VAR_COLLISIONS, libsumo::VAR_FOES, libsumo::VAR_NEIGHBORS, libsumo::VAR_NEXT_LINKS, libsumo::VAR_NEXT_STOPS2, libsumo::VAR_NEXT_TLS, libsumo::VAR_STAGE, and libsumo::VAR_TAXI_RESERVATIONS.

Referenced by readContextSubscription(), and readVariableSubscription().

Here is the caller graph for this function:

◆ readVariableSubscription()

void libtraci::Connection::readVariableSubscription ( int responseID,
tcpip::Storage & inMsg )

Definition at line 542 of file Connection.cpp.

References mySubscriptionResults, tcpip::Storage::readString(), tcpip::Storage::readUnsignedByte(), and readVariables().

Referenced by simulationStep(), and subscribe().

Here is the caller graph for this function:

◆ setOrder()

void libtraci::Connection::setOrder ( int order)

◆ simulationStep()

◆ subscribe()

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.

Parameters
[in]domIDThe domain of the variable
[in]objIDThe object to subscribe the variables from
[in]beginTimeThe begin time step of subscriptions
[in]endTimeThe end time step of subscriptions
[in]domainThe domain of the objects which values shall be returned (-1 means variable subscription)
[in]rangeThe range around the obj to investigate (only meaningful for context subscription)
[in]varsThe variables to subscribe
[in]paramsmap 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, libsumo::LAST_STEP_VEHICLE_NUMBER, myMutex, mySocket, readContextSubscription(), readVariableSubscription(), 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().

◆ switchCon()

void libtraci::Connection::switchCon ( const std::string & label)
inlinestatic

Definition at line 68 of file Connection.h.

References myActive, and myConnections.

◆ toHex()

template<typename T>
std::string libtraci::Connection::toHex ( const T i,
std::streamsize numDigits = 2 )
inlineprivate

Definition at line 159 of file Connection.h.

Referenced by check_resultState(), and readVariables().

Here is the caller graph for this function:

◆ toString()

template<class T>
std::string libtraci::Connection::toString ( const T & t,
std::streamsize accuracy = PRECISION )
inlinestaticprivate

Definition at line 150 of file Connection.h.

References PRECISION.

Referenced by check_commandGetResult(), and Connection().

Here is the caller graph for this function:

Field Documentation

◆ myActive

Connection * libtraci::Connection::myActive = nullptr
staticprivate

Definition at line 191 of file Connection.h.

Referenced by close(), getActive(), isActive(), and switchCon().

◆ myConnections

std::map< const std::string, Connection * > libtraci::Connection::myConnections
staticprivate

Definition at line 192 of file Connection.h.

Referenced by close(), connect(), and switchCon().

◆ myContextSubscriptionResults

std::map<int, libsumo::ContextSubscriptionResults> libtraci::Connection::myContextSubscriptionResults
private

◆ myInput

tcpip::Storage libtraci::Connection::myInput
mutableprivate

The reusable input storage.

Definition at line 184 of file Connection.h.

Referenced by addFilter(), and doCommand().

◆ myLabel

const std::string libtraci::Connection::myLabel
private

Definition at line 176 of file Connection.h.

Referenced by close(), Connection(), and getLabel().

◆ myMutex

std::mutex libtraci::Connection::myMutex
mutableprivate

Definition at line 186 of file Connection.h.

Referenced by addFilter(), close(), getMutex(), setOrder(), simulationStep(), and subscribe().

◆ myOutput

tcpip::Storage libtraci::Connection::myOutput
mutableprivate

The reusable output storage.

Definition at line 182 of file Connection.h.

Referenced by addFilter(), createCommand(), and doCommand().

◆ myProcessPipe

FILE* const libtraci::Connection::myProcessPipe
private

Definition at line 177 of file Connection.h.

Referenced by close(), Connection(), and readOutput().

◆ myProcessReader

std::thread* libtraci::Connection::myProcessReader
private

Definition at line 178 of file Connection.h.

Referenced by close(), and Connection().

◆ mySocket

tcpip::Socket libtraci::Connection::mySocket
private

◆ mySubscriptionResults

std::map<int, libsumo::SubscriptionResults> libtraci::Connection::mySubscriptionResults
private

The documentation for this class was generated from the following files: