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.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // APIs for getting/setting route values via TraCI
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <microsim/MSNet.h>
26 #include <microsim/MSRoute.h>
27 #include <microsim/MSEdge.h>
28 #include <libsumo/Route.h>
29 #include <libsumo/TraCIConstants.h>
30 #include "TraCIServerAPI_Route.h"
31 
32 
33 // ===========================================================================
34 // method definitions
35 // ===========================================================================
36 bool
38  tcpip::Storage& outputStorage) {
39  const int variable = inputStorage.readUnsignedByte();
40  const std::string id = inputStorage.readString();
42  try {
43  if (!libsumo::Route::handleVariable(id, variable, &server)) {
44  switch (variable) {
46  std::string paramName = "";
47  if (!server.readTypeCheckingString(inputStorage, paramName)) {
48  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
49  }
52  break;
53  }
54  default:
55  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
56  }
57  }
58  } catch (libsumo::TraCIException& e) {
59  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
60  }
62  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
63  return true;
64 }
65 
66 
67 bool
69  tcpip::Storage& outputStorage) {
70  std::string warning = ""; // additional description for response
71  // variable
72  int variable = inputStorage.readUnsignedByte();
73  if (variable != libsumo::ADD && variable != libsumo::VAR_PARAMETER) {
74  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
75  }
76  // id
77  std::string id = inputStorage.readString();
78 
79  try {
80  // process
81  switch (variable) {
82  case libsumo::ADD: {
83  std::vector<std::string> edgeIDs;
84  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
85  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
86  }
87  libsumo::Route::add(id, edgeIDs);
88  }
89  break;
91  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
92  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
93  }
94  //read itemNo
95  inputStorage.readInt();
96  std::string name;
97  if (!server.readTypeCheckingString(inputStorage, name)) {
98  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
99  }
100  std::string value;
101  if (!server.readTypeCheckingString(inputStorage, value)) {
102  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
103  }
104  libsumo::Route::setParameter(id, name, value);
105  }
106  break;
107  default:
108  break;
109  }
110  } catch (libsumo::TraCIException& e) {
111  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
112  }
113  server.writeStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
114  return true;
115 }
116 
117 
118 /****************************************************************************/
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:352
TraCIServerAPI_Route::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
Definition: TraCIServerAPI_Route.cpp:68
TraCIServerAPI_Route.h
MSNet.h
libsumo::Route::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Route.cpp:123
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1414
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:939
MSEdge.h
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:174
libsumo::ADD
TRACI_CONST int ADD
Definition: TraCIConstants.h:943
TraCIServer::writeStatusCmd
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Definition: TraCIServer.cpp:968
libsumo::Route::add
static void add(const std::string &routeID, const std::vector< std::string > &edgeIDs)
Definition: Route.cpp:84
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:102
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
tcpip::Storage::readString
virtual std::string readString()
TraCIConstants.h
tcpip::Storage::readInt
virtual int readInt()
libsumo::Route::setParameter
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Route.cpp:77
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:57
Route.h
TraCIServerAPI_Route::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
Definition: TraCIServerAPI_Route.cpp:37
TraCIServer::readTypeCheckingStringList
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
Definition: TraCIServer.cpp:1424
libsumo::TraCIException
Definition: TraCIDefs.h:89
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
libsumo::RESPONSE_GET_ROUTE_VARIABLE
TRACI_CONST int RESPONSE_GET_ROUTE_VARIABLE
Definition: TraCIConstants.h:181
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:337
MSRoute.h
config.h
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
libsumo::CMD_SET_ROUTE_VARIABLE
TRACI_CONST int CMD_SET_ROUTE_VARIABLE
Definition: TraCIConstants.h:183
libsumo::Route::getParameter
static std::string getParameter(const std::string &routeID, const std::string &param)
Definition: Route.cpp:71
tcpip::Storage
Definition: storage.h:36
libsumo::CMD_GET_ROUTE_VARIABLE
TRACI_CONST int CMD_GET_ROUTE_VARIABLE
Definition: TraCIConstants.h:179
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:341
TraCIServer::writeErrorStatusCmd
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
Definition: TraCIServer.cpp:982