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-2012 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 
84 
88  static void openSocket(const std::map<int, CmdExecutor>& execs);
89 
90 
92  static void close();
93 
94 
98  static bool wasClosed();
100 
101 
102 
103 
105  static void processCommandsUntilSimStep(SUMOTime step);
106 
107 
108 
109 
110 #ifdef HAVE_PYTHON
111 
112  static std::string execute(std::string cmd);
113 
115  static void runEmbedded(std::string pyFile);
116 #endif
117 
118  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
119 
120  void writeStatusCmd(int commandId, int status, const std::string& description);
121  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
122 
123  const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
124  return myVehicleStateChanges;
125  }
126 
127  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
128 
129  void collectObjectsInRange(int domain, const PositionVector& shape, SUMOReal range, std::set<std::string>& into);
130 
131 
132 private:
136  TraCIServer(int port = 0);
137 
138 
140  virtual ~TraCIServer();
141 
142 
143 
146 
150  bool commandGetVersion();
151 
152 
156  bool commandCloseConnection();
157 
158 
162 
163 
167  bool commandAddVehicle();
169 
170 
171 
172 
173 
174  int dispatchCommand();
175 
176 
177 private:
180 
182  static bool myDoCloseConnection;
183 
186 
189 
192 
195 
199 
202 
204  const bool myAmEmbedded;
205 
207  std::map<int, CmdExecutor> myExecutors;
208 
209 
213  class Subscription {
214  public:
225  Subscription(int commandIdArg, const std::string& idArg, const std::vector<int>& variablesArg,
226  SUMOTime beginTimeArg, SUMOTime endTimeArg, bool contextVarsArg, int contextDomainArg, SUMOReal rangeArg)
227  : commandId(commandIdArg), id(idArg), variables(variablesArg), beginTime(beginTimeArg), endTime(endTimeArg),
228  contextVars(contextVarsArg), contextDomain(contextDomainArg), range(rangeArg) {}
229 
233  std::string id;
235  std::vector<int> variables;
246 
247  };
248 
250  std::vector<Subscription> mySubscriptions;
251 
253  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
254 
256  std::map<int, TraCIRTree*> myObjects;
257 
258 
259 private:
260  bool addObjectVariableSubscription(int commandId);
261  bool addObjectContextSubscription(int commandId);
262  void initialiseSubscription(const Subscription& s);
263  void removeSubscription(int commandId, const std::string& identity, int domain);
265  std::string& errors);
266 
267 
268  bool findObjectShape(int domain, const std::string& id, PositionVector& shape);
269 
270 
271 private:
273  TraCIServer& operator=(const TraCIServer& s);
274 
275 };
276 
277 }
278 
279 
280 #endif
281 
282 #endif