Eclipse SUMO - Simulation of Urban MObility
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 More...
 
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. More...
 
void setOrder (int order)
 Sends a SetOrder command. More...
 
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. More...
 
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. More...
 

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. More...
 
void check_resultState (tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0)
 Validates the result state of a command. More...
 
 Connection (const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
 Constructor, connects to the specified SUMO server. More...
 
Connectionoperator= (const Connection &)
 Invalidated assignment operator. More...
 
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. More...
 
const std::string myLabel
 
std::mutex myMutex
 
tcpip::Storage myOutput
 The reusable output storage. More...
 
FILE *const myProcessPipe
 
std::thread * myProcessReader
 
tcpip::Socket mySocket
 The socket. More...
 
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 tcpip::Socket::close(), close(), tcpip::Socket::connect(), myProcessReader, mySocket, readOutput(), and toString().

Referenced by connect().

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(), tcpip::Socket::receiveExact(), 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(), 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().

Here is the caller graph for this function:

◆ connect()

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

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, mySocket, tcpip::Storage::reset(), and tcpip::Socket::sendExact().

Referenced by libtraci::Domain< GET, SET >::get(), and libtraci::Domain< GET, SET >::set().

Here is the caller graph for this function:

◆ getActive()

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

◆ isActive()

static 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.

◆ readContextSubscription()

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

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 
)

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

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

◆ switchCon()

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

Here is the caller graph for this function:

◆ toString()

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

Definition at line 150 of file Connection.h.

Referenced by check_commandGetResult(), Connection(), and readVariables().

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(), 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(), 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: