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