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-2014 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 
122  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 
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