Eclipse SUMO - Simulation of Urban MObility
libtraci/Simulation.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2020 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 /****************************************************************************/
19 // C++ TraCI client API implementation
20 /****************************************************************************/
21 #include <config.h>
22 #include <cstdlib>
23 
24 #include <foreign/tcpip/socket.h>
25 #define LIBTRACI 1
26 #include "Connection.h"
27 #include "Domain.h"
28 #include <libsumo/Simulation.h>
29 
30 
31 namespace libtraci {
32 
33 typedef Domain<libsumo::CMD_GET_SIM_VARIABLE, libsumo::CMD_SET_SIM_VARIABLE> Dom;
34 
35 
36 // ===========================================================================
37 // static member definitions
38 // ===========================================================================
39 std::pair<int, std::string>
40 Simulation::init(int port, int numRetries, const std::string& host, const std::string& label, FILE* const pipe) {
41  Connection::connect(host, port, numRetries, label, pipe);
42  switchConnection(label);
43  return getVersion();
44 }
45 
46 
47 std::pair<int, std::string>
48 Simulation::start(const std::vector<std::string>& cmd, int port, int numRetries, const std::string& label, const bool verbose) {
49  if (port == -1) {
51  }
52  std::ostringstream oss;
53  for (const std::string& s : cmd) {
54  oss << s << " ";
55  }
56  oss << "--remote-port " << port << " 2>&1";
57 #ifndef WIN32
58  oss << " &";
59 #endif
60  if (verbose) {
61  std::cout << "Calling " << oss.str() << std::endl;
62  }
63 #ifdef WIN32
64  FILE* pipe = _popen(oss.str().c_str(), "r");
65 #else
66  FILE* pipe = popen(oss.str().c_str(), "r");
67 #endif
68  return init(port, numRetries, "localhost", label, pipe);
69 }
70 
71 
72 bool
73 Simulation::isLibsumo() {
74  return false;
75 }
76 
77 
78 bool
79 Simulation::hasGUI() {
80  return true;
81 }
82 
83 
84 void
85 Simulation::switchConnection(const std::string& label) {
86  Connection::switchCon(label);
87 }
88 
89 
90 const std::string&
91 Simulation::getLabel() {
93 }
94 
95 
96 void
97 Simulation::setOrder(int order) {
99 }
100 
101 
102 void
103 Simulation::load(const std::vector<std::string>& args) {
105 }
106 
107 
108 bool
109 Simulation::isLoaded() {
110  return Connection::isActive();
111 }
112 
113 
114 void
115 Simulation::step(const double time) {
117 }
118 
119 
120 void
121 Simulation::close(const std::string& /* reason */) {
123 }
124 
125 
126 std::pair<int, std::string>
127 Simulation::getVersion() {
129  inMsg.readUnsignedByte(); // msg length
130  inMsg.readUnsignedByte(); // libsumo::CMD_GETVERSION again, see #7284
131  const int traciVersion = inMsg.readInt(); // to fix evaluation order
132  return std::make_pair(traciVersion, inMsg.readString());
133 }
134 
135 
136 int
137 Simulation::getCurrentTime() {
139 }
140 
141 
142 double
143 Simulation::getTime() {
144  return Dom::getDouble(libsumo::VAR_TIME, "");
145 }
146 
147 
148 int
149 Simulation::getLoadedNumber() {
151 }
152 
153 
154 std::vector<std::string>
155 Simulation::getLoadedIDList() {
157 }
158 
159 
160 int
161 Simulation::getDepartedNumber() {
163 }
164 
165 
166 std::vector<std::string>
167 Simulation::getDepartedIDList() {
169 }
170 
171 
172 int
173 Simulation::getArrivedNumber() {
175 }
176 
177 
178 std::vector<std::string>
179 Simulation::getArrivedIDList() {
181 }
182 
183 
184 int
185 Simulation::getParkingStartingVehiclesNumber() {
187 }
188 
189 
190 std::vector<std::string>
191 Simulation::getParkingStartingVehiclesIDList() {
193 }
194 
195 
196 int
197 Simulation::getParkingEndingVehiclesNumber() {
199 }
200 
201 
202 std::vector<std::string>
203 Simulation::getParkingEndingVehiclesIDList() {
205 }
206 
207 
208 int
209 Simulation::getStopStartingVehiclesNumber() {
211 }
212 
213 
214 std::vector<std::string>
215 Simulation::getStopStartingVehiclesIDList() {
217 }
218 
219 
220 int
221 Simulation::getStopEndingVehiclesNumber() {
223 }
224 
225 
226 std::vector<std::string>
227 Simulation::getStopEndingVehiclesIDList() {
229 }
230 
231 
232 int
233 Simulation::getCollidingVehiclesNumber() {
235 }
236 
237 
238 std::vector<std::string>
239 Simulation::getCollidingVehiclesIDList() {
241 }
242 
243 
244 int
245 Simulation::getEmergencyStoppingVehiclesNumber() {
247 }
248 
249 
250 std::vector<std::string>
251 Simulation::getEmergencyStoppingVehiclesIDList() {
253 }
254 
255 
256 int
257 Simulation::getStartingTeleportNumber() {
259 }
260 
261 
262 std::vector<std::string>
263 Simulation::getStartingTeleportIDList() {
265 }
266 
267 
268 int
269 Simulation::getEndingTeleportNumber() {
271 }
272 
273 
274 std::vector<std::string>
275 Simulation::getEndingTeleportIDList() {
277 }
278 
279 
280 std::vector<std::string>
281 Simulation::getBusStopIDList() {
283 }
284 
285 int
286 Simulation::getBusStopWaiting(const std::string& stopID) {
288 }
289 
290 std::vector<std::string>
291 Simulation::getBusStopWaitingIDList(const std::string& stopID) {
293 }
294 
295 
296 double
297 Simulation::getDeltaT() {
299 }
300 
301 
303 Simulation::getNetBoundary() {
305 }
306 
307 
308 int
309 Simulation::getMinExpectedNumber() {
311 }
312 
313 /*
314 libsumo::TraCIPosition
315 Simulation::convert2D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
316 }
317 
318 
319 libsumo::TraCIPosition
320 Simulation::convert3D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
321 }
322 
323 
324 libsumo::TraCIRoadPosition
325 Simulation::convertRoad(double x, double y, bool isGeo, const std::string& vClass) {
326 }
327 
328 
329 libsumo::TraCIPosition
330 Simulation::convertGeo(double x, double y, bool fromGeo) {
331 }
332 
333 
334 double
335 Simulation::getDistance2D(double x1, double y1, double x2, double y2, bool isGeo, bool isDriving) {
336 }
337 
338 
339 double
340 Simulation::getDistanceRoad(const std::string& edgeID1, double pos1, const std::string& edgeID2, double pos2, bool isDriving) {
341 }
342 
343 
344 libsumo::TraCIStage
345 Simulation::findRoute(const std::string& from, const std::string& to, const std::string& typeID, const double depart, const int routingMode) {
346 }
347 
348 
349 std::vector<libsumo::TraCIStage>
350 Simulation::findIntermodalRoute(const std::string& from, const std::string& to,
351  const std::string& modes, double depart, const int routingMode, double speed, double walkFactor,
352  double departPos, double arrivalPos, const double departPosLat,
353  const std::string& pType, const std::string& vType, const std::string& destStop) {
354 }
355 
356 
357 std::string
358 Simulation::getParameter(const std::string& objectID, const std::string& key) {
359 }
360 
361 
362 LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(Simulation)
363 */
364 
365 void
366 Simulation::clearPending(const std::string& routeID) {
368 }
369 
370 
371 void
372 Simulation::saveState(const std::string& fileName) {
374 }
375 
376 double
377 Simulation::loadState(const std::string& fileName) {
379  return 0.;
380 }
381 
382 void
383 Simulation::writeMessage(const std::string& msg) {
385 }
386 
387 
388 }
389 
390 
391 /****************************************************************************/
void simulationStep(double time)
Sends a SimulationStep command.
Definition: Connection.cpp:101
static void connect(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Definition: Connection.h:49
static bool isActive()
Definition: Connection.h:57
void close()
ends the simulation and closes the connection
Definition: Connection.cpp:62
void setStringVector(int command, int var, const std::string &id, const std::vector< std::string > &value)
Definition: Connection.h:312
const std::string & getLabel()
Definition: Connection.h:65
static Connection & getActive()
Definition: Connection.h:53
void send_commandSetOrder(int order)
Sends a SetOrder command.
Definition: Connection.cpp:130
tcpip::Storage & doCommand(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.cpp:366
static void switchCon(const std::string &label)
Definition: Connection.h:61
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:140
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:116
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:124
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:120
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:160
static int getFreeSocketPort()
Returns an free port on the system.
Definition: socket.cpp:118
virtual std::string readString()
Definition: storage.cpp:175
virtual int readUnsignedByte()
Definition: storage.cpp:150
virtual int readInt()
Definition: storage.cpp:306
TRACI_CONST int VAR_MIN_EXPECTED_VEHICLES
TRACI_CONST int CMD_SAVE_SIMSTATE
TRACI_CONST int VAR_STOP_ENDING_VEHICLES_IDS
TRACI_CONST int CMD_LOAD
TRACI_CONST int VAR_ARRIVED_VEHICLES_NUMBER
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_NUMBER
TRACI_CONST int VAR_DEPARTED_VEHICLES_NUMBER
TRACI_CONST int CMD_LOAD_SIMSTATE
TRACI_CONST int VAR_COLLIDING_VEHICLES_NUMBER
TRACI_CONST int CMD_CLEAR_PENDING_VEHICLES
TRACI_CONST int VAR_PARKING_ENDING_VEHICLES_IDS
TRACI_CONST int VAR_PARKING_STARTING_VEHICLES_IDS
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:196
TRACI_CONST int VAR_TIME
TRACI_CONST int VAR_NET_BOUNDING_BOX
TRACI_CONST int VAR_BUS_STOP_ID_LIST
TRACI_CONST int VAR_EMERGENCYSTOPPING_VEHICLES_IDS
TRACI_CONST int VAR_BUS_STOP_WAITING_IDS
TRACI_CONST int VAR_DEPARTED_VEHICLES_IDS
TRACI_CONST int VAR_TELEPORT_ENDING_VEHICLES_NUMBER
TRACI_CONST int CMD_MESSAGE
TRACI_CONST int VAR_PARKING_ENDING_VEHICLES_NUMBER
TRACI_CONST int VAR_LOADED_VEHICLES_IDS
TRACI_CONST int VAR_DELTA_T
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_IDS
TRACI_CONST int VAR_STOP_ENDING_VEHICLES_NUMBER
TRACI_CONST int VAR_LOADED_VEHICLES_NUMBER
TRACI_CONST int VAR_TELEPORT_ENDING_VEHICLES_IDS
TRACI_CONST int VAR_COLLIDING_VEHICLES_IDS
TRACI_CONST int VAR_TELEPORT_STARTING_VEHICLES_NUMBER
TRACI_CONST int VAR_TELEPORT_STARTING_VEHICLES_IDS
TRACI_CONST int CMD_GETVERSION
TRACI_CONST int VAR_BUS_STOP_WAITING
TRACI_CONST int VAR_TIME_STEP
TRACI_CONST int VAR_ARRIVED_VEHICLES_IDS
TRACI_CONST int VAR_PARKING_STARTING_VEHICLES_NUMBER
TRACI_CONST int VAR_EMERGENCYSTOPPING_VEHICLES_NUMBER
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom