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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
25 // TraCI server used to control sumo by a remote TraCI client
26 /****************************************************************************/
27 #ifndef TRACISERVER_H
28 #define TRACISERVER_H
29 
30 
31 // ===========================================================================
32 // included modules
33 // ===========================================================================
34 #ifdef _MSC_VER
35 #include <windows_config.h>
36 #else
37 #include <config.h>
38 #endif
39 
40 #ifndef NO_TRACI
41 
42 #include <map>
43 #include <string>
44 #include <set>
45 
46 #define BUILD_TCPIP
47 #include <foreign/tcpip/socket.h>
48 #include <foreign/tcpip/storage.h>
50 #include <utils/common/SUMOTime.h>
51 #include <utils/common/ToString.h>
52 #include <utils/geom/Boundary.h>
53 #include <utils/geom/Position.h>
54 #include <utils/geom/GeomHelper.h>
56 #include <microsim/MSNet.h>
58 #include <libsumo/TraCIDefs.h>
59 #include "TraCIConstants.h"
60 #include "TraCIServerAPI_Lane.h"
61 
62 
63 // ===========================================================================
64 // class definitions
65 // ===========================================================================
70 public:
72  typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
73 
74 
76  return myTargetTime;
77  }
78  bool isEmbedded() const {
79  return myAmEmbedded;
80  }
82  return myInstance;
83  }
84 
87 
91  static void openSocket(const std::map<int, CmdExecutor>& execs);
92 
93 
95  static void close();
96 
97 
101  static bool wasClosed();
103 
104 
109 
111  void cleanup();
112 
113 
114 #ifdef HAVE_PYTHON
115  static std::string execute(std::string cmd);
117 
119  static void runEmbedded(std::string pyFile);
120 #endif
121 
122  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
123 
126 
133  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
134 
135 
141  void writeStatusCmd(int commandId, int status, const std::string& description);
142 
143 
149  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
151 
152 
153 
154  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
155  if (myAmEmbedded) {
156  return myVehicleStateChanges;
157  } else if (myCurrentSocket == mySockets.end()) {
158  // Requested in context of a subscription update
159  return myVehicleStateChanges;
160  } else {
161  // Requested in the context of a custom query by active client
162  return myCurrentSocket->second->vehicleStateChanges;
163  }
164  }
165 
166  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
167 
168  void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
169 
170 
173 
180  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
181 
182 
189  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
190 
191 
198  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
199 
200 
207  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
208 
209 
216  bool readTypeCheckingColor(tcpip::Storage& inputStorage, libsumo::TraCIColor& into);
217 
218 
226 
227 
234  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
235 
236 
243  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
244 
245 
252  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
253 
254 
261  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
263 
264 
267  void setTargetTime(SUMOTime targetTime);
268 
269  std::vector<std::string>& getLoadArgs() {
270  return myLoadArgs;
271  }
272 
273 
274 private:
278  TraCIServer(const SUMOTime begin, const int port, const int numClients);
279 
280 
282  virtual ~TraCIServer();
283 
284 
285 
286  struct SocketInfo {
287  public:
290  : targetTime(t), socket(socket) {}
293  delete socket;
294  }
300  std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
301  private:
302  SocketInfo(const SocketInfo&);
303  };
304 
307 
311  bool commandGetVersion();
312 
313 
318 
319 
323  int readCommandID(int& commandStart, int& commandLength);
324 
326  int dispatchCommand();
327 
330  void checkClientOrdering();
331 
334 
336  SUMOTime nextTargetTime() const;
337 
339  void sendOutputToAll() const;
340 
343 
345  std::map<int, SocketInfo*>::iterator removeCurrentSocket();
346 
347 
348 private:
351 
353  static bool myDoCloseConnection;
354 
357 
360  std::map<int, SocketInfo*> mySockets;
361 
363  std::map<int, SocketInfo*> mySocketReorderRequests;
364 
366  std::map<int, SocketInfo*>::iterator myCurrentSocket;
367 
370 
373 
376 
379 
381  const bool myAmEmbedded;
382 
384  std::map<int, CmdExecutor> myExecutors;
385 
387  std::map<int, int> myParameterSizes;
388 
389  std::vector<std::string> myLoadArgs;
390 
394  class Subscription {
395  public:
406  Subscription(int commandIdArg, const std::string& idArg,
407  const std::vector<int>& variablesArg, const std::vector<std::vector<unsigned char> >& paramsArg,
408  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, double rangeArg)
409  : commandId(commandIdArg), id(idArg), variables(variablesArg), parameters(paramsArg), beginTime(beginTimeArg), endTime(endTimeArg),
410  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
411 
415  std::string id;
417  std::vector<int> variables;
419  std::vector<std::vector<unsigned char> > parameters;
429  double range;
430 
431  };
432 
434  std::vector<Subscription> mySubscriptions;
435 
443  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
444 
445 private:
446  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
447  void initialiseSubscription(const Subscription& s);
448  void removeSubscription(int commandId, const std::string& identity, int domain);
450  std::string& errors);
451 
452 
453  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
454 
455 
456 private:
458  TraCIServer& operator=(const TraCIServer& s);
459 
460 };
461 
462 
463 #endif
464 
465 #endif
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
tcpip::Storage mySubscriptionCache
The last timestep&#39;s subscription results.
Definition: TraCIServer.h:378
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)
SUMOTime nextTargetTime() const
get the minimal next target time among all clients
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
Definition: TraCIServer.h:425
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:509
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
double range
The range of the context.
Definition: TraCIServer.h:429
virtual ~TraCIServer()
Destructor.
bool commandGetVersion()
Returns the TraCI-version.
std::vector< std::vector< unsigned char > > parameters
The parameters for the subscribed variables.
Definition: TraCIServer.h:419
SUMOTime beginTime
The begin time of the subscription.
Definition: TraCIServer.h:421
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:72
tcpip::Storage myOutputStorage
The storage to writeto.
Definition: TraCIServer.h:375
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:389
void sendOutputToAll() const
send out subscription results (actually just the content of myOutputStorage) to clients which will ac...
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:372
Representation of a subscription.
Definition: TraCIServer.h:394
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:269
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:353
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:296
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:384
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to)
Called if a vehicle changes its state.
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
bool isEmbedded() const
Definition: TraCIServer.h:78
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:298
int commandId
commandIdArg The command id of the subscription
Definition: TraCIServer.h:413
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:434
Representation of a vehicle.
Definition: SUMOVehicle.h:66
Subscription(int commandIdArg, const std::string &idArg, const std::vector< int > &variablesArg, const std::vector< std::vector< unsigned char > > &paramsArg, SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, double rangeArg)
Constructor.
Definition: TraCIServer.h:406
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:154
std::string id
The id of the object that is subscribed.
Definition: TraCIServer.h:415
A list of positions.
std::vector< int > variables
The subscribed variables.
Definition: TraCIServer.h:417
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:289
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:350
SUMOTime getTargetTime() const
Definition: TraCIServer.h:75
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:69
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...
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:482
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:381
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 collectObjectsInRange(int domain, const PositionVector &shape, double range, std::set< std::string > &into)
std::map< MSNet::VehicleState, std::vector< std::string > > vehicleStateChanges
container for vehicle state changes since last step taken by this client
Definition: TraCIServer.h:300
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:366
void setTargetTime(SUMOTime targetTime)
Sets myTargetTime on server and sockets to the given value.
static TraCIServer * getInstance()
Definition: TraCIServer.h:81
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:369
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
~SocketInfo()
destructor
Definition: TraCIServer.h:292
int dispatchCommand()
Handles command, writes response to myOutputStorage.
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:360
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:363
long long int SUMOTime
Definition: TraCIDefs.h:51
int contextDomain
The domain ID of the context.
Definition: TraCIServer.h:427
A 3D-position.
Definition: TraCIDefs.h:71
void initialiseSubscription(const Subscription &s)
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:443
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
tcpip::Socket * myServerSocket
The server socket.
Definition: TraCIServer.h:356
SUMOTime endTime
The end time of the subscription.
Definition: TraCIServer.h:423
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:387
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...