SUMO - Simulation of Urban MObility
TraCIServer.h
Go to the documentation of this file.
1 /****************************************************************************/
15 /****************************************************************************/
16 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
17 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
18 /****************************************************************************/
19 //
20 // This file is part of SUMO.
21 // SUMO is free software: you can redistribute it and/or modify
22 // it under the terms of the GNU General Public License as published by
23 // the Free Software Foundation, either version 3 of the License, or
24 // (at your option) any later version.
25 //
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 #include "TraCIDefs.h"
47 #include "TraCIConstants.h"
48 
49 #define BUILD_TCPIP
50 #include <foreign/tcpip/socket.h>
51 #include <foreign/tcpip/storage.h>
53 #include <utils/common/SUMOTime.h>
54 #include <utils/common/ToString.h>
55 
56 #include <utils/geom/Boundary.h>
57 #include <utils/geom/Position.h>
58 #include <utils/geom/GeomHelper.h>
59 #include <utils/shapes/Polygon.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  }
84  return myInstance;
85  }
86 
89 
93  static void openSocket(const std::map<int, CmdExecutor>& execs);
94 
95 
97  static void close();
98 
99 
103  static bool wasClosed();
105 
106 
107 
108 
110  static void processCommandsUntilSimStep(SUMOTime step);
111 
112  void setVTDControlled(MSVehicle* v, Position xyPos, MSLane* l, double pos, double posLat, double angle,
113  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
114 
115  void postProcessVTD();
116 
118  void cleanup();
119 
120 
121 #ifdef HAVE_PYTHON
122  static std::string execute(std::string cmd);
124 
126  static void runEmbedded(std::string pyFile);
127 #endif
128 
129  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
130 
133 
140  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
141 
142 
148  void writeStatusCmd(int commandId, int status, const std::string& description);
149 
150 
156  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
158 
159 
160 
161  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
162  return myVehicleStateChanges;
163  }
164 
165  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
166 
167  void collectObjectsInRange(int domain, const PositionVector& shape, double range, std::set<std::string>& into);
168 
169 
172 
179  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
180 
181 
188  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
189 
190 
197  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
198 
199 
206  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
207 
208 
215  bool readTypeCheckingColor(tcpip::Storage& inputStorage, TraCIColor& into);
216 
217 
224  bool readTypeCheckingPosition2D(tcpip::Storage& inputStorage, TraCIPosition& into);
225 
226 
233  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
234 
235 
242  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
243 
244 
251  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
252 
253 
260  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
262 
263 
264 private:
268  TraCIServer(const SUMOTime begin, const int port = 0);
269 
270 
272  virtual ~TraCIServer();
273 
274 
275 
278 
282  bool commandGetVersion();
283 
284 
289 
290 
291  int dispatchCommand();
292 
293 
294 
295 private:
298 
300  static bool myDoCloseConnection;
301 
304 
307 
310 
313 
317 
319  const bool myAmEmbedded;
320 
322  std::map<int, CmdExecutor> myExecutors;
323 
325  std::map<int, int> myParameterSizes;
326 
327  std::map<std::string, MSVehicle*> myVTDControlledVehicles;
328 
329 
333  class Subscription {
334  public:
345  Subscription(int commandIdArg, const std::string& idArg,
346  const std::vector<int>& variablesArg, const std::vector<std::vector<unsigned char> >& paramsArg,
347  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, double rangeArg)
348  : commandId(commandIdArg), id(idArg), variables(variablesArg), parameters(paramsArg), beginTime(beginTimeArg), endTime(endTimeArg),
349  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
350 
354  std::string id;
356  std::vector<int> variables;
358  std::vector<std::vector<unsigned char> > parameters;
368  double range;
369 
370  };
371 
373  std::vector<Subscription> mySubscriptions;
374 
376  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
377 
379  std::map<int, NamedRTree*> myObjects;
380 
383 
384 
385 private:
386  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
387  void initialiseSubscription(const Subscription& s);
388  void removeSubscription(int commandId, const std::string& identity, int domain);
390  std::string& errors);
391 
392 
393  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
394 
395 
396 private:
398  TraCIServer& operator=(const TraCIServer& s);
399 
400 };
401 
402 
403 #endif
404 
405 #endif
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
bool myDoingSimStep
Whether a step is currently done.
Definition: TraCIServer.h:316
tcpip::Socket * mySocket
The socket on which server is listening on.
Definition: TraCIServer.h:303
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
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
Definition: TraCIServer.h:364
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:603
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
double range
The range of the context.
Definition: TraCIServer.h:368
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:358
SUMOTime beginTime
The begin time of the subscription.
Definition: TraCIServer.h:360
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:312
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
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:309
Representation of a subscription.
Definition: TraCIServer.h:333
void cleanup()
clean up subscriptions
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
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:300
void postProcessVTD()
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:322
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.
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)
int commandId
commandIdArg The command id of the subscription
Definition: TraCIServer.h:352
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:373
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:345
A 3D-position.
Definition: TraCIDefs.h:71
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:354
A list of positions.
std::vector< int > variables
The subscribed variables.
Definition: TraCIServer.h:356
SUMOTime getTargetTime()
Definition: TraCIServer.h:80
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.
void postProcessSimulationStep2()
Handles subscriptions to send after a simstep2 command.
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:297
TraCIServer(const SUMOTime begin, const int port=0)
Constructor.
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
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:576
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:319
static void processCommandsUntilSimStep(SUMOTime step)
process all commands until a simulation step is wanted
LANE_RTREE_QUAL * myLaneTree
A storage of lanes.
Definition: TraCIServer.h:382
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)
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:161
static TraCIServer * getInstance()
Definition: TraCIServer.h:83
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:306
#define LANE_RTREE_QUAL
std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: TraCIServer.h:379
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:327
int dispatchCommand()
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
long long int SUMOTime
Definition: TraCIDefs.h:52
int contextDomain
The domain ID of the context.
Definition: TraCIServer.h:366
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:376
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
SUMOTime endTime
The end time of the subscription.
Definition: TraCIServer.h:362
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:325