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.sourceforge.net/
12 // Copyright (C) 2001-2012 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  std::string warning = ""; // additional description for response
59  // variable & id
60  int variable = inputStorage.readUnsignedByte();
61  std::string id = inputStorage.readString();
62  // check variable
63  if (variable != ID_LIST && variable != VAR_EDGES && variable != ID_COUNT) {
64  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Get Route Variable: unsupported variable specified", outputStorage);
65  return false;
66  }
67  // begin response building
68  tcpip::Storage tempMsg;
69  // response-code, variableID, objectID
71  tempMsg.writeUnsignedByte(variable);
72  tempMsg.writeString(id);
73  // process request
74  if (variable == ID_LIST) {
75  std::vector<std::string> ids;
76  MSRoute::insertIDs(ids);
78  tempMsg.writeStringList(ids);
79  } else if (variable == ID_COUNT) {
80  std::vector<std::string> ids;
81  MSRoute::insertIDs(ids);
83  tempMsg.writeInt((int) ids.size());
84  } else {
85  const MSRoute* r = MSRoute::dictionary(id);
86  if (r == 0) {
87  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Route '" + id + "' is not known", outputStorage);
88  return false;
89  }
90  switch (variable) {
91  case VAR_EDGES:
93  tempMsg.writeInt(r->size());
94  for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
95  tempMsg.writeString((*i)->getID());
96  }
97  break;
98  default:
99  break;
100  }
101  }
102  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
103  server.writeResponseWithLength(outputStorage, tempMsg);
104  return true;
105 }
106 
107 
108 bool
110  tcpip::Storage& outputStorage) {
111  std::string warning = ""; // additional description for response
112  // variable
113  int variable = inputStorage.readUnsignedByte();
114  if (variable != ADD) {
115  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Change Route State: unsupported variable specified", outputStorage);
116  return false;
117  }
118  // id
119  std::string id = inputStorage.readString();
120  // process
121  int valueDataType = inputStorage.readUnsignedByte();
122  switch (variable) {
123  case ADD: {
124  if (valueDataType != TYPE_STRINGLIST) {
125  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "A string list is needed for adding a new route.", outputStorage);
126  return false;
127  }
128  //read itemNo
129  int numEdges = inputStorage.readInt();
130  MSEdgeVector edges;
131  while (numEdges--) {
132  MSEdge* edge = MSEdge::dictionary(inputStorage.readString());
133  if (edge == 0) {
134  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Unknown edge in route.", outputStorage);
135  return false;
136  }
137  edges.push_back(edge);
138  }
139  const std::vector<SUMOVehicleParameter::Stop> stops;
140  if (!MSRoute::dictionary(id, new MSRoute(id, edges, 1, RGBColor::DEFAULT_COLOR, stops))) {
141  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Could not add route.", outputStorage);
142  return false;
143  }
144  }
145  break;
146  default:
147  break;
148  }
149  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
150  return true;
151 }
152 
153 #endif
154 
155 
156 /****************************************************************************/
157