TraCIServerAPI_POI.cpp

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

Generated on Wed May 5 00:06:37 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6