SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 "TraCIConstants.h"
47 
48 #define BUILD_TCPIP
49 #include <foreign/tcpip/socket.h>
50 #include <foreign/tcpip/storage.h>
52 #include <utils/common/SUMOTime.h>
53 #include <utils/common/ToString.h>
54 
55 #include <utils/geom/Boundary.h>
56 #include <utils/geom/Position.h>
57 #include <utils/geom/GeomHelper.h>
58 #include <utils/shapes/Polygon.h>
61 #include <microsim/MSNet.h>
63 #include "TraCIServerAPI_Lane.h"
64 
65 
66 
67 // ===========================================================================
68 // class definitions
69 // ===========================================================================
74 public:
76  typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
77 
78 
80  return myTargetTime;
81  }
83  return myInstance;
84  }
85 
88 
92  static void openSocket(const std::map<int, CmdExecutor>& execs);
93 
94 
96  static void close();
97 
98 
102  static bool wasClosed();
104 
105 
106 
107 
109  static void processCommandsUntilSimStep(SUMOTime step);
110 
111  void setVTDControlled(MSVehicle* v, Position xyPos, MSLane* l, SUMOReal pos, SUMOReal posLat, SUMOReal angle,
112  int edgeOffset, ConstMSEdgeVector route, SUMOTime t);
113 
114  void postProcessVTD();
115 
116 
117 
118 #ifdef HAVE_PYTHON
119  static std::string execute(std::string cmd);
121 
123  static void runEmbedded(std::string pyFile);
124 #endif
125 
126  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
127 
130 
137  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
138 
139 
145  void writeStatusCmd(int commandId, int status, const std::string& description);
146 
147 
153  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
155 
156 
157 
158  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
159  return myVehicleStateChanges;
160  }
161 
162  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
163 
164  void collectObjectsInRange(int domain, const PositionVector& shape, SUMOReal range, std::set<std::string>& into);
165 
166 
169 
176  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
177 
178 
185  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
186 
187 
194  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
195 
196 
203  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
204 
205 
212  bool readTypeCheckingColor(tcpip::Storage& inputStorage, RGBColor& into);
213 
214 
221  bool readTypeCheckingPosition2D(tcpip::Storage& inputStorage, Position& into);
222 
223 
230  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
231 
232 
239  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
240 
241 
248  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
249 
250 
257  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
259 
260 
261 private:
265  TraCIServer(const SUMOTime begin, const int port = 0);
266 
267 
269  virtual ~TraCIServer();
270 
271 
272 
275 
279  bool commandGetVersion();
280 
281 
286 
287 
288  int dispatchCommand();
289 
290 
291 
292 private:
295 
297  static bool myDoCloseConnection;
298 
301 
304 
307 
310 
314 
316  const bool myAmEmbedded;
317 
319  std::map<int, CmdExecutor> myExecutors;
320 
322  std::map<int, int> myParameterSizes;
323 
324  std::map<std::string, MSVehicle*> myVTDControlledVehicles;
325 
326 
330  class Subscription {
331  public:
342  Subscription(int commandIdArg, const std::string& idArg,
343  const std::vector<int>& variablesArg, const std::vector<std::vector<unsigned char> >& paramsArg,
344  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, SUMOReal rangeArg)
345  : commandId(commandIdArg), id(idArg), variables(variablesArg), parameters(paramsArg), beginTime(beginTimeArg), endTime(endTimeArg),
346  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
347 
351  std::string id;
353  std::vector<int> variables;
355  std::vector<std::vector<unsigned char> > parameters;
366 
367  };
368 
370  std::vector<Subscription> mySubscriptions;
371 
373  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
374 
376  std::map<int, NamedRTree*> myObjects;
377 
380 
381 
382 private:
383  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
384  void initialiseSubscription(const Subscription& s);
385  void removeSubscription(int commandId, const std::string& identity, int domain);
387  std::string& errors);
388 
389 
390  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
391 
392 
393 private:
395  TraCIServer& operator=(const TraCIServer& s);
396 
397 };
398 
399 
400 #endif
401 
402 #endif
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
bool myDoingSimStep
Whether a step is currently done.
Definition: TraCIServer.h:313
tcpip::Socket * mySocket
The socket on which server is listening on.
Definition: TraCIServer.h:300
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:82
long long int SUMOTime
Definition: SUMOTime.h:43
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
Definition: TraCIServer.h:361
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:564
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
void collectObjectsInRange(int domain, const PositionVector &shape, SUMOReal range, std::set< std::string > &into)
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, SUMOReal rangeArg)
Constructor.
Definition: TraCIServer.h:342
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:355
void setVTDControlled(MSVehicle *v, Position xyPos, MSLane *l, SUMOReal pos, SUMOReal posLat, SUMOReal angle, int edgeOffset, ConstMSEdgeVector route, SUMOTime t)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
SUMOTime beginTime
The begin time of the subscription.
Definition: TraCIServer.h:357
tcpip::Storage myOutputStorage
The storage to writeto.
Definition: TraCIServer.h:309
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:306
Representation of a subscription.
Definition: TraCIServer.h:330
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:297
void postProcessVTD()
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:319
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:349
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:370
Representation of a vehicle.
Definition: SUMOVehicle.h:66
SUMOReal range
The range of the context.
Definition: TraCIServer.h:365
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:158
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:351
A list of positions.
std::vector< int > variables
The subscribed variables.
Definition: TraCIServer.h:353
SUMOTime getTargetTime()
Definition: TraCIServer.h:79
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:294
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:73
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:537
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:316
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
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:379
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
static TraCIServer * getInstance()
Definition: TraCIServer.h:82
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:303
#define LANE_RTREE_QUAL
std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: TraCIServer.h:376
std::map< std::string, MSVehicle * > myVTDControlledVehicles
Definition: TraCIServer.h:324
int dispatchCommand()
#define SUMOReal
Definition: config.h:214
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
int contextDomain
The domain ID of the context.
Definition: TraCIServer.h:363
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:76
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:373
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:359
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:322