SUMO - Simulation of Urban MObility

TraCIServer.h

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