Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 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 230 : TraCIServerAPI_Route::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
37 : tcpip::Storage& outputStorage) {
38 230 : const int variable = inputStorage.readUnsignedByte();
39 230 : const std::string id = inputStorage.readString();
40 230 : server.initWrapper(libsumo::RESPONSE_GET_ROUTE_VARIABLE, variable, id);
41 : try {
42 230 : if (!libsumo::Route::handleVariable(id, variable, &server, &inputStorage)) {
43 6 : 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 226 : server.writeStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
49 226 : server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
50 : return true;
51 : }
52 :
53 :
54 : bool
55 148 : TraCIServerAPI_Route::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
56 : tcpip::Storage& outputStorage) {
57 148 : std::string warning = ""; // additional description for response
58 : // variable
59 148 : int variable = inputStorage.readUnsignedByte();
60 148 : if (variable != libsumo::ADD && 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 148 : std::string id = inputStorage.readString();
65 :
66 : try {
67 : // process
68 148 : switch (variable) {
69 : case libsumo::ADD: {
70 : std::vector<std::string> edgeIDs;
71 113 : 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 113 : libsumo::Route::add(id, edgeIDs);
75 113 : }
76 : break;
77 35 : case libsumo::VAR_PARAMETER: {
78 35 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
79 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
80 : }
81 : //read itemNo
82 35 : inputStorage.readInt();
83 : std::string name;
84 35 : if (!server.readTypeCheckingString(inputStorage, name)) {
85 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
86 : }
87 : std::string value;
88 35 : if (!server.readTypeCheckingString(inputStorage, value)) {
89 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
90 : }
91 35 : libsumo::Route::setParameter(id, name, value);
92 : }
93 : break;
94 : default:
95 : break;
96 : }
97 6 : } catch (libsumo::TraCIException& e) {
98 6 : return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
99 6 : }
100 142 : server.writeStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
101 : return true;
102 : }
103 :
104 :
105 : /****************************************************************************/
|