SUMO - Simulation of Urban MObility
TraCIServer.h
Go to the documentation of this file.
1 /****************************************************************************/
15 // TraCI server used to control sumo by a remote TraCI client
16 /****************************************************************************/
17 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
18 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
19 /****************************************************************************/
20 //
21 // This file is part of SUMO.
22 // SUMO is free software: you can redistribute it and/or modify
23 // it under the terms of the GNU General Public License as published by
24 // the Free Software Foundation, either version 3 of the License, or
25 // (at your option) any later version.
26 //
27 /****************************************************************************/
28 #ifndef TRACISERVER_H
29 #define TRACISERVER_H
30 
31 
32 // ===========================================================================
33 // included modules
34 // ===========================================================================
35 #ifdef _MSC_VER
36 #include <windows_config.h>
37 #else
38 #include <config.h>
39 #endif
40 
41 #ifndef NO_TRACI
42 
43 #include <map>
44 #include <string>
45 #include <set>
46 
47 #include "TraCIDefs.h"
48 #include "TraCIConstants.h"
49 
50 #define BUILD_TCPIP
51 #include <foreign/tcpip/socket.h>
52 #include <foreign/tcpip/storage.h>
54 #include <utils/common/SUMOTime.h>
55 #include <utils/common/ToString.h>
56 
57 #include <utils/geom/Boundary.h>
58 #include <utils/geom/Position.h>
59 #include <utils/geom/GeomHelper.h>
62 #include <microsim/MSNet.h>
64 #include "TraCIServerAPI_Lane.h"
65 
66 
67 
68 // ===========================================================================
69 // class definitions
70 // ===========================================================================
75 public:
77  typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
78 
79 
81  return myTargetTime;
82  }
83  bool isEmbedded() const {
84  return myAmEmbedded;
85  }
87  return myInstance;
88  }
89 
92 
96  static void openSocket(const std::map<int, CmdExecutor>& execs);
97 
98 
100  static void close();
101 
102 
106  static bool wasClosed();
108 
109 
114 
115 
116  void setVTDControlled(MSVehicle* v, Position xyPos, MSLane* l, double pos, double posLat, double angle,
117  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
118 
119  void postProcessVTD();
120 
122  void cleanup();
123 
124 
125 #ifdef HAVE_PYTHON
126  static std::string execute(std::string cmd);
128 
130  static void runEmbedded(std::string pyFile);
131 #endif
132 
133  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
134 
137 
144  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
145 
146 
152  void writeStatusCmd(int commandId, int status, const std::string& description);
153 
154 
160  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
162 
163 
164 
165  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
166  if (myAmEmbedded) {
167  return myVehicleStateChanges;
168  } else if (myCurrentSocket == mySockets.end()) {
169  // Requested in context of a subscription update
170  return myVehicleStateChanges;
171  } else {
172  // Requested in the context of a custom query by active client
173  return myCurrentSocket->second->vehicleStateChanges;
174  }
175  }
176 
177  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
178 
179  void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
180 
181 
184 
191  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
192 
193 
200  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
201 
202 
209  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
210 
211 
218  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
219 
220 
227  bool readTypeCheckingColor(tcpip::Storage& inputStorage, TraCIColor& into);
228 
229 
236  bool readTypeCheckingPosition2D(tcpip::Storage& inputStorage, TraCIPosition& into);
237 
238 
245  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
246 
247 
254  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
255 
256 
263  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
264 
265 
272  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
274 
275 
276 private:
280  TraCIServer(const SUMOTime begin, const int port, const int numClients);
281 
282 
284  virtual ~TraCIServer();
285 
286 
287 
288  struct SocketInfo {
289  public:
292  : targetTime(t), socket(socket) {}
295  delete socket;
296  }
302  std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
303  private:
304  SocketInfo(const SocketInfo&);
305  };
306 
309 
313  bool commandGetVersion();
314 
315 
320 
321 
325  int readCommandID(int& commandStart, int& commandLength);
326 
328  int dispatchCommand();
329 
332  void checkClientOrdering();
333 
336 
338  SUMOTime nextTargetTime() const;
339 
341  void sendOutputToAll() const;
342 
345 
347  std::map<int, SocketInfo*>::iterator removeCurrentSocket();
348 
349 
350 private:
353 
355  static bool myDoCloseConnection;
356 
359 
362  std::map<int, SocketInfo*> mySockets;
363 
365  std::map<int, SocketInfo*> mySocketReorderRequests;
366 
368  std::map<int, SocketInfo*>::iterator myCurrentSocket;
369 
372 
375 
378 
381 
383  const bool myAmEmbedded;
384 
386  std::map<int, CmdExecutor> myExecutors;
387 
389  std::map<int, int> myParameterSizes;
390 
391  std::map<std::string, MSVehicle*> myVTDControlledVehicles;
392 
393 
397  class Subscription {
398  public:
409  Subscription(int commandIdArg, const std::string& idArg,
410  const std::vector<int>& variablesArg, const std::vector<std::vector<unsigned char> >& paramsArg,
411  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, double rangeArg)
412  : commandId(commandIdArg), id(idArg), variables(variablesArg), parameters(paramsArg), beginTime(beginTimeArg), endTime(endTimeArg),
413  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
414 
418  std::string id;
420  std::vector<int> variables;
422  std::vector<std::vector<unsigned char> > parameters;
432  double range;
433 
434  };
435 
437  std::vector<Subscription> mySubscriptions;
438 
446  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
447 
449  std::map<int, NamedRTree*> myObjects;
450 
453 
454 
455 private:
456  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
457  void initialiseSubscription(const Subscription& s);
458  void removeSubscription(int commandId, const std::string& identity, int domain);
460  std::string& errors);
461 
462 
463  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
464 
465 
466 private:
468  TraCIServer& operator=(const TraCIServer& s);
469 
470 };
471 
472 
473 #endif
474 
475 #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:380
bool readTypeCheckingColor(tcpip::Storage &inputStorage, 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 vehicle in the micro simulation.
Definition: MSVehicle.h:83
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:428
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:598
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
double range
The range of the context.
Definition: TraCIServer.h:432
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:422
SUMOTime beginTime
The begin time of the subscription.
Definition: TraCIServer.h:424
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:77
tcpip::Storage myOutputStorage
The storage to writeto.
Definition: TraCIServer.h:377
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.
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:374
Representation of a subscription.
Definition: TraCIServer.h:397
void cleanup()
clean up subscriptions
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:79
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
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:48
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:355
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:298
void postProcessVTD()
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:386
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:83
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:300
int commandId
commandIdArg The command id of the subscription
Definition: TraCIServer.h:416
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:437
Representation of a vehicle.
Definition: SUMOVehicle.h:67
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:409
A 3D-position.
Definition: TraCIDefs.h:71
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:165
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
std::string id
The id of the object that is subscribed.
Definition: TraCIServer.h:418
A list of positions.
std::vector< int > variables
The subscribed variables.
Definition: TraCIServer.h:420
void setVTDControlled(MSVehicle *v, Position xyPos, MSLane *l, double pos, double posLat, double angle, int edgeOffset, ConstMSEdgeVector route, SUMOTime t)
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:291
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:352
SUMOTime getTargetTime() const
Definition: TraCIServer.h:80
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:74
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:571
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:383
void processCommandsUntilSimStep(SUMOTime step)
process all commands until the next SUMO simulation step. It is guaranteed that t->getTargetTime() >=...
LANE_RTREE_QUAL * myLaneTree
A storage of lanes.
Definition: TraCIServer.h:452
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:302
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
Definition: TraCIServer.h:368
static TraCIServer * getInstance()
Definition: TraCIServer.h:86
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:371
#define LANE_RTREE_QUAL
std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: TraCIServer.h:449
~SocketInfo()
destructor
Definition: TraCIServer.h:294
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
std::map< std::string, MSVehicle * > myVTDControlledVehicles
Definition: TraCIServer.h:391
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:362
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:365
long long int SUMOTime
Definition: TraCIDefs.h:52
int contextDomain
The domain ID of the context.
Definition: TraCIServer.h:430
void initialiseSubscription(const Subscription &s)
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
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:446
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:358
SUMOTime endTime
The end time of the subscription.
Definition: TraCIServer.h:426
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:389
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...