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 /****************************************************************************/
19 // APIs for getting/setting lane values via TraCI
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <microsim/MSEdge.h>
29 #include <microsim/MSEdgeControl.h>
30 #include <microsim/MSLane.h>
31 #include <microsim/MSNet.h>
32 #include <microsim/MSVehicle.h>
34 #include <libsumo/Lane.h>
35 #include <libsumo/TraCIConstants.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)) {
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  std::string toLane;
100  if (!server.readTypeCheckingString(inputStorage, toLane)) {
101  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "foe retrieval requires a string.", outputStorage);
102  }
104  if (toLane == "") {
106  } else {
108  }
109  break;
110  }
111  case libsumo::VAR_SHAPE:
113  break;
114  case libsumo::VAR_PARAMETER: {
115  std::string paramName = "";
116  if (!server.readTypeCheckingString(inputStorage, paramName)) {
117  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
118  }
121  break;
122  }
123  default:
124  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
125  }
126  }
127  } catch (libsumo::TraCIException& e) {
128  return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
129  }
131  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
132  return true;
133 }
134 
135 
136 bool
138  tcpip::Storage& outputStorage) {
139  std::string warning = ""; // additional description for response
140  // variable
141  int variable = inputStorage.readUnsignedByte();
142  if (variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_LENGTH && variable != libsumo::LANE_ALLOWED && variable != libsumo::LANE_DISALLOWED
143  && variable != libsumo::VAR_PARAMETER) {
144  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
145  }
146  // id
147  std::string id = inputStorage.readString();
148  MSLane* l = MSLane::dictionary(id);
149  if (l == nullptr) {
150  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
151  }
152  // process
153  switch (variable) {
154  case libsumo::VAR_MAXSPEED: {
155  double value = 0;
156  if (!server.readTypeCheckingDouble(inputStorage, value)) {
157  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
158  }
159  libsumo::Lane::setMaxSpeed(id, value);
160  }
161  break;
162  case libsumo::VAR_LENGTH: {
163  double value = 0;
164  if (!server.readTypeCheckingDouble(inputStorage, value)) {
165  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
166  }
167  libsumo::Lane::setLength(id, value);
168  }
169  break;
170  case libsumo::LANE_ALLOWED: {
171  std::vector<std::string> classes;
172  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
173  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
174  }
175  libsumo::Lane::setAllowed(id, classes);
176  }
177  break;
179  std::vector<std::string> classes;
180  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
181  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
182  }
183  libsumo::Lane::setDisallowed(id, classes);
184  }
185  break;
186  case libsumo::VAR_PARAMETER: {
187  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
188  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
189  }
190  //readt itemNo
191  inputStorage.readInt();
192  std::string name;
193  if (!server.readTypeCheckingString(inputStorage, name)) {
194  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
195  }
196  std::string value;
197  if (!server.readTypeCheckingString(inputStorage, value)) {
198  return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
199  }
200  libsumo::Lane::setParameter(id, name, value);
201  }
202  break;
203  default:
204  break;
205  }
206  server.writeStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
207  return true;
208 }
209 
210 
211 /****************************************************************************/
212 
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:352
libsumo::Lane::getInternalFoes
static std::vector< std::string > getInternalFoes(const std::string &laneID)
Definition: Lane.cpp:287
MSLane::dictionary
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1866
libsumo::Lane::setMaxSpeed
static void setMaxSpeed(std::string laneID, double speed)
Definition: Lane.cpp:328
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
libsumo::VAR_MAXSPEED
TRACI_CONST int VAR_MAXSPEED
Definition: TraCIConstants.h:615
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1414
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
libsumo::LANE_DISALLOWED
TRACI_CONST int LANE_DISALLOWED
Definition: TraCIConstants.h:600
libsumo::Lane::getFoes
static std::vector< std::string > getFoes(const std::string &laneID, const std::string &toLaneID)
Definition: Lane.cpp:271
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:939
libsumo::Lane::setParameter
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Lane.cpp:348
libsumo::Lane::setLength
static void setLength(std::string laneID, double length)
Definition: Lane.cpp:335
MSEdge.h
libsumo::CMD_GET_LANE_VARIABLE
TRACI_CONST int CMD_GET_LANE_VARIABLE
Definition: TraCIConstants.h:134
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:174
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1404
libsumo::Lane::setAllowed
static void setAllowed(std::string laneID, std::vector< std::string > allowedClasses)
Definition: Lane.cpp:306
libsumo::Lane::getShape
static TraCIPositionVector getShape(std::string laneID)
Definition: Lane.cpp:124
MSVehicle.h
TraCIServer::writeStatusCmd
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Definition: TraCIServer.cpp:968
libsumo::Lane::getParameter
static std::string getParameter(const std::string &laneID, const std::string &param)
Definition: Lane.cpp:342
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:102
libsumo::VAR_SHAPE
TRACI_CONST int VAR_SHAPE
Definition: TraCIConstants.h:669
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
tcpip::Storage::writeInt
virtual void writeInt(int)
libsumo::TYPE_DOUBLE
TRACI_CONST int TYPE_DOUBLE
Definition: TraCIConstants.h:335
tcpip::Storage::readString
virtual std::string readString()
TraCIConstants.h
tcpip::Storage::readInt
virtual int readInt()
libsumo::TYPE_INTEGER
TRACI_CONST int TYPE_INTEGER
Definition: TraCIConstants.h:333
tcpip::Storage::writeStorage
virtual void writeStorage(tcpip::Storage &store)
tcpip::Storage::writeStringList
virtual void writeStringList(const std::vector< std::string > &s)
libsumo::CMD_SET_LANE_VARIABLE
TRACI_CONST int CMD_SET_LANE_VARIABLE
Definition: TraCIConstants.h:138
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:57
libsumo::VAR_FOES
TRACI_CONST int VAR_FOES
Definition: TraCIConstants.h:603
TraCIServer::readTypeCheckingStringList
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
Definition: TraCIServer.cpp:1424
libsumo::VAR_LENGTH
TRACI_CONST int VAR_LENGTH
Definition: TraCIConstants.h:627
libsumo::TraCIException
Definition: TraCIDefs.h:89
libsumo::Lane::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Lane.cpp:380
TraCIServer::writePositionVector
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
Definition: TraCIServer.cpp:1378
libsumo::TYPE_STRINGLIST
TRACI_CONST int TYPE_STRINGLIST
Definition: TraCIConstants.h:339
libsumo::Lane::getLinks
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: Lane.cpp:86
TraCIServerAPI_Lane.h
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
MSEdgeControl.h
TraCIServerAPI_Lane::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
Definition: TraCIServerAPI_Lane.cpp:137
libsumo::LANE_ALLOWED
TRACI_CONST int LANE_ALLOWED
Definition: TraCIConstants.h:597
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:337
MSTransportable.h
TraCIServerAPI_Lane::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
Definition: TraCIServerAPI_Lane.cpp:44
libsumo::TYPE_UBYTE
TRACI_CONST int TYPE_UBYTE
Definition: TraCIConstants.h:329
Lane.h
TraCIServer.h
config.h
libsumo::RESPONSE_GET_LANE_VARIABLE
TRACI_CONST int RESPONSE_GET_LANE_VARIABLE
Definition: TraCIConstants.h:136
tcpip::Storage::writeDouble
virtual void writeDouble(double)
libsumo::LANE_LINKS
TRACI_CONST int LANE_LINKS
Definition: TraCIConstants.h:594
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
MSLane.h
tcpip::Storage
Definition: storage.h:36
libsumo::Lane::setDisallowed
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: Lane.cpp:317
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:341
TraCIServer::writeErrorStatusCmd
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
Definition: TraCIServer.cpp:982