SUMO - Simulation of Urban MObility

TraCIServerAPI_Route.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // APIs for getting/setting route values via TraCI
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 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 <microsim/MSRoute.h>
00032 #include <microsim/MSEdge.h>
00033 #include "TraCIConstants.h"
00034 #include "TraCIServerAPI_Route.h"
00035 
00036 #ifdef CHECK_MEMORY_LEAKS
00037 #include <foreign/nvwa/debug_new.h>
00038 #endif // CHECK_MEMORY_LEAKS
00039 
00040 
00041 // ===========================================================================
00042 // used namespaces
00043 // ===========================================================================
00044 using namespace traci;
00045 
00046 
00047 // ===========================================================================
00048 // method definitions
00049 // ===========================================================================
00050 bool
00051 TraCIServerAPI_Route::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
00052                                  tcpip::Storage& outputStorage) {
00053     std::string warning = ""; // additional description for response
00054     // variable & id
00055     int variable = inputStorage.readUnsignedByte();
00056     std::string id = inputStorage.readString();
00057     // check variable
00058     if (variable!=ID_LIST&&variable!=VAR_EDGES&&variable!=ID_COUNT) {
00059         server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Get Route Variable: unsupported variable specified", outputStorage);
00060         return false;
00061     }
00062     // begin response building
00063     tcpip::Storage tempMsg;
00064     //  response-code, variableID, objectID
00065     tempMsg.writeUnsignedByte(RESPONSE_GET_ROUTE_VARIABLE);
00066     tempMsg.writeUnsignedByte(variable);
00067     tempMsg.writeString(id);
00068     // process request
00069     if (variable==ID_LIST) {
00070         std::vector<std::string> ids;
00071         MSRoute::insertIDs(ids);
00072         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00073         tempMsg.writeStringList(ids);
00074     } else if (variable==ID_COUNT) {
00075         std::vector<std::string> ids;
00076         MSRoute::insertIDs(ids);
00077         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00078         tempMsg.writeInt((int) ids.size());
00079     } else {
00080         const MSRoute* r = MSRoute::dictionary(id);
00081         if (r==0) {
00082             server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Route '" + id + "' is not known", outputStorage);
00083             return false;
00084         }
00085         switch (variable) {
00086         case VAR_EDGES:
00087             tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00088             tempMsg.writeInt(r->size());
00089             for (MSRouteIterator i=r->begin(); i!=r->end(); ++i) {
00090                 tempMsg.writeString((*i)->getID());
00091             }
00092             break;
00093         default:
00094             break;
00095         }
00096     }
00097     server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
00098     server.writeResponseWithLength(outputStorage, tempMsg);
00099     return true;
00100 }
00101 
00102 
00103 bool
00104 TraCIServerAPI_Route::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00105                                  tcpip::Storage& outputStorage) {
00106     std::string warning = ""; // additional description for response
00107     // variable
00108     int variable = inputStorage.readUnsignedByte();
00109     if (variable!=ADD) {
00110         server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Change Route State: unsupported variable specified", outputStorage);
00111         return false;
00112     }
00113     // id
00114     std::string id = inputStorage.readString();
00115     // process
00116     int valueDataType = inputStorage.readUnsignedByte();
00117     switch (variable) {
00118     case ADD: {
00119         if (valueDataType!=TYPE_STRINGLIST) {
00120             server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "A string list is needed for adding a new route.", outputStorage);
00121             return false;
00122         }
00123         //read itemNo
00124         int numEdges = inputStorage.readInt();
00125         MSEdgeVector edges;
00126         while (numEdges--) {
00127             MSEdge* edge = MSEdge::dictionary(inputStorage.readString());
00128             if (edge==0) {
00129                 server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Unknown edge in route.", outputStorage);
00130                 return false;
00131             }
00132             edges.push_back(edge);
00133         }
00134         const std::vector<SUMOVehicleParameter::Stop> stops;
00135         if (!MSRoute::dictionary(id, new MSRoute(id, edges, 1, RGBColor::DEFAULT_COLOR, stops))) {
00136             server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Could not add route.", outputStorage);
00137             return false;
00138         }
00139     }
00140     break;
00141     default:
00142         break;
00143     }
00144     server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
00145     return true;
00146 }
00147 
00148 
00149 
00150 /****************************************************************************/
00151 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines