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 <utils/common/StdDefs.h>
00031 #include <microsim/MSNet.h>
00032 #include <utils/shapes/PointOfInterest.h>
00033 #include <utils/shapes/ShapeContainer.h>
00034 #include "TraCIConstants.h"
00035 #include "TraCIServerAPIHelper.h"
00036 #include "TraCIServerAPI_Polygon.h"
00037
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041
00042
00043
00044
00045
00046 using namespace std;
00047 using namespace traci;
00048 using namespace tcpip;
00049
00050
00051
00052
00053
00054 bool
00055 TraCIServerAPI_Polygon::processGet(tcpip::Storage &inputStorage,
00056 tcpip::Storage &outputStorage,
00057 bool withStatus) throw(TraCIException) {
00058 std::string warning = "";
00059
00060 int variable = inputStorage.readUnsignedByte();
00061 std::string id = inputStorage.readString();
00062
00063 if (variable!=ID_LIST&&variable!=VAR_TYPE&&variable!=VAR_COLOR&&variable!=VAR_SHAPE&&variable!=VAR_FILL) {
00064 TraCIServerAPIHelper::writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_ERR, "Get Polygon Variable: unsupported variable specified", outputStorage);
00065 return false;
00066 }
00067
00068 Storage tempMsg;
00069
00070 tempMsg.writeUnsignedByte(RESPONSE_GET_POLYGON_VARIABLE);
00071 tempMsg.writeUnsignedByte(variable);
00072 tempMsg.writeString(id);
00073
00074 if (variable==ID_LIST) {
00075 std::vector<std::string> ids;
00076 ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
00077 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) {
00078 shapeCont.getPolygonCont(i).insertIDs(ids);
00079 }
00080 tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00081 tempMsg.writeStringList(ids);
00082 } else {
00083 Polygon2D *p = 0;
00084 ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
00085 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer()&&p==0; ++i) {
00086 p = shapeCont.getPolygonCont(i).get(id);
00087 }
00088 if (p==0) {
00089 TraCIServerAPIHelper::writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_ERR, "Polygon '" + id + "' is not known", outputStorage);
00090 return false;
00091 }
00092 switch (variable) {
00093 case VAR_TYPE:
00094 tempMsg.writeUnsignedByte(TYPE_STRING);
00095 tempMsg.writeString(p->getType());
00096 break;
00097 case VAR_COLOR:
00098 tempMsg.writeUnsignedByte(TYPE_COLOR);
00099 tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().red()*255.+.5));
00100 tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().green()*255.+.5));
00101 tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().blue()*255.+.5));
00102 tempMsg.writeUnsignedByte(255);
00103 break;
00104 case VAR_SHAPE:
00105 tempMsg.writeUnsignedByte(TYPE_POLYGON);
00106 tempMsg.writeUnsignedByte(MIN2(static_cast<size_t>(255),p->getShape().size()));
00107 for (int iPoint=0; iPoint < MIN2(static_cast<size_t>(255),p->getShape().size()); ++iPoint) {
00108 tempMsg.writeFloat(p->getShape()[iPoint].x());
00109 tempMsg.writeFloat(p->getShape()[iPoint].y());
00110 }
00111 break;
00112 case VAR_FILL:
00113 tempMsg.writeUnsignedByte(TYPE_UBYTE);
00114 tempMsg.writeUnsignedByte(p->fill() ? 1 : 0);
00115 break;
00116 default:
00117 break;
00118 }
00119 }
00120 if (withStatus) {
00121 TraCIServerAPIHelper::writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
00122 }
00123
00124 outputStorage.writeUnsignedByte(0);
00125 outputStorage.writeInt(1 + 4 + tempMsg.size());
00126 outputStorage.writeStorage(tempMsg);
00127 return true;
00128 }
00129
00130
00131 bool
00132 TraCIServerAPI_Polygon::processSet(tcpip::Storage &inputStorage,
00133 tcpip::Storage &outputStorage) throw(TraCIException) {
00134 std::string warning = "";
00135
00136 int variable = inputStorage.readUnsignedByte();
00137 if (variable!=VAR_TYPE&&variable!=VAR_COLOR&&variable!=VAR_SHAPE&&variable!=VAR_FILL
00138 &&variable!=ADD&&variable!=REMOVE) {
00139 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "Change Polygon State: unsupported variable specified", outputStorage);
00140 return false;
00141 }
00142
00143 std::string id = inputStorage.readString();
00144 Polygon2D *p = 0;
00145 ShapeContainer& shapeCont = MSNet::getInstance()->getShapeContainer();
00146 if (variable!=ADD&&variable!=REMOVE) {
00147 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer()&&p==0; ++i) {
00148 p = shapeCont.getPolygonCont(i).get(id);
00149 }
00150 if (p==0) {
00151 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "Polygon '" + id + "' is not known", outputStorage);
00152 return false;
00153 }
00154 }
00155
00156 int valueDataType = inputStorage.readUnsignedByte();
00157 switch (variable) {
00158 case VAR_TYPE: {
00159 if (valueDataType!=TYPE_STRING) {
00160 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The type must be given as a string.", outputStorage);
00161 return false;
00162 }
00163 std::string type = inputStorage.readString();
00164 p->setType(type);
00165 }
00166 break;
00167 case VAR_COLOR: {
00168 if (valueDataType!=TYPE_COLOR) {
00169 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The color must be given using an accoring type.", outputStorage);
00170 return false;
00171 }
00172 SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00173 SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00174 SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00175 SUMOReal a = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00176 p->setColor(RGBColor(r,g,b));
00177 }
00178 break;
00179 case VAR_SHAPE: {
00180 if (valueDataType!=TYPE_POLYGON) {
00181 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The shape must be given using an accoring type.", outputStorage);
00182 return false;
00183 }
00184 unsigned int noEntries = inputStorage.readUnsignedByte();
00185 Position2DVector shape;
00186 for (int i=0; i<noEntries; ++i) {
00187 SUMOReal x = inputStorage.readFloat();
00188 SUMOReal y = inputStorage.readFloat();
00189 shape.push_back(Position2D(x, y));
00190 }
00191 p->setShape(shape);
00192 }
00193 break;
00194 case VAR_FILL: {
00195 if (valueDataType!=TYPE_UBYTE) {
00196 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "'fill' must be defined using an unsigned byte.", outputStorage);
00197 return false;
00198 }
00199 bool fill = inputStorage.readUnsignedByte()!=0;
00200 p->setFill(fill);
00201 }
00202 break;
00203 case ADD: {
00204 if (valueDataType!=TYPE_COMPOUND) {
00205 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "A compound object is needed for setting a new polygon.", outputStorage);
00206 return false;
00207 }
00208 unsigned int itemNo = inputStorage.readInt();
00209
00210 if (inputStorage.readUnsignedByte()!=TYPE_STRING) {
00211 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The first polygon parameter must be the type encoded as a string.", outputStorage);
00212 return false;
00213 }
00214 std::string type = inputStorage.readString();
00215
00216 if (inputStorage.readUnsignedByte()!=TYPE_COLOR) {
00217 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The second polygon parameter must be the color.", outputStorage);
00218 return false;
00219 }
00220 SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00221 SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00222 SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00223 SUMOReal a = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00224
00225 if (inputStorage.readUnsignedByte()!=TYPE_UBYTE) {
00226 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
00227 return false;
00228 }
00229 bool fill = inputStorage.readUnsignedByte()!=0;
00230
00231 if (inputStorage.readUnsignedByte()!=TYPE_INTEGER) {
00232 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
00233 return false;
00234 }
00235 int layer = inputStorage.readInt();
00236
00237 if (inputStorage.readUnsignedByte()!=TYPE_POLYGON) {
00238 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "The fifth polygon parameter must be the shape.", outputStorage);
00239 return false;
00240 }
00241 unsigned int noEntries = inputStorage.readUnsignedByte();
00242 Position2DVector shape;
00243 for (int i=0; i<noEntries; ++i) {
00244 SUMOReal x = inputStorage.readFloat();
00245 SUMOReal y = inputStorage.readFloat();
00246 shape.push_back(Position2D(x, y));
00247 }
00248
00249 p = new Polygon2D(id, type, RGBColor(r, g, b), shape, fill);
00250 if (!shapeCont.add(layer, p)) {
00251 delete p;
00252 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_ERR, "Could not add polygon.", outputStorage);
00253 return false;
00254 }
00255 }
00256 break;
00257 case REMOVE: {
00258 if (valueDataType!=TYPE_INTEGER) {
00259 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The layer must be given using an int.", outputStorage);
00260 return false;
00261 }
00262 int layer = inputStorage.readInt();
00263 if (!shapeCont.removePolygon(layer, id)) {
00264 bool removed = false;
00265 for (int i = shapeCont.getMinLayer(); i <= shapeCont.getMaxLayer(); ++i) {
00266 removed |= shapeCont.removePolygon(i, id);
00267 }
00268 if (!removed) {
00269 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not remove PoI '" + id + "'", outputStorage);
00270 return false;
00271 }
00272 }
00273 }
00274 break;
00275 default:
00276 break;
00277 }
00278 TraCIServerAPIHelper::writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
00279 return true;
00280 }
00281
00282
00283
00284
00285