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"
60 #include "TraCIRTree.h"
61 
62 #include <map>
63 #include <string>
64 #include <set>
65 
66 
67 
68 // ===========================================================================
69 // class definitions
70 // ===========================================================================
71 namespace traci {
72 
77 public:
79  typedef bool(*CmdExecutor)(traci::TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
80 
81 
83  return myTargetTime;
84  }
86  return myInstance;
87  }
88 
91 
95  static void openSocket(const std::map<int, CmdExecutor>& execs);
96 
97 
99  static void close();
100 
101 
105  static bool wasClosed();
107 
108 
109 
110 
112  static void processCommandsUntilSimStep(SUMOTime step);
113 
114  void setVTDControlled(MSVehicle* v, MSLane* l, SUMOReal pos, int edgeOffset, MSEdgeVector route);
115 
116  void postProcessVTD();
117 
118  bool vtdDebug() const;
119 
120 
121 
122 #ifdef HAVE_PYTHON
123  static std::string execute(std::string cmd);
125 
127  static void runEmbedded(std::string pyFile);
128 #endif
129 
130  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
131 
134 
141  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
142 
143 
149  void writeStatusCmd(int commandId, int status, const std::string& description);
150 
151 
157  bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
159 
160 
161 
162  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
163  return myVehicleStateChanges;
164  }
165 
166  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
167 
168  void collectObjectsInRange(int domain, const PositionVector& shape, SUMOReal range, std::set<std::string>& into);
169 
170 
173 
180  bool readTypeCheckingInt(tcpip::Storage& inputStorage, int& into);
181 
182 
189  bool readTypeCheckingDouble(tcpip::Storage& inputStorage, double& into);
190 
191 
198  bool readTypeCheckingString(tcpip::Storage& inputStorage, std::string& into);
199 
200 
207  bool readTypeCheckingStringList(tcpip::Storage& inputStorage, std::vector<std::string>& into);
208 
209 
216  bool readTypeCheckingColor(tcpip::Storage& inputStorage, RGBColor& into);
217 
218 
225  bool readTypeCheckingPosition2D(tcpip::Storage& inputStorage, Position& into);
226 
227 
234  bool readTypeCheckingBoundary(tcpip::Storage& inputStorage, Boundary& into);
235 
236 
243  bool readTypeCheckingByte(tcpip::Storage& inputStorage, int& into);
244 
245 
252  bool readTypeCheckingUnsignedByte(tcpip::Storage& inputStorage, int& into);
253 
254 
261  bool readTypeCheckingPolygon(tcpip::Storage& inputStorage, PositionVector& into);
263 
264 private:
268  TraCIServer(int port = 0);
269 
270 
272  virtual ~TraCIServer();
273 
274 
275 
278 
282  bool commandGetVersion();
283 
284 
288  bool commandCloseConnection();
289 
290 
294 
295 
299  bool commandAddVehicle();
301 
302 
303 
304 
305 
306  int dispatchCommand();
307 
308 
309 
310 
311 
312 
313 private:
316 
318  static bool myDoCloseConnection;
319 
322 
325 
328 
331 
335 
338 
340  const bool myAmEmbedded;
341 
343  std::map<int, CmdExecutor> myExecutors;
344 
345  std::map<std::string, MSVehicle*> myVTDControlledVehicles;
346 
347 
351  class Subscription {
352  public:
363  Subscription(int commandIdArg, const std::string& idArg, const std::vector<int>& variablesArg,
364  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, SUMOReal rangeArg)
365  : commandId(commandIdArg), id(idArg), variables(variablesArg), beginTime(beginTimeArg), endTime(endTimeArg),
366  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
367 
371  std::string id;
373  std::vector<int> variables;
384 
385  };
386 
388  std::vector<Subscription> mySubscriptions;
389 
391  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
392 
394  std::map<int, TraCIRTree*> myObjects;
395 
396 
397 private:
398  bool addObjectVariableSubscription(int commandId);
399  bool addObjectContextSubscription(int commandId);
400  void initialiseSubscription(const Subscription& s);
401  void removeSubscription(int commandId, const std::string& identity, int domain);
403  std::string& errors);
404 
405 
406  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
407 
408 
409 private:
411  TraCIServer& operator=(const TraCIServer& s);
412 
413 };
414 
415 }
416 
417 
418 #endif
419 
420 #endif
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
Definition: TraCIServer.h:391
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:363
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
Interface for objects listening to vehicle state changes.
Definition: MSNet.h:424
tcpip::Storage myOutputStorage
The storage to writeto.
Definition: TraCIServer.h:330
bool myDoingSimStep
Whether a step is currently done.
Definition: TraCIServer.h:334
bool(* CmdExecutor)(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Definition of a method to be called for serving an associated commandID.
Definition: TraCIServer.h:79
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
void initialiseSubscription(const Subscription &s)
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
Definition: TraCIServer.h:388
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
Definition: TraCIServer.h:379
std::map< int, CmdExecutor > myExecutors
Map of commandIds -&gt; their executors; applicable if the executor applies to the method footprint...
Definition: TraCIServer.h:343
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
std::string id
The id of the object that is subscribed.
Definition: TraCIServer.h:371
void postProcessSimulationStep2()
Handles subscriptions to send after a simstep2 command.
Representation of a subscription.
Definition: TraCIServer.h:351
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
SUMOReal range
The range of the context.
Definition: TraCIServer.h:383
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to)
Called if a vehicle changes its state.
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
tcpip::Storage myInputStorage
The storage to read from.
Definition: TraCIServer.h:327
static bool wasClosed()
check whether close was requested
const bool myAmEmbedded
Whether the server runs in embedded mode.
Definition: TraCIServer.h:340
Representation of a vehicle.
Definition: SUMOVehicle.h:63
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
void setVTDControlled(MSVehicle *v, MSLane *l, SUMOReal pos, int edgeOffset, MSEdgeVector route)
bool addObjectVariableSubscription(int commandId)
SUMOTime beginTime
The begin time of the subscription.
Definition: TraCIServer.h:375
std::map< std::string, MSVehicle * > myVTDControlledVehicles
Definition: TraCIServer.h:345
static void close()
request termination of connection
SUMOTime endTime
The end time of the subscription.
Definition: TraCIServer.h:377
static TraCIServer * myInstance
Singleton instance of the server.
Definition: TraCIServer.h:315
virtual ~TraCIServer()
Destructor.
static TraCIServer * getInstance()
Definition: TraCIServer.h:85
tcpip::Socket * mySocket
The socket on which server is listening on.
Definition: TraCIServer.h:321
VehicleState
Definition of a vehicle state.
Definition: MSNet.h:405
SUMOTime myTargetTime
The time step to reach until processing the next commands.
Definition: TraCIServer.h:324
bool vtdDebug() const
void removeSubscription(int commandId, const std::string &identity, int domain)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static bool myDoCloseConnection
Whether the connection was set to be to close.
Definition: TraCIServer.h:318
std::map< int, TraCIRTree * > myObjects
A storage of objects.
Definition: TraCIServer.h:394
TraCIServer(int port=0)
Constructor.
bool commandCloseConnection()
Indicates the connection as being closed.
static void processCommandsUntilSimStep(SUMOTime step)
process all commands until a simulation step is wanted
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
SUMOTime getTargetTime()
Definition: TraCIServer.h:82
bool addObjectContextSubscription(int commandId)
int commandId
commandIdArg The command id of the subscription
Definition: TraCIServer.h:369
#define SUMOReal
Definition: config.h:221
int contextDomain
The domain ID of the context.
Definition: TraCIServer.h:381
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
Definition: TraCIServer.h:162
bool commandGetVersion()
Returns the TraCI-version.
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
void collectObjectsInRange(int domain, const PositionVector &shape, SUMOReal range, std::set< std::string > &into)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
bool commandAddVehicle()
Adds a vehicle.
Representation of a lane in the micro simulation.
Definition: MSLane.h:73
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
TraCIServer & operator=(const TraCIServer &s)
Invalidated assignment operator.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
bool myHaveWarnedDeprecation
Whether the usage of deprecated methods was already reported.
Definition: TraCIServer.h:337
std::vector< int > variables
The subscribed variables.
Definition: TraCIServer.h:373