Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
Connection.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2012-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// C++ TraCI client API implementation
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <vector>
26#include <map>
27#include <limits>
28#include <string>
29#include <sstream>
30#include <iomanip>
31#include <thread>
32#include <mutex>
35
36
37// ===========================================================================
38// global definitions
39// ===========================================================================
40#define PRECISION 2
41
42
43// ===========================================================================
44// class definitions
45// ===========================================================================
46namespace libtraci {
52public:
53 static void connect(const std::string& host, int port, int numRetries, const std::string& label, FILE* const pipe) {
54 myConnections[label] = new Connection(host, port, numRetries, label, pipe);
55 }
56
58 if (myActive == nullptr) {
59 throw libsumo::FatalTraCIError("Not connected.");
60 }
61 return *myActive;
62 }
63
64 static bool isActive() {
65 return myActive != nullptr;
66 }
67
68 static void switchCon(const std::string& label) {
69 myActive = myConnections.find(label)->second;
70 }
71
72 const std::string& getLabel() const {
73 return myLabel;
74 }
75
76 std::mutex& getMutex() const {
77 return myMutex;
78 }
79
81 void close();
82
86
90
93
96 void simulationStep(double time);
97
98
101 void setOrder(int order);
102
110 void createCommand(int cmdID, int varID, const std::string* const objID, tcpip::Storage* add = nullptr) const;
111
112
123 void subscribe(int domID, const std::string& objID, double beginTime, double endTime,
124 int domain, double range, const std::vector<int>& vars, const libsumo::TraCIResults& params);
126
127
128 tcpip::Storage& doCommand(int command, int var = -1, const std::string& id = "", tcpip::Storage* add = nullptr, int expectedType = -1);
129 void addFilter(int var, tcpip::Storage* add = nullptr);
130
131 void readVariableSubscription(int responseID, tcpip::Storage& inMsg);
132 void readContextSubscription(int responseID, tcpip::Storage& inMsg);
133 void readVariables(tcpip::Storage& inMsg, const std::string& objectID, int variableCount, libsumo::SubscriptionResults& into);
134
135private:
142 void check_resultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId = false, std::string* acknowledgement = 0);
143
147 int check_commandGetResult(tcpip::Storage& inMsg, int command, int expectedType = -1, bool ignoreCommandId = false) const;
148
149 template <class T>
150 static inline std::string toString(const T& t, std::streamsize accuracy = PRECISION) {
151 std::ostringstream oss;
152 oss.setf(std::ios::fixed, std::ios::floatfield);
153 oss << std::setprecision(accuracy);
154 oss << t;
155 return oss.str();
156 }
157
158 template<typename T>
159 inline std::string toHex(const T i, std::streamsize numDigits = 2) {
160 // inspired by http://stackoverflow.com/questions/5100718/int-to-hex-string-in-c
161 std::stringstream stream;
162 stream << "0x" << std::setfill('0') << std::setw(numDigits == 0 ? sizeof(T) * 2 : numDigits) << std::hex << i;
163 return stream.str();
164 }
165
166 void readOutput();
167
173 Connection(const std::string& host, int port, int numRetries, const std::string& label, FILE* const pipe);
174
175private:
176 const std::string myLabel;
177 FILE* const myProcessPipe;
178 std::thread* myProcessReader;
185
186 mutable std::mutex myMutex;
187
188 std::map<int, libsumo::SubscriptionResults> mySubscriptionResults;
189 std::map<int, libsumo::ContextSubscriptionResults> myContextSubscriptionResults;
190
192 static std::map<const std::string, Connection*> myConnections;
193
194private:
197
198};
199
200}
#define PRECISION
Definition Connection.h:40
An error which is not recoverable.
Definition TraCIDefs.h:155
C++ TraCI client API implementation.
Definition Connection.h:51
void simulationStep(double time)
Sends a SimulationStep command.
static void connect(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Definition Connection.h:53
static bool isActive()
Definition Connection.h:64
void close()
ends the simulation and closes the connection
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...
int check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
Validates the result state of a command.
static Connection & getActive()
Definition Connection.h:57
void addFilter(int var, tcpip::Storage *add=nullptr)
void readVariableSubscription(int responseID, tcpip::Storage &inMsg)
libsumo::ContextSubscriptionResults & getAllContextSubscriptionResults(const int domain)
Definition Connection.h:87
tcpip::Socket mySocket
The socket.
Definition Connection.h:180
std::map< int, libsumo::SubscriptionResults > mySubscriptionResults
Definition Connection.h:188
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0)
Validates the result state of a command.
std::mutex & getMutex() const
Definition Connection.h:76
tcpip::Storage myInput
The reusable input storage.
Definition Connection.h:184
FILE *const myProcessPipe
Definition Connection.h:177
void readVariables(tcpip::Storage &inMsg, const std::string &objectID, int variableCount, libsumo::SubscriptionResults &into)
std::map< int, libsumo::ContextSubscriptionResults > myContextSubscriptionResults
Definition Connection.h:189
tcpip::Storage myOutput
The reusable output storage.
Definition Connection.h:182
Connection & operator=(const Connection &)
Invalidated assignment operator.
void setOrder(int order)
Sends a SetOrder command.
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.
libsumo::SubscriptionResults & getAllSubscriptionResults(const int domain)
Definition Connection.h:83
static std::map< const std::string, Connection * > myConnections
Definition Connection.h:192
const std::string myLabel
Definition Connection.h:176
void readContextSubscription(int responseID, tcpip::Storage &inMsg)
tcpip::Storage & doCommand(int command, int var=-1, const std::string &id="", tcpip::Storage *add=nullptr, int expectedType=-1)
static Connection * myActive
Definition Connection.h:191
static std::string toString(const T &t, std::streamsize accuracy=PRECISION)
Definition Connection.h:150
std::thread * myProcessReader
Definition Connection.h:178
static void switchCon(const std::string &label)
Definition Connection.h:68
const std::string & getLabel() const
Definition Connection.h:72
std::string toHex(const T i, std::streamsize numDigits=2)
Definition Connection.h:159
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition TraCIDefs.h:338
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:337
std::map< int, std::shared_ptr< libsumo::TraCIResult > > TraCIResults
{variable->value}
Definition TraCIDefs.h:335