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-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 /****************************************************************************/
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 "TraCIConstants.h"
36 #include "TraCIServer.h"
37 #include "TraCIServerAPI_Lane.h"
38 #include <libsumo/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();
49  server.initWrapper(RESPONSE_GET_LANE_VARIABLE, variable, id);
50  try {
51  if (!libsumo::Lane::handleVariable(id, variable, &server)) {
52  switch (variable) {
53  case LANE_LINKS: {
55  const std::vector<libsumo::TraCIConnection> links = libsumo::Lane::getLinks(id);
56  tcpip::Storage tempContent;
57  int cnt = 0;
58  tempContent.writeUnsignedByte(TYPE_INTEGER);
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)
63  tempContent.writeUnsignedByte(TYPE_STRING);
64  tempContent.writeString(i->approachedLane);
65  ++cnt;
66  // approached "via", internal lane (if any)
67  tempContent.writeUnsignedByte(TYPE_STRING);
68  tempContent.writeString(i->approachedInternal);
69  ++cnt;
70  // priority
71  tempContent.writeUnsignedByte(TYPE_UBYTE);
72  tempContent.writeUnsignedByte(i->hasPrio);
73  ++cnt;
74  // opened
75  tempContent.writeUnsignedByte(TYPE_UBYTE);
76  tempContent.writeUnsignedByte(i->isOpen);
77  ++cnt;
78  // approaching foe
79  tempContent.writeUnsignedByte(TYPE_UBYTE);
80  tempContent.writeUnsignedByte(i->hasFoe);
81  ++cnt;
82  // state (not implemented, yet)
83  tempContent.writeUnsignedByte(TYPE_STRING);
84  tempContent.writeString(i->state);
85  ++cnt;
86  // direction
87  tempContent.writeUnsignedByte(TYPE_STRING);
88  tempContent.writeString(i->direction);
89  ++cnt;
90  // length
91  tempContent.writeUnsignedByte(TYPE_DOUBLE);
92  tempContent.writeDouble(i->length);
93  ++cnt;
94  }
95  server.getWrapperStorage().writeInt(cnt);
96  server.getWrapperStorage().writeStorage(tempContent);
97  break;
98  }
99  case VAR_FOES: {
100  std::string toLane;
101  if (!server.readTypeCheckingString(inputStorage, toLane)) {
102  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "foe retrieval requires a string.", outputStorage);
103  }
105  if (toLane == "") {
107  } else {
109  }
110  break;
111  }
112  case VAR_SHAPE: {
115  server.getWrapperStorage().writeUnsignedByte(MIN2(255, (int)shp.size()));
116  for (int iPoint = 0; iPoint < MIN2(255, (int) shp.size()); ++iPoint) {
117  server.getWrapperStorage().writeDouble(shp[iPoint].x);
118  server.getWrapperStorage().writeDouble(shp[iPoint].y);
119  }
120  break;
121  }
122  case VAR_PARAMETER: {
123  std::string paramName = "";
124  if (!server.readTypeCheckingString(inputStorage, paramName)) {
125  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
126  }
129  break;
130  }
131  default:
132  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
133  }
134  }
135  } catch (libsumo::TraCIException& e) {
136  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
137  }
138  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
139  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
140  return true;
141 }
142 
143 
144 bool
146  tcpip::Storage& outputStorage) {
147  std::string warning = ""; // additional description for response
148  // variable
149  int variable = inputStorage.readUnsignedByte();
150  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED
151  && variable != VAR_PARAMETER) {
152  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
153  }
154  // id
155  std::string id = inputStorage.readString();
156  MSLane* l = MSLane::dictionary(id);
157  if (l == 0) {
158  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
159  }
160  // process
161  switch (variable) {
162  case VAR_MAXSPEED: {
163  double value = 0;
164  if (!server.readTypeCheckingDouble(inputStorage, value)) {
165  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
166  }
167  libsumo::Lane::setMaxSpeed(id, value);
168  }
169  break;
170  case VAR_LENGTH: {
171  double value = 0;
172  if (!server.readTypeCheckingDouble(inputStorage, value)) {
173  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
174  }
175  libsumo::Lane::setLength(id, value);
176  }
177  break;
178  case LANE_ALLOWED: {
179  std::vector<std::string> classes;
180  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
181  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
182  }
183  libsumo::Lane::setAllowed(id, classes);
184  }
185  break;
186  case LANE_DISALLOWED: {
187  std::vector<std::string> classes;
188  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
189  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
190  }
191  libsumo::Lane::setDisallowed(id, classes);
192  }
193  break;
194  case VAR_PARAMETER: {
195  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
196  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
197  }
198  //readt itemNo
199  inputStorage.readInt();
200  std::string name;
201  if (!server.readTypeCheckingString(inputStorage, name)) {
202  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
203  }
204  std::string value;
205  if (!server.readTypeCheckingString(inputStorage, value)) {
206  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
207  }
208  libsumo::Lane::setParameter(id, name, value);
209  }
210  break;
211  default:
212  break;
213  }
214  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
215  return true;
216 }
217 
218 
219 /****************************************************************************/
220 
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
#define VAR_LENGTH
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
#define TYPE_COMPOUND
#define LANE_LINKS
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Lane.cpp:346
#define TYPE_UBYTE
#define RTYPE_OK
#define TYPE_POLYGON
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.
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, 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 VAR_SHAPE
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
#define RESPONSE_GET_LANE_VARIABLE
#define LANE_ALLOWED
virtual int readInt()
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.
virtual void writeStringList(const std::vector< std::string > &s)
#define CMD_GET_LANE_VARIABLE
static void setMaxSpeed(std::string laneID, double speed)
Definition: Lane.cpp:326
T MIN2(T a, T b)
Definition: StdDefs.h:70
tcpip::Storage & getWrapperStorage()
virtual std::string readString()
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: Lane.cpp:318
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:340
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1708
virtual void writeString(const std::string &s)
#define LANE_DISALLOWED
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:59
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:145
#define CMD_SET_LANE_VARIABLE
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Lane.cpp:378
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:333
#define VAR_MAXSPEED
void initWrapper(const int domainID, const int variable, const std::string &objID)
#define VAR_PARAMETER
#define TYPE_INTEGER
Representation of a lane in the micro simulation.
Definition: MSLane.h:71
#define VAR_FOES