SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2017 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 "lib/TraCI_Route.h"
40 #include "TraCIServerAPI_Route.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 bool
48  tcpip::Storage& outputStorage) {
49  // variable & id
50  int variable = inputStorage.readUnsignedByte();
51  std::string id = inputStorage.readString();
52  // check variable
53  if (variable != ID_LIST && variable != VAR_EDGES && variable != ID_COUNT && variable != VAR_PARAMETER) {
54  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Get Route Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
55  }
56  // begin response building
57  tcpip::Storage tempMsg;
58  // response-code, variableID, objectID
60  tempMsg.writeUnsignedByte(variable);
61  tempMsg.writeString(id);
62  // process request
63  try {
64  switch (variable) {
65  case ID_LIST:
68  break;
69  case ID_COUNT:
72  break;
73  case VAR_EDGES:
76  break;
77  case VAR_PARAMETER: {
78  std::string paramName = "";
79  if (!server.readTypeCheckingString(inputStorage, paramName)) {
80  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
81  }
83  tempMsg.writeString(TraCI_Route::getParameter(id, paramName));
84  break;
85  }
86  default:
87  break;
88  }
89  } catch (TraCIException& e) {
90  return server.writeErrorStatusCmd(CMD_GET_ROUTE_VARIABLE, e.what(), outputStorage);
91  }
92  server.writeStatusCmd(CMD_GET_ROUTE_VARIABLE, RTYPE_OK, "", outputStorage);
93  server.writeResponseWithLength(outputStorage, tempMsg);
94  return true;
95 }
96 
97 
98 bool
100  tcpip::Storage& outputStorage) {
101  std::string warning = ""; // additional description for response
102  // variable
103  int variable = inputStorage.readUnsignedByte();
104  if (variable != ADD && variable != VAR_PARAMETER) {
105  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "Change Route State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
106  }
107  // id
108  std::string id = inputStorage.readString();
109 
110  try {
111  // process
112  switch (variable) {
113  case ADD: {
114  std::vector<std::string> edgeIDs;
115  if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
116  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "A string list is needed for adding a new route.", outputStorage);
117  }
118  TraCI_Route::add(id, edgeIDs);
119  }
120  break;
121  case VAR_PARAMETER: {
122  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
123  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
124  }
125  //read itemNo
126  inputStorage.readInt();
127  std::string name;
128  if (!server.readTypeCheckingString(inputStorage, name)) {
129  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
130  }
131  std::string value;
132  if (!server.readTypeCheckingString(inputStorage, value)) {
133  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
134  }
135  TraCI_Route::setParameter(id, name, value);
136  }
137  break;
138  default:
139  break;
140  }
141  } catch (TraCIException& e) {
142  return server.writeErrorStatusCmd(CMD_SET_ROUTE_VARIABLE, e.what(), outputStorage);
143  }
144  server.writeStatusCmd(CMD_SET_ROUTE_VARIABLE, RTYPE_OK, warning, outputStorage);
145  return true;
146 }
147 
148 #endif
149 
150 
151 /****************************************************************************/
152 
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: TraCI_Route.cpp:75
#define TYPE_COMPOUND
static std::vector< std::string > getIDList()
Definition: TraCI_Route.cpp:45
#define RTYPE_OK
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
static int getIDCount()
Definition: TraCI_Route.cpp:63
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_STRINGLIST
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)
#define TYPE_STRING
virtual int readUnsignedByte()
#define CMD_SET_ROUTE_VARIABLE
static void add(const std::string &routeID, const std::vector< std::string > &edgeIDs)
Definition: TraCI_Route.cpp:82
virtual int readInt()
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)
static std::vector< std::string > getEdges(const std::string &routeID)
Definition: TraCI_Route.cpp:52
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
#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)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_PARAMETER
#define ID_COUNT
static std::string getParameter(const std::string &routeID, const std::string &param)
Definition: TraCI_Route.cpp:69
#define TYPE_INTEGER
#define ID_LIST