|
SUMO - Simulation of Urban MObility
|
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 #include <microsim/MSNet.h> 00034 #include <utils/shapes/PointOfInterest.h> 00035 #include <utils/shapes/ShapeContainer.h> 00036 #include "TraCIConstants.h" 00037 #include "TraCIServerAPI_POI.h" 00038 00039 #ifdef CHECK_MEMORY_LEAKS 00040 #include <foreign/nvwa/debug_new.h> 00041 #endif // CHECK_MEMORY_LEAKS 00042 00043 00044 // =========================================================================== 00045 // used namespaces 00046 // =========================================================================== 00047 using namespace traci; 00048 00049 00050 // =========================================================================== 00051 // method definitions 00052 // =========================================================================== 00053 bool 00054 TraCIServerAPI_POI::processGet(TraCIServer& server, tcpip::Storage& inputStorage, 00055 tcpip::Storage& outputStorage) { 00056 std::string warning = ""; // additional description for response 00057 // variable & id 00058 int variable = inputStorage.readUnsignedByte(); 00059 std::string id = inputStorage.readString(); 00060 // check variable 00061 if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION && variable != ID_COUNT) { 00062 server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_ERR, "Get PoI Variable: unsupported variable specified", outputStorage); 00063 return false; 00064 } 00065 // begin response building 00066 tcpip::Storage tempMsg; 00067 // response-code, variableID, objectID 00068 tempMsg.writeUnsignedByte(RESPONSE_GET_POI_VARIABLE); 00069 tempMsg.writeUnsignedByte(variable); 00070 tempMsg.writeString(id); 00071 // process request 00072 if (variable == ID_LIST || variable == ID_COUNT) { 00073 std::vector<std::string> ids; 00074 ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer(); 00075 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) { 00076 shapeCont.getPOICont(i).insertIDs(ids); 00077 } 00078 if (variable == ID_LIST) { 00079 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00080 tempMsg.writeStringList(ids); 00081 } else { 00082 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00083 tempMsg.writeInt((int) ids.size()); 00084 } 00085 } else { 00086 PointOfInterest* p = 0; 00087 ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer(); 00088 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer() && p == 0; ++i) { 00089 p = shapeCont.getPOICont(i).get(id); 00090 } 00091 if (p == 0) { 00092 server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_ERR, "POI '" + id + "' is not known", outputStorage); 00093 return false; 00094 } 00095 switch (variable) { 00096 case VAR_TYPE: 00097 tempMsg.writeUnsignedByte(TYPE_STRING); 00098 tempMsg.writeString(p->getType()); 00099 break; 00100 case VAR_COLOR: 00101 tempMsg.writeUnsignedByte(TYPE_COLOR); 00102 tempMsg.writeUnsignedByte(static_cast<int>(p->red() * 255. + .5)); 00103 tempMsg.writeUnsignedByte(static_cast<int>(p->green() * 255. + .5)); 00104 tempMsg.writeUnsignedByte(static_cast<int>(p->blue() * 255. + .5)); 00105 tempMsg.writeUnsignedByte(255); 00106 break; 00107 case VAR_POSITION: 00108 tempMsg.writeUnsignedByte(POSITION_2D); 00109 tempMsg.writeDouble(p->x()); 00110 tempMsg.writeDouble(p->y()); 00111 break; 00112 default: 00113 break; 00114 } 00115 } 00116 server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, warning, outputStorage); 00117 server.writeResponseWithLength(outputStorage, tempMsg); 00118 return true; 00119 } 00120 00121 00122 bool 00123 TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage, 00124 tcpip::Storage& outputStorage) { 00125 std::string warning = ""; // additional description for response 00126 // variable 00127 int variable = inputStorage.readUnsignedByte(); 00128 if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION 00129 && variable != ADD && variable != REMOVE) { 00130 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Change PoI State: unsupported variable specified", outputStorage); 00131 return false; 00132 } 00133 // id 00134 std::string id = inputStorage.readString(); 00135 PointOfInterest* p = 0; 00136 int layer = 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 layer = i; 00142 } 00143 if (p == 0) { 00144 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "POI '" + id + "' is not known", outputStorage); 00145 return false; 00146 } 00147 } 00148 // process 00149 int valueDataType = inputStorage.readUnsignedByte(); 00150 switch (variable) { 00151 case VAR_TYPE: { 00152 if (valueDataType != TYPE_STRING) { 00153 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The type must be given as a string.", outputStorage); 00154 return false; 00155 } 00156 std::string type = inputStorage.readString(); 00157 p->setType(type); 00158 } 00159 break; 00160 case VAR_COLOR: { 00161 if (valueDataType != TYPE_COLOR) { 00162 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The color must be given using an accoring type.", outputStorage); 00163 return false; 00164 } 00165 SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.; 00166 SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.; 00167 SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.; 00168 //read SUMOReal a 00169 inputStorage.readUnsignedByte(); 00170 dynamic_cast<RGBColor*>(p)->set(r, g, b); 00171 } 00172 break; 00173 case VAR_POSITION: { 00174 if (valueDataType != POSITION_2D) { 00175 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The position must be given using an accoring type.", outputStorage); 00176 return false; 00177 } 00178 SUMOReal x = inputStorage.readDouble(); 00179 SUMOReal y = inputStorage.readDouble(); 00180 shapeCont.movePoI(layer, id, Position(x, y)); 00181 } 00182 break; 00183 case ADD: { 00184 if (valueDataType != TYPE_COMPOUND) { 00185 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "A compound object is needed for setting a new PoI.", outputStorage); 00186 return false; 00187 } 00188 //read itemNo 00189 inputStorage.readInt(); 00190 // type 00191 if (inputStorage.readUnsignedByte() != TYPE_STRING) { 00192 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The first PoI parameter must be the type encoded as a string.", outputStorage); 00193 return false; 00194 } 00195 std::string type = inputStorage.readString(); 00196 // color 00197 if (inputStorage.readUnsignedByte() != TYPE_COLOR) { 00198 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The second PoI parameter must be the color.", outputStorage); 00199 return false; 00200 } 00201 SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.; 00202 SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.; 00203 SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.; 00204 //read SUMOReal a 00205 inputStorage.readUnsignedByte(); 00206 // layer 00207 if (inputStorage.readUnsignedByte() != TYPE_INTEGER) { 00208 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The third PoI parameter must be the layer encoded as int.", outputStorage); 00209 return false; 00210 } 00211 layer = inputStorage.readInt(); 00212 // pos 00213 if (inputStorage.readUnsignedByte() != POSITION_2D) { 00214 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The fourth PoI parameter must be the position.", outputStorage); 00215 return false; 00216 } 00217 SUMOReal x = inputStorage.readDouble(); 00218 SUMOReal y = inputStorage.readDouble(); 00219 // 00220 if (!shapeCont.addPoI(id, layer, type, RGBColor(r, g, b), Position(x, y))) { 00221 delete p; 00222 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not add PoI.", outputStorage); 00223 return false; 00224 } 00225 } 00226 break; 00227 case REMOVE: { 00228 if (valueDataType != TYPE_INTEGER) { 00229 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The layer must be given using an int.", outputStorage); 00230 return false; 00231 } 00232 layer = inputStorage.readInt(); 00233 if (!shapeCont.removePoI(layer, id)) { 00234 bool removed = false; 00235 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) { 00236 removed |= shapeCont.removePoI(i, id); 00237 } 00238 if (!removed) { 00239 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not remove PoI '" + id + "'", outputStorage); 00240 return false; 00241 } 00242 } 00243 } 00244 break; 00245 default: 00246 break; 00247 } 00248 server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage); 00249 return true; 00250 } 00251 00252 00253 00254 /****************************************************************************/ 00255