SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServer.h
Go to the documentation of this file.
1 /****************************************************************************/
15 /****************************************************************************/
16 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
17 // Copyright (C) 2001-2013 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, MSEdgeVector route);
113 
114  void postProcessVTD();
115 
116  bool vtdDebug() const;
117 
118 
119 
120 #ifdef HAVE_PYTHON
121  static std::string execute(std::string cmd);
123 
125  static void runEmbedded(std::string pyFile);
126 #endif
127 
128  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
129 
132 
139  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
140 
141 
147  void writeStatusCmd(int commandId, int status, const std::string& description);
148 
149 
155  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
157 
158 
159 
160  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
161  return myVehicleStateChanges;
162  }
163 
164  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
165 
166  void collectObjectsInRange(int domain, const PositionVector& shape, SUMOReal range, std::set<std::string>& into);
167 
168 
171 
178  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
179 
180 
187  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
188 
189 
196  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
197 
198 
205  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
206 
207 
214  bool readTypeCheckingColor(tcpip::Storage& inputStorage, RGBColor& into);
215 
216 
223  bool readTypeCheckingPosition2D(tcpip::Storage& inputStorage, Position& into);
224 
225 
232  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
233 
234 
241  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
242 
243 
250  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
251 
252 
259  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
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 
328  std::map<std::string, MSVehicle*> myVTDControlledVehicles;
329 
330 
334  class Subscription {
335  public:
346  Subscription(int commandIdArg, const std::string& idArg, const std::vector<int>& variablesArg,
347  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, SUMOReal rangeArg)
348  : commandId(commandIdArg), id(idArg), variables(variablesArg), beginTime(beginTimeArg), endTime(endTimeArg),
349  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
350 
354  std::string id;
356  std::vector<int> variables;
367 
368  };
369 
371  std::vector<Subscription> mySubscriptions;
372 
374  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
375 
377  std::map<int, NamedRTree*> myObjects;
378 
381 
382 
383 private:
384  bool addObjectVariableSubscription(int commandId);
385  bool addObjectContextSubscription(int commandId);
386  void initialiseSubscription(const Subscription& s);
387  void removeSubscription(int commandId, const std::string& identity, int domain);
389  std::string& errors);
390 
391 
392  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
393 
394 
395 private:
397  TraCIServer& operator=(const TraCIServer& s);
398 
399 };
400 
401 
402 #endif
403 
404 #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:77
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
Definition: TraCIServer.h:362
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:434
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
void collectObjectsInRange(int domain, const PositionVector &shape, SUMOReal range, std::set< std::string > &into)
virtual ~TraCIServer()
Destructor.
bool commandGetVersion()
Returns the TraCI-version.
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:358
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:334
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 -&gt; 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.
Subscription(int commandIdArg, const std::string &idArg, const std::vector< int > &variablesArg, SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, SUMOReal rangeArg)
Constructor.
Definition: TraCIServer.h:346
void removeSubscription(int commandId, const std::string &identity, int domain)
static void close()
request termination of connection
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:371
Representation of a vehicle.
Definition: SUMOVehicle.h:63
SUMOReal range
The range of the context.
Definition: TraCIServer.h:366
bool commandCloseConnection()
Indicates the connection as being closed.
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:160
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
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.
RTree< MSLane *, MSLane, float, 2, TraCIServerAPI_Lane::StoringVisitor > * myLaneTree
A storage of lanes.
Definition: TraCIServer.h:380
void setVTDControlled(MSVehicle *v, MSLane *l, SUMOReal pos, int edgeOffset, MSEdgeVector route)
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:301
TraCIServer(const SUMOTime begin, const int port=0)
Constructor.
bool addObjectVariableSubscription(int commandId)
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:407
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:323
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
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
std::map< int, NamedRTree * > myObjects
A storage of objects.
Definition: TraCIServer.h:377
std::map< std::string, MSVehicle * > myVTDControlledVehicles
Definition: TraCIServer.h:328
int dispatchCommand()
#define SUMOReal
Definition: config.h:215
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool vtdDebug() const
int contextDomain
The domain ID of the context.
Definition: TraCIServer.h:364
bool addObjectContextSubscription(int commandId)
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:374
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:360