SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_Junction.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting junction 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 "TraCIConstants.h"
36 #include <microsim/MSNet.h>
37 #include <microsim/MSJunction.h>
39 #include <microsim/MSNet.h>
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // used namespaces
49 // ===========================================================================
50 using namespace traci;
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 bool
58  tcpip::Storage& outputStorage) {
59  // variable
60  int variable = inputStorage.readUnsignedByte();
61  std::string id = inputStorage.readString();
62  // check variable
63  if (variable != ID_LIST && variable != VAR_POSITION && variable != ID_COUNT) {
64  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, "Get Junction Variable: unsupported variable specified", outputStorage);
65  }
66  // begin response building
67  tcpip::Storage tempMsg;
68  // response-code, variableID, objectID
70  tempMsg.writeUnsignedByte(variable);
71  tempMsg.writeString(id);
72  if (variable == ID_LIST) {
73  std::vector<std::string> ids;
76  tempMsg.writeStringList(ids);
77  } else if (variable == ID_COUNT) {
78  std::vector<std::string> ids;
81  tempMsg.writeInt((int) ids.size());
82  } else {
84  if (j == 0) {
85  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, "Junction '" + id + "' is not known", outputStorage);
86  }
87  switch (variable) {
88  case ID_LIST:
89  break;
90  case VAR_POSITION:
92  tempMsg.writeDouble(j->getPosition().x());
93  tempMsg.writeDouble(j->getPosition().y());
94  break;
95  default:
96  break;
97  }
98  }
99  server.writeStatusCmd(CMD_GET_JUNCTION_VARIABLE, RTYPE_OK, "", outputStorage);
100  server.writeResponseWithLength(outputStorage, tempMsg);
101  return true;
102 }
103 
104 
105 bool
106 TraCIServerAPI_Junction::getPosition(const std::string& id, Position& p) {
108  if (j == 0) {
109  return false;
110  }
111  p = j->getPosition();
112  return true;
113 }
114 
115 
116 TraCIRTree*
118  TraCIRTree* t = new TraCIRTree();
119  const std::map<std::string, MSJunction*>& junctions = MSNet::getInstance()->getJunctionControl().getMyMap();
120  for (std::map<std::string, MSJunction*>::const_iterator i = junctions.begin(); i != junctions.end(); ++i) {
121  Boundary b = (*i).second->getShape().getBoxBoundary();
122  t->addObject((*i).second, b);
123  }
124  return t;
125 }
126 
127 #endif
128 
129 
130 /****************************************************************************/
131 
#define POSITION_2D
#define VAR_POSITION
The base class for an intersection.
Definition: MSJunction.h:57
#define RTYPE_OK
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
#define TYPE_STRINGLIST
virtual void writeUnsignedByte(int)
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
virtual void writeInt(int)
virtual int readUnsignedByte()
T get(const std::string &id) const
Retrieves an item.
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 point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
void insertIDs(std::vector< std::string > &into) const
virtual void writeStringList(const std::vector< std::string > &s)
void addObject(Named *o, Boundary &b)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: TraCIRTree.h:115
virtual std::string readString()
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa9: Get Junction Variable)
#define CMD_GET_JUNCTION_VARIABLE
const IDMap & getMyMap() const
A RT-tree for efficient storing of SUMO&#39;s GL-objects.
Definition: TraCIRTree.h:59
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
static bool getPosition(const std::string &id, Position &p)
Returns the named junction&#39;s position.
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
#define RESPONSE_GET_JUNCTION_VARIABLE
virtual void writeDouble(double)
MSJunctionControl & getJunctionControl()
Returns the junctions control.
Definition: MSNet.h:309
#define ID_COUNT
#define TYPE_INTEGER
#define ID_LIST
const Position & getPosition() const
Definition: MSJunction.cpp:58