TraCIServerAPI_Polygon Class Reference

#include <TraCIServerAPI_Polygon.h>


Detailed Description

APIs for getting/setting polygon values via TraCI.

Definition at line 43 of file TraCIServerAPI_Polygon.h.


Static Public Member Functions

static bool processGet (tcpip::Storage &inputStorage, tcpip::Storage &outputStorage, bool withStatus=true) throw (traci::TraCIException)
 Processes a get value command (Command 0xa8: Get Polygon Variable).
static bool processSet (tcpip::Storage &inputStorage, tcpip::Storage &outputStorage) throw (traci::TraCIException)
 Processes a set value command (Command 0xc8: Change Polygon State).

Private Member Functions

TraCIServerAPI_Polygonoperator= (const TraCIServerAPI_Polygon &s)
 invalidated assignment operator
 TraCIServerAPI_Polygon (const TraCIServerAPI_Polygon &s)
 invalidated copy constructor

Constructor & Destructor Documentation

TraCIServerAPI_Polygon::TraCIServerAPI_Polygon ( const TraCIServerAPI_Polygon s  )  [private]

invalidated copy constructor


Member Function Documentation

TraCIServerAPI_Polygon& TraCIServerAPI_Polygon::operator= ( const TraCIServerAPI_Polygon s  )  [private]

invalidated assignment operator

bool TraCIServerAPI_Polygon::processGet ( tcpip::Storage inputStorage,
tcpip::Storage outputStorage,
bool  withStatus = true 
) throw (traci::TraCIException) [static]

Processes a get value command (Command 0xa8: Get Polygon Variable).

Parameters:
[in] inputStorage The storage to read the command from
[out] outputStorage The storage to write the result to
[in] withStatus Whether the status message shall be written (not in subscription)

Definition at line 55 of file TraCIServerAPI_Polygon.cpp.

References RGBColor::blue(), CMD_GET_POLYGON_VARIABLE, Polygon2D::fill(), Polygon2D::getColor(), MSNet::getInstance(), ShapeContainer::getMaxLayer(), ShapeContainer::getMinLayer(), ShapeContainer::getPolygonCont(), Polygon2D::getShape(), MSNet::getShapeContainer(), Polygon2D::getType(), RGBColor::green(), ID_LIST, MIN2(), RGBColor::red(), RESPONSE_GET_POLYGON_VARIABLE, RTYPE_ERR, RTYPE_OK, tcpip::Storage::size(), Position2DVector::size(), TYPE_COLOR, TYPE_POLYGON, TYPE_STRING, TYPE_STRINGLIST, TYPE_UBYTE, VAR_COLOR, VAR_FILL, VAR_SHAPE, VAR_TYPE, tcpip::Storage::writeFloat(), TraCIServerAPIHelper::writeStatusCmd(), tcpip::Storage::writeString(), tcpip::Storage::writeStringList(), and tcpip::Storage::writeUnsignedByte().

Referenced by traci::TraCIServer::dispatchCommand(), and traci::TraCIServer::processSingleSubscription().

00057                                                                           {
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_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     // begin response building
00068     Storage tempMsg;
00069     //  response-code, variableID, objectID
00070     tempMsg.writeUnsignedByte(RESPONSE_GET_POLYGON_VARIABLE);
00071     tempMsg.writeUnsignedByte(variable);
00072     tempMsg.writeString(id);
00073     // process request
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     // send response
00124     outputStorage.writeUnsignedByte(0); // command length -> extended
00125     outputStorage.writeInt(1 + 4 + tempMsg.size());
00126     outputStorage.writeStorage(tempMsg);
00127     return true;
00128 }

bool TraCIServerAPI_Polygon::processSet ( tcpip::Storage inputStorage,
tcpip::Storage outputStorage 
) throw (traci::TraCIException) [static]

Processes a set value command (Command 0xc8: Change Polygon State).

Parameters:
[in] inputStorage The storage to read the command from
[out] outputStorage The storage to write the result to

Definition at line 132 of file TraCIServerAPI_Polygon.cpp.

References ShapeContainer::add(), ADD, CMD_SET_POI_VARIABLE, CMD_SET_POLYGON_VARIABLE, MSNet::getInstance(), ShapeContainer::getMaxLayer(), ShapeContainer::getMinLayer(), ShapeContainer::getPolygonCont(), MSNet::getShapeContainer(), Position2DVector::push_back(), REMOVE, ShapeContainer::removePolygon(), RTYPE_ERR, RTYPE_OK, Polygon2D::setColor(), Polygon2D::setFill(), Polygon2D::setShape(), Polygon2D::setType(), SUMOReal, TYPE_COLOR, TYPE_COMPOUND, TYPE_INTEGER, TYPE_POLYGON, TYPE_STRING, TYPE_UBYTE, VAR_COLOR, VAR_FILL, VAR_SHAPE, VAR_TYPE, and TraCIServerAPIHelper::writeStatusCmd().

Referenced by traci::TraCIServer::dispatchCommand().

00133                                                                                       {
00134     std::string warning = ""; // additional description for response
00135     // variable
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     // id
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     // process
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         // type
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         // color
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         // fill
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         // layer
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         // shape
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 }


The documentation for this class was generated from the following files:

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