SUMO - Simulation of Urban MObility
TraCIServer.h
Go to the documentation of this file.
00001 /****************************************************************************/
00015 /****************************************************************************/
00016 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00017 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00018 /****************************************************************************/
00019 //
00020 //   This file is part of SUMO.
00021 //   SUMO is free software: you can redistribute it and/or modify
00022 //   it under the terms of the GNU General Public License as published by
00023 //   the Free Software Foundation, either version 3 of the License, or
00024 //   (at your option) any later version.
00025 //
00026 /****************************************************************************/
00027 #ifndef TRACISERVER_H
00028 #define TRACISERVER_H
00029 
00030 
00031 // ===========================================================================
00032 // included modules
00033 // ===========================================================================
00034 #ifdef _MSC_VER
00035 #include <windows_config.h>
00036 #else
00037 #include <config.h>
00038 #endif
00039 
00040 #ifndef NO_TRACI
00041 
00042 #include "TraCIConstants.h"
00043 
00044 #define BUILD_TCPIP
00045 #include <foreign/tcpip/socket.h>
00046 #include <foreign/tcpip/storage.h>
00047 #include <utils/common/SUMOTime.h>
00048 #include <utils/common/ToString.h>
00049 
00050 #include <utils/geom/Boundary.h>
00051 #include <utils/geom/Position.h>
00052 #include <utils/geom/GeomHelper.h>
00053 #include <utils/shapes/Polygon.h>
00054 #include <utils/shapes/PointOfInterest.h>
00055 #include <utils/options/OptionsCont.h>
00056 #include <microsim/MSVehicle.h>
00057 #include <microsim/MSNet.h>
00058 #include <microsim/traffic_lights/MSTrafficLightLogic.h>
00059 #include "TraCIException.h"
00060 
00061 #include <map>
00062 #include <string>
00063 #include <set>
00064 
00065 
00066 // ===========================================================================
00067 // class definitions
00068 // ===========================================================================
00072 namespace traci {
00073 // TraCIServer
00074 // Allows communication of sumo with external program. The external
00075 // program will control sumo.
00076 class TraCIServer : public MSNet::VehicleStateListener {
00077 public:
00079     typedef bool(*CmdExecutor)(traci::TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
00080 
00081 
00085     static void openSocket(const std::map<int, CmdExecutor> &execs);
00086 
00088     static void processCommandsUntilSimStep(SUMOTime step);
00089 
00091     static bool wasClosed();
00092 
00094     static void close();
00095 
00096 #ifdef HAVE_PYTHON
00097 
00098     static std::string execute(std::string cmd);
00099 
00101     static void runEmbedded(std::string pyFile);
00102 #endif
00103 
00104     void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to);
00105 
00106     void writeStatusCmd(int commandId, int status, const std::string& description);
00107     void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
00108 
00109     const std::map<MSNet::VehicleState, std::vector<std::string> > &getVehicleStateChanges() const {
00110         return myVehicleStateChanges;
00111     }
00112 
00120     std::map<int, CmdExecutor> &getExecutors() {
00121         return myExecutors;
00122     }
00123 
00124     void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
00125 
00126 
00127 private:
00128 
00129     // Constructor
00130     TraCIServer(int port = 0);
00131 
00132     // Destructor
00133     // final cleanup
00134     virtual ~TraCIServer();
00135 
00136     int dispatchCommand();
00137 
00138     void postProcessSimulationStep2();
00139 
00140     bool commandGetVersion();
00141 
00142     bool commandCloseConnection();
00143 
00144     bool commandAddVehicle();
00145 
00146     bool addSubscription(int commandId);
00147 
00149     static TraCIServer* myInstance;
00150     static bool myDoCloseConnection;
00151 
00153     tcpip::Socket* mySocket;
00154 
00155     // simulation begin and end time
00156     SUMOTime myTargetTime;
00157 
00158 
00159     tcpip::Storage myInputStorage;
00160     tcpip::Storage myOutputStorage;
00161     bool myDoingSimStep;
00162     bool myHaveWarnedDeprecation;
00163     const bool myAmEmbedded;
00164 
00166     std::map<int, CmdExecutor> myExecutors;
00167 
00168 
00169     class Subscription {
00170     public:
00171         Subscription(int commandIdArg, const std::string& idArg, const std::vector<int> &variablesArg,
00172                      SUMOTime beginTimeArg, SUMOTime endTimeArg)
00173             : commandId(commandIdArg), id(idArg), variables(variablesArg), beginTime(beginTimeArg), endTime(endTimeArg) {}
00174         int commandId;
00175         std::string id;
00176         std::vector<int> variables;
00177         SUMOTime beginTime;
00178         SUMOTime endTime;
00179 
00180     };
00181 
00182     std::vector<Subscription> mySubscriptions;
00183 
00184     bool processSingleSubscription(const TraCIServer::Subscription& s, tcpip::Storage& writeInto,
00185                                    std::string& errors);
00186 
00187     std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
00188 
00189 };
00190 
00191 }
00192 
00193 
00194 #endif
00195 
00196 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines