Path: blob/main/src/traci-server/TraCIServerAPI_Route.cpp
169665 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file TraCIServerAPI_Route.cpp14/// @author Daniel Krajzewicz15/// @author Laura Bieker16/// @author Michael Behrisch17/// @date 07.05.200918///19// APIs for getting/setting route values via TraCI20/****************************************************************************/21#include <config.h>2223#include <microsim/MSNet.h>24#include <microsim/MSRoute.h>25#include <microsim/MSEdge.h>26#include <libsumo/Route.h>27#include <libsumo/TraCIConstants.h>28#include "TraCIServerAPI_Route.h"293031// ===========================================================================32// method definitions33// ===========================================================================34bool35TraCIServerAPI_Route::processGet(TraCIServer& server, tcpip::Storage& inputStorage,36tcpip::Storage& outputStorage) {37const int variable = inputStorage.readUnsignedByte();38const std::string id = inputStorage.readString();39server.initWrapper(libsumo::RESPONSE_GET_ROUTE_VARIABLE, variable, id);40try {41if (!libsumo::Route::handleVariable(id, variable, &server, &inputStorage)) {42return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);43}44} catch (libsumo::TraCIException& e) {45return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);46}47server.writeStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);48server.writeResponseWithLength(outputStorage, server.getWrapperStorage());49return true;50}515253bool54TraCIServerAPI_Route::processSet(TraCIServer& server, tcpip::Storage& inputStorage,55tcpip::Storage& outputStorage) {56std::string warning = ""; // additional description for response57// variable58int variable = inputStorage.readUnsignedByte();59if (variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {60return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);61}62// id63std::string id = inputStorage.readString();6465try {66// process67switch (variable) {68case libsumo::ADD: {69std::vector<std::string> edgeIDs;70if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {71return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);72}73libsumo::Route::add(id, edgeIDs);74}75break;76case libsumo::REMOVE: {77libsumo::Route::remove(id);78}79break;80case libsumo::VAR_PARAMETER: {81if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {82return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);83}84//read itemNo85inputStorage.readInt();86std::string name;87if (!server.readTypeCheckingString(inputStorage, name)) {88return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);89}90std::string value;91if (!server.readTypeCheckingString(inputStorage, value)) {92return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);93}94libsumo::Route::setParameter(id, name, value);95}96break;97default:98break;99}100} catch (libsumo::TraCIException& e) {101return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);102}103server.writeStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);104return true;105}106107108/****************************************************************************/109110111