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 
61 #include <map>
62 #include <string>
63 #include <set>
64 
65 
66 // ===========================================================================
67 // class definitions
68 // ===========================================================================
72 namespace traci {
73 // TraCIServer
74 // Allows communication of sumo with external program. The external
75 // program will control sumo.
77 public:
79  typedef bool(*CmdExecutor)(traci::TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
80 
81 
85  static void openSocket(const std::map<int, CmdExecutor> &execs);
86 
88  static void processCommandsUntilSimStep(SUMOTime step);
89 
91  static bool wasClosed();
92 
94  static void close();
95 
96 #ifdef HAVE_PYTHON
97 
98  static std::string execute(std::string cmd);
99 
101  static void runEmbedded(std::string pyFile);
102 #endif
103 
104  void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
105 
106  void writeStatusCmd(int commandId, int status, const std::string& description);
107  void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
108 
109  const std::map<MSNet::VehicleState, std::vector<std::string> > &getVehicleStateChanges() const {
110  return myVehicleStateChanges;
111  }
112 
120  std::map<int, CmdExecutor> &getExecutors() {
121  return myExecutors;
122  }
123 
124  void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
125 
126 
127 private:
128 
129  // Constructor
130  TraCIServer(int port = 0);
131 
132  // Destructor
133  // final cleanup
134  virtual ~TraCIServer();
135 
136  int dispatchCommand();
137 
139 
140  bool commandGetVersion();
141 
142  bool commandCloseConnection();
143 
144  bool commandAddVehicle();
145 
146  bool addSubscription(int commandId);
147 
150  static bool myDoCloseConnection;
151 
154 
155  // simulation begin and end time
157 
158 
163  const bool myAmEmbedded;
164 
166  std::map<int, CmdExecutor> myExecutors;
167 
168 
169  class Subscription {
170  public:
171  Subscription(int commandIdArg, const std::string& idArg, const std::vector<int> &variablesArg,
172  SUMOTime beginTimeArg, SUMOTime endTimeArg)
173  : commandId(commandIdArg), id(idArg), variables(variablesArg), beginTime(beginTimeArg), endTime(endTimeArg) {}
175  std::string id;
176  std::vector<int> variables;
179 
180  };
181 
182  std::vector<Subscription> mySubscriptions;
183 
185  std::string& errors);
186 
187  std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
188 
189 };
190 
191 }
192 
193 
194 #endif
195 
196 #endif