SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_Route.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting route values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifndef NO_TRACI
34 
35 #include <microsim/MSNet.h>
36 #include <microsim/MSRoute.h>
37 #include <microsim/MSEdge.h>
38 #include "TraCIConstants.h"
39 #include "TraCIServerAPI_Route.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 bool
51  tcpip::Storage& outputStorage) {
52  // variable & id
53  int variable = inputStorage.readUnsignedByte();
54  std::string id = inputStorage.readString();
55  // check variable
56  if (variable != ID_LIST && variable != VAR_EDGES && variable != ID_COUNT) {
57  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable specified", outputStorage);
58  }
59  // begin response building
60  tcpip::Storage tempMsg;
61  // response-code, variableID, objectID
63  tempMsg.writeUnsignedByte(variable);
64  tempMsg.writeString(id);
65  // process request
66  if (variable == ID_LIST) {
67  std::vector<std::string> ids;
68  MSRoute::insertIDs(ids);
70  tempMsg.writeStringList(ids);
71  } else if (variable == ID_COUNT) {
72  std::vector<std::string> ids;
73  MSRoute::insertIDs(ids);
75  tempMsg.writeInt((int) ids.size());
76  } else {
77  const MSRoute* r = MSRoute::dictionary(id);
78  if (r == 0) {
79  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Route '" + id + "' is not known", outputStorage);
80  }
81  switch (variable) {
82  case VAR_EDGES:
84  tempMsg.writeInt(r->size());
85  for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
86  tempMsg.writeString((*i)->getID());
87  }
88  break;
89  default:
90  break;
91  }
92  }
93  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, "", outputStorage);
94  server.writeResponseWithLength(outputStorage, tempMsg);
95  return true;
96 }
97 
98 
99 bool
101  tcpip::Storage& outputStorage) {
102  std::string warning = ""; // additional description for response
103  // variable
104  int variable = inputStorage.readUnsignedByte();
105  if (variable != ADD) {
106  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable specified", outputStorage);
107  }
108  // id
109  std::string id = inputStorage.readString();
110  // process
111  switch (variable) {
112  case ADD: {
113  std::vector<std::string> edgeIDs;
114  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
115  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
116  }
117  //read itemNo
118  MSEdgeVector edges;
119  for (std::vector<std::string>::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
120  MSEdge* edge = MSEdge::dictionary(*i);
121  if (edge == 0) {
122  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Unknown edge '" + *i + "' in route.", outputStorage);
123  }
124  edges.push_back(edge);
125  }
126  const std::vector<SUMOVehicleParameter::Stop> stops;
127  if (!MSRoute::dictionary(id, new MSRoute(id, edges, true, 0, stops))) {
128  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Could not add route.", outputStorage);
129  }
130  }
131  break;
132  default:
133  break;
134  }
135  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
136  return true;
137 }
138 
139 #endif
140 
141 
142 /****************************************************************************/
143 
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:59
#define RTYPE_OK
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
#define TYPE_STRINGLIST
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary...
Definition: MSEdge.cpp:465
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 CMD_GET_ROUTE_VARIABLE
virtual void writeInt(int)
virtual int readUnsignedByte()
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:186
A road/street connecting two junctions.
Definition: MSEdge.h:73
#define CMD_SET_ROUTE_VARIABLE
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
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)
virtual std::string readString()
#define VAR_EDGES
#define ADD
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:80
#define RESPONSE_GET_ROUTE_VARIABLE
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
unsigned size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:86
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define ID_COUNT
#define TYPE_INTEGER
#define ID_LIST
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:74
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:115