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 /****************************************************************************/
22 // TraCI server used to control sumo by a remote TraCI client
23 /****************************************************************************/
24 #ifndef TRACISERVER_H
25 #define TRACISERVER_H
26 
27 
28 // ===========================================================================
29 // included modules
30 // ===========================================================================
31 #include <config.h>
32 
33 #include <map>
34 #include <string>
35 #include <set>
36 
37 #define BUILD_TCPIP
38 #include <foreign/tcpip/socket.h>
39 #include <foreign/tcpip/storage.h>
41 #include <utils/common/SUMOTime.h>
42 #include <utils/common/ToString.h>
43 #include <utils/geom/Boundary.h>
44 #include <utils/geom/Position.h>
45 #include <utils/geom/GeomHelper.h>
47 #include <microsim/MSNet.h>
49 #include <libsumo/TraCIConstants.h>
50 #include <libsumo/Subscription.h>
51 #include <libsumo/TraCIDefs.h>
52 #include "TraCIServerAPI_Lane.h"
53 
54 
55 // ===========================================================================
56 // class definitions
57 // ===========================================================================
62 public:
64  typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
65 
67  return myTargetTime;
68  }
69 
71  return myInstance;
72  }
73 
76 
80  static void openSocket(const std::map<int, CmdExecutor>& execs);
81 
82 
84  static void close();
85 
86 
90  static bool wasClosed();
92 
93 
98 
100  void cleanup();
101 
102 
103  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
104 
107 
114  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
115 
116 
122  void writeStatusCmd(int commandId, int status, const std::string& description);
123 
124 
130  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
132 
133 
134 
135  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
136  if (myCurrentSocket == mySockets.end()) {
137  // Requested in context of a subscription update
138  return myVehicleStateChanges;
139  } else {
140  // Requested in the context of a custom query by active client
141  return myCurrentSocket->second->vehicleStateChanges;
142  }
143  }
144 
145  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
146 
147  void writePositionVector(tcpip::Storage& outputStorage, const libsumo::TraCIPositionVector& shape);
148 
149 
152 
159  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
160 
161 
168  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
169 
170 
177  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
178 
179 
186  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
187 
188 
195  bool readTypeCheckingDoubleList(tcpip::Storage& inputStorage, std::vector<double>& into);
196 
197 
204  bool readTypeCheckingColor(tcpip::Storage& inputStorage, libsumo::TraCIColor& into);
205 
206 
214 
215 
222  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
223 
224 
231  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
232 
233 
240  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
242 
243 
246  void setTargetTime(SUMOTime targetTime);
247 
248  std::vector<std::string>& getLoadArgs() {
249  return myLoadArgs;
250  }
251 
254  void initWrapper(const int domainID, const int variable, const std::string& objID);
255  bool wrapDouble(const std::string& objID, const int variable, const double value);
256  bool wrapInt(const std::string& objID, const int variable, const int value);
257  bool wrapString(const std::string& objID, const int variable, const std::string& value);
258  bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
259  bool wrapPosition(const std::string& objID, const int variable, const libsumo::TraCIPosition& value);
260  bool wrapColor(const std::string& objID, const int variable, const libsumo::TraCIColor& value);
261  bool wrapRoadPosition(const std::string& objID, const int variable, const libsumo::TraCIRoadPosition& value);
264 
265 
266 private:
270  TraCIServer(const SUMOTime begin, const int port, const int numClients);
271 
272 
274  virtual ~TraCIServer();
275 
276 
277 
278  struct SocketInfo {
279  public:
282  : targetTime(t), socket(socket) {}
285  delete socket;
286  }
292  std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
293  private:
294  SocketInfo(const SocketInfo&);
295  };
296 
299 
303  bool commandGetVersion();
304 
305 
310 
311 
315  int readCommandID(int& commandStart, int& commandLength);
316 
318  int dispatchCommand();
319 
322  void checkClientOrdering();
323 
326 
328  SUMOTime nextTargetTime() const;
329 
331  void sendOutputToAll() const;
332 
335 
337  std::map<int, SocketInfo*>::iterator removeCurrentSocket();
338 
339 
340 private:
343 
345  static bool myDoCloseConnection;
346 
349 
352  std::map<int, SocketInfo*> mySockets;
353 
355  std::map<int, SocketInfo*> mySocketReorderRequests;
356 
358  std::map<int, SocketInfo*>::iterator myCurrentSocket;
359 
362 
365 
368 
371 
374 
376  std::map<int, CmdExecutor> myExecutors;
377 
379  std::map<int, int> myParameterSizes;
380 
381  std::vector<std::string> myLoadArgs;
382 
384  std::vector<libsumo::Subscription> mySubscriptions;
385 
388 
396  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
397 
398 private:
399  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
401  void removeSubscription(int commandId, const std::string& identity, int domain);
403  std::string& errors);
404 
405 
406  bool addSubscriptionFilter();
407  void removeFilters();
408  void addSubscriptionFilterLanes(std::vector<int> lanes);
410  void addSubscriptionFilterDownstreamDistance(double dist);
411  void addSubscriptionFilterUpstreamDistance(double dist);
413  // TODO: for libsumo, implement convenience definitions present in python client:
414  // void addSubscriptionFilterCF();
415  // void addSubscriptionFilterLC(int direction);
418  void addSubscriptionFilterVType(std::set<std::string> vTypes);
423  void addSubscriptionFilterFieldOfVision(double openingAngle);
425 
426  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
427 
429  bool centralObject(const libsumo::Subscription& s, const std::string& objID);
430 
431 
432 private:
434  TraCIServer& operator=(const TraCIServer& s);
435 
436 };
437 
438 
439 #endif
TraCIServer::addSubscriptionFilterUpstreamDistance
void addSubscriptionFilterUpstreamDistance(double dist)
Definition: TraCIServer.cpp:1314
Boundary.h
TraCIServer::wrapRoadPosition
bool wrapRoadPosition(const std::string &objID, const int variable, const libsumo::TraCIRoadPosition &value)
Definition: TraCIServer.cpp:167
ToString.h
TraCIServer::myVehicleStateChanges
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: TraCIServer.h:396
TraCIServer::setTargetTime
void setTargetTime(SUMOTime targetTime)
Sets myTargetTime on server and sockets to the given value.
Definition: TraCIServer.cpp:1512
TraCIServer::checkClientOrdering
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...
Definition: TraCIServer.cpp:334
TraCIServer::addSubscriptionFilterFieldOfVision
void addSubscriptionFilterFieldOfVision(double openingAngle)
Filter only vehicles within field of vision.
Definition: TraCIServer.cpp:1357
TraCIServer::TraCIServer
TraCIServer(const SUMOTime begin, const int port, const int numClients)
Constructor.
Definition: TraCIServer.cpp:180
tcpip::Socket
Definition: socket.h:59
SUMOTime.h
MSNet.h
TraCIServer::readTypeCheckingUnsignedByte
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
Definition: TraCIServer.cpp:1479
TraCIServer::addSubscriptionFilter
bool addSubscriptionFilter()
Definition: TraCIServer.cpp:1196
libsumo::TraCIPosition
A 3D-position.
Definition: TraCIDefs.h:109
TraCIServer::removeFilters
void removeFilters()
Definition: TraCIServer.cpp:1280
libsumo::VariableWrapper
Definition: Subscription.h:132
TraCIServer::operator=
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
TraCIServer::dispatchCommand
int dispatchCommand()
Handles command, writes response to myOutputStorage.
Definition: TraCIServer.cpp:715
OptionsCont.h
TraCIServer::myInstance
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:342
TraCIServer::readTypeCheckingColor
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
Definition: TraCIServer.cpp:1444
MSNet::VehicleStateListener
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:568
TraCIServer::addSubscriptionFilterNoOpposite
void addSubscriptionFilterNoOpposite()
Definition: TraCIServer.cpp:1297
TraCIServer::addObjectVariableSubscription
bool addObjectVariableSubscription(const int commandId, const bool hasContext)
Definition: TraCIServer.cpp:1163
TraCIServer::mySocketReorderRequests
std::map< int, SocketInfo * > mySocketReorderRequests
This stores the setOrder(int) requests of the clients.
Definition: TraCIServer.h:355
TraCIServer::commandGetVersion
bool commandGetVersion()
Returns the TraCI-version.
Definition: TraCIServer.cpp:863
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1414
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
TraCIServer::wrapDouble
bool wrapDouble(const std::string &objID, const int variable, const double value)
Definition: TraCIServer.cpp:111
TraCIServer::SocketInfo::vehicleStateChanges
std::map< MSNet::VehicleState, std::vector< std::string > > vehicleStateChanges
container for vehicle state changes since last step taken by this client
Definition: TraCIServer.h:292
TraCIServer::mySubscriptions
std::vector< libsumo::Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:384
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:135
NamedRTree.h
TraCIServer::addSubscriptionFilterLeadFollow
void addSubscriptionFilterLeadFollow()
Definition: TraCIServer.cpp:1323
TraCIServer::myTargetTime
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:361
PositionVector
A list of positions.
Definition: PositionVector.h:45
TraCIServer::close
static void close()
request termination of connection
Definition: TraCIServer.cpp:303
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:174
TraCIServer::removeCurrentSocket
std::map< int, SocketInfo * >::iterator removeCurrentSocket()
removes myCurrentSocket from mySockets and returns an iterator pointing to the next member according ...
Definition: TraCIServer.cpp:667
TraCIServer::findObjectShape
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
TraCIServer::addSubscriptionFilterLanes
void addSubscriptionFilterLanes(std::vector< int > lanes)
Definition: TraCIServer.cpp:1288
TraCIServer::readCommandID
int readCommandID(int &commandStart, int &commandLength)
Reads the next command ID from the input storage.
Definition: TraCIServer.cpp:697
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1404
TraCIServer::initialiseSubscription
void initialiseSubscription(libsumo::Subscription &s)
Definition: TraCIServer.cpp:989
TraCIServer::SocketInfo::socket
tcpip::Socket * socket
Socket object for this client.
Definition: TraCIServer.h:290
TraCIServer::mySubscriptionCache
tcpip::Storage mySubscriptionCache
The last timestep's subscription results.
Definition: TraCIServer.h:373
TraCIServer::writeStatusCmd
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Definition: TraCIServer.cpp:968
MSTrafficLightLogic.h
TraCIServer::~TraCIServer
virtual ~TraCIServer()
Destructor.
Definition: TraCIServer.cpp:274
TraCIServer::getLoadArgs
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:248
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:102
TraCIServer::SocketInfo
Definition: TraCIServer.h:278
TraCIServer::myInputStorage
tcpip::Storage myInputStorage
The storage to read from.
Definition: TraCIServer.h:364
TraCIServer::centralObject
bool centralObject(const libsumo::Subscription &s, const std::string &objID)
check whether a found objID refers to the central object of a context subscription
Definition: TraCIServer.cpp:1521
TraCIServer::addSubscriptionFilterVType
void addSubscriptionFilterVType(std::set< std::string > vTypes)
Definition: TraCIServer.cpp:1348
TraCIServer::SocketInfo::~SocketInfo
~SocketInfo()
destructor
Definition: TraCIServer.h:284
TraCIServer::readTypeCheckingDoubleList
bool readTypeCheckingDoubleList(tcpip::Storage &inputStorage, std::vector< double > &into)
Reads the value type and a double list, verifying the type.
Definition: TraCIServer.cpp:1434
TraCIServer::readTypeCheckingPolygon
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
Definition: TraCIServer.cpp:1489
TraCIServer::SocketInfo::SocketInfo
SocketInfo(tcpip::Socket *socket, SUMOTime t)
constructor
Definition: TraCIServer.h:281
TraCIConstants.h
TraCIServer::readTypeCheckingByte
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
Definition: TraCIServer.cpp:1469
TraCIServer::wrapPosition
bool wrapPosition(const std::string &objID, const int variable, const libsumo::TraCIPosition &value)
Definition: TraCIServer.cpp:143
libsumo::Subscription
Representation of a subscription.
Definition: Subscription.h:67
SVCPermissions
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Definition: SUMOVehicleClass.h:218
MSNet::VehicleState
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:535
TraCIServer::myOutputStorage
tcpip::Storage myOutputStorage
The storage to write to.
Definition: TraCIServer.h:367
TraCIServer::addSubscriptionFilterDownstreamDistance
void addSubscriptionFilterDownstreamDistance(double dist)
Definition: TraCIServer.cpp:1305
TraCIServer::postProcessSimulationStep
void postProcessSimulationStep()
Handles subscriptions to send after a simstep2 command.
Definition: TraCIServer.cpp:882
TraCIServer::vehicleStateChanged
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
Definition: TraCIServer.cpp:323
TraCIServer::myDoCloseConnection
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:345
TraCIServer::myLoadArgs
std::vector< std::string > myLoadArgs
Definition: TraCIServer.h:381
TraCIServer::wrapColor
bool wrapColor(const std::string &objID, const int variable, const libsumo::TraCIColor &value)
Definition: TraCIServer.cpp:156
TraCIServer::myParameterSizes
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:379
TraCIServer::processReorderingRequests
void processReorderingRequests()
checks for and processes reordering requests (relevant for multiple clients)
Definition: TraCIServer.cpp:401
TraCIServer::myWrapperStorage
tcpip::Storage myWrapperStorage
A temporary storage to let the wrapper write to.
Definition: TraCIServer.h:370
TraCIServer::mySockets
std::map< int, SocketInfo * > mySockets
The socket connections to the clients the first component (index) determines the client's order (lowe...
Definition: TraCIServer.h:352
TraCIServer::cleanup
void cleanup()
clean up subscriptions
Definition: TraCIServer.cpp:649
TraCIServer::getTargetTime
SUMOTime getTargetTime() const
Definition: TraCIServer.h:66
TraCIServer::processSingleSubscription
bool processSingleSubscription(const libsumo::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
Definition: TraCIServer.cpp:1061
TraCIServer::readTypeCheckingStringList
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
Definition: TraCIServer.cpp:1424
TraCIServer::processCommandsUntilSimStep
void processCommandsUntilSimStep(SUMOTime step)
process all commands until the next SUMO simulation step. It is guaranteed that t->getTargetTime() >=...
Definition: TraCIServer.cpp:491
TraCIServer::openSocket
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
Definition: TraCIServer.cpp:285
TraCIServer::writePositionVector
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
Definition: TraCIServer.cpp:1378
TraCIServer::getVehicleStateChanges
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:135
TraCIServer::CmdExecutor
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:64
TraCIServerAPI_Lane.h
Position.h
TraCIServer::myCurrentSocket
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:358
TraCIServer::myLastContextSubscription
libsumo::Subscription * myLastContextSubscription
The last modified context subscription (the one to add a filter to, see @addSubscriptionFilter(),...
Definition: TraCIServer.h:387
TraCIServer::addSubscriptionFilterVClass
void addSubscriptionFilterVClass(SVCPermissions vClasses)
Definition: TraCIServer.cpp:1339
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1394
storage.h
TraCIServer::SocketInfo::targetTime
SUMOTime targetTime
Target time: next point of action for the client.
Definition: TraCIServer.h:288
TraCIServer::sendOutputToAll
void sendOutputToAll() const
send out subscription results (actually just the content of myOutputStorage) to clients which will ac...
Definition: TraCIServer.cpp:469
Subscription.h
socket.h
libsumo::TraCIRoadPosition
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:121
config.h
TraCIServer::wrapStringList
bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)
Definition: TraCIServer.cpp:135
TraCIServer::nextTargetTime
SUMOTime nextTargetTime() const
get the minimal next target time among all clients
Definition: TraCIServer.cpp:445
GeomHelper.h
TraCIServer::getInstance
static TraCIServer * getInstance()
Definition: TraCIServer.h:70
TraCIServer::myServerSocket
tcpip::Socket * myServerSocket
The server socket.
Definition: TraCIServer.h:348
TraCIServer::removeSubscription
void removeSubscription(int commandId, const std::string &identity, int domain)
Definition: TraCIServer.cpp:1031
libsumo::TraCIPositionVector
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:149
TraCIServer::wrapInt
bool wrapInt(const std::string &objID, const int variable, const int value)
Definition: TraCIServer.cpp:119
TraCIServer::wasClosed
static bool wasClosed()
check whether close was requested
Definition: TraCIServer.cpp:314
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
TraCIServer::addSubscriptionFilterTurn
void addSubscriptionFilterTurn()
Definition: TraCIServer.cpp:1331
TraCIServer::isVehicleToVehicleContextSubscription
bool isVehicleToVehicleContextSubscription(const libsumo::Subscription &s)
Definition: TraCIServer.cpp:1055
TraCIServer::sendSingleSimStepResponse
void sendSingleSimStepResponse()
sends an empty response to a simstep command to the current client. (This applies to a situation wher...
Definition: TraCIServer.cpp:941
TraCIServer::myExecutors
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint.
Definition: TraCIServer.h:376
TraCIDefs.h
TraCIServer::readTypeCheckingPosition2D
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
Definition: TraCIServer.cpp:1457
tcpip::Storage
Definition: storage.h:36
TraCIServer::wrapString
bool wrapString(const std::string &objID, const int variable, const std::string &value)
Definition: TraCIServer.cpp:127
TraCIServer::writeErrorStatusCmd
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
Definition: TraCIServer.cpp:982