Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_Route.cpp
Go to the documentation of this file.
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 /****************************************************************************/
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
37  tcpip::Storage& outputStorage) {
38  const int variable = inputStorage.readUnsignedByte();
39  const std::string id = inputStorage.readString();
41  try {
42  if (!libsumo::Route::handleVariable(id, variable, &server, &inputStorage)) {
43  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
44  }
45  } catch (libsumo::TraCIException& e) {
46  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
47  }
49  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
50  return true;
51 }
52 
53 
54 bool
56  tcpip::Storage& outputStorage) {
57  std::string warning = ""; // additional description for response
58  // variable
59  int variable = inputStorage.readUnsignedByte();
60  if (variable != libsumo::ADD && variable != libsumo::VAR_PARAMETER) {
61  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
62  }
63  // id
64  std::string id = inputStorage.readString();
65 
66  try {
67  // process
68  switch (variable) {
69  case libsumo::ADD: {
70  std::vector<std::string> edgeIDs;
71  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
72  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
73  }
74  libsumo::Route::add(id, edgeIDs);
75  }
76  break;
78  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
79  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
80  }
81  //read itemNo
82  inputStorage.readInt();
83  std::string name;
84  if (!server.readTypeCheckingString(inputStorage, name)) {
85  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  if (!server.readTypeCheckingString(inputStorage, value)) {
89  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
90  }
91  libsumo::Route::setParameter(id, name, value);
92  }
93  break;
94  default:
95  break;
96  }
97  } catch (libsumo::TraCIException& e) {
98  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
99  }
100  server.writeStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
101  return true;
102 }
103 
104 
105 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:56
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:59
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
tcpip::Storage & getWrapperStorage()
void initWrapper(const int domainID, const int variable, const std::string &objID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
An error which allows to continue.
Definition: TraCIDefs.h:144
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual int readInt()
Definition: storage.cpp:311
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int CMD_GET_ROUTE_VARIABLE
TRACI_CONST int CMD_SET_ROUTE_VARIABLE
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int RESPONSE_GET_ROUTE_VARIABLE
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD