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-2015 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 "TraCIConstants.h"
43 
44 #define BUILD_TCPIP
45 #include <foreign/tcpip/socket.h>
46 #include <foreign/tcpip/storage.h>
47 #include <utils/common/SUMOTime.h>
48 #include <utils/common/ToString.h>
49 
50 #include <utils/geom/Boundary.h>
51 #include <utils/geom/Position.h>
52 #include <utils/geom/GeomHelper.h>
53 #include <utils/shapes/Polygon.h>
56 #include <microsim/MSVehicle.h>
57 #include <microsim/MSNet.h>
59 #include "TraCIException.h"
61 
62 #include <map>
63 #include <string>
64 #include <set>
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, MSLane* l, SUMOReal pos, int edgeOffset, ConstMSEdgeVector route,
113  SUMOTime t);
114 
115  void postProcessVTD();
116 
117 
118 
119 #ifdef HAVE_PYTHON
120  static std::string execute(std::string cmd);
122 
124  static void runEmbedded(std::string pyFile);
125 #endif
126 
127  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
128 
131 
138  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
139 
140 
146  void writeStatusCmd(int commandId, int status, const std::string& description);
147 
148 
154  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
156 
157 
158 
159  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
160  return myVehicleStateChanges;
161  }
162 
163  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
164 
165  void collectObjectsInRange(int domain, const PositionVector& shape, SUMOReal range, std::set<std::string>& into);
166 
167 
170 
177  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
178 
179 
186  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
187 
188 
195  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
196 
197 
204  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
205 
206 
213  bool readTypeCheckingColor(tcpip::Storage& inputStorage, RGBColor& into);
214 
215 
222  bool readTypeCheckingPosition2D(tcpip::Storage& inputStorage, Position& into);
223 
224 
231  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
232 
233 
240  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
241 
242 
249  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
250 
251 
258  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
260 
261 
262 private:
266  TraCIServer(const SUMOTime begin, const int port = 0);
267 
268 
270  virtual ~TraCIServer();
271 
272 
273 
276 
280  bool commandGetVersion();
281 
282 
286  bool commandCloseConnection();
287 
288 
293 
294 
295  int dispatchCommand();
296 
297 
298 
299 private:
302 
304  static bool myDoCloseConnection;
305 
308 
311 
314 
317 
321 
323  const bool myAmEmbedded;
324 
326  std::map<int, CmdExecutor> myExecutors;
327 
329  std::map<int, int> myParameterSizes;
330 
331  std::map<std::string, MSVehicle*> myVTDControlledVehicles;
332 
333 
337  class Subscription {
338  public:
349  Subscription(int commandIdArg, const std::string& idArg,
350  const std::vector<int>& variablesArg, const std::vector<std::vector<unsigned char> >& paramsArg,
351  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, SUMOReal rangeArg)
352  : commandId(commandIdArg), id(idArg), variables(variablesArg), parameters(paramsArg), beginTime(beginTimeArg), endTime(endTimeArg),
353  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
354 
358  std::string id;
360  std::vector<int> variables;
362  std::vector<std::vector<unsigned char> > parameters;
373 
374  };
375 
377  std::vector<Subscription> mySubscriptions;
378 
380  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
381 
383  std::map<int, NamedRTree*> myObjects;
384 
387 
388 
389 private:
390  bool addObjectVariableSubscription(const int commandId, const bool hasContext);
391  void initialiseSubscription(const Subscription& s);
392  void removeSubscription(int commandId, const std::string& identity, int domain);
394  std::string& errors);
395 
396 
397  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
398 
399 
400 private:
402  TraCIServer& operator=(const TraCIServer& s);
403 
404 };
405 
406 
407 #endif
408 
409 #endif
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
bool myDoingSimStep
Whether a step is currently done.
Definition: TraCIServer.h:320
tcpip::Socket * mySocket
The socket on which server is listening on.
Definition: TraCIServer.h:307
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
Definition: TraCIServer.h:368
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:510
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:349
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:362
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:364
tcpip::Storage myOutputStorage
The storage to writeto.
Definition: TraCIServer.h:316
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:313
Representation of a subscription.
Definition: TraCIServer.h:337
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:304
void postProcessVTD()
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:326
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:356
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:377
Representation of a vehicle.
Definition: SUMOVehicle.h:65
SUMOReal range
The range of the context.
Definition: TraCIServer.h:372
bool commandCloseConnection()
Indicates the connection as being closed.
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:159
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:358
A list of positions.
std::vector< int > variables
The subscribed variables.
Definition: TraCIServer.h:360
SUMOTime getTargetTime()
Definition: TraCIServer.h:80
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:301
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:483
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:323
void setVTDControlled(MSVehicle *v, MSLane *l, SUMOReal pos, int edgeOffset, ConstMSEdgeVector route, SUMOTime t)
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:386
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
static TraCIServer * getInstance()
Definition: TraCIServer.h:83
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:310
int SUMOTime
Definition: SUMOTime.h:43
#define LANE_RTREE_QUAL
std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: TraCIServer.h:383
std::map< std::string, MSVehicle * > myVTDControlledVehicles
Definition: TraCIServer.h:331
int dispatchCommand()
#define SUMOReal
Definition: config.h:218
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:370
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
void initialiseSubscription(const Subscription &s)
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
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:380
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:366
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.
Definition: TraCIServer.h:329