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
TraCIServer::addSubscriptionFilterUpstreamDistance
void addSubscriptionFilterUpstreamDistance(double dist)
Definition: TraCIServer.cpp:1325
Boundary.h
TraCIServer::wrapRoadPosition
bool wrapRoadPosition(const std::string &objID, const int variable, const libsumo::TraCIRoadPosition &value)
Definition: TraCIServer.cpp:168
ToString.h
TraCIServer::myVehicleStateChanges
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: TraCIServer.h:397
TraCIServer::setTargetTime
void setTargetTime(SUMOTime targetTime)
Sets myTargetTime on server and sockets to the given value.
Definition: TraCIServer.cpp:1514
TraCIServer::checkClientOrdering
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...
Definition: TraCIServer.cpp:333
TraCIServer::TraCIServer
TraCIServer(const SUMOTime begin, const int port, const int numClients)
Constructor.
Definition: TraCIServer.cpp:181
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:1481
TraCIServer::addSubscriptionFilter
bool addSubscriptionFilter()
Definition: TraCIServer.cpp:1213
libsumo::TraCIPosition
A 3D-position.
Definition: TraCIDefs.h:110
TraCIServer::removeFilters
void removeFilters()
Definition: TraCIServer.cpp:1291
libsumo::VariableWrapper
Definition: Subscription.h:114
TraCIServer::operator=
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
TraCIServer::dispatchCommand
int dispatchCommand()
Handles command, writes response to myOutputStorage.
Definition: TraCIServer.cpp:714
OptionsCont.h
TraCIServer::myInstance
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:343
TraCIServer::readTypeCheckingColor
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
Definition: TraCIServer.cpp:1446
MSNet::VehicleStateListener
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:567
TraCIServer::addSubscriptionFilterNoOpposite
void addSubscriptionFilterNoOpposite()
Definition: TraCIServer.cpp:1308
TraCIServer::addObjectVariableSubscription
bool addObjectVariableSubscription(const int commandId, const bool hasContext)
Definition: TraCIServer.cpp:1180
TraCIServer::mySocketReorderRequests
std::map< int, SocketInfo * > mySocketReorderRequests
This stores the setOrder(int) requests of the clients.
Definition: TraCIServer.h:356
TraCIServer::commandGetVersion
bool commandGetVersion()
Returns the TraCI-version.
Definition: TraCIServer.cpp:862
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1416
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1368
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:61
TraCIServer::wrapDouble
bool wrapDouble(const std::string &objID, const int variable, const double value)
Definition: TraCIServer.cpp:112
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:293
TraCIServer::mySubscriptions
std::vector< libsumo::Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:385
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:136
NamedRTree.h
TraCIServer::addSubscriptionFilterLeadFollow
void addSubscriptionFilterLeadFollow()
Definition: TraCIServer.cpp:1334
TraCIServer::myTargetTime
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:362
PositionVector
A list of positions.
Definition: PositionVector.h:46
TraCIServer::close
static void close()
request termination of connection
Definition: TraCIServer.cpp:302
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:175
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:666
TraCIServer::findObjectShape
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
TraCIServer::addSubscriptionFilterLanes
void addSubscriptionFilterLanes(std::vector< int > lanes)
Definition: TraCIServer.cpp:1299
TraCIServer::readCommandID
int readCommandID(int &commandStart, int &commandLength)
Reads the next command ID from the input storage.
Definition: TraCIServer.cpp:696
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1406
TraCIServer::initialiseSubscription
void initialiseSubscription(libsumo::Subscription &s)
Definition: TraCIServer.cpp:988
TraCIServer::SocketInfo::socket
tcpip::Socket * socket
Socket object for this client.
Definition: TraCIServer.h:291
TraCIServer::mySubscriptionCache
tcpip::Storage mySubscriptionCache
The last timestep's subscription results.
Definition: TraCIServer.h:374
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:967
MSTrafficLightLogic.h
TraCIServer::~TraCIServer
virtual ~TraCIServer()
Destructor.
Definition: TraCIServer.cpp:273
TraCIServer::getLoadArgs
std::vector< std::string > & getLoadArgs()
Definition: TraCIServer.h:249
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:103
TraCIServer::SocketInfo
Definition: TraCIServer.h:279
TraCIServer::myInputStorage
tcpip::Storage myInputStorage
The storage to read from.
Definition: TraCIServer.h:365
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:1523
TraCIServer::addSubscriptionFilterVType
void addSubscriptionFilterVType(std::set< std::string > vTypes)
Definition: TraCIServer.cpp:1359
TraCIServer::SocketInfo::~SocketInfo
~SocketInfo()
destructor
Definition: TraCIServer.h:285
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:1436
TraCIServer::readTypeCheckingPolygon
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
Definition: TraCIServer.cpp:1491
TraCIServer::SocketInfo::SocketInfo
SocketInfo(tcpip::Socket *socket, SUMOTime t)
constructor
Definition: TraCIServer.h:282
TraCIConstants.h
TraCIServer::readTypeCheckingByte
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
Definition: TraCIServer.cpp:1471
TraCIServer::wrapPosition
bool wrapPosition(const std::string &objID, const int variable, const libsumo::TraCIPosition &value)
Definition: TraCIServer.cpp:144
libsumo::Subscription
Representation of a subscription.
Definition: Subscription.h:65
SVCPermissions
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Definition: SUMOVehicleClass.h:219
MSNet::VehicleState
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:536
TraCIServer::myOutputStorage
tcpip::Storage myOutputStorage
The storage to write to.
Definition: TraCIServer.h:368
TraCIServer::addSubscriptionFilterDownstreamDistance
void addSubscriptionFilterDownstreamDistance(double dist)
Definition: TraCIServer.cpp:1316
TraCIServer::postProcessSimulationStep
void postProcessSimulationStep()
Handles subscriptions to send after a simstep2 command.
Definition: TraCIServer.cpp:881
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:322
TraCIServer::myDoCloseConnection
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:346
TraCIServer::myLoadArgs
std::vector< std::string > myLoadArgs
Definition: TraCIServer.h:382
TraCIServer::wrapColor
bool wrapColor(const std::string &objID, const int variable, const libsumo::TraCIColor &value)
Definition: TraCIServer.cpp:157
TraCIServer::myParameterSizes
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:380
TraCIServer::processReorderingRequests
void processReorderingRequests()
checks for and processes reordering requests (relevant for multiple clients)
Definition: TraCIServer.cpp:400
TraCIServer::myWrapperStorage
tcpip::Storage myWrapperStorage
A temporary storage to let the wrapper write to.
Definition: TraCIServer.h:371
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:353
TraCIServer::cleanup
void cleanup()
clean up subscriptions
Definition: TraCIServer.cpp:648
TraCIServer::getTargetTime
SUMOTime getTargetTime() const
Definition: TraCIServer.h:67
TraCIServer::processSingleSubscription
bool processSingleSubscription(const libsumo::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
Definition: TraCIServer.cpp:1078
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:1426
TraCIServer::processCommandsUntilSimStep
void processCommandsUntilSimStep(SUMOTime step)
process all commands until the next SUMO simulation step. It is guaranteed that t->getTargetTime() >=...
Definition: TraCIServer.cpp:490
TraCIServer::openSocket
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
Definition: TraCIServer.cpp:284
TraCIServer::writePositionVector
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
Definition: TraCIServer.cpp:1380
TraCIServer::getVehicleStateChanges
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:136
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:65
TraCIServerAPI_Lane.h
Position.h
TraCIServer::myCurrentSocket
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:359
TraCIServer::myLastContextSubscription
libsumo::Subscription * myLastContextSubscription
The last modified context subscription (the one to add a filter to, see @addSubscriptionFilter(),...
Definition: TraCIServer.h:388
TraCIServer::addSubscriptionFilterVClass
void addSubscriptionFilterVClass(SVCPermissions vClasses)
Definition: TraCIServer.cpp:1350
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1396
storage.h
TraCIServer::SocketInfo::targetTime
SUMOTime targetTime
Target time: next point of action for the client.
Definition: TraCIServer.h:289
TraCIServer::sendOutputToAll
void sendOutputToAll() const
send out subscription results (actually just the content of myOutputStorage) to clients which will ac...
Definition: TraCIServer.cpp:468
Subscription.h
socket.h
libsumo::TraCIRoadPosition
An edgeId, position and laneIndex.
Definition: TraCIDefs.h:122
config.h
TraCIServer::wrapStringList
bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)
Definition: TraCIServer.cpp:136
TraCIServer::nextTargetTime
SUMOTime nextTargetTime() const
get the minimal next target time among all clients
Definition: TraCIServer.cpp:444
GeomHelper.h
TraCIServer::getInstance
static TraCIServer * getInstance()
Definition: TraCIServer.h:71
TraCIServer::myServerSocket
tcpip::Socket * myServerSocket
The server socket.
Definition: TraCIServer.h:349
TraCIServer::removeSubscription
void removeSubscription(int commandId, const std::string &identity, int domain)
Definition: TraCIServer.cpp:1048
libsumo::TraCIPositionVector
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:150
TraCIServer::wrapInt
bool wrapInt(const std::string &objID, const int variable, const int value)
Definition: TraCIServer.cpp:120
TraCIServer::wasClosed
static bool wasClosed()
check whether close was requested
Definition: TraCIServer.cpp:313
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
TraCIServer::addSubscriptionFilterTurn
void addSubscriptionFilterTurn()
Definition: TraCIServer.cpp:1342
TraCIServer::isVehicleToVehicleContextSubscription
bool isVehicleToVehicleContextSubscription(const libsumo::Subscription &s)
Definition: TraCIServer.cpp:1072
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:940
TraCIServer::myExecutors
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint.
Definition: TraCIServer.h:377
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:1459
tcpip::Storage
Definition: storage.h:36
TraCIServer::wrapString
bool wrapString(const std::string &objID, const int variable, const std::string &value)
Definition: TraCIServer.cpp:128
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:981