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