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-2018 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/Subscription.h>
51 #include <libsumo/TraCIDefs.h>
52 #include "TraCIConstants.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  bool isEmbedded() const {
71  return myAmEmbedded;
72  }
74  return myInstance;
75  }
76 
79 
83  static void openSocket(const std::map<int, CmdExecutor>& execs);
84 
85 
87  static void close();
88 
89 
93  static bool wasClosed();
95 
96 
101 
103  void cleanup();
104 
105 
106 #ifdef HAVE_PYTHON
107  static std::string execute(std::string cmd);
109 
111  static void runEmbedded(std::string pyFile);
112 #endif
113 
114  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
115 
118 
125  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
126 
127 
133  void writeStatusCmd(int commandId, int status, const std::string& description);
134 
135 
141  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
143 
144 
145 
146  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
147  if (myAmEmbedded) {
148  return myVehicleStateChanges;
149  } else if (myCurrentSocket == mySockets.end()) {
150  // Requested in context of a subscription update
151  return myVehicleStateChanges;
152  } else {
153  // Requested in the context of a custom query by active client
154  return myCurrentSocket->second->vehicleStateChanges;
155  }
156  }
157 
158  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
159 
160 
163 
170  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
171 
172 
179  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
180 
181 
188  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
189 
190 
197  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
198 
199 
206  bool readTypeCheckingColor(tcpip::Storage& inputStorage, libsumo::TraCIColor& into);
207 
208 
216 
217 
224  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
225 
226 
233  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
234 
235 
242  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
244 
245 
248  void setTargetTime(SUMOTime targetTime);
249 
250  std::vector<std::string>& getLoadArgs() {
251  return myLoadArgs;
252  }
253 
256  void initWrapper(const int domainID, const int variable, const std::string& objID);
257  bool wrapDouble(const std::string& objID, const int variable, const double value);
258  bool wrapInt(const std::string& objID, const int variable, const int value);
259  bool wrapString(const std::string& objID, const int variable, const std::string& value);
260  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
261  bool wrapPosition(const std::string& objID, const int variable, const libsumo::TraCIPosition& value);
262  bool wrapColor(const std::string& objID, const int variable, const libsumo::TraCIColor& 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  const bool myAmEmbedded;
378 
380  std::map<int, CmdExecutor> myExecutors;
381 
383  std::map<int, int> myParameterSizes;
384 
385  std::vector<std::string> myLoadArgs;
386 
388  std::vector<libsumo::Subscription> mySubscriptions;
389 
392 
400  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
401 
402 private:
403  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
405  void removeSubscription(int commandId, const std::string& identity, int domain);
407  std::string& errors);
408 
409 
410  bool addSubscriptionFilter();
411  void removeFilters();
412  void addSubscriptionFilterLanes(std::vector<int> lanes);
414  void addSubscriptionFilterDownstreamDistance(double dist);
415  void addSubscriptionFilterUpstreamDistance(double dist);
420  void addSubscriptionFilterVType(std::vector<std::string> vTypes);
422 
423  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
424 
426  bool centralObject(const libsumo::Subscription& s, const std::string& objID);
427 
428 
429 private:
431  TraCIServer& operator=(const TraCIServer& s);
432 
433 };
434 
435 
436 #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:63
long long int SUMOTime
Definition: SUMOTime.h:36
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:518
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
bool wrapString(const std::string &objID, const int variable, const std::string &value)
bool processSingleSubscription(const libsumo::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
virtual ~TraCIServer()
Destructor.
bool commandGetVersion()
Returns the TraCI-version.
void addSubscriptionFilterVType(std::vector< std::string > vTypes)
bool wrapInt(const std::string &objID, const int variable, const int value)
void addSubscriptionFilterTurnManeuver()
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:385
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.
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:250
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:380
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
void addSubscriptionFilterCFManeuver()
Representation of a vehicle.
Definition: SUMOVehicle.h:60
void addSubscriptionFilterNoOpposite()
A list of positions.
void addSubscriptionFilterLCManeuver()
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:391
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:487
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:377
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.
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:146
static TraCIServer * getInstance()
Definition: TraCIServer.h:73
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:388
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
bool isEmbedded() const
Definition: TraCIServer.h:70
void initWrapper(const int domainID, const int variable, const std::string &objID)
A 3D-position.
Definition: TraCIDefs.h:107
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:400
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:383
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...