Eclipse SUMO - Simulation of Urban MObility
TraCIServer.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
23 // TraCI server used to control sumo by a remote TraCI client
24 /****************************************************************************/
25 #ifndef TRACISERVER_H
26 #define TRACISERVER_H
27 
28 
29 // ===========================================================================
30 // included modules
31 // ===========================================================================
32 #include <config.h>
33 
34 #include <map>
35 #include <string>
36 #include <set>
37 
38 #define BUILD_TCPIP
39 #include <foreign/tcpip/socket.h>
40 #include <foreign/tcpip/storage.h>
42 #include <utils/common/SUMOTime.h>
43 #include <utils/common/ToString.h>
44 #include <utils/geom/Boundary.h>
45 #include <utils/geom/Position.h>
46 #include <utils/geom/GeomHelper.h>
48 #include <microsim/MSNet.h>
50 #include <libsumo/TraCIConstants.h>
51 #include <libsumo/Subscription.h>
52 #include <libsumo/TraCIDefs.h>
53 #include "TraCIServerAPI_Lane.h"
54 
55 
56 // ===========================================================================
57 // class definitions
58 // ===========================================================================
63 public:
65  typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
66 
68  return myTargetTime;
69  }
70 
72  return myInstance;
73  }
74 
77 
81  static void openSocket(const std::map<int, CmdExecutor>& execs);
82 
83 
85  static void close();
86 
87 
91  static bool wasClosed();
93 
94 
99 
101  void cleanup();
102 
103 
104  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
105 
108 
115  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
116 
117 
123  void writeStatusCmd(int commandId, int status, const std::string& description);
124 
125 
131  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
133 
134 
135 
136  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
137  if (myCurrentSocket == mySockets.end()) {
138  // Requested in context of a subscription update
139  return myVehicleStateChanges;
140  } else {
141  // Requested in the context of a custom query by active client
142  return myCurrentSocket->second->vehicleStateChanges;
143  }
144  }
145 
146  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
147 
148  void writePositionVector(tcpip::Storage& outputStorage, const libsumo::TraCIPositionVector& shape);
149 
150 
153 
160  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
161 
162 
169  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
170 
171 
178  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
179 
180 
187  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
188 
189 
196  bool readTypeCheckingDoubleList(tcpip::Storage& inputStorage, std::vector<double>& into);
197 
198 
205  bool readTypeCheckingColor(tcpip::Storage& inputStorage, libsumo::TraCIColor& into);
206 
207 
215 
216 
223  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
224 
225 
232  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
233 
234 
241  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
243 
244 
247  void setTargetTime(SUMOTime targetTime);
248 
249  std::vector<std::string>& getLoadArgs() {
250  return myLoadArgs;
251  }
252 
255  void initWrapper(const int domainID, const int variable, const std::string& objID);
256  bool wrapDouble(const std::string& objID, const int variable, const double value);
257  bool wrapInt(const std::string& objID, const int variable, const int value);
258  bool wrapString(const std::string& objID, const int variable, const std::string& value);
259  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
260  bool wrapPosition(const std::string& objID, const int variable, const libsumo::TraCIPosition& value);
261  bool wrapColor(const std::string& objID, const int variable, const libsumo::TraCIColor& value);
262  bool wrapRoadPosition(const std::string& objID, const int variable, const libsumo::TraCIRoadPosition& value);
265 
266 
267 private:
271  TraCIServer(const SUMOTime begin, const int port, const int numClients);
272 
273 
275  virtual ~TraCIServer();
276 
277 
278 
279  struct SocketInfo {
280  public:
283  : targetTime(t), socket(socket) {}
286  delete socket;
287  }
293  std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
294  private:
295  SocketInfo(const SocketInfo&);
296  };
297 
300 
304  bool commandGetVersion();
305 
306 
311 
312 
316  int readCommandID(int& commandStart, int& commandLength);
317 
319  int dispatchCommand();
320 
323  void checkClientOrdering();
324 
327 
329  SUMOTime nextTargetTime() const;
330 
332  void sendOutputToAll() const;
333 
336 
338  std::map<int, SocketInfo*>::iterator removeCurrentSocket();
339 
340 
341 private:
344 
346  static bool myDoCloseConnection;
347 
350 
353  std::map<int, SocketInfo*> mySockets;
354 
356  std::map<int, SocketInfo*> mySocketReorderRequests;
357 
359  std::map<int, SocketInfo*>::iterator myCurrentSocket;
360 
363 
366 
369 
372 
375 
377  std::map<int, CmdExecutor> myExecutors;
378 
380  std::map<int, int> myParameterSizes;
381 
382  std::vector<std::string> myLoadArgs;
383 
385  std::vector<libsumo::Subscription> mySubscriptions;
386 
389 
397  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
398 
399 private:
400  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
402  void removeSubscription(int commandId, const std::string& identity, int domain);
404  std::string& errors);
405 
406 
407  bool addSubscriptionFilter();
408  void removeFilters();
409  void addSubscriptionFilterLanes(std::vector<int> lanes);
411  void addSubscriptionFilterDownstreamDistance(double dist);
412  void addSubscriptionFilterUpstreamDistance(double dist);
414  // TODO: for libsumo, implement convenience definitions present in python client:
415  // void addSubscriptionFilterCF();
416  // void addSubscriptionFilterLC(int direction);
419  void addSubscriptionFilterVType(std::set<std::string> vTypes);
421 
422  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
423 
425  bool centralObject(const libsumo::Subscription& s, const std::string& objID);
426 
427 
428 private:
430  TraCIServer& operator=(const TraCIServer& s);
431 
432 };
433 
434 
435 #endif
void addSubscriptionFilterDownstreamDistance(double dist)
tcpip::Storage mySubscriptionCache
The last timestep&#39;s subscription results.
Definition: TraCIServer.h:374
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
Representation of a subscription.
Definition: Subscription.h:65
long long int SUMOTime
Definition: SUMOTime.h:35
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:567
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
bool wrapString(const std::string &objID, const int variable, const std::string &value)
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:122
bool processSingleSubscription(const libsumo::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
virtual ~TraCIServer()
Destructor.
bool commandGetVersion()
Returns the TraCI-version.
bool wrapInt(const std::string &objID, const int variable, const int value)
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
bool(* CmdExecutor)(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Definition of a method to be called for serving an associated commandID.
Definition: TraCIServer.h:65
tcpip::Storage myOutputStorage
The storage to write to.
Definition: TraCIServer.h:368
void initialiseSubscription(libsumo::Subscription &s)
void postProcessSimulationStep()
Handles subscriptions to send after a simstep2 command.
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
std::vector< std::string > myLoadArgs
Definition: TraCIServer.h:382
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
tcpip::Storage myInputStorage
The storage to read from.
Definition: TraCIServer.h:365
bool centralObject(const libsumo::Subscription &s, const std::string &objID)
check whether a found objID refers to the central object of a context subscription ...
void cleanup()
clean up subscriptions
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
bool wrapRoadPosition(const std::string &objID, const int variable, const libsumo::TraCIRoadPosition &value)
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:249
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:346
std::map< int, SocketInfo * >::iterator removeCurrentSocket()
removes myCurrentSocket from mySockets and returns an iterator pointing to the next member according ...
SUMOTime targetTime
Target time: next point of action for the client.
Definition: TraCIServer.h:289
void addSubscriptionFilterVClass(SVCPermissions vClasses)
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:377
void removeSubscription(int commandId, const std::string &identity, int domain)
static void close()
request termination of connection
bool addObjectVariableSubscription(const int commandId, const bool hasContext)
tcpip::Socket * socket
Socket object for this client.
Definition: TraCIServer.h:291
Representation of a vehicle.
Definition: SUMOVehicle.h:61
bool readTypeCheckingDoubleList(tcpip::Storage &inputStorage, std::vector< double > &into)
Reads the value type and a double list, verifying the type.
void addSubscriptionFilterNoOpposite()
A list of positions.
SUMOTime getTargetTime() const
Definition: TraCIServer.h:67
bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
SocketInfo(tcpip::Socket *socket, SUMOTime t)
constructor
Definition: TraCIServer.h:282
void addSubscriptionFilterUpstreamDistance(double dist)
bool addSubscriptionFilter()
tcpip::Storage & getWrapperStorage()
libsumo::Subscription * myLastContextSubscription
The last modified context subscription (the one to add a filter to, see (), currently only for vehicl...
Definition: TraCIServer.h:388
void addSubscriptionFilterTurn()
void addSubscriptionFilterLeadFollow()
bool isVehicleToVehicleContextSubscription(const libsumo::Subscription &s)
void removeFilters()
int readCommandID(int &commandStart, int &commandLength)
Reads the next command ID from the input storage.
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:343
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
void sendOutputToAll() const
send out subscription results (actually just the content of myOutputStorage) to clients which will ac...
TraCIServer(const SUMOTime begin, const int port, const int numClients)
Constructor.
void sendSingleSimStepResponse()
sends an empty response to a simstep command to the current client. (This applies to a situation wher...
bool wrapColor(const std::string &objID, const int variable, const libsumo::TraCIColor &value)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:536
void processCommandsUntilSimStep(SUMOTime step)
process all commands until the next SUMO simulation step. It is guaranteed that t->getTargetTime() >=...
void processReorderingRequests()
checks for and processes reordering requests (relevant for multiple clients)
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:150
bool wrapPosition(const std::string &objID, const int variable, const libsumo::TraCIPosition &value)
std::map< MSNet::VehicleState, std::vector< std::string > > vehicleStateChanges
container for vehicle state changes since last step taken by this client
Definition: TraCIServer.h:293
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:359
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
SUMOTime nextTargetTime() const
get the minimal next target time among all clients
void setTargetTime(SUMOTime targetTime)
Sets myTargetTime on server and sockets to the given value.
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:136
static TraCIServer * getInstance()
Definition: TraCIServer.h:71
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:362
std::vector< libsumo::Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:385
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
~SocketInfo()
destructor
Definition: TraCIServer.h:285
void addSubscriptionFilterLanes(std::vector< int > lanes)
int dispatchCommand()
Handles command, writes response to myOutputStorage.
tcpip::Storage myWrapperStorage
A temporary storage to let the wrapper write to.
Definition: TraCIServer.h:371
std::map< int, SocketInfo * > mySockets
The socket connections to the clients the first component (index) determines the client&#39;s order (lowe...
Definition: TraCIServer.h:353
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
std::map< int, SocketInfo * > mySocketReorderRequests
This stores the setOrder(int) requests of the clients.
Definition: TraCIServer.h:356
void initWrapper(const int domainID, const int variable, const std::string &objID)
A 3D-position.
Definition: TraCIDefs.h:110
void addSubscriptionFilterVType(std::set< std::string > vTypes)
static bool wasClosed()
check whether close was requested
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: TraCIServer.h:397
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
bool wrapDouble(const std::string &objID, const int variable, const double value)
tcpip::Socket * myServerSocket
The server socket.
Definition: TraCIServer.h:349
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:380
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...