Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2009-2020 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 /****************************************************************************/
23 // APIs for getting/setting lane values via TraCI
24 /****************************************************************************/
25 #include <config.h>
26 
27 #include <microsim/MSEdge.h>
28 #include <microsim/MSEdgeControl.h>
29 #include <microsim/MSLane.h>
30 #include <microsim/MSNet.h>
31 #include <microsim/MSVehicle.h>
33 #include <libsumo/Lane.h>
34 #include <libsumo/TraCIConstants.h>
35 #include "TraCIServer.h"
36 #include "TraCIServerAPI_Lane.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 bool
44  tcpip::Storage& outputStorage) {
45  const int variable = inputStorage.readUnsignedByte();
46  const std::string id = inputStorage.readString();
48  try {
49  if (!libsumo::Lane::handleVariable(id, variable, &server)) {
50  switch (variable) {
51  case libsumo::LANE_LINKS: {
53  const std::vector<libsumo::TraCIConnection> links = libsumo::Lane::getLinks(id);
54  tcpip::Storage tempContent;
55  int cnt = 0;
57  tempContent.writeInt((int) links.size());
58  ++cnt;
59  for (std::vector<libsumo::TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
60  // approached non-internal lane (if any)
62  tempContent.writeString(i->approachedLane);
63  ++cnt;
64  // approached "via", internal lane (if any)
66  tempContent.writeString(i->approachedInternal);
67  ++cnt;
68  // priority
70  tempContent.writeUnsignedByte(i->hasPrio);
71  ++cnt;
72  // opened
74  tempContent.writeUnsignedByte(i->isOpen);
75  ++cnt;
76  // approaching foe
78  tempContent.writeUnsignedByte(i->hasFoe);
79  ++cnt;
80  // state (not implemented, yet)
82  tempContent.writeString(i->state);
83  ++cnt;
84  // direction
86  tempContent.writeString(i->direction);
87  ++cnt;
88  // length
90  tempContent.writeDouble(i->length);
91  ++cnt;
92  }
93  server.getWrapperStorage().writeInt(cnt);
94  server.getWrapperStorage().writeStorage(tempContent);
95  break;
96  }
97  case libsumo::VAR_FOES: {
98  std::string toLane;
99  if (!server.readTypeCheckingString(inputStorage, toLane)) {
100  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "foe retrieval requires a string.", outputStorage);
101  }
103  if (toLane == "") {
104  server.getWrapperStorage().writeStringList(libsumo::Lane::getInternalFoes(id));
105  } else {
106  server.getWrapperStorage().writeStringList(libsumo::Lane::getFoes(id, toLane));
107  }
108  break;
109  }
110  case libsumo::VAR_SHAPE:
111  server.writePositionVector(server.getWrapperStorage(), libsumo::Lane::getShape(id));
112  break;
113  case libsumo::VAR_PARAMETER: {
114  std::string paramName = "";
115  if (!server.readTypeCheckingString(inputStorage, paramName)) {
116  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
117  }
119  server.getWrapperStorage().writeString(libsumo::Lane::getParameter(id, paramName));
120  break;
121  }
123  std::string paramName = "";
124  if (!server.readTypeCheckingString(inputStorage, paramName)) {
125  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
126  }
128  server.getWrapperStorage().writeInt(2);
130  server.getWrapperStorage().writeString(paramName);
132  server.getWrapperStorage().writeString(libsumo::Lane::getParameter(id, paramName));
133  break;
134  }
135  default:
136  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
137  }
138  }
139  } catch (libsumo::TraCIException& e) {
140  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
141  }
143  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
144  return true;
145 }
146 
147 
148 bool
150  tcpip::Storage& outputStorage) {
151  std::string warning = ""; // additional description for response
152  // variable
153  int variable = inputStorage.readUnsignedByte();
154  if (variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_LENGTH && variable != libsumo::LANE_ALLOWED && variable != libsumo::LANE_DISALLOWED
155  && variable != libsumo::VAR_PARAMETER) {
156  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
157  }
158  // id
159  std::string id = inputStorage.readString();
160  MSLane* l = MSLane::dictionary(id);
161  if (l == nullptr) {
162  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
163  }
164  // process
165  switch (variable) {
166  case libsumo::VAR_MAXSPEED: {
167  double value = 0;
168  if (!server.readTypeCheckingDouble(inputStorage, value)) {
169  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
170  }
171  libsumo::Lane::setMaxSpeed(id, value);
172  }
173  break;
174  case libsumo::VAR_LENGTH: {
175  double value = 0;
176  if (!server.readTypeCheckingDouble(inputStorage, value)) {
177  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
178  }
179  libsumo::Lane::setLength(id, value);
180  }
181  break;
182  case libsumo::LANE_ALLOWED: {
183  std::vector<std::string> classes;
184  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
185  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
186  }
187  libsumo::Lane::setAllowed(id, classes);
188  }
189  break;
191  std::vector<std::string> classes;
192  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
193  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
194  }
195  libsumo::Lane::setDisallowed(id, classes);
196  }
197  break;
198  case libsumo::VAR_PARAMETER: {
199  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
200  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
201  }
202  //readt itemNo
203  inputStorage.readInt();
204  std::string name;
205  if (!server.readTypeCheckingString(inputStorage, name)) {
206  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
207  }
208  std::string value;
209  if (!server.readTypeCheckingString(inputStorage, value)) {
210  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
211  }
212  libsumo::Lane::setParameter(id, name, value);
213  }
214  break;
215  default:
216  break;
217  }
218  server.writeStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
219  return true;
220 }
221 
222 
223 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:54
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1908
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
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.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual std::string readString()
Definition: storage.cpp:175
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
virtual void writeInt(int)
Definition: storage.cpp:316
virtual void writeDouble(double)
Definition: storage.cpp:349
virtual int readUnsignedByte()
Definition: storage.cpp:150
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:242
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
virtual void writeStorage(tcpip::Storage &store)
Definition: storage.cpp:383
virtual int readInt()
Definition: storage.cpp:306
TRACI_CONST int LANE_LINKS
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int TYPE_UBYTE
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int VAR_SHAPE
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int RESPONSE_GET_LANE_VARIABLE
TRACI_CONST int CMD_SET_LANE_VARIABLE
TRACI_CONST int CMD_GET_LANE_VARIABLE
TRACI_CONST int LANE_DISALLOWED
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int RTYPE_OK
TRACI_CONST int VAR_FOES
TRACI_CONST int LANE_ALLOWED
TRACI_CONST int TYPE_STRING