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 close ()
 ends the simulation and closes the connection More...
 
tcpip::StoragedoCommand (int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
 
libsumo::ContextSubscriptionResultsgetAllContextSubscriptionResults (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 &params)
 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 &params)
 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 ConnectiongetActive ()
 
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...
 
Connectionoperator= (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::ContextSubscriptionResultsmyContextSubscriptionResults
 
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::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 47 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 45 of file Connection.cpp.

References tcpip::Socket::connect(), and mySocket.

Referenced by connect().

Here is the caller graph for this function:

Member Function Documentation

◆ check_commandGetResult()

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

Validates the result state of a command.

Returns
The command Id

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

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 
)

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

Here is the caller graph for this function:

◆ close()

void libtraci::Connection::close ( )

◆ 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 49 of file Connection.h.

References Connection(), and myConnections.

◆ createCommand()

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.

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

Here is the caller graph for this function:

◆ createFilterCommand()

void libtraci::Connection::createFilterCommand ( int  cmdID,
int  varID,
tcpip::Storage add = nullptr 
) const

◆ doCommand()

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

Here is the caller graph for this function:

◆ getActive()

◆ getAllContextSubscriptionResults()

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

Definition at line 76 of file Connection.h.

References myContextSubscriptionResults.

◆ getAllSubscriptionResults()

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

Definition at line 72 of file Connection.h.

References mySubscriptionResults.

◆ getByte()

int libtraci::Connection::getByte ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getCol()

libsumo::TraCIColor libtraci::Connection::getCol ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getDouble()

double libtraci::Connection::getDouble ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getInt()

int libtraci::Connection::getInt ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getLabel()

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

Definition at line 65 of file Connection.h.

References myLabel.

◆ getPolygon()

libsumo::TraCIPositionVector libtraci::Connection::getPolygon ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getPos()

libsumo::TraCIPosition libtraci::Connection::getPos ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getPos3D()

libsumo::TraCIPosition libtraci::Connection::getPos3D ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getString()

std::string libtraci::Connection::getString ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getStringVector()

std::vector<std::string> libtraci::Connection::getStringVector ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ getTraCIStage()

◆ getUnsignedByte()

int libtraci::Connection::getUnsignedByte ( int  command,
int  var,
const std::string &  id,
tcpip::Storage add = nullptr 
)
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().

Here is the caller graph for this function:

◆ isActive()

static bool libtraci::Connection::isActive ( )
inlinestatic

Definition at line 57 of file Connection.h.

References myActive.

◆ operator=()

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

Invalidated assignment operator.

◆ processGet()

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

Here is the caller graph for this function:

◆ readContextSubscription()

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

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

Here is the caller graph for this function:

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

Here is the caller graph for this function:

◆ send_commandSetOrder()

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

◆ setCol()

void libtraci::Connection::setCol ( int  command,
int  var,
const std::string &  id,
const libsumo::TraCIColor  c 
)
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().

Here is the caller graph for this function:

◆ setDouble()

void libtraci::Connection::setDouble ( int  command,
int  var,
const std::string &  id,
double  value 
)
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().

Here is the caller graph for this function:

◆ setInt()

void libtraci::Connection::setInt ( int  command,
int  var,
const std::string &  id,
int  value 
)
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().

Here is the caller graph for this function:

◆ setString()

void libtraci::Connection::setString ( int  command,
int  var,
const std::string &  id,
const std::string &  value 
)
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().

Here is the caller graph for this function:

◆ setStringVector()

void libtraci::Connection::setStringVector ( int  command,
int  var,
const std::string &  id,
const std::vector< std::string > &  value 
)
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().

Here is the caller graph for this function:

◆ simulationStep()

◆ subscribeObjectContext()

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.

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
[in]rangeThe range around the obj to investigate
[in]varsThe 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().

◆ subscribeObjectVariable()

void libtraci::Connection::subscribeObjectVariable ( int  domID,
const std::string &  objID,
double  beginTime,
double  endTime,
const std::vector< int > &  vars,
const libsumo::TraCIResults params 
)

◆ switchCon()

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

Definition at line 61 of file Connection.h.

References myActive, and myConnections.

◆ toString()

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

Definition at line 335 of file Connection.h.

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

Here is the caller graph for this function:

Field Documentation

◆ myActive

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

Definition at line 363 of file Connection.h.

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

◆ myConnections

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

Definition at line 364 of file Connection.h.

Referenced by connect(), and switchCon().

◆ myContextSubscriptionResults

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

◆ myInput

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

◆ myLabel

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

Definition at line 351 of file Connection.h.

Referenced by getLabel().

◆ myOutput

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

The reusable output storage.

Definition at line 356 of file Connection.h.

Referenced by createCommand(), createFilterCommand(), doCommand(), and processGet().

◆ myProcessPipe

FILE* const libtraci::Connection::myProcessPipe
private

Definition at line 352 of file Connection.h.

Referenced by close().

◆ 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: