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-2022 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 <libsumo/StorageHelper.h>
36 #include "TraCIServer.h"
37 #include "TraCIServerAPI_Lane.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 bool
45  tcpip::Storage& outputStorage) {
46  const int variable = inputStorage.readUnsignedByte();
47  const std::string id = inputStorage.readString();
49  try {
50  if (!libsumo::Lane::handleVariable(id, variable, &server, &inputStorage)) {
51  switch (variable) {
52  case libsumo::LANE_LINKS: {
54  const std::vector<libsumo::TraCIConnection> links = libsumo::Lane::getLinks(id);
55  tcpip::Storage tempContent;
56  int cnt = 0;
58  tempContent.writeInt((int) links.size());
59  ++cnt;
60  for (std::vector<libsumo::TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
61  // approached non-internal lane (if any)
63  tempContent.writeString(i->approachedLane);
64  ++cnt;
65  // approached "via", internal lane (if any)
67  tempContent.writeString(i->approachedInternal);
68  ++cnt;
69  // priority
71  tempContent.writeUnsignedByte(i->hasPrio);
72  ++cnt;
73  // opened
75  tempContent.writeUnsignedByte(i->isOpen);
76  ++cnt;
77  // approaching foe
79  tempContent.writeUnsignedByte(i->hasFoe);
80  ++cnt;
81  // state (not implemented, yet)
83  tempContent.writeString(i->state);
84  ++cnt;
85  // direction
87  tempContent.writeString(i->direction);
88  ++cnt;
89  // length
91  tempContent.writeDouble(i->length);
92  ++cnt;
93  }
94  server.getWrapperStorage().writeInt(cnt);
95  server.getWrapperStorage().writeStorage(tempContent);
96  break;
97  }
98  case libsumo::VAR_FOES: {
99  const std::string toLane = StoHelp::readTypedString(inputStorage, "Foe retrieval requires a string.");
100  StoHelp::writeTypedStringList(server.getWrapperStorage(), toLane == "" ? libsumo::Lane::getInternalFoes(id) : libsumo::Lane::getFoes(id, toLane));
101  break;
102  }
103  default:
104  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
105  }
106  }
107  } catch (libsumo::TraCIException& e) {
108  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
109  }
111  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
112  return true;
113 }
114 
115 
116 bool
118  tcpip::Storage& outputStorage) {
119  std::string warning = ""; // additional description for response
120  // variable
121  int variable = inputStorage.readUnsignedByte();
122  if (variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_LENGTH && variable != libsumo::LANE_ALLOWED && variable != libsumo::LANE_DISALLOWED
123  && variable != libsumo::VAR_PARAMETER) {
124  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
125  }
126  // id
127  std::string id = inputStorage.readString();
128  MSLane* l = MSLane::dictionary(id);
129  if (l == nullptr) {
130  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
131  }
132  try {
133  // process
134  switch (variable) {
135  case libsumo::VAR_MAXSPEED: {
136  const double value = StoHelp::readTypedDouble(inputStorage, "The speed must be given as a double.");
137  libsumo::Lane::setMaxSpeed(id, value);
138  break;
139  }
140  case libsumo::VAR_LENGTH: {
141  const double value = StoHelp::readTypedDouble(inputStorage, "The length must be given as a double.");
142  libsumo::Lane::setLength(id, value);
143  break;
144  }
145  case libsumo::LANE_ALLOWED: {
146  const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Allowed vehicle classes must be given as a list of strings.");
147  libsumo::Lane::setAllowed(id, classes);
148  break;
149  }
151  const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Not allowed vehicle classes must be given as a list of strings.");
152  libsumo::Lane::setDisallowed(id, classes);
153  break;
154  }
155  case libsumo::VAR_PARAMETER: {
156  StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
157  const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
158  const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
159  libsumo::Lane::setParameter(id, name, value);
160  break;
161  }
162  default:
163  break;
164  }
165  } catch (libsumo::TraCIException& e) {
166  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, e.what(), outputStorage);
167  }
168  server.writeStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
169  return true;
170 }
171 
172 
173 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:56
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:1991
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.
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.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
Definition: StorageHelper.h:85
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static void writeTypedStringList(tcpip::Storage &content, const std::vector< std::string > &value)
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
An error which allows to continue.
Definition: TraCIDefs.h:130
virtual std::string readString()
Definition: storage.cpp:180
virtual void writeString(const std::string &s)
Definition: storage.cpp:197
virtual void writeInt(int)
Definition: storage.cpp:321
virtual void writeDouble(double)
Definition: storage.cpp:354
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
virtual void writeStorage(tcpip::Storage &store)
Definition: storage.cpp:388
TRACI_CONST int LANE_LINKS
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int TYPE_UBYTE
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int TYPE_INTEGER
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 TYPE_DOUBLE
TRACI_CONST int RTYPE_OK
TRACI_CONST int VAR_FOES
TRACI_CONST int LANE_ALLOWED
TRACI_CONST int TYPE_STRING