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  // 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  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Get Route Variable: unsupported variable specified", outputStorage);
64  return false;
65  }
66  // begin response building
67  tcpip::Storage tempMsg;
68  // response-code, variableID, objectID
70  tempMsg.writeUnsignedByte(variable);
71  tempMsg.writeString(id);
72  // process request
73  if (variable == ID_LIST) {
74  std::vector<std::string> ids;
75  MSRoute::insertIDs(ids);
77  tempMsg.writeStringList(ids);
78  } else if (variable == ID_COUNT) {
79  std::vector<std::string> ids;
80  MSRoute::insertIDs(ids);
82  tempMsg.writeInt((int) ids.size());
83  } else {
84  const MSRoute* r = MSRoute::dictionary(id);
85  if (r == 0) {
86  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_ERR, "Route '" + id + "' is not known", outputStorage);
87  return false;
88  }
89  switch (variable) {
90  case VAR_EDGES:
92  tempMsg.writeInt(r->size());
93  for (MSRouteIterator i = r->begin(); i != r->end(); ++i) {
94  tempMsg.writeString((*i)->getID());
95  }
96  break;
97  default:
98  break;
99  }
100  }
101  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, "", outputStorage);
102  server.writeResponseWithLength(outputStorage, tempMsg);
103  return true;
104 }
105 
106 
107 bool
109  tcpip::Storage& outputStorage) {
110  std::string warning = ""; // additional description for response
111  // variable
112  int variable = inputStorage.readUnsignedByte();
113  if (variable != ADD) {
114  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Change Route State: unsupported variable specified", outputStorage);
115  return false;
116  }
117  // id
118  std::string id = inputStorage.readString();
119  // process
120  int valueDataType = inputStorage.readUnsignedByte();
121  switch (variable) {
122  case ADD: {
123  if (valueDataType != TYPE_STRINGLIST) {
124  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "A string list is needed for adding a new route.", outputStorage);
125  return false;
126  }
127  //read itemNo
128  int numEdges = inputStorage.readInt();
129  MSEdgeVector edges;
130  while (numEdges--) {
131  std::string edgeID = inputStorage.readString();
132  MSEdge* edge = MSEdge::dictionary(edgeID);
133  if (edge == 0) {
134  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_ERR, "Unknown edge '" + edgeID + "' 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, 0, 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