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-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 /****************************************************************************/
20 // APIs for getting/setting lane values via TraCI
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <microsim/MSEdge.h>
30 #include <microsim/MSEdgeControl.h>
31 #include <microsim/MSLane.h>
32 #include <microsim/MSNet.h>
33 #include <microsim/MSVehicle.h>
35 #include <libsumo/Lane.h>
36 #include <libsumo/TraCIConstants.h>
37 #include "TraCIServer.h"
38 #include "TraCIServerAPI_Lane.h"
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
44 bool
46  tcpip::Storage& outputStorage) {
47  const int variable = inputStorage.readUnsignedByte();
48  const std::string id = inputStorage.readString();
50  try {
51  if (!libsumo::Lane::handleVariable(id, variable, &server)) {
52  switch (variable) {
53  case libsumo::LANE_LINKS: {
55  const std::vector<libsumo::TraCIConnection> links = libsumo::Lane::getLinks(id);
56  tcpip::Storage tempContent;
57  int cnt = 0;
59  tempContent.writeInt((int) links.size());
60  ++cnt;
61  for (std::vector<libsumo::TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
62  // approached non-internal lane (if any)
64  tempContent.writeString(i->approachedLane);
65  ++cnt;
66  // approached "via", internal lane (if any)
68  tempContent.writeString(i->approachedInternal);
69  ++cnt;
70  // priority
72  tempContent.writeUnsignedByte(i->hasPrio);
73  ++cnt;
74  // opened
76  tempContent.writeUnsignedByte(i->isOpen);
77  ++cnt;
78  // approaching foe
80  tempContent.writeUnsignedByte(i->hasFoe);
81  ++cnt;
82  // state (not implemented, yet)
84  tempContent.writeString(i->state);
85  ++cnt;
86  // direction
88  tempContent.writeString(i->direction);
89  ++cnt;
90  // length
92  tempContent.writeDouble(i->length);
93  ++cnt;
94  }
95  server.getWrapperStorage().writeInt(cnt);
96  server.getWrapperStorage().writeStorage(tempContent);
97  break;
98  }
99  case libsumo::VAR_FOES: {
100  std::string toLane;
101  if (!server.readTypeCheckingString(inputStorage, toLane)) {
102  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "foe retrieval requires a string.", outputStorage);
103  }
105  if (toLane == "") {
107  } else {
109  }
110  break;
111  }
112  case libsumo::VAR_SHAPE:
114  break;
115  case libsumo::VAR_PARAMETER: {
116  std::string paramName = "";
117  if (!server.readTypeCheckingString(inputStorage, paramName)) {
118  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
119  }
122  break;
123  }
124  default:
125  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
126  }
127  }
128  } catch (libsumo::TraCIException& e) {
129  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
130  }
132  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
133  return true;
134 }
135 
136 
137 bool
139  tcpip::Storage& outputStorage) {
140  std::string warning = ""; // additional description for response
141  // variable
142  int variable = inputStorage.readUnsignedByte();
143  if (variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_LENGTH && variable != libsumo::LANE_ALLOWED && variable != libsumo::LANE_DISALLOWED
144  && variable != libsumo::VAR_PARAMETER) {
145  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
146  }
147  // id
148  std::string id = inputStorage.readString();
149  MSLane* l = MSLane::dictionary(id);
150  if (l == nullptr) {
151  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
152  }
153  // process
154  switch (variable) {
155  case libsumo::VAR_MAXSPEED: {
156  double value = 0;
157  if (!server.readTypeCheckingDouble(inputStorage, value)) {
158  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
159  }
160  libsumo::Lane::setMaxSpeed(id, value);
161  }
162  break;
163  case libsumo::VAR_LENGTH: {
164  double value = 0;
165  if (!server.readTypeCheckingDouble(inputStorage, value)) {
166  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
167  }
168  libsumo::Lane::setLength(id, value);
169  }
170  break;
171  case libsumo::LANE_ALLOWED: {
172  std::vector<std::string> classes;
173  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
174  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
175  }
176  libsumo::Lane::setAllowed(id, classes);
177  }
178  break;
180  std::vector<std::string> classes;
181  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
182  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
183  }
184  libsumo::Lane::setDisallowed(id, classes);
185  }
186  break;
187  case libsumo::VAR_PARAMETER: {
188  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
189  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
190  }
191  //readt itemNo
192  inputStorage.readInt();
193  std::string name;
194  if (!server.readTypeCheckingString(inputStorage, name)) {
195  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
196  }
197  std::string value;
198  if (!server.readTypeCheckingString(inputStorage, value)) {
199  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
200  }
201  libsumo::Lane::setParameter(id, name, value);
202  }
203  break;
204  default:
205  break;
206  }
207  server.writeStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
208  return true;
209 }
210 
211 
212 /****************************************************************************/
213 
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
static void setAllowed(std::string laneID, std::vector< std::string > allowedClasses)
Definition: Lane.cpp:307
static std::vector< std::string > getFoes(const std::string &laneID, const std::string &toLaneID)
Definition: Lane.cpp:272
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: Lane.cpp:87
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
TRACI_CONST int CMD_GET_LANE_VARIABLE
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Lane.cpp:349
TRACI_CONST int RTYPE_OK
TRACI_CONST int VAR_PARAMETER
static std::vector< std::string > getInternalFoes(const std::string &laneID)
Definition: Lane.cpp:288
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
TRACI_CONST int LANE_DISALLOWED
virtual void writeUnsignedByte(int)
TRACI_CONST int VAR_MAXSPEED
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
virtual void writeInt(int)
virtual int readUnsignedByte()
TRACI_CONST int TYPE_INTEGER
virtual int readInt()
TRACI_CONST int VAR_FOES
static TraCIPositionVector getShape(std::string laneID)
Definition: Lane.cpp:125
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
TRACI_CONST int VAR_SHAPE
TRACI_CONST int VAR_LENGTH
virtual void writeStringList(const std::vector< std::string > &s)
TRACI_CONST int CMD_SET_LANE_VARIABLE
static void setMaxSpeed(std::string laneID, double speed)
Definition: Lane.cpp:329
TRACI_CONST int TYPE_DOUBLE
tcpip::Storage & getWrapperStorage()
TRACI_CONST int TYPE_STRINGLIST
virtual std::string readString()
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: Lane.cpp:318
TRACI_CONST int TYPE_STRING
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
virtual void writeStorage(tcpip::Storage &store)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static std::string getParameter(const std::string &laneID, const std::string &param)
Definition: Lane.cpp:343
TRACI_CONST int LANE_ALLOWED
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1855
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:58
TRACI_CONST int LANE_LINKS
TRACI_CONST int RESPONSE_GET_LANE_VARIABLE
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Lane.cpp:381
TRACI_CONST int TYPE_UBYTE
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static void setLength(std::string laneID, double length)
Definition: Lane.cpp:336
void initWrapper(const int domainID, const int variable, const std::string &objID)
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
TRACI_CONST int TYPE_COMPOUND