TraCIServerAPI_Simulation.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // APIs for getting/setting edge values via TraCI
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <utils/common/StdDefs.h>
00031 #include <microsim/MSNet.h>
00032 #include <microsim/MSVehicle.h>
00033 #include "TraCIConstants.h"
00034 #include "TraCIServerAPIHelper.h"
00035 #include "TraCIServerAPI_Simulation.h"
00036 
00037 #ifdef CHECK_MEMORY_LEAKS
00038 #include <foreign/nvwa/debug_new.h>
00039 #endif // CHECK_MEMORY_LEAKS
00040 
00041 
00042 // ===========================================================================
00043 // used namespaces
00044 // ===========================================================================
00045 using namespace std;
00046 using namespace traci;
00047 using namespace tcpip;
00048 
00049 
00050 // ===========================================================================
00051 // method definitions
00052 // ===========================================================================
00053 bool
00054 TraCIServerAPI_Simulation::processGet(tcpip::Storage &inputStorage,
00055                                       tcpip::Storage &outputStorage,
00056                                       const std::map<MSNet::VehicleState, std::vector<std::string> > &infos,
00057                                       bool withStatus) throw(TraCIException) {
00058     std::string warning = ""; // additional description for response
00059     // variable & id
00060     int variable = inputStorage.readUnsignedByte();
00061     std::string id = inputStorage.readString();
00062     // check variable
00063     if (variable!=VAR_TIME_STEP
00064             &&variable!=VAR_LOADED_VEHICLES_NUMBER&&variable!=VAR_LOADED_VEHICLES_IDS
00065             &&variable!=VAR_DEPARTED_VEHICLES_NUMBER&&variable!=VAR_DEPARTED_VEHICLES_IDS
00066             &&variable!=VAR_TELEPORT_STARTING_VEHICLES_NUMBER&&variable!=VAR_TELEPORT_STARTING_VEHICLES_IDS
00067             &&variable!=VAR_TELEPORT_ENDING_VEHICLES_NUMBER&&variable!=VAR_TELEPORT_ENDING_VEHICLES_IDS
00068             &&variable!=VAR_ARRIVED_VEHICLES_NUMBER&&variable!=VAR_ARRIVED_VEHICLES_IDS
00069        ) {
00070         TraCIServerAPIHelper::writeStatusCmd(CMD_GET_SIM_VARIABLE, RTYPE_ERR, "Get Simulation Variable: unsupported variable specified", outputStorage);
00071         return false;
00072     }
00073     // begin response building
00074     Storage tempMsg;
00075     //  response-code, variableID, objectID
00076     tempMsg.writeUnsignedByte(RESPONSE_GET_SIM_VARIABLE);
00077     tempMsg.writeUnsignedByte(variable);
00078     tempMsg.writeString(id);
00079     // process request
00080     switch (variable) {
00081     case VAR_TIME_STEP:
00082         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00083         tempMsg.writeInt(MSNet::getInstance()->getCurrentTimeStep());
00084         break;
00085     case VAR_LOADED_VEHICLES_NUMBER: {
00086         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_BUILT)->second;
00087         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00088         tempMsg.writeInt((int) ids.size());
00089     }
00090     break;
00091     case VAR_LOADED_VEHICLES_IDS: {
00092         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_BUILT)->second;
00093         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00094         tempMsg.writeStringList(ids);
00095     }
00096     break;
00097     case VAR_DEPARTED_VEHICLES_NUMBER: {
00098         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_DEPARTED)->second;
00099         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00100         tempMsg.writeInt((int) ids.size());
00101     }
00102     break;
00103     case VAR_DEPARTED_VEHICLES_IDS: {
00104         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_DEPARTED)->second;
00105         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00106         tempMsg.writeStringList(ids);
00107     }
00108     break;
00109     case VAR_TELEPORT_STARTING_VEHICLES_NUMBER: {
00110         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_STARTING_TELEPORT)->second;
00111         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00112         tempMsg.writeInt((int) ids.size());
00113     }
00114     break;
00115     case VAR_TELEPORT_STARTING_VEHICLES_IDS: {
00116         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_STARTING_TELEPORT)->second;
00117         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00118         tempMsg.writeStringList(ids);
00119     }
00120     break;
00121     case VAR_TELEPORT_ENDING_VEHICLES_NUMBER: {
00122         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_ENDING_TELEPORT)->second;
00123         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00124         tempMsg.writeInt((int) ids.size());
00125     }
00126     break;
00127     case VAR_TELEPORT_ENDING_VEHICLES_IDS: {
00128         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_ENDING_TELEPORT)->second;
00129         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00130         tempMsg.writeStringList(ids);
00131     }
00132     break;
00133     case VAR_ARRIVED_VEHICLES_NUMBER: {
00134         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_ARRIVED)->second;
00135         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00136         tempMsg.writeInt((int) ids.size());
00137     }
00138     break;
00139     case VAR_ARRIVED_VEHICLES_IDS: {
00140         const std::vector<std::string> &ids = infos.find(MSNet::VEHICLE_STATE_ARRIVED)->second;
00141         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00142         tempMsg.writeStringList(ids);
00143     }
00144     break;
00145     default:
00146         break;
00147     }
00148     if (withStatus) {
00149         TraCIServerAPIHelper::writeStatusCmd(CMD_GET_SIM_VARIABLE, RTYPE_OK, warning, outputStorage);
00150     }
00151     // send response
00152     outputStorage.writeUnsignedByte(0); // command length -> extended
00153     outputStorage.writeInt(1 + 4 + tempMsg.size());
00154     outputStorage.writeStorage(tempMsg);
00155     return true;
00156 }
00157 
00158 
00159 /****************************************************************************/
00160 

Generated on Wed May 5 00:06:37 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6