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-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 /****************************************************************************/
26 // TraCI server used to control sumo by a remote TraCI client
27 /****************************************************************************/
28 #pragma once
29 #include <config.h>
30 
31 #include <map>
32 #include <string>
33 #include <set>
34 
35 #define BUILD_TCPIP
36 #include <foreign/tcpip/socket.h>
37 #include <foreign/tcpip/storage.h>
39 #include <utils/common/SUMOTime.h>
40 #include <utils/common/ToString.h>
41 #include <utils/geom/Boundary.h>
42 #include <utils/geom/Position.h>
43 #include <utils/geom/GeomHelper.h>
45 #include <microsim/MSNet.h>
47 #include <libsumo/TraCIConstants.h>
48 #include <libsumo/Subscription.h>
49 #include <libsumo/TraCIDefs.h>
50 #include "TraCIServerAPI_Lane.h"
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
60 public:
62  typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
63 
65  return myTargetTime;
66  }
67 
69  return myInstance;
70  }
71 
74 
78  static void openSocket(const std::map<int, CmdExecutor>& execs);
79 
80 
82  static void close();
83 
84 
88  static bool wasClosed();
90 
91 
96 
98  void cleanup();
99 
100 
101  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
102 
105 
112  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
113 
114 
120  void writeStatusCmd(int commandId, int status, const std::string& description);
121 
122 
128  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
130 
131 
132 
133  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
134  if (myCurrentSocket == mySockets.end()) {
135  // Requested in context of a subscription update
136  return myVehicleStateChanges;
137  } else {
138  // Requested in the context of a custom query by active client
139  return myCurrentSocket->second->vehicleStateChanges;
140  }
141  }
142 
143  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
144 
145  void writePositionVector(tcpip::Storage& outputStorage, const libsumo::TraCIPositionVector& shape);
146 
147 
150 
157  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
158 
159 
166  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
167 
168 
175  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
176 
177 
184  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
185 
186 
193  bool readTypeCheckingDoubleList(tcpip::Storage& inputStorage, std::vector<double>& into);
194 
195 
202  bool readTypeCheckingColor(tcpip::Storage& inputStorage, libsumo::TraCIColor& into);
203 
204 
212 
213 
220  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
221 
222 
229  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
230 
231 
238  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
240 
241 
244  void stateLoaded(SUMOTime targetTime);
245 
246  std::vector<std::string>& getLoadArgs() {
247  return myLoadArgs;
248  }
249 
252  void initWrapper(const int domainID, const int variable, const std::string& objID);
253  bool wrapDouble(const std::string& objID, const int variable, const double value);
254  bool wrapInt(const std::string& objID, const int variable, const int value);
255  bool wrapString(const std::string& objID, const int variable, const std::string& value);
256  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
257  bool wrapPosition(const std::string& objID, const int variable, const libsumo::TraCIPosition& value);
258  bool wrapColor(const std::string& objID, const int variable, const libsumo::TraCIColor& value);
259  bool wrapRoadPosition(const std::string& objID, const int variable, const libsumo::TraCIRoadPosition& value);
262 
263 
264 private:
268  TraCIServer(const SUMOTime begin, const int port, const int numClients);
269 
270 
272  virtual ~TraCIServer();
273 
274 
275 
276  struct SocketInfo {
277  public:
280  : targetTime(t), socket(socket) {}
283  delete socket;
284  }
290  std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
291  private:
293  };
294 
297 
301  bool commandGetVersion();
302 
303 
308 
309 
313  int readCommandID(int& commandStart, int& commandLength);
314 
316  int dispatchCommand();
317 
320  void checkClientOrdering();
321 
324 
326  SUMOTime nextTargetTime() const;
327 
329  void sendOutputToAll() const;
330 
333 
335  std::map<int, SocketInfo*>::iterator removeCurrentSocket();
336 
337 
338 private:
341 
343  static bool myDoCloseConnection;
344 
347  std::map<int, SocketInfo*> mySockets;
348 
350  std::map<int, SocketInfo*> mySocketReorderRequests;
351 
353  std::map<int, SocketInfo*>::iterator myCurrentSocket;
354 
357 
360 
363 
366 
369 
371  std::map<int, CmdExecutor> myExecutors;
372 
374  std::map<int, int> myParameterSizes;
375 
376  std::vector<std::string> myLoadArgs;
377 
379  std::vector<libsumo::Subscription> mySubscriptions;
380 
383 
391  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
392 
393 private:
394  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
396  void removeSubscription(int commandId, const std::string& identity, int domain);
398  std::string& errors);
399 
400 
401  bool addSubscriptionFilter();
402  void removeFilters();
403  void addSubscriptionFilterLanes(std::vector<int> lanes);
405  void addSubscriptionFilterDownstreamDistance(double dist);
406  void addSubscriptionFilterUpstreamDistance(double dist);
408  // TODO: for libsumo, implement convenience definitions present in python client:
409  // void addSubscriptionFilterCF();
410  // void addSubscriptionFilterLC(int direction);
413  void addSubscriptionFilterVType(std::set<std::string> vTypes);
418  void addSubscriptionFilterFieldOfVision(double openingAngle);
423  void addSubscriptionFilterLateralDistance(double dist);
424 
425  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
426 
428  bool centralObject(const libsumo::Subscription& s, const std::string& objID);
429 
430 
431 private:
434 
435 };
436 
437 
long long int SUMOTime
Definition: SUMOTime.h:31
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:620
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:587
A list of positions.
Representation of a vehicle.
Definition: SUMOVehicle.h:58
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:59
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:62
void addSubscriptionFilterDownstreamDistance(double dist)
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:343
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:374
tcpip::Storage myWrapperStorage
A temporary storage to let the wrapper write to.
Definition: TraCIServer.h:365
std::map< int, SocketInfo * > mySockets
The socket connections to the clients the first component (index) determines the client's order (lowe...
Definition: TraCIServer.h:347
void addSubscriptionFilterLateralDistance(double dist)
Filter only vehicles within the given lateral distance.
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static bool wasClosed()
check whether close was requested
bool centralObject(const libsumo::Subscription &s, const std::string &objID)
check whether a found objID refers to the central object of a context subscription
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:353
void addSubscriptionFilterVType(std::set< std::string > vTypes)
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool wrapInt(const std::string &objID, const int variable, const int value)
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
std::map< int, SocketInfo * >::iterator removeCurrentSocket()
removes myCurrentSocket from mySockets and returns an iterator pointing to the next member according ...
tcpip::Storage myOutputStorage
The storage to write to.
Definition: TraCIServer.h:362
bool addObjectVariableSubscription(const int commandId, const bool hasContext)
void addSubscriptionFilterUpstreamDistance(double dist)
SUMOTime getTargetTime() const
Definition: TraCIServer.h:64
void stateLoaded(SUMOTime targetTime)
updates myTargetTime and resets vehicle state changes after loading a simulation state
void addSubscriptionFilterLeadFollow()
void addSubscriptionFilterNoOpposite()
tcpip::Storage & getWrapperStorage()
SUMOTime nextTargetTime() const
get the minimal next target time among all clients
void removeSubscription(int commandId, const std::string &identity, int domain)
bool wrapDouble(const std::string &objID, const int variable, const double value)
tcpip::Storage mySubscriptionCache
The last timestep's subscription results.
Definition: TraCIServer.h:368
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
bool wrapColor(const std::string &objID, const int variable, const libsumo::TraCIColor &value)
bool wrapString(const std::string &objID, const int variable, const std::string &value)
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:246
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
int dispatchCommand()
Handles command, writes response to myOutputStorage.
void initWrapper(const int domainID, const int variable, const std::string &objID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
void processReorderingRequests()
checks for and processes reordering requests (relevant for multiple clients)
void sendOutputToAll() const
send out subscription results (actually just the content of myOutputStorage) to clients which will ac...
void cleanup()
clean up subscriptions
static void close()
request termination of connection
void processCommandsUntilSimStep(SUMOTime step)
process all commands until the next SUMO simulation step. It is guaranteed that t->getTargetTime() >=...
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.
bool commandGetVersion()
Returns the TraCI-version.
bool wrapPosition(const std::string &objID, const int variable, const libsumo::TraCIPosition &value)
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
std::vector< std::string > myLoadArgs
Definition: TraCIServer.h:376
void removeFilters()
static TraCIServer * getInstance()
Definition: TraCIServer.h:68
void addSubscriptionFilterLanes(std::vector< int > lanes)
bool readTypeCheckingDoubleList(tcpip::Storage &inputStorage, std::vector< double > &into)
Reads the value type and a double list, verifying the type.
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:356
virtual ~TraCIServer()
Destructor.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
void addSubscriptionFilterTurn()
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: TraCIServer.h:391
int readCommandID(int &commandStart, int &commandLength)
Reads the next command ID from the input storage.
void sendSingleSimStepResponse()
sends an empty response to a simstep command to the current client. (This applies to a situation wher...
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint.
Definition: TraCIServer.h:371
std::vector< libsumo::Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:379
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
TraCIServer(const SUMOTime begin, const int port, const int numClients)
Constructor.
tcpip::Storage myInputStorage
The storage to read from.
Definition: TraCIServer.h:359
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:133
libsumo::Subscription * myLastContextSubscription
The last modified context subscription (the one to add a filter to, see @addSubscriptionFilter(),...
Definition: TraCIServer.h:382
bool wrapRoadPosition(const std::string &objID, const int variable, const libsumo::TraCIRoadPosition &value)
void addSubscriptionFilterVClass(SVCPermissions vClasses)
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:340
std::map< int, SocketInfo * > mySocketReorderRequests
This stores the setOrder(int) requests of the clients.
Definition: TraCIServer.h:350
bool addSubscriptionFilter()
bool processSingleSubscription(const libsumo::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
void initialiseSubscription(libsumo::Subscription &s)
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
void addSubscriptionFilterFieldOfVision(double openingAngle)
Filter only vehicles within field of vision.
bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
Representation of a subscription.
Definition: Subscription.h:67
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:196
SocketInfo(const SocketInfo &)
std::map< MSNet::VehicleState, std::vector< std::string > > vehicleStateChanges
container for vehicle state changes since last step taken by this client
Definition: TraCIServer.h:290
SocketInfo(tcpip::Socket *socket, SUMOTime t)
constructor
Definition: TraCIServer.h:279
tcpip::Socket * socket
Socket object for this client.
Definition: TraCIServer.h:288
SUMOTime targetTime
Target time: next point of action for the client.
Definition: TraCIServer.h:286
A 3D-position.
Definition: TraCIDefs.h:141
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:153