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 /****************************************************************************/
17 // APIs for getting/setting route values via TraCI
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <microsim/MSNet.h>
27 #include <microsim/MSRoute.h>
28 #include <microsim/MSEdge.h>
29 #include <libsumo/Route.h>
30 #include <libsumo/TraCIConstants.h>
31 #include "TraCIServerAPI_Route.h"
32 
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
37 bool
39  tcpip::Storage& outputStorage) {
40  const int variable = inputStorage.readUnsignedByte();
41  const std::string id = inputStorage.readString();
43  try {
44  if (!libsumo::Route::handleVariable(id, variable, &server)) {
45  switch (variable) {
47  std::string paramName = "";
48  if (!server.readTypeCheckingString(inputStorage, paramName)) {
49  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
50  }
53  break;
54  }
55  default:
56  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
57  }
58  }
59  } catch (libsumo::TraCIException& e) {
60  return server.writeErrorStatusCmd(libsumo::CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
61  }
63  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
64  return true;
65 }
66 
67 
68 bool
70  tcpip::Storage& outputStorage) {
71  std::string warning = ""; // additional description for response
72  // variable
73  int variable = inputStorage.readUnsignedByte();
74  if (variable != libsumo::ADD && variable != libsumo::VAR_PARAMETER) {
75  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
76  }
77  // id
78  std::string id = inputStorage.readString();
79 
80  try {
81  // process
82  switch (variable) {
83  case libsumo::ADD: {
84  std::vector<std::string> edgeIDs;
85  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
86  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
87  }
88  libsumo::Route::add(id, edgeIDs);
89  }
90  break;
92  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
93  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
94  }
95  //read itemNo
96  inputStorage.readInt();
97  std::string name;
98  if (!server.readTypeCheckingString(inputStorage, name)) {
99  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
100  }
101  std::string value;
102  if (!server.readTypeCheckingString(inputStorage, value)) {
103  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
104  }
105  libsumo::Route::setParameter(id, name, value);
106  }
107  break;
108  default:
109  break;
110  }
111  } catch (libsumo::TraCIException& e) {
112  return server.writeErrorStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
113  }
114  server.writeStatusCmd(libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
115  return true;
116 }
117 
118 
119 /****************************************************************************/
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:353
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:69
TraCIServerAPI_Route.h
MSNet.h
libsumo::Route::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Route.cpp:124
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:1416
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1368
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:928
MSEdge.h
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:175
libsumo::ADD
TRACI_CONST int ADD
Definition: TraCIConstants.h:932
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:967
libsumo::Route::add
static void add(const std::string &routeID, const std::vector< std::string > &edgeIDs)
Definition: Route.cpp:85
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:103
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:78
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:58
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:38
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:1426
libsumo::TraCIException
Definition: TraCIDefs.h:90
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:182
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:338
MSRoute.h
config.h
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
libsumo::CMD_SET_ROUTE_VARIABLE
TRACI_CONST int CMD_SET_ROUTE_VARIABLE
Definition: TraCIConstants.h:184
libsumo::Route::getParameter
static std::string getParameter(const std::string &routeID, const std::string &param)
Definition: Route.cpp:72
tcpip::Storage
Definition: storage.h:36
libsumo::CMD_GET_ROUTE_VARIABLE
TRACI_CONST int CMD_GET_ROUTE_VARIABLE
Definition: TraCIConstants.h:180
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:342
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:981