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>
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  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  void writePositionVector(tcpip::Storage& outputStorage, const libsumo::TraCIPositionVector& shape);
161 
162 
165 
172  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
173 
174 
181  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
182 
183 
190  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
191 
192 
199  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
200 
201 
208  bool readTypeCheckingColor(tcpip::Storage& inputStorage, libsumo::TraCIColor& into);
209 
210 
218 
219 
226  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
227 
228 
235  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
236 
237 
244  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
246 
247 
250  void setTargetTime(SUMOTime targetTime);
251 
252  std::vector<std::string>& getLoadArgs() {
253  return myLoadArgs;
254  }
255 
258  void initWrapper(const int domainID, const int variable, const std::string& objID);
259  bool wrapDouble(const std::string& objID, const int variable, const double value);
260  bool wrapInt(const std::string& objID, const int variable, const int value);
261  bool wrapString(const std::string& objID, const int variable, const std::string& value);
262  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
263  bool wrapPosition(const std::string& objID, const int variable, const libsumo::TraCIPosition& value);
264  bool wrapColor(const std::string& objID, const int variable, const libsumo::TraCIColor& value);
267 
268 
269 private:
273  TraCIServer(const SUMOTime begin, const int port, const int numClients);
274 
275 
277  virtual ~TraCIServer();
278 
279 
280 
281  struct SocketInfo {
282  public:
285  : targetTime(t), socket(socket) {}
288  delete socket;
289  }
295  std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
296  private:
297  SocketInfo(const SocketInfo&);
298  };
299 
302 
306  bool commandGetVersion();
307 
308 
313 
314 
318  int readCommandID(int& commandStart, int& commandLength);
319 
321  int dispatchCommand();
322 
325  void checkClientOrdering();
326 
329 
331  SUMOTime nextTargetTime() const;
332 
334  void sendOutputToAll() const;
335 
338 
340  std::map<int, SocketInfo*>::iterator removeCurrentSocket();
341 
342 
343 private:
346 
348  static bool myDoCloseConnection;
349 
352 
355  std::map<int, SocketInfo*> mySockets;
356 
358  std::map<int, SocketInfo*> mySocketReorderRequests;
359 
361  std::map<int, SocketInfo*>::iterator myCurrentSocket;
362 
365 
368 
371 
374 
377 
379  const bool myAmEmbedded;
380 
382  std::map<int, CmdExecutor> myExecutors;
383 
385  std::map<int, int> myParameterSizes;
386 
387  std::vector<std::string> myLoadArgs;
388 
390  std::vector<libsumo::Subscription> mySubscriptions;
391 
394 
402  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
403 
404 private:
405  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
407  void removeSubscription(int commandId, const std::string& identity, int domain);
409  std::string& errors);
410 
411 
412  bool addSubscriptionFilter();
413  void removeFilters();
414  void addSubscriptionFilterLanes(std::vector<int> lanes);
416  void addSubscriptionFilterDownstreamDistance(double dist);
417  void addSubscriptionFilterUpstreamDistance(double dist);
419  // TODO: for libsumo, implement convenience definitions present in python client:
420  // void addSubscriptionFilterCF();
421  // void addSubscriptionFilterLC(int direction);
424  void addSubscriptionFilterVType(std::set<std::string> vTypes);
426 
427  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
428 
430  bool centralObject(const libsumo::Subscription& s, const std::string& objID);
431 
432 
433 private:
435  TraCIServer& operator=(const TraCIServer& s);
436 
437 };
438 
439 
440 #endif
void addSubscriptionFilterDownstreamDistance(double dist)
tcpip::Storage mySubscriptionCache
The last timestep&#39;s subscription results.
Definition: TraCIServer.h:376
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:36
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:525
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.
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:370
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:387
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:367
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:252
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:348
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:291
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:382
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:293
Representation of a vehicle.
Definition: SUMOVehicle.h:60
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:284
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:393
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:345
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:494
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:379
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:145
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:295
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:361
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:364
std::vector< libsumo::Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:390
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
~SocketInfo()
destructor
Definition: TraCIServer.h:287
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:373
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:355
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:358
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
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:402
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:351
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:385
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...