SUMO - Simulation of Urban MObility
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // APIs for getting/setting POI values via TraCI
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #ifndef NO_TRACI
00034 
00035 #include <microsim/MSNet.h>
00036 #include <utils/shapes/PointOfInterest.h>
00037 #include <utils/shapes/ShapeContainer.h>
00038 #include "TraCIConstants.h"
00039 #include "TraCIServerAPI_POI.h"
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // used namespaces
00048 // ===========================================================================
00049 using namespace traci;
00050 
00051 
00052 // ===========================================================================
00053 // method definitions
00054 // ===========================================================================
00055 bool
00056 TraCIServerAPI_POI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
00057                                tcpip::Storage& outputStorage) {
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 != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION && variable != ID_COUNT) {
00064         server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_ERR, "Get PoI Variable: unsupported variable specified", outputStorage);
00065         return false;
00066     }
00067     // begin response building
00068     tcpip::Storage tempMsg;
00069     //  response-code, variableID, objectID
00070     tempMsg.writeUnsignedByte(RESPONSE_GET_POI_VARIABLE);
00071     tempMsg.writeUnsignedByte(variable);
00072     tempMsg.writeString(id);
00073     // process request
00074     if (variable == ID_LIST || variable == ID_COUNT) {
00075         std::vector<std::string> ids;
00076         ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
00077         for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) {
00078             shapeCont.getPOICont(i).insertIDs(ids);
00079         }
00080         if (variable == ID_LIST) {
00081             tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00082             tempMsg.writeStringList(ids);
00083         } else {
00084             tempMsg.writeUnsignedByte(TYPE_INTEGER);
00085             tempMsg.writeInt((int) ids.size());
00086         }
00087     } else {
00088         PointOfInterest* p = 0;
00089         ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
00090         for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer() && p == 0; ++i) {
00091             p = shapeCont.getPOICont(i).get(id);
00092         }
00093         if (p == 0) {
00094             server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_ERR, "POI '" + id + "' is not known", outputStorage);
00095             return false;
00096         }
00097         switch (variable) {
00098             case VAR_TYPE:
00099                 tempMsg.writeUnsignedByte(TYPE_STRING);
00100                 tempMsg.writeString(p->getType());
00101                 break;
00102             case VAR_COLOR:
00103                 tempMsg.writeUnsignedByte(TYPE_COLOR);
00104                 tempMsg.writeUnsignedByte(static_cast<int>(p->red() * 255. + .5));
00105                 tempMsg.writeUnsignedByte(static_cast<int>(p->green() * 255. + .5));
00106                 tempMsg.writeUnsignedByte(static_cast<int>(p->blue() * 255. + .5));
00107                 tempMsg.writeUnsignedByte(255);
00108                 break;
00109             case VAR_POSITION:
00110                 tempMsg.writeUnsignedByte(POSITION_2D);
00111                 tempMsg.writeDouble(p->x());
00112                 tempMsg.writeDouble(p->y());
00113                 break;
00114             default:
00115                 break;
00116         }
00117     }
00118     server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
00119     server.writeResponseWithLength(outputStorage, tempMsg);
00120     return true;
00121 }
00122 
00123 
00124 bool
00125 TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00126                                tcpip::Storage& outputStorage) {
00127     std::string warning = ""; // additional description for response
00128     // variable
00129     int variable = inputStorage.readUnsignedByte();
00130     if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION
00131             && variable != ADD && variable != REMOVE) {
00132         server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Change PoI State: unsupported variable specified", outputStorage);
00133         return false;
00134     }
00135     // id
00136     std::string id = inputStorage.readString();
00137     PointOfInterest* p = 0;
00138     int layer = 0;
00139     ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
00140     if (variable != ADD && variable != REMOVE) {
00141         for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer() && p == 0; ++i) {
00142             p = shapeCont.getPOICont(i).get(id);
00143             layer = i;
00144         }
00145         if (p == 0) {
00146             server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "POI '" + id + "' is not known", outputStorage);
00147             return false;
00148         }
00149     }
00150     // process
00151     int valueDataType = inputStorage.readUnsignedByte();
00152     switch (variable) {
00153         case VAR_TYPE: {
00154             if (valueDataType != TYPE_STRING) {
00155                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The type must be given as a string.", outputStorage);
00156                 return false;
00157             }
00158             std::string type = inputStorage.readString();
00159             p->setType(type);
00160         }
00161         break;
00162         case VAR_COLOR: {
00163             if (valueDataType != TYPE_COLOR) {
00164                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The color must be given using an accoring type.", outputStorage);
00165                 return false;
00166             }
00167             SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00168             SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00169             SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00170             //read SUMOReal a
00171             inputStorage.readUnsignedByte();
00172             dynamic_cast<RGBColor*>(p)->set(r, g, b);
00173         }
00174         break;
00175         case VAR_POSITION: {
00176             if (valueDataType != POSITION_2D) {
00177                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The position must be given using an accoring type.", outputStorage);
00178                 return false;
00179             }
00180             SUMOReal x = inputStorage.readDouble();
00181             SUMOReal y = inputStorage.readDouble();
00182             shapeCont.movePoI(layer, id, Position(x, y));
00183         }
00184         break;
00185         case ADD: {
00186             if (valueDataType != TYPE_COMPOUND) {
00187                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "A compound object is needed for setting a new PoI.", outputStorage);
00188                 return false;
00189             }
00190             //read itemNo
00191             inputStorage.readInt();
00192             // type
00193             if (inputStorage.readUnsignedByte() != TYPE_STRING) {
00194                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The first PoI parameter must be the type encoded as a string.", outputStorage);
00195                 return false;
00196             }
00197             std::string type = inputStorage.readString();
00198             // color
00199             if (inputStorage.readUnsignedByte() != TYPE_COLOR) {
00200                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The second PoI parameter must be the color.", outputStorage);
00201                 return false;
00202             }
00203             SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00204             SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00205             SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00206             //read SUMOReal a
00207             inputStorage.readUnsignedByte();
00208             // layer
00209             if (inputStorage.readUnsignedByte() != TYPE_INTEGER) {
00210                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The third PoI parameter must be the layer encoded as int.", outputStorage);
00211                 return false;
00212             }
00213             layer = inputStorage.readInt();
00214             // pos
00215             if (inputStorage.readUnsignedByte() != POSITION_2D) {
00216                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The fourth PoI parameter must be the position.", outputStorage);
00217                 return false;
00218             }
00219             SUMOReal x = inputStorage.readDouble();
00220             SUMOReal y = inputStorage.readDouble();
00221             //
00222             if (!shapeCont.addPoI(id, layer, type, RGBColor(r, g, b), Position(x, y))) {
00223                 delete p;
00224                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not add PoI.", outputStorage);
00225                 return false;
00226             }
00227         }
00228         break;
00229         case REMOVE: {
00230             if (valueDataType != TYPE_INTEGER) {
00231                 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The layer must be given using an int.", outputStorage);
00232                 return false;
00233             }
00234             layer = inputStorage.readInt();
00235             if (!shapeCont.removePoI(layer, id)) {
00236                 bool removed = false;
00237                 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) {
00238                     removed |= shapeCont.removePoI(i, id);
00239                 }
00240                 if (!removed) {
00241                     server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not remove PoI '" + id + "'", outputStorage);
00242                     return false;
00243                 }
00244             }
00245         }
00246         break;
00247         default:
00248             break;
00249     }
00250     server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
00251     return true;
00252 }
00253 
00254 #endif
00255 
00256 
00257 /****************************************************************************/
00258 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines