|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00010 // APIs for getting/setting lane values via TraCI 00011 /****************************************************************************/ 00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00014 /****************************************************************************/ 00015 // 00016 // This file is part of SUMO. 00017 // SUMO is free software: you can redistribute it and/or modify 00018 // it under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation, either version 3 of the License, or 00020 // (at your option) any later version. 00021 // 00022 /****************************************************************************/ 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 #include "TraCIConstants.h" 00035 #include <microsim/MSLane.h> 00036 #include "TraCIServerAPI_Lane.h" 00037 00038 #ifdef CHECK_MEMORY_LEAKS 00039 #include <foreign/nvwa/debug_new.h> 00040 #endif // CHECK_MEMORY_LEAKS 00041 00042 00043 // =========================================================================== 00044 // used namespaces 00045 // =========================================================================== 00046 using namespace traci; 00047 00048 00049 // =========================================================================== 00050 // method definitions 00051 // =========================================================================== 00052 bool 00053 TraCIServerAPI_Lane::processGet(TraCIServer& server, tcpip::Storage& inputStorage, 00054 tcpip::Storage& outputStorage) { 00055 std::string warning = ""; // additional description for response 00056 // variable 00057 int variable = inputStorage.readUnsignedByte(); 00058 std::string id = inputStorage.readString(); 00059 // check variable 00060 if (variable != ID_LIST && variable != LANE_LINK_NUMBER && variable != LANE_EDGE_ID && variable != VAR_LENGTH 00061 && variable != VAR_MAXSPEED && variable != LANE_LINKS && variable != VAR_SHAPE 00062 && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION && variable != VAR_PMXEMISSION 00063 && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION 00064 && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER 00065 && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER 00066 && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME 00067 && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_WIDTH && variable != ID_COUNT) { 00068 server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_ERR, "Get Lane Variable: unsupported variable specified", outputStorage); 00069 return false; 00070 } 00071 // begin response building 00072 tcpip::Storage tempMsg; 00073 // response-code, variableID, objectID 00074 tempMsg.writeUnsignedByte(RESPONSE_GET_LANE_VARIABLE); 00075 tempMsg.writeUnsignedByte(variable); 00076 tempMsg.writeString(id); 00077 if (variable == ID_LIST) { 00078 std::vector<std::string> ids; 00079 MSLane::insertIDs(ids); 00080 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00081 tempMsg.writeStringList(ids); 00082 } else if (variable == ID_COUNT) { 00083 std::vector<std::string> ids; 00084 MSLane::insertIDs(ids); 00085 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00086 tempMsg.writeInt((int) ids.size()); 00087 } else { 00088 MSLane* lane = MSLane::dictionary(id); 00089 if (lane == 0) { 00090 server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_ERR, "Lane '" + id + "' is not known", outputStorage); 00091 return false; 00092 } 00093 switch (variable) { 00094 case LANE_LINK_NUMBER: 00095 tempMsg.writeUnsignedByte(TYPE_UBYTE); 00096 tempMsg.writeUnsignedByte((int) lane->getLinkCont().size()); 00097 break; 00098 case LANE_EDGE_ID: 00099 tempMsg.writeUnsignedByte(TYPE_STRING); 00100 tempMsg.writeString(lane->getEdge().getID()); 00101 break; 00102 case VAR_LENGTH: 00103 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00104 tempMsg.writeDouble(lane->getLength()); 00105 break; 00106 case VAR_MAXSPEED: 00107 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00108 tempMsg.writeDouble(lane->getMaxSpeed()); 00109 break; 00110 case LANE_LINKS: { 00111 tempMsg.writeUnsignedByte(TYPE_COMPOUND); 00112 tcpip::Storage tempContent; 00113 unsigned int cnt = 0; 00114 tempContent.writeUnsignedByte(TYPE_INTEGER); 00115 const MSLinkCont& links = lane->getLinkCont(); 00116 tempContent.writeInt((int) links.size()); 00117 ++cnt; 00118 for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) { 00119 MSLink* link = (*i); 00120 // approached non-internal lane (if any) 00121 tempContent.writeUnsignedByte(TYPE_STRING); 00122 tempContent.writeString(link->getLane() != 0 ? link->getLane()->getID() : ""); 00123 ++cnt; 00124 // approached "via", internal lane (if any) 00125 tempContent.writeUnsignedByte(TYPE_STRING); 00126 #ifdef HAVE_INTERNAL_LANES 00127 tempContent.writeString(link->getViaLane() != 0 ? link->getViaLane()->getID() : ""); 00128 #else 00129 tempContent.writeString(""); 00130 #endif 00131 ++cnt; 00132 // priority 00133 tempContent.writeUnsignedByte(TYPE_UBYTE); 00134 tempContent.writeUnsignedByte(link->havePriority() ? 1 : 0); 00135 ++cnt; 00136 // opened 00137 tempContent.writeUnsignedByte(TYPE_UBYTE); 00138 tempContent.writeUnsignedByte(link->opened(MSNet::getInstance()->getCurrentTimeStep(), MSNet::getInstance()->getCurrentTimeStep(), 0.) ? 1 : 0); 00139 ++cnt; 00140 // approaching foe 00141 tempContent.writeUnsignedByte(TYPE_UBYTE); 00142 tempContent.writeUnsignedByte(link->hasApproachingFoe(MSNet::getInstance()->getCurrentTimeStep(), MSNet::getInstance()->getCurrentTimeStep()) ? 1 : 0); 00143 ++cnt; 00144 // state (not implemented, yet) 00145 tempContent.writeUnsignedByte(TYPE_STRING); 00146 tempContent.writeString(""); 00147 ++cnt; 00148 // direction (not implemented, yet) 00149 tempContent.writeUnsignedByte(TYPE_STRING); 00150 tempContent.writeString(""); 00151 ++cnt; 00152 // length 00153 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00154 tempContent.writeDouble(link->getLength()); 00155 ++cnt; 00156 } 00157 tempMsg.writeInt((int) cnt); 00158 tempMsg.writeStorage(tempContent); 00159 } 00160 break; 00161 case LANE_ALLOWED: { 00162 const SUMOVehicleClasses& allowed = lane->getAllowedClasses(); 00163 std::vector<std::string> allowedS; 00164 for (SUMOVehicleClasses::const_iterator i = allowed.begin(); i != allowed.end(); ++i) { 00165 allowedS.push_back(toString(*i)); 00166 } 00167 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00168 tempMsg.writeStringList(allowedS); 00169 } 00170 case LANE_DISALLOWED: { 00171 const SUMOVehicleClasses& disallowed = lane->getNotAllowedClasses(); 00172 std::vector<std::string> disallowedS; 00173 for (SUMOVehicleClasses::const_iterator i = disallowed.begin(); i != disallowed.end(); ++i) { 00174 disallowedS.push_back(toString(*i)); 00175 } 00176 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00177 tempMsg.writeStringList(disallowedS); 00178 } 00179 break; 00180 case VAR_SHAPE: 00181 tempMsg.writeUnsignedByte(TYPE_POLYGON); 00182 tempMsg.writeUnsignedByte((int)MIN2(static_cast<size_t>(255), lane->getShape().size())); 00183 for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), lane->getShape().size()); ++iPoint) { 00184 tempMsg.writeDouble(lane->getShape()[iPoint].x()); 00185 tempMsg.writeDouble(lane->getShape()[iPoint].y()); 00186 } 00187 break; 00188 case VAR_CO2EMISSION: 00189 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00190 tempMsg.writeDouble(lane->getHBEFA_CO2Emissions()); 00191 break; 00192 case VAR_COEMISSION: 00193 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00194 tempMsg.writeDouble(lane->getHBEFA_COEmissions()); 00195 break; 00196 case VAR_HCEMISSION: 00197 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00198 tempMsg.writeDouble(lane->getHBEFA_HCEmissions()); 00199 break; 00200 case VAR_PMXEMISSION: 00201 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00202 tempMsg.writeDouble(lane->getHBEFA_PMxEmissions()); 00203 break; 00204 case VAR_NOXEMISSION: 00205 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00206 tempMsg.writeDouble(lane->getHBEFA_NOxEmissions()); 00207 break; 00208 case VAR_FUELCONSUMPTION: 00209 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00210 tempMsg.writeDouble(lane->getHBEFA_FuelConsumption()); 00211 break; 00212 case VAR_NOISEEMISSION: 00213 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00214 tempMsg.writeDouble(lane->getHarmonoise_NoiseEmissions()); 00215 break; 00216 case LAST_STEP_VEHICLE_NUMBER: 00217 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00218 tempMsg.writeInt((int) lane->getVehicleNumber()); 00219 break; 00220 case LAST_STEP_MEAN_SPEED: 00221 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00222 tempMsg.writeDouble(lane->getMeanSpeed()); 00223 break; 00224 case LAST_STEP_VEHICLE_ID_LIST: { 00225 std::vector<std::string> vehIDs; 00226 const std::deque<MSVehicle*> &vehs = lane->getVehiclesSecure(); 00227 for (std::deque<MSVehicle*>::const_iterator j = vehs.begin(); j != vehs.end(); ++j) { 00228 vehIDs.push_back((*j)->getID()); 00229 } 00230 lane->releaseVehicles(); 00231 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00232 tempMsg.writeStringList(vehIDs); 00233 } 00234 break; 00235 case LAST_STEP_OCCUPANCY: 00236 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00237 tempMsg.writeDouble(lane->getOccupancy()); 00238 break; 00239 case LAST_STEP_VEHICLE_HALTING_NUMBER: { 00240 int halting = 0; 00241 const std::deque<MSVehicle*> &vehs = lane->getVehiclesSecure(); 00242 for (std::deque<MSVehicle*>::const_iterator j = vehs.begin(); j != vehs.end(); ++j) { 00243 if ((*j)->getSpeed() < 0.1) { 00244 ++halting; 00245 } 00246 } 00247 lane->releaseVehicles(); 00248 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00249 tempMsg.writeInt(halting); 00250 } 00251 break; 00252 case LAST_STEP_LENGTH: { 00253 SUMOReal lengthSum = 0; 00254 const std::deque<MSVehicle*> &vehs = lane->getVehiclesSecure(); 00255 for (std::deque<MSVehicle*>::const_iterator j = vehs.begin(); j != vehs.end(); ++j) { 00256 lengthSum += (*j)->getVehicleType().getLengthWithGap(); 00257 } 00258 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00259 if (vehs.size() == 0) { 00260 tempMsg.writeDouble(0); 00261 } else { 00262 tempMsg.writeDouble(lengthSum / (SUMOReal) vehs.size()); 00263 } 00264 lane->releaseVehicles(); 00265 } 00266 break; 00267 case VAR_CURRENT_TRAVELTIME: { 00268 SUMOReal meanSpeed = lane->getMeanSpeed(); 00269 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00270 if (meanSpeed != 0) { 00271 tempMsg.writeDouble(lane->getLength() / meanSpeed); 00272 } else { 00273 tempMsg.writeDouble(1000000.); 00274 } 00275 } 00276 break; 00277 case VAR_WIDTH: 00278 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00279 tempMsg.writeDouble(lane->getWidth()); 00280 break; 00281 default: 00282 break; 00283 } 00284 } 00285 server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage); 00286 server.writeResponseWithLength(outputStorage, tempMsg); 00287 return true; 00288 } 00289 00290 00291 bool 00292 TraCIServerAPI_Lane::processSet(TraCIServer& server, tcpip::Storage& inputStorage, 00293 tcpip::Storage& outputStorage) { 00294 std::string warning = ""; // additional description for response 00295 // variable 00296 int variable = inputStorage.readUnsignedByte(); 00297 if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED) { 00298 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_ERR, "Change Lane State: unsupported variable specified", outputStorage); 00299 return false; 00300 } 00301 // id 00302 std::string id = inputStorage.readString(); 00303 MSLane* l = MSLane::dictionary(id); 00304 if (l == 0) { 00305 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_ERR, "Lane '" + id + "' is not known", outputStorage); 00306 return false; 00307 } 00308 // process 00309 int valueDataType = inputStorage.readUnsignedByte(); 00310 switch (variable) { 00311 case VAR_MAXSPEED: { 00312 // speed 00313 if (valueDataType != TYPE_DOUBLE) { 00314 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_ERR, "The speed must be given as a double.", outputStorage); 00315 return false; 00316 } 00317 SUMOReal val = inputStorage.readDouble(); 00318 l->setMaxSpeed(val); 00319 } 00320 break; 00321 case VAR_LENGTH: { 00322 // speed 00323 if (valueDataType != TYPE_DOUBLE) { 00324 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_ERR, "The length must be given as a double.", outputStorage); 00325 return false; 00326 } 00327 SUMOReal val = inputStorage.readDouble(); 00328 l->setLength(val); 00329 } 00330 break; 00331 case LANE_ALLOWED: { 00332 if (valueDataType != TYPE_STRINGLIST) { 00333 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_ERR, "Allowed classes must be given as a list of strings.", outputStorage); 00334 return false; 00335 } 00336 SUMOVehicleClasses allowed; 00337 parseVehicleClasses(inputStorage.readStringList(), allowed); 00338 l->setAllowedClasses(allowed); 00339 l->getEdge().rebuildAllowedLanes(); 00340 } 00341 break; 00342 case LANE_DISALLOWED: { 00343 if (valueDataType != TYPE_STRINGLIST) { 00344 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_ERR, "Not allowed classes must be given as a list of strings.", outputStorage); 00345 return false; 00346 } 00347 SUMOVehicleClasses disallowed; 00348 parseVehicleClasses(inputStorage.readStringList(), disallowed); 00349 l->setNotAllowedClasses(disallowed); 00350 l->getEdge().rebuildAllowedLanes(); 00351 } 00352 break; 00353 default: 00354 break; 00355 } 00356 server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage); 00357 return true; 00358 } 00359 00360 00361 /****************************************************************************/ 00362