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