SUMO - Simulation of Urban MObility

TraCIServerAPI_Route.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00009 // APIs for getting/setting route 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 <microsim/MSRoute.h>
00035 #include <microsim/MSEdge.h>
00036 #include "TraCIConstants.h"
00037 #include "TraCIServerAPI_Route.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_Route::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_EDGES && variable != ID_COUNT) {
00062         server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Get Route 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_ROUTE_VARIABLE);
00069     tempMsg.writeUnsignedByte(variable);
00070     tempMsg.writeString(id);
00071     // process request
00072     if (variable == ID_LIST) {
00073         std::vector<std::string> ids;
00074         MSRoute::insertIDs(ids);
00075         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00076         tempMsg.writeStringList(ids);
00077     } else if (variable == ID_COUNT) {
00078         std::vector<std::string> ids;
00079         MSRoute::insertIDs(ids);
00080         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00081         tempMsg.writeInt((int) ids.size());
00082     } else {
00083         const MSRoute* r = MSRoute::dictionary(id);
00084         if (r == 0) {
00085             server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Route '" + id + "' is not known", outputStorage);
00086             return false;
00087         }
00088         switch (variable) {
00089             case VAR_EDGES:
00090                 tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00091                 tempMsg.writeInt(r->size());
00092                 for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
00093                     tempMsg.writeString((*i)->getID());
00094                 }
00095                 break;
00096             default:
00097                 break;
00098         }
00099     }
00100     server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
00101     server.writeResponseWithLength(outputStorage, tempMsg);
00102     return true;
00103 }
00104 
00105 
00106 bool
00107 TraCIServerAPI_Route::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00108                                  tcpip::Storage& outputStorage) {
00109     std::string warning = ""; // additional description for response
00110     // variable
00111     int variable = inputStorage.readUnsignedByte();
00112     if (variable != ADD) {
00113         server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Change Route State: unsupported variable specified", outputStorage);
00114         return false;
00115     }
00116     // id
00117     std::string id = inputStorage.readString();
00118     // process
00119     int valueDataType = inputStorage.readUnsignedByte();
00120     switch (variable) {
00121         case ADD: {
00122             if (valueDataType != TYPE_STRINGLIST) {
00123                 server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "A string list is needed for adding a new route.", outputStorage);
00124                 return false;
00125             }
00126             //read itemNo
00127             int numEdges = inputStorage.readInt();
00128             MSEdgeVector edges;
00129             while (numEdges--) {
00130                 MSEdge* edge = MSEdge::dictionary(inputStorage.readString());
00131                 if (edge == 0) {
00132                     server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Unknown edge in route.", outputStorage);
00133                     return false;
00134                 }
00135                 edges.push_back(edge);
00136             }
00137             const std::vector<SUMOVehicleParameter::Stop> stops;
00138             if (!MSRoute::dictionary(id, new MSRoute(id, edges, 1, RGBColor::DEFAULT_COLOR, stops))) {
00139                 server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Could not add route.", outputStorage);
00140                 return false;
00141             }
00142         }
00143         break;
00144         default:
00145             break;
00146     }
00147     server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
00148     return true;
00149 }
00150 
00151 
00152 
00153 /****************************************************************************/
00154 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines