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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // APIs for getting/setting route values via TraCI
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #ifndef NO_TRACI
33 
34 #include <microsim/MSNet.h>
35 #include <microsim/MSRoute.h>
36 #include <microsim/MSEdge.h>
37 #include "TraCIConstants.h"
38 #include <libsumo/Route.h>
39 #include "TraCIServerAPI_Route.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 bool
47  tcpip::Storage& outputStorage) {
48  // variable & id
49  int variable = inputStorage.readUnsignedByte();
50  std::string id = inputStorage.readString();
51  // check variable
52  if (variable != ID_LIST && variable != VAR_EDGES && variable != ID_COUNT && variable != VAR_PARAMETER) {
53  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
54  }
55  // begin response building
56  tcpip::Storage tempMsg;
57  // response-code, variableID, objectID
59  tempMsg.writeUnsignedByte(variable);
60  tempMsg.writeString(id);
61  // process request
62  try {
63  switch (variable) {
64  case ID_LIST:
67  break;
68  case ID_COUNT:
71  break;
72  case VAR_EDGES:
75  break;
76  case VAR_PARAMETER: {
77  std::string paramName = "";
78  if (!server.readTypeCheckingString(inputStorage, paramName)) {
79  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
80  }
82  tempMsg.writeString(libsumo::Route::getParameter(id, paramName));
83  break;
84  }
85  default:
86  break;
87  }
88  } catch (libsumo::TraCIException& e) {
89  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
90  }
91  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, "", outputStorage);
92  server.writeResponseWithLength(outputStorage, tempMsg);
93  return true;
94 }
95 
96 
97 bool
99  tcpip::Storage& outputStorage) {
100  std::string warning = ""; // additional description for response
101  // variable
102  int variable = inputStorage.readUnsignedByte();
103  if (variable != ADD && variable != VAR_PARAMETER) {
104  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
105  }
106  // id
107  std::string id = inputStorage.readString();
108 
109  try {
110  // process
111  switch (variable) {
112  case ADD: {
113  std::vector<std::string> edgeIDs;
114  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
115  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
116  }
117  libsumo::Route::add(id, edgeIDs);
118  }
119  break;
120  case VAR_PARAMETER: {
121  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
122  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
123  }
124  //read itemNo
125  inputStorage.readInt();
126  std::string name;
127  if (!server.readTypeCheckingString(inputStorage, name)) {
128  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
129  }
130  std::string value;
131  if (!server.readTypeCheckingString(inputStorage, value)) {
132  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
133  }
134  libsumo::Route::setParameter(id, name, value);
135  }
136  break;
137  default:
138  break;
139  }
140  } catch (libsumo::TraCIException& e) {
141  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
142  }
143  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
144  return true;
145 }
146 
147 #endif
148 
149 
150 /****************************************************************************/
151 
#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:82
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_STRINGLIST
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
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
#define CMD_SET_ROUTE_VARIABLE
virtual int readInt()
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
#define VAR_EDGES
#define ADD
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:69
static std::string getParameter(const std::string &routeID, const std::string &param)
Definition: Route.cpp:69
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:65
#define RESPONSE_GET_ROUTE_VARIABLE
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Route.cpp:75
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
static std::vector< std::string > getEdges(const std::string &routeID)
Definition: Route.cpp:52
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_PARAMETER
#define ID_COUNT
static int getIDCount()
Definition: Route.cpp:63
#define TYPE_INTEGER
#define ID_LIST
static std::vector< std::string > getIDList()
Definition: Route.cpp:45