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.sourceforge.net/
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 
124  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