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-2022 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/StorageHelper.h>
29 #include <libsumo/GUI.h>
30 #include <libsumo/Simulation.h>
31 
32 
33 namespace libtraci {
34 
35 typedef Domain<libsumo::CMD_GET_SIM_VARIABLE, libsumo::CMD_SET_SIM_VARIABLE> Dom;
36 
37 
38 // ===========================================================================
39 // static member definitions
40 // ===========================================================================
41 std::pair<int, std::string>
42 Simulation::init(int port, int numRetries, const std::string& host, const std::string& label, FILE* const pipe) {
43  Connection::connect(host, port, numRetries, label, pipe);
44  switchConnection(label);
45  return getVersion();
46 }
47 
48 
49 std::pair<int, std::string>
50 Simulation::start(const std::vector<std::string>& cmd, int port, int numRetries, const std::string& label, const bool verbose,
51  const std::string& /* traceFile */, bool /* traceGetters */, void* /* _stdout */) {
52  if (port == -1) {
54  }
55  std::ostringstream oss;
56  for (const std::string& s : cmd) {
57  oss << s << " ";
58  }
59  oss << "--remote-port " << port << " 2>&1";
60 #ifndef WIN32
61  oss << " &";
62 #endif
63  if (verbose) {
64  std::cout << "Calling " << oss.str() << std::endl;
65  }
66 #ifdef WIN32
67  FILE* pipe = _popen(oss.str().c_str(), "r");
68 #else
69  FILE* pipe = popen(oss.str().c_str(), "r");
70 #endif
71  return init(port, numRetries, "localhost", label, pipe);
72 }
73 
74 
75 bool
76 Simulation::isLibsumo() {
77  return false;
78 }
79 
80 
81 bool
82 Simulation::hasGUI() {
83  try {
84  GUI::getIDList();
85  return true;
86  } catch (libsumo::TraCIException&) {
87  return false;
88  }
89 }
90 
91 
92 void
93 Simulation::switchConnection(const std::string& label) {
94  Connection::switchCon(label);
95 }
96 
97 
98 const std::string&
99 Simulation::getLabel() {
100  return Connection::getActive().getLabel();
101 }
102 
103 
104 void
105 Simulation::setOrder(int order) {
107 }
108 
109 
110 void
111 Simulation::load(const std::vector<std::string>& args) {
112  tcpip::Storage content;
114  content.writeStringList(args);
116 }
117 
118 
119 bool
120 Simulation::isLoaded() {
121  return Connection::isActive();
122 }
123 
124 
125 void
126 Simulation::step(const double time) {
128 }
129 
130 
131 void
132 Simulation::close(const std::string& /* reason */) {
134 }
135 
136 
137 std::pair<int, std::string>
138 Simulation::getVersion() {
140  inMsg.readUnsignedByte(); // msg length
141  inMsg.readUnsignedByte(); // libsumo::CMD_GETVERSION again, see #7284
142  const int traciVersion = inMsg.readInt(); // to fix evaluation order
143  return std::make_pair(traciVersion, inMsg.readString());
144 }
145 
146 
147 int
148 Simulation::getCurrentTime() {
150 }
151 
152 
153 double
154 Simulation::getTime() {
155  return Dom::getDouble(libsumo::VAR_TIME, "");
156 }
157 
158 
159 double
160 Simulation::getEndTime() {
161  return Dom::getDouble(libsumo::VAR_END, "");
162 }
163 
164 
165 int
166 Simulation::getLoadedNumber() {
168 }
169 
170 
171 std::vector<std::string>
172 Simulation::getLoadedIDList() {
174 }
175 
176 
177 int
178 Simulation::getDepartedNumber() {
180 }
181 
182 
183 std::vector<std::string>
184 Simulation::getDepartedIDList() {
186 }
187 
188 
189 int
190 Simulation::getArrivedNumber() {
192 }
193 
194 
195 std::vector<std::string>
196 Simulation::getArrivedIDList() {
198 }
199 
200 
201 int
202 Simulation::getParkingStartingVehiclesNumber() {
204 }
205 
206 
207 std::vector<std::string>
208 Simulation::getParkingStartingVehiclesIDList() {
210 }
211 
212 
213 int
214 Simulation::getParkingEndingVehiclesNumber() {
216 }
217 
218 
219 std::vector<std::string>
220 Simulation::getParkingEndingVehiclesIDList() {
222 }
223 
224 
225 int
226 Simulation::getStopStartingVehiclesNumber() {
228 }
229 
230 
231 std::vector<std::string>
232 Simulation::getStopStartingVehiclesIDList() {
234 }
235 
236 
237 int
238 Simulation::getStopEndingVehiclesNumber() {
240 }
241 
242 
243 std::vector<std::string>
244 Simulation::getStopEndingVehiclesIDList() {
246 }
247 
248 
249 int
250 Simulation::getCollidingVehiclesNumber() {
252 }
253 
254 
255 std::vector<std::string>
256 Simulation::getCollidingVehiclesIDList() {
258 }
259 
260 
261 int
262 Simulation::getEmergencyStoppingVehiclesNumber() {
264 }
265 
266 
267 std::vector<std::string>
268 Simulation::getEmergencyStoppingVehiclesIDList() {
270 }
271 
272 
273 int
274 Simulation::getStartingTeleportNumber() {
276 }
277 
278 
279 std::vector<std::string>
280 Simulation::getStartingTeleportIDList() {
282 }
283 
284 
285 int
286 Simulation::getEndingTeleportNumber() {
288 }
289 
290 
291 std::vector<std::string>
292 Simulation::getEndingTeleportIDList() {
294 }
295 
296 
297 int
298 Simulation::getDepartedPersonNumber() {
300 }
301 
302 
303 std::vector<std::string>
304 Simulation::getDepartedPersonIDList() {
306 }
307 
308 
309 int
310 Simulation::getArrivedPersonNumber() {
312 }
313 
314 
315 std::vector<std::string>
316 Simulation::getArrivedPersonIDList() {
318 }
319 
320 
321 std::vector<std::string>
322 Simulation::getBusStopIDList() {
324 }
325 
326 int
327 Simulation::getBusStopWaiting(const std::string& stopID) {
329 }
330 
331 std::vector<std::string>
332 Simulation::getBusStopWaitingIDList(const std::string& stopID) {
334 }
335 
336 
337 std::vector<std::string>
338 Simulation::getPendingVehicles() {
340 }
341 
342 std::vector<libsumo::TraCICollision>
343 Simulation::getCollisions() {
344  std::vector<libsumo::TraCICollision> result;
345  return result;
346 }
347 
348 
349 
350 double
351 Simulation::getDeltaT() {
353 }
354 
355 
357 Simulation::getNetBoundary() {
359 }
360 
361 
362 int
363 Simulation::getMinExpectedNumber() {
365 }
366 
367 
369 Simulation::convert2D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
370  tcpip::Storage content;
371  StoHelp::writeCompound(content, 2);
373  content.writeString(edgeID);
374  content.writeDouble(pos);
375  content.writeUnsignedByte(laneIndex);
378  return Dom::getPos(libsumo::POSITION_CONVERSION, "", &content, toGeo);
379 }
380 
381 
383 Simulation::convert3D(const std::string& edgeID, double pos, int laneIndex, bool toGeo) {
384  tcpip::Storage content;
385  StoHelp::writeCompound(content, 2);
387  content.writeString(edgeID);
388  content.writeDouble(pos);
389  content.writeUnsignedByte(laneIndex);
392  return Dom::getPos3D(libsumo::POSITION_CONVERSION, "", &content, toGeo);
393 }
394 
395 
397 Simulation::convertRoad(double x, double y, bool isGeo, const std::string& vClass) {
398  tcpip::Storage content;
399  StoHelp::writeCompound(content, 3);
401  content.writeDouble(x);
402  content.writeDouble(y);
405  StoHelp::writeTypedString(content, vClass);
408  result.edgeID = ret.readString();
409  result.pos = ret.readDouble();
410  result.laneIndex = ret.readByte();
411  return result;
412 }
413 
414 
416 Simulation::convertGeo(double x, double y, bool fromGeo) {
417  tcpip::Storage content;
418  StoHelp::writeCompound(content, 2);
420  content.writeDouble(x);
421  content.writeDouble(y);
424  return Dom::getPos(libsumo::POSITION_CONVERSION, "", &content, !fromGeo);
425 }
426 
427 
428 double
429 Simulation::getDistance2D(double x1, double y1, double x2, double y2, bool isGeo, bool isDriving) {
430  tcpip::Storage content;
431  StoHelp::writeCompound(content, 3);
433  content.writeDouble(x1);
434  content.writeDouble(y1);
436  content.writeDouble(x2);
437  content.writeDouble(y2);
439  return Dom::getDouble(libsumo::DISTANCE_REQUEST, "", &content);
440 }
441 
442 
443 double
444 Simulation::getDistanceRoad(const std::string& edgeID1, double pos1, const std::string& edgeID2, double pos2, bool isDriving) {
445  tcpip::Storage content;
446  StoHelp::writeCompound(content, 3);
448  content.writeString(edgeID1);
449  content.writeDouble(pos1);
450  content.writeUnsignedByte(0);
452  content.writeString(edgeID2);
453  content.writeDouble(pos2);
454  content.writeUnsignedByte(0);
456  return Dom::getDouble(libsumo::DISTANCE_REQUEST, "", &content);
457 }
458 
459 
461 Simulation::findRoute(const std::string& fromEdge, const std::string& toEdge, const std::string& vType, const double depart, const int routingMode) {
462  tcpip::Storage content;
463  StoHelp::writeCompound(content, 5);
464  StoHelp::writeTypedString(content, fromEdge);
465  StoHelp::writeTypedString(content, toEdge);
466  StoHelp::writeTypedString(content, vType);
467  StoHelp::writeTypedDouble(content, depart);
468  StoHelp::writeTypedInt(content, routingMode);
469  return Dom::getTraCIStage(libsumo::FIND_ROUTE, "", &content);
470 }
471 
472 
473 std::vector<libsumo::TraCIStage>
474 Simulation::findIntermodalRoute(const std::string& fromEdge, const std::string& toEdge,
475  const std::string& modes, double depart, const int routingMode, double speed, double walkFactor,
476  double departPos, double arrivalPos, const double departPosLat,
477  const std::string& pType, const std::string& vType, const std::string& destStop) {
478  tcpip::Storage content;
479  StoHelp::writeCompound(content, 13);
480  StoHelp::writeTypedString(content, fromEdge);
481  StoHelp::writeTypedString(content, toEdge);
482  StoHelp::writeTypedString(content, modes);
483  StoHelp::writeTypedDouble(content, depart);
484  StoHelp::writeTypedInt(content, routingMode);
485  StoHelp::writeTypedDouble(content, speed);
486  StoHelp::writeTypedDouble(content, walkFactor);
487  StoHelp::writeTypedDouble(content, departPos);
488  StoHelp::writeTypedDouble(content, arrivalPos);
489  StoHelp::writeTypedDouble(content, departPosLat);
490  StoHelp::writeTypedString(content, pType);
491  StoHelp::writeTypedString(content, vType);
492  StoHelp::writeTypedString(content, destStop);
493  tcpip::Storage& result = Dom::get(libsumo::FIND_INTERMODAL_ROUTE, "", &content);
494  int numStages = result.readInt();
495  std::vector<libsumo::TraCIStage> ret;
496  while (numStages-- > 0) {
498  StoHelp::readCompound(result, 13);
499  s.type = StoHelp::readTypedInt(result);
500  s.vType = StoHelp::readTypedString(result);
501  s.line = StoHelp::readTypedString(result);
505  s.cost = StoHelp::readTypedDouble(result);
506  s.length = StoHelp::readTypedDouble(result);
508  s.depart = StoHelp::readTypedDouble(result);
512  ret.emplace_back(s);
513  }
514  return ret;
515 }
516 
517 LIBTRACI_PARAMETER_IMPLEMENTATION(Simulation, SIM)
518 
519 void
520 Simulation::clearPending(const std::string& routeID) {
522 }
523 
524 
525 void
526 Simulation::saveState(const std::string& fileName) {
528 }
529 
530 double
531 Simulation::loadState(const std::string& fileName) {
533  return 0.;
534 }
535 
536 void
537 Simulation::writeMessage(const std::string& msg) {
539 }
540 
541 
542 void
543 Simulation::subscribe(const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) {
544  libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_SIM_VARIABLE, "", begin, end, -1, -1, varIDs, params);
545 }
546 
547 
549 Simulation::getSubscriptionResults() {
551 }
552 
553 
555 
556 }
557 
558 
559 /****************************************************************************/
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:38
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN)
Definition: Domain.h:76
static void writeTypedDouble(tcpip::Storage &content, double value)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
Definition: StorageHelper.h:85
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:50
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static void writeTypedInt(tcpip::Storage &content, int value)
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
An error which allows to continue.
Definition: TraCIDefs.h:130
std::string intended
id of the intended vehicle for public transport ride
Definition: TraCIDefs.h:523
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:507
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:513
double length
length in m
Definition: TraCIDefs.h:521
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:517
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:527
std::string description
arbitrary description string
Definition: TraCIDefs.h:531
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:511
double cost
effort needed
Definition: TraCIDefs.h:519
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:525
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:515
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:529
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:509
void simulationStep(double time)
Sends a SimulationStep command.
Definition: Connection.cpp:121
static void connect(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Definition: Connection.h:51
static bool isActive()
Definition: Connection.h:59
void close()
ends the simulation and closes the connection
Definition: Connection.cpp:90
const std::string & getLabel()
Definition: Connection.h:67
static Connection & getActive()
Definition: Connection.h:55
void setOrder(int order)
Sends a SetOrder command.
Definition: Connection.cpp:149
libsumo::SubscriptionResults getAllSubscriptionResults(const int domain)
Definition: Connection.h:74
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.
Definition: Connection.cpp:202
tcpip::Storage & doCommand(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.cpp:326
static void switchCon(const std::string &label)
Definition: Connection.h:63
static libsumo::TraCIPosition getPos(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:149
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:170
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:124
static libsumo::TraCIStage getTraCIStage(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:184
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:132
static libsumo::TraCIPosition getPos3D(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:157
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:128
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition: Domain.h:110
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:222
static int getFreeSocketPort()
Returns an free port on the system.
Definition: socket.cpp:118
virtual std::string readString()
Definition: storage.cpp:180
virtual void writeString(const std::string &s)
Definition: storage.cpp:197
virtual void writeDouble(double)
Definition: storage.cpp:354
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:247
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
virtual int readByte()
Definition: storage.cpp:128
virtual double readDouble()
Definition: storage.cpp:362
virtual int readInt()
Definition: storage.cpp:311
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_SUBSCRIBE_SIM_VARIABLE
TRACI_CONST int CMD_LOAD
TRACI_CONST int POSITION_3D
TRACI_CONST int POSITION_ROADMAP
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
TRACI_CONST int FIND_INTERMODAL_ROUTE
TRACI_CONST int VAR_DEPARTED_PERSONS_NUMBER
TRACI_CONST int TYPE_UBYTE
TRACI_CONST int VAR_END
TRACI_CONST int POSITION_2D
TRACI_CONST int VAR_ARRIVED_PERSONS_IDS
TRACI_CONST int VAR_TIME
TRACI_CONST int VAR_NET_BOUNDING_BOX
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int VAR_PENDING_VEHICLES
TRACI_CONST int VAR_BUS_STOP_ID_LIST
TRACI_CONST int POSITION_LON_LAT
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 REQUEST_DRIVINGDIST
TRACI_CONST int VAR_DEPARTED_PERSONS_IDS
TRACI_CONST int VAR_ARRIVED_PERSONS_NUMBER
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_IDS
TRACI_CONST int FIND_ROUTE
TRACI_CONST int VAR_STOP_ENDING_VEHICLES_NUMBER
TRACI_CONST int POSITION_CONVERSION
TRACI_CONST int DISTANCE_REQUEST
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 REQUEST_AIRDIST
TRACI_CONST int VAR_BUS_STOP_WAITING
TRACI_CONST int VAR_TIME_STEP
TRACI_CONST int VAR_ARRIVED_VEHICLES_IDS
TRACI_CONST int RESPONSE_SUBSCRIBE_SIM_VARIABLE
TRACI_CONST int VAR_PARKING_STARTING_VEHICLES_NUMBER
TRACI_CONST int POSITION_LON_LAT_ALT
TRACI_CONST int VAR_EMERGENCYSTOPPING_VEHICLES_NUMBER
std::map< int, std::shared_ptr< libsumo::TraCIResult > > TraCIResults
{variable->value}
Definition: TraCIDefs.h:276
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom
A 3D-position.
Definition: TraCIDefs.h:164
A list of positions.
Definition: TraCIDefs.h:207
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:176