Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libtraci/Domain.h
169666 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2012-2025 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
/****************************************************************************/
14
/// @file Domain.h
15
/// @author Daniel Krajzewicz
16
/// @author Mario Krumnow
17
/// @author Michael Behrisch
18
/// @author Robert Hilbrich
19
/// @date 30.05.2012
20
///
21
// C++ TraCI client API implementation
22
/****************************************************************************/
23
#pragma once
24
#include <config.h>
25
26
#include <vector>
27
#include <limits>
28
#include <map>
29
#include <string>
30
#include <stdexcept>
31
#include <sstream>
32
#include <memory>
33
#include <foreign/tcpip/storage.h>
34
#include <libtraci/Connection.h>
35
#include <libsumo/StorageHelper.h>
36
37
38
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN) \
39
const int CLASS::DOMAIN_ID(libsumo::CMD_GET_##DOMAIN##_VARIABLE); \
40
void CLASS::subscribe(const std::string& objectID, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
41
libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_VARIABLE, objectID, begin, end, -1, -1, varIDs, params); \
42
} \
43
\
44
void CLASS::unsubscribe(const std::string& objectID) { \
45
subscribe(objectID, std::vector<int>()); \
46
} \
47
\
48
void CLASS::subscribeContext(const std::string& objectID, int domain, double dist, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
49
libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_CONTEXT, objectID, begin, end, domain, dist, varIDs, params); \
50
} \
51
\
52
void CLASS::unsubscribeContext(const std::string& objectID, int domain, double dist) { \
53
subscribeContext(objectID, domain, dist, std::vector<int>()); \
54
} \
55
\
56
const libsumo::SubscriptionResults CLASS::getAllSubscriptionResults() { \
57
return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE); \
58
} \
59
\
60
const libsumo::TraCIResults CLASS::getSubscriptionResults(const std::string& objectID) { \
61
return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE)[objectID]; \
62
} \
63
\
64
const libsumo::ContextSubscriptionResults CLASS::getAllContextSubscriptionResults() { \
65
return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT); \
66
} \
67
\
68
const libsumo::SubscriptionResults CLASS::getContextSubscriptionResults(const std::string& objectID) { \
69
return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT)[objectID]; \
70
} \
71
\
72
void CLASS::subscribeParameterWithKey(const std::string& objectID, const std::string& key, double beginTime, double endTime) { \
73
subscribe(objectID, std::vector<int>({libsumo::VAR_PARAMETER_WITH_KEY}), beginTime, endTime, libsumo::TraCIResults {{libsumo::VAR_PARAMETER_WITH_KEY, std::make_shared<libsumo::TraCIString>(key)}}); \
74
}
75
76
77
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN) \
78
std::string \
79
CLASS::getParameter(const std::string& objectID, const std::string& param) { \
80
tcpip::Storage content; \
81
content.writeByte(libsumo::TYPE_STRING); \
82
content.writeString(param); \
83
return Dom::getString(libsumo::VAR_PARAMETER, objectID, &content); \
84
} \
85
\
86
void \
87
CLASS::setParameter(const std::string& objectID, const std::string& key, const std::string& value) { \
88
tcpip::Storage content; \
89
content.writeUnsignedByte(libsumo::TYPE_COMPOUND); \
90
content.writeInt(2); \
91
content.writeUnsignedByte(libsumo::TYPE_STRING); \
92
content.writeString(key); \
93
content.writeUnsignedByte(libsumo::TYPE_STRING); \
94
content.writeString(value); \
95
Connection::getActive().doCommand(libsumo::CMD_SET_##DOMAIN##_VARIABLE, libsumo::VAR_PARAMETER, objectID, &content); \
96
} \
97
\
98
const std::pair<std::string, std::string> \
99
CLASS::getParameterWithKey(const std::string& objectID, const std::string& key) { \
100
return std::make_pair(key, getParameter(objectID, key)); \
101
}
102
103
104
// ===========================================================================
105
// class and type definitions
106
// ===========================================================================
107
namespace libtraci {
108
template<int GET, int SET>
109
class Domain {
110
public:
111
static inline tcpip::Storage& get(int var, const std::string& id, tcpip::Storage* add = nullptr, int expectedType = libsumo::TYPE_COMPOUND) {
112
return libtraci::Connection::getActive().doCommand(GET, var, id, add, expectedType);
113
}
114
115
static int getUnsignedByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
116
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
117
return get(var, id, add, libsumo::TYPE_UBYTE).readUnsignedByte();
118
}
119
120
static int getByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
121
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
122
return get(var, id, add, libsumo::TYPE_BYTE).readByte();
123
}
124
125
static int getInt(int var, const std::string& id, tcpip::Storage* add = nullptr) {
126
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
127
return get(var, id, add, libsumo::TYPE_INTEGER).readInt();
128
}
129
130
static double getDouble(int var, const std::string& id, tcpip::Storage* add = nullptr) {
131
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
132
return get(var, id, add, libsumo::TYPE_DOUBLE).readDouble();
133
}
134
135
static libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = nullptr) {
136
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
137
tcpip::Storage& result = get(var, id, add, libsumo::TYPE_POLYGON);
138
libsumo::TraCIPositionVector poly;
139
StoHelp::readPolygon(result, poly);
140
return poly;
141
}
142
143
static libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
144
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
145
tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT : libsumo::POSITION_2D);
146
libsumo::TraCIPosition p;
147
p.x = result.readDouble();
148
p.y = result.readDouble();
149
return p;
150
}
151
152
static libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
153
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
154
tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT_ALT : libsumo::POSITION_3D);
155
libsumo::TraCIPosition p;
156
p.x = result.readDouble();
157
p.y = result.readDouble();
158
p.z = result.readDouble();
159
return p;
160
}
161
162
static std::string getString(int var, const std::string& id, tcpip::Storage* add = nullptr) {
163
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
164
return get(var, id, add, libsumo::TYPE_STRING).readString();
165
}
166
167
static std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
168
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
169
return get(var, id, add, libsumo::TYPE_STRINGLIST).readStringList();
170
}
171
172
static std::vector<double> getDoubleVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
173
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
174
return get(var, id, add, libsumo::TYPE_DOUBLELIST).readDoubleList();
175
}
176
177
static libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = nullptr) {
178
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
179
tcpip::Storage& result = get(var, id, add, libsumo::TYPE_COLOR);
180
libsumo::TraCIColor c;
181
c.r = (unsigned char)result.readUnsignedByte();
182
c.g = (unsigned char)result.readUnsignedByte();
183
c.b = (unsigned char)result.readUnsignedByte();
184
c.a = (unsigned char)result.readUnsignedByte();
185
return c;
186
}
187
188
static libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = nullptr) {
189
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
190
tcpip::Storage& result = get(var, id, add);
191
libsumo::TraCIStage s;
192
result.readInt(); // components
193
s.type = StoHelp::readTypedInt(result);
194
s.vType = StoHelp::readTypedString(result);
195
s.line = StoHelp::readTypedString(result);
196
s.destStop = StoHelp::readTypedString(result);
197
s.edges = StoHelp::readTypedStringList(result);
198
s.travelTime = StoHelp::readTypedDouble(result);
199
s.cost = StoHelp::readTypedDouble(result);
200
s.length = StoHelp::readTypedDouble(result);
201
s.intended = StoHelp::readTypedString(result);
202
s.depart = StoHelp::readTypedDouble(result);
203
s.departPos = StoHelp::readTypedDouble(result);
204
s.arrivalPos = StoHelp::readTypedDouble(result);
205
s.description = StoHelp::readTypedString(result);
206
return s;
207
}
208
209
static void set(int var, const std::string& id, tcpip::Storage* add) {
210
std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
211
libtraci::Connection::getActive().doCommand(SET, var, id, add);
212
}
213
214
static void setInt(int var, const std::string& id, int value) {
215
tcpip::Storage content;
216
content.writeUnsignedByte(libsumo::TYPE_INTEGER);
217
content.writeInt(value);
218
set(var, id, &content);
219
}
220
221
static void setDouble(int var, const std::string& id, double value) {
222
tcpip::Storage content;
223
content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
224
content.writeDouble(value);
225
set(var, id, &content);
226
}
227
228
static void setString(int var, const std::string& id, const std::string& value) {
229
tcpip::Storage content;
230
content.writeUnsignedByte(libsumo::TYPE_STRING);
231
content.writeString(value);
232
set(var, id, &content);
233
}
234
235
static void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) {
236
tcpip::Storage content;
237
content.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
238
content.writeStringList(value);
239
set(var, id, &content);
240
}
241
242
static void setCol(int var, const std::string& id, const libsumo::TraCIColor value) {
243
tcpip::Storage content;
244
content.writeUnsignedByte(libsumo::TYPE_COLOR);
245
content.writeUnsignedByte(value.r);
246
content.writeUnsignedByte(value.g);
247
content.writeUnsignedByte(value.b);
248
content.writeUnsignedByte(value.a);
249
set(var, id, &content);
250
}
251
252
};
253
254
}
255
256