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-2013 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 // used namespaces
48 // ===========================================================================
49 using namespace traci;
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 bool
57  tcpip::Storage& outputStorage) {
58  // variable & id
59  int variable = inputStorage.readUnsignedByte();
60  std::string id = inputStorage.readString();
61  // check variable
62  if (variable != ID_LIST && variable != VAR_EDGES && variable != ID_COUNT) {
63  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable specified", outputStorage);
64  }
65  // begin response building
66  tcpip::Storage tempMsg;
67  // response-code, variableID, objectID
69  tempMsg.writeUnsignedByte(variable);
70  tempMsg.writeString(id);
71  // process request
72  if (variable == ID_LIST) {
73  std::vector<std::string> ids;
74  MSRoute::insertIDs(ids);
76  tempMsg.writeStringList(ids);
77  } else if (variable == ID_COUNT) {
78  std::vector<std::string> ids;
79  MSRoute::insertIDs(ids);
81  tempMsg.writeInt((int) ids.size());
82  } else {
83  const MSRoute* r = MSRoute::dictionary(id);
84  if (r == 0) {
85  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Route '" + id + "' is not known", outputStorage);
86  }
87  switch (variable) {
88  case VAR_EDGES:
90  tempMsg.writeInt(r->size());
91  for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
92  tempMsg.writeString((*i)->getID());
93  }
94  break;
95  default:
96  break;
97  }
98  }
99  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, "", outputStorage);
100  server.writeResponseWithLength(outputStorage, tempMsg);
101  return true;
102 }
103 
104 
105 bool
107  tcpip::Storage& outputStorage) {
108  std::string warning = ""; // additional description for response
109  // variable
110  int variable = inputStorage.readUnsignedByte();
111  if (variable != ADD) {
112  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable specified", outputStorage);
113  }
114  // id
115  std::string id = inputStorage.readString();
116  // process
117  switch (variable) {
118  case ADD: {
119  std::vector<std::string> edgeIDs;
120  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
121  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
122  }
123  //read itemNo
124  MSEdgeVector edges;
125  for (std::vector<std::string>::const_iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
126  MSEdge* edge = MSEdge::dictionary(*i);
127  if (edge == 0) {
128  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Unknown edge '" + *i + "' in route.", outputStorage);
129  }
130  edges.push_back(edge);
131  }
132  const std::vector<SUMOVehicleParameter::Stop> stops;
133  if (!MSRoute::dictionary(id, new MSRoute(id, edges, 1, 0, stops))) {
134  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Could not add route.", outputStorage);
135  }
136  }
137  break;
138  default:
139  break;
140  }
141  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
142  return true;
143 }
144 
145 #endif
146 
147 
148 /****************************************************************************/
149 
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:59
#define RTYPE_OK
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
#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&#39;t already in the dictionary...
Definition: MSEdge.cpp:461
virtual void writeUnsignedByte(int)
#define CMD_GET_ROUTE_VARIABLE
virtual void writeInt(int)
virtual int readUnsignedByte()
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:173
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
A road/street connecting two junctions.
Definition: MSEdge.h:73
#define CMD_SET_ROUTE_VARIABLE
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
#define VAR_EDGES
#define ADD
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:81
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
#define RESPONSE_GET_ROUTE_VARIABLE
unsigned size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:87
#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:75
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116