Eclipse SUMO - Simulation of Urban MObility
Connection.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-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 /****************************************************************************/
20 // C++ TraCI client API implementation
21 /****************************************************************************/
22 #pragma once
23 #include <vector>
24 #include <map>
25 #include <limits>
26 #include <string>
27 #include <sstream>
28 #include <iomanip>
29 #include <foreign/tcpip/socket.h>
30 #include <libsumo/Subscription.h>
31 
32 
33 // ===========================================================================
34 // global definitions
35 // ===========================================================================
36 #define PRECISION 2
37 
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
42 namespace libtraci {
47 class Connection {
48 public:
49  static void connect(const std::string& host, int port, int numRetries, const std::string& label, FILE* const pipe) {
50  myConnections[label] = new Connection(host, port, numRetries, label, pipe);
51  }
52 
53  static Connection& getActive() {
54  return *myActive;
55  }
56 
57  static bool isActive() {
58  return myActive != nullptr;
59  }
60 
61  static void switchCon(const std::string& label) {
62  myActive = myConnections.find(label)->second;
63  }
64 
65  const std::string& getLabel() {
66  return myLabel;
67  }
68 
70  void close();
71 
73  return mySubscriptionResults[domain];
74  }
75 
77  return myContextSubscriptionResults[domain];
78  }
79 
82 
85  void simulationStep(double time);
86 
87 
90  void send_commandSetOrder(int order);
91 
99  void createCommand(int cmdID, int varID, const std::string& objID, tcpip::Storage* add = nullptr) const;
100  void createFilterCommand(int cmdID, int varID, tcpip::Storage* add = nullptr) const;
101 
102 
110  void subscribeObjectVariable(int domID, const std::string& objID, double beginTime, double endTime, const std::vector<int>& vars, const libsumo::TraCIResults& params);
111 
112 
122  void subscribeObjectContext(int domID, const std::string& objID, double beginTime, double endTime,
123  int domain, double range, const std::vector<int>& vars, const libsumo::TraCIResults& params);
125 
126 
129 
136  void check_resultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId = false, std::string* acknowledgement = 0);
137 
141  int check_commandGetResult(tcpip::Storage& inMsg, int command, int expectedType = -1, bool ignoreCommandId = false) const;
142 
143  bool processGet(int command, int expectedType, bool ignoreCommandId = false);
145 
146  int getUnsignedByte(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
147  createCommand(command, var, id, add);
148  if (processGet(command, libsumo::TYPE_UBYTE)) {
149  return myInput.readUnsignedByte();
150  }
152  }
153  int getByte(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
154  createCommand(command, var, id, add);
155  if (processGet(command, libsumo::TYPE_BYTE)) {
156  return myInput.readByte();
157  }
159  }
160  int getInt(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
161  createCommand(command, var, id, add);
162  if (processGet(command, libsumo::TYPE_INTEGER)) {
163  return myInput.readInt();
164  }
166  }
167  double getDouble(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
168  createCommand(command, var, id, add);
169  if (processGet(command, libsumo::TYPE_DOUBLE)) {
170  return myInput.readDouble();
171  }
173  }
174  libsumo::TraCIPositionVector getPolygon(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
176  createCommand(command, var, id, add);
177  if (processGet(command, libsumo::TYPE_POLYGON)) {
178  int size = myInput.readUnsignedByte();
179  if (size == 0) {
180  size = myInput.readInt();
181  }
182  for (int i = 0; i < size; ++i) {
184  p.x = myInput.readDouble();
185  p.y = myInput.readDouble();
186  p.z = 0.;
187  ret.push_back(p);
188  }
189  }
190  return ret;
191  }
192  libsumo::TraCIPosition getPos(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
194  createCommand(command, var, id, add);
195  if (processGet(command, libsumo::POSITION_2D)) {
196  p.x = myInput.readDouble();
197  p.y = myInput.readDouble();
198  }
199  return p;
200  }
201 
202  libsumo::TraCIPosition getPos3D(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
204  createCommand(command, var, id, add);
205  if (processGet(command, libsumo::POSITION_3D)) {
206  p.x = myInput.readDouble();
207  p.y = myInput.readDouble();
208  p.z = myInput.readDouble();
209  }
210  return p;
211  }
212  std::string getString(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
213  createCommand(command, var, id, add);
214  if (processGet(command, libsumo::TYPE_STRING)) {
215  return myInput.readString();
216  }
217  return "";
218  }
219  std::vector<std::string> getStringVector(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
220  std::vector<std::string> r;
221  createCommand(command, var, id, add);
222  if (processGet(command, libsumo::TYPE_STRINGLIST)) {
223  const int size = myInput.readInt();
224  for (int i = 0; i < size; ++i) {
225  r.push_back(myInput.readString());
226  }
227  }
228  return r;
229  }
230 
231  libsumo::TraCIColor getCol(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
233  createCommand(command, var, id, add);
234  if (processGet(command, libsumo::TYPE_COLOR)) {
235  c.r = (unsigned char)myInput.readUnsignedByte();
236  c.g = (unsigned char)myInput.readUnsignedByte();
237  c.b = (unsigned char)myInput.readUnsignedByte();
238  c.a = (unsigned char)myInput.readUnsignedByte();
239  }
240  return c;
241  }
242 
243  libsumo::TraCIStage getTraCIStage(int command, int var, const std::string& id, tcpip::Storage* add = nullptr) {
245  createCommand(command, var, id, add);
246  if (processGet(command, libsumo::TYPE_COMPOUND)) {
247  myInput.readInt(); // components
249  s.type = myInput.readInt();
250 
252  s.vType = myInput.readString();
253 
255  s.line = myInput.readString();
256 
259 
262 
265 
267  s.cost = myInput.readDouble();
268 
270  s.length = myInput.readDouble();
271 
274 
276  s.depart = myInput.readDouble();
277 
280 
283 
286  }
287  return s;
288  }
289 
290  tcpip::Storage& doCommand(int command, int var, const std::string& id, tcpip::Storage* add = nullptr);
291 
292  void setInt(int command, int var, const std::string& id, int value) {
293  tcpip::Storage content;
295  content.writeInt(value);
296  doCommand(command, var, id, &content);
297  }
298  void setDouble(int command, int var, const std::string& id, double value) {
299  tcpip::Storage content;
301  content.writeDouble(value);
302  doCommand(command, var, id, &content);
303  }
304 
305  void setString(int command, int var, const std::string& id, const std::string& value) {
306  tcpip::Storage content;
308  content.writeString(value);
309  doCommand(command, var, id, &content);
310  }
311 
312  void setStringVector(int command, int var, const std::string& id, const std::vector<std::string>& value) {
313  tcpip::Storage content;
315  content.writeStringList(value);
316  doCommand(command, var, id, &content);
317  }
318 
319  void setCol(int command, int var, const std::string& id, const libsumo::TraCIColor c) {
320  tcpip::Storage content;
322  content.writeUnsignedByte(c.r);
323  content.writeUnsignedByte(c.g);
324  content.writeUnsignedByte(c.b);
325  content.writeUnsignedByte(c.a);
326  doCommand(command, var, id, &content);
327  }
328 
329  void readVariableSubscription(int responseID, tcpip::Storage& inMsg);
330  void readContextSubscription(int responseID, tcpip::Storage& inMsg);
331  void readVariables(tcpip::Storage& inMsg, const std::string& objectID, int variableCount, libsumo::SubscriptionResults& into);
332 
333 private:
334  template <class T>
335  static inline std::string toString(const T& t, std::streamsize accuracy = PRECISION) {
336  std::ostringstream oss;
337  oss.setf(std::ios::fixed, std::ios::floatfield);
338  oss << std::setprecision(accuracy);
339  oss << t;
340  return oss.str();
341  }
342 
348  Connection(const std::string& host, int port, int numRetries, const std::string& label, FILE* const pipe);
349 
350 private:
351  const std::string myLabel;
352  FILE* const myProcessPipe;
359 
360  std::map<int, libsumo::SubscriptionResults> mySubscriptionResults;
361  std::map<int, libsumo::ContextSubscriptionResults> myContextSubscriptionResults;
362 
364  static std::map<const std::string, Connection*> myConnections;
365 
366 private:
369 
370 };
371 
372 }
#define PRECISION
Definition: Connection.h:36
std::string intended
id of the intended vehicle for public transport ride
Definition: TraCIDefs.h:494
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:478
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:484
double length
length in m
Definition: TraCIDefs.h:492
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:488
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:498
std::string description
arbitrary description string
Definition: TraCIDefs.h:502
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:482
double cost
effort needed
Definition: TraCIDefs.h:490
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:496
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:486
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:500
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:480
C++ TraCI client API implementation.
Definition: Connection.h:47
std::vector< std::string > getStringVector(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:219
void simulationStep(double time)
Sends a SimulationStep command.
Definition: Connection.cpp:101
void setCol(int command, int var, const std::string &id, const libsumo::TraCIColor c)
Definition: Connection.h:319
int getByte(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:153
static void connect(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Definition: Connection.h:49
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.
Definition: Connection.cpp:195
Connection(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Constructor, connects to the specified SUMO server.
Definition: Connection.cpp:45
static bool isActive()
Definition: Connection.h:57
void close()
ends the simulation and closes the connection
Definition: Connection.cpp:62
void setInt(int command, int var, const std::string &id, int value)
Definition: Connection.h:292
int check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
Validates the result state of a command.
Definition: Connection.cpp:329
libsumo::TraCIColor getCol(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:231
void setStringVector(int command, int var, const std::string &id, const std::vector< std::string > &value)
Definition: Connection.h:312
bool processGet(int command, int expectedType, bool ignoreCommandId=false)
Definition: Connection.cpp:353
void readVariableSubscription(int responseID, tcpip::Storage &inMsg)
Definition: Connection.cpp:446
libsumo::TraCIStage getTraCIStage(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:243
tcpip::Socket mySocket
The socket.
Definition: Connection.h:354
std::map< int, libsumo::SubscriptionResults > mySubscriptionResults
Definition: Connection.h:360
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0)
Validates the result state of a command.
Definition: Connection.cpp:290
tcpip::Storage myInput
The reusable input storage.
Definition: Connection.h:358
FILE *const myProcessPipe
Definition: Connection.h:352
libsumo::TraCIPosition getPos(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:192
const std::string & getLabel()
Definition: Connection.h:65
void createFilterCommand(int cmdID, int varID, tcpip::Storage *add=nullptr) const
Definition: Connection.cpp:172
void readVariables(tcpip::Storage &inMsg, const std::string &objectID, int variableCount, libsumo::SubscriptionResults &into)
Definition: Connection.cpp:378
std::map< int, libsumo::ContextSubscriptionResults > myContextSubscriptionResults
Definition: Connection.h:361
tcpip::Storage myOutput
The reusable output storage.
Definition: Connection.h:356
static Connection & getActive()
Definition: Connection.h:53
std::string getString(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:212
Connection & operator=(const Connection &)
Invalidated assignment operator.
libsumo::TraCIPosition getPos3D(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:202
libsumo::SubscriptionResults getAllSubscriptionResults(const int domain)
Definition: Connection.h:72
void setString(int command, int var, const std::string &id, const std::string &value)
Definition: Connection.h:305
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 std::map< const std::string, Connection * > myConnections
Definition: Connection.h:364
void setDouble(int command, int var, const std::string &id, double value)
Definition: Connection.h:298
const std::string myLabel
Definition: Connection.h:351
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...
Definition: Connection.cpp:143
void readContextSubscription(int responseID, tcpip::Storage &inMsg)
Definition: Connection.cpp:454
double getDouble(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:167
libsumo::ContextSubscriptionResults & getAllContextSubscriptionResults(const int domain)
Definition: Connection.h:76
static Connection * myActive
Definition: Connection.h:363
libsumo::TraCIPositionVector getPolygon(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:174
static std::string toString(const T &t, std::streamsize accuracy=PRECISION)
Definition: Connection.h:335
int getInt(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:160
int getUnsignedByte(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.h:146
static void switchCon(const std::string &label)
Definition: Connection.h:61
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.
Definition: Connection.cpp:249
virtual std::string readString()
Definition: storage.cpp:175
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
virtual void writeInt(int)
Definition: storage.cpp:316
virtual void writeDouble(double)
Definition: storage.cpp:349
virtual int readUnsignedByte()
Definition: storage.cpp:150
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:242
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
virtual int readByte()
Definition: storage.cpp:123
virtual std::vector< std::string > readStringList()
Definition: storage.cpp:206
virtual double readDouble()
Definition: storage.cpp:357
virtual int readInt()
Definition: storage.cpp:306
TRACI_CONST double INVALID_DOUBLE_VALUE
TRACI_CONST int TYPE_COLOR
TRACI_CONST int POSITION_3D
std::map< int, std::shared_ptr< TraCIResult > > TraCIResults
{variable->value}
Definition: TraCIDefs.h:248
TRACI_CONST int TYPE_COMPOUND
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:196
TRACI_CONST int TYPE_UBYTE
TRACI_CONST int POSITION_2D
TRACI_CONST int TYPE_POLYGON
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int TYPE_INTEGER
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:250
TRACI_CONST int INVALID_INT_VALUE
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int TYPE_BYTE
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251
TRACI_CONST int TYPE_STRING
A 3D-position.
Definition: TraCIDefs.h:141