TraCIServerAPI_Simulation.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00044
00045 using namespace std;
00046 using namespace traci;
00047 using namespace tcpip;
00048
00049
00050
00051
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 = "";
00059
00060 int variable = inputStorage.readUnsignedByte();
00061 std::string id = inputStorage.readString();
00062
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
00074 Storage tempMsg;
00075
00076 tempMsg.writeUnsignedByte(RESPONSE_GET_SIM_VARIABLE);
00077 tempMsg.writeUnsignedByte(variable);
00078 tempMsg.writeString(id);
00079
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
00152 outputStorage.writeUnsignedByte(0);
00153 outputStorage.writeInt(1 + 4 + tempMsg.size());
00154 outputStorage.writeStorage(tempMsg);
00155 return true;
00156 }
00157
00158
00159
00160