Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file TraCIServerAPI_Route.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @date 07.05.2009
19 : ///
20 : // APIs for getting/setting route values via TraCI
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <microsim/MSNet.h>
25 : #include <microsim/MSRoute.h>
26 : #include <microsim/MSEdge.h>
27 : #include <libsumo/Route.h>
28 : #include <libsumo/TraCIConstants.h>
29 : #include "TraCIServerAPI_Route.h"
30 :
31 :
32 : // ===========================================================================
33 : // method definitions
34 : // ===========================================================================
35 : bool
36 308 : TraCIServerAPI_Route::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
37 : tcpip::Storage& outputStorage) {
38 308 : const int variable = inputStorage.readUnsignedByte();
39 308 : const std::string id = inputStorage.readString();
40 308 : server.initWrapper(libsumo::RESPONSE_GET_ROUTE_VARIABLE, variable, id);
41 : try {
42 308 : if (!libsumo::Route::handleVariable(id, variable, &server, &inputStorage)) {
43 15 : return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
44 : }
45 2 : } catch (libsumo::TraCIException& e) {
46 2 : return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
47 2 : }
48 301 : server.writeStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
49 301 : server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
50 : return true;
51 : }
52 :
53 :
54 : bool
55 187 : TraCIServerAPI_Route::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
56 : tcpip::Storage& outputStorage) {
57 187 : std::string warning = ""; // additional description for response
58 : // variable
59 187 : int variable = inputStorage.readUnsignedByte();
60 187 : if (variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {
61 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
62 : }
63 : // id
64 187 : std::string id = inputStorage.readString();
65 :
66 : try {
67 : // process
68 187 : switch (variable) {
69 : case libsumo::ADD: {
70 : std::vector<std::string> edgeIDs;
71 122 : if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
72 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
73 : }
74 122 : libsumo::Route::add(id, edgeIDs);
75 122 : }
76 : break;
77 3 : case libsumo::REMOVE: {
78 3 : libsumo::Route::remove(id);
79 : }
80 : break;
81 62 : case libsumo::VAR_PARAMETER: {
82 62 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
83 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
84 : }
85 : //read itemNo
86 62 : inputStorage.readInt();
87 : std::string name;
88 62 : if (!server.readTypeCheckingString(inputStorage, name)) {
89 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
90 : }
91 : std::string value;
92 62 : if (!server.readTypeCheckingString(inputStorage, value)) {
93 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
94 : }
95 62 : libsumo::Route::setParameter(id, name, value);
96 : }
97 : break;
98 : default:
99 : break;
100 : }
101 6 : } catch (libsumo::TraCIException& e) {
102 6 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
103 6 : }
104 181 : server.writeStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
105 : return true;
106 : }
107 :
108 :
109 : /****************************************************************************/
|