SUMO - Simulation of Urban MObility

TraCIServerAPI_VehicleType.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00010 // APIs for getting/setting vehicle type 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 <limits>
00035 #include <microsim/MSNet.h>
00036 #include <microsim/MSVehicleType.h>
00037 #include "TraCIConstants.h"
00038 #include "TraCIServerAPI_VehicleType.h"
00039 
00040 #ifdef CHECK_MEMORY_LEAKS
00041 #include <foreign/nvwa/debug_new.h>
00042 #endif // CHECK_MEMORY_LEAKS
00043 
00044 
00045 // ===========================================================================
00046 // used namespaces
00047 // ===========================================================================
00048 using namespace traci;
00049 
00050 
00051 // ===========================================================================
00052 // method definitions
00053 // ===========================================================================
00054 bool
00055 TraCIServerAPI_VehicleType::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
00056                                        tcpip::Storage& outputStorage) {
00057     std::string warning = ""; // additional description for response
00058     // variable & id
00059     int variable = inputStorage.readUnsignedByte();
00060     std::string id = inputStorage.readString();
00061     // check variable
00062     if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
00063             && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
00064             && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
00065             && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT) {
00066         server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
00067         return false;
00068     }
00069     // begin response building
00070     tcpip::Storage tempMsg;
00071     //  response-code, variableID, objectID
00072     tempMsg.writeUnsignedByte(RESPONSE_GET_VEHICLETYPE_VARIABLE);
00073     tempMsg.writeUnsignedByte(variable);
00074     tempMsg.writeString(id);
00075     // process request
00076     if (variable == ID_LIST) {
00077         std::vector<std::string> ids;
00078         MSNet::getInstance()->getVehicleControl().insertVTypeIDs(ids);
00079         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00080         tempMsg.writeStringList(ids);
00081     } else if (variable == ID_COUNT) {
00082         std::vector<std::string> ids;
00083         MSNet::getInstance()->getVehicleControl().insertVTypeIDs(ids);
00084         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00085         tempMsg.writeInt((int) ids.size());
00086     } else {
00087         MSVehicleType* v = MSNet::getInstance()->getVehicleControl().getVType(id);
00088         if (v == 0) {
00089             server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
00090             return false;
00091         }
00092         getVariable(variable, *v, tempMsg);
00093     }
00094     server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
00095     server.writeResponseWithLength(outputStorage, tempMsg);
00096     return true;
00097 }
00098 
00099 bool
00100 TraCIServerAPI_VehicleType::getVariable(const int variable, const MSVehicleType& v, tcpip::Storage& tempMsg) {
00101     switch (variable) {
00102         case VAR_LENGTH:
00103             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00104             tempMsg.writeDouble(v.getLength());
00105             break;
00106         case VAR_MINGAP:
00107             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00108             tempMsg.writeDouble(v.getMinGap());
00109             break;
00110         case VAR_MAXSPEED:
00111             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00112             tempMsg.writeDouble(v.getMaxSpeed());
00113             break;
00114         case VAR_ACCEL:
00115             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00116             tempMsg.writeDouble(v.getCarFollowModel().getMaxAccel());
00117             break;
00118         case VAR_DECEL:
00119             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00120             tempMsg.writeDouble(v.getCarFollowModel().getMaxDecel());
00121             break;
00122         case VAR_IMPERFECTION:
00123             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00124             tempMsg.writeDouble(v.getCarFollowModel().getImperfection());
00125             break;
00126         case VAR_TAU:
00127             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00128             tempMsg.writeDouble(v.getCarFollowModel().getHeadwayTime());
00129             break;
00130         case VAR_SPEED_FACTOR:
00131             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00132             tempMsg.writeDouble(v.getSpeedFactor());
00133             break;
00134         case VAR_SPEED_DEVIATION:
00135             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00136             tempMsg.writeDouble(v.getSpeedDeviation());
00137             break;
00138         case VAR_VEHICLECLASS:
00139             tempMsg.writeUnsignedByte(TYPE_STRING);
00140             tempMsg.writeString(toString(v.getVehicleClass()));
00141             break;
00142         case VAR_EMISSIONCLASS:
00143             tempMsg.writeUnsignedByte(TYPE_STRING);
00144             tempMsg.writeString(getVehicleEmissionTypeName(v.getEmissionClass()));
00145             break;
00146         case VAR_SHAPECLASS:
00147             tempMsg.writeUnsignedByte(TYPE_STRING);
00148             tempMsg.writeString(getVehicleShapeName(v.getGuiShape()));
00149             break;
00150         case VAR_WIDTH:
00151             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00152             tempMsg.writeDouble(v.getGuiWidth());
00153             break;
00154         case VAR_COLOR:
00155             tempMsg.writeUnsignedByte(TYPE_COLOR);
00156             tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().red() * 255. + 0.5));
00157             tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().green() * 255. + 0.5));
00158             tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().blue() * 255. + 0.5));
00159             tempMsg.writeUnsignedByte(255);
00160             break;
00161         default:
00162             break;
00163     }
00164     return true;
00165 }
00166 
00167 bool
00168 TraCIServerAPI_VehicleType::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00169                                        tcpip::Storage& outputStorage) {
00170     std::string warning = ""; // additional description for response
00171     // variable
00172     int variable = inputStorage.readUnsignedByte();
00173     if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
00174             && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
00175             && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
00176             && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
00177             && variable != VAR_TAU && variable != VAR_COLOR
00178        ) {
00179         server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Change Vehicle Type State: unsupported variable specified", outputStorage);
00180         return false;
00181     }
00182     // id
00183     std::string id = inputStorage.readString();
00184     MSVehicleType* v = MSNet::getInstance()->getVehicleControl().getVType(id);
00185     if (v == 0) {
00186         server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
00187         return false;
00188     }
00189     // process
00190     try {
00191         if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, inputStorage.readUnsignedByte(),
00192                         *v, server, inputStorage, outputStorage)) {
00193             server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
00194             return true;
00195         }
00196     } catch (ProcessError& e) {
00197         server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, e.what(), outputStorage);
00198     }
00199     return false;
00200 }
00201 
00202 
00203 bool
00204 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable, const int valueDataType,
00205                                         MSVehicleType& v, traci::TraCIServer& server,
00206                                         tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
00207     switch (variable) {
00208         case VAR_LENGTH: {
00209             if (valueDataType != TYPE_DOUBLE) {
00210                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting length requires a double.", outputStorage);
00211                 return false;
00212             }
00213             double val = inputStorage.readDouble();
00214             if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
00215                 server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid length.", outputStorage);
00216                 return false;
00217             }
00218             v.setLength(val);
00219         }
00220         break;
00221         case VAR_MAXSPEED: {
00222             if (valueDataType != TYPE_DOUBLE) {
00223                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting maximum speed requires a double.", outputStorage);
00224                 return false;
00225             }
00226             double val = inputStorage.readDouble();
00227             if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
00228                 server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid maximum speed.", outputStorage);
00229                 return false;
00230             }
00231             v.setMaxSpeed(val);
00232         }
00233         break;
00234         case VAR_VEHICLECLASS: {
00235             if (valueDataType != TYPE_STRING) {
00236                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle class requires a string.", outputStorage);
00237                 return false;
00238             }
00239             v.setVClass(getVehicleClassID(inputStorage.readString()));
00240         }
00241         break;
00242         case VAR_SPEED_FACTOR: {
00243             if (valueDataType != TYPE_DOUBLE) {
00244                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed factor requires a double.", outputStorage);
00245                 return false;
00246             }
00247             v.setSpeedFactor(inputStorage.readDouble());
00248         }
00249         break;
00250         case VAR_SPEED_DEVIATION: {
00251             if (valueDataType != TYPE_DOUBLE) {
00252                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed deviation requires a double.", outputStorage);
00253                 return false;
00254             }
00255             v.setSpeedDeviation(inputStorage.readDouble());
00256         }
00257         break;
00258         case VAR_EMISSIONCLASS: {
00259             if (valueDataType != TYPE_STRING) {
00260                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting emission class requires a string.", outputStorage);
00261                 return false;
00262             }
00263             v.setEmissionClass(getVehicleEmissionTypeID(inputStorage.readString()));
00264         }
00265         break;
00266         case VAR_WIDTH: {
00267             if (valueDataType != TYPE_DOUBLE) {
00268                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting width requires a double.", outputStorage);
00269                 return false;
00270             }
00271             v.setWidth(inputStorage.readDouble());
00272         }
00273         break;
00274         case VAR_MINGAP: {
00275             if (valueDataType != TYPE_DOUBLE) {
00276                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting minimum gap requires a double.", outputStorage);
00277                 return false;
00278             }
00279             v.setMinGap(inputStorage.readDouble());
00280         }
00281         break;
00282         case VAR_SHAPECLASS: {
00283             if (valueDataType != TYPE_STRING) {
00284                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle shape requires a string.", outputStorage);
00285                 return false;
00286             }
00287             v.setShape(getVehicleShapeID(inputStorage.readString()));
00288         }
00289         break;
00290         case VAR_ACCEL: {
00291             if (valueDataType != TYPE_DOUBLE) {
00292                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting acceleration requires a double.", outputStorage);
00293                 return false;
00294             }
00295             v.getCarFollowModel().setMaxAccel(inputStorage.readDouble());
00296         }
00297         break;
00298         case VAR_DECEL: {
00299             if (valueDataType != TYPE_DOUBLE) {
00300                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting deceleration requires a double.", outputStorage);
00301                 return false;
00302             }
00303             v.getCarFollowModel().setMaxDecel(inputStorage.readDouble());
00304         }
00305         break;
00306         case VAR_IMPERFECTION: {
00307             if (valueDataType != TYPE_DOUBLE) {
00308                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting driver imperfection requires a double.", outputStorage);
00309                 return false;
00310             }
00311             v.getCarFollowModel().setImperfection(inputStorage.readDouble());
00312         }
00313         break;
00314         case VAR_TAU: {
00315             if (valueDataType != TYPE_DOUBLE) {
00316                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting headway time requires a double.", outputStorage);
00317                 return false;
00318             }
00319             v.getCarFollowModel().setHeadwayTime(inputStorage.readDouble());
00320         }
00321         break;
00322         case VAR_COLOR: {
00323             if (valueDataType != TYPE_COLOR) {
00324                 server.writeStatusCmd(cmd, RTYPE_ERR, "The color must be given using the according type.", outputStorage);
00325                 return false;
00326             }
00327             SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00328             SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00329             SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00330             inputStorage.readUnsignedByte(); // skip alpha level
00331             RGBColor col(r, g, b);
00332             v.setColor(col);
00333         }
00334         break;
00335         default:
00336             break;
00337     }
00338     return true;
00339 }
00340 
00341 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines