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-2018 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 "TraCIConstants.h"
30 #include <libsumo/Route.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();
42  server.initWrapper(RESPONSE_GET_ROUTE_VARIABLE, variable, id);
43  try {
44  if (!libsumo::Route::handleVariable(id, variable, &server)) {
45  switch (variable) {
46  case VAR_PARAMETER: {
47  std::string paramName = "";
48  if (!server.readTypeCheckingString(inputStorage, paramName)) {
49  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
50  }
53  break;
54  }
55  default:
56  return server.writeErrorStatusCmd(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(CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
61  }
62  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, "", outputStorage);
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 != ADD && variable != VAR_PARAMETER) {
75  return server.writeErrorStatusCmd(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 ADD: {
84  std::vector<std::string> edgeIDs;
85  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
86  return server.writeErrorStatusCmd(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;
91  case VAR_PARAMETER: {
92  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
93  return server.writeErrorStatusCmd(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(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(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(CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
113  }
114  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
115  return true;
116 }
117 
118 
119 /****************************************************************************/
#define TYPE_COMPOUND
#define RTYPE_OK
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
static void add(const std::string &routeID, const std::vector< std::string > &edgeIDs)
Definition: Route.cpp:85
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
virtual void writeUnsignedByte(int)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define CMD_GET_ROUTE_VARIABLE
#define TYPE_STRING
virtual int readUnsignedByte()
#define CMD_SET_ROUTE_VARIABLE
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Route.cpp:124
virtual int readInt()
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
tcpip::Storage & getWrapperStorage()
virtual std::string readString()
#define ADD
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
static std::string getParameter(const std::string &routeID, const std::string &param)
Definition: Route.cpp:72
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:59
#define RESPONSE_GET_ROUTE_VARIABLE
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Route.cpp:78
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
void initWrapper(const int domainID, const int variable, const std::string &objID)
#define VAR_PARAMETER