Eclipse SUMO - Simulation of Urban MObility
TraCITestClient.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // A test execution class
22 /****************************************************************************/
23 #pragma once
24 #include <string>
25 #include <sstream>
26 #include <vector>
27 
28 #include <foreign/tcpip/socket.h>
29 #include <utils/traci/TraCIAPI.h>
30 
31 
32 // ===========================================================================
33 // class definitions
34 // ===========================================================================
41 class TraCITestClient : public TraCIAPI {
42 public:
46  TraCITestClient(std::string outputFileName = "testclient_result.out");
47 
48 
51 
52 
58  int run(std::string fileName, int port, std::string host = "localhost");
59 
60 
61 protected:
64 
68  void commandSimulationStep(double time);
69 
70 
73  void commandClose();
74 
75 
78  void commandSetOrder(int order);
79 
80 
87  void commandGetVariable(int domID, int varID, const std::string& objID, tcpip::Storage* addData = 0);
88 
89 
96  void commandSetValue(int domID, int varID, const std::string& objID, std::ifstream& defFile);
97 
98 
107  void commandSubscribeObjectVariable(int domID, const std::string& objID, double beginTime, double endTime, int varNo, std::ifstream& defFile);
108 
109 
120  void commandSubscribeContextVariable(int domID, const std::string& objID, double beginTime, double endTime, int domain, double range, int varNo, std::ifstream& defFile);
122 
123 
124 
125 private:
128 
131  void writeResult();
132 
133 
137  void errorMsg(std::stringstream& msg);
139 
140 
141 
144 
150 
151 
158 
159 
160 
163 
171  int setValueTypeDependant(tcpip::Storage& into, std::ifstream& defFile, std::stringstream& msg);
172 
173 
178  void readAndReportTypeDependent(tcpip::Storage& inMsg, int valueDataType);
180 
181 
183  void testAPI();
184 
185  inline std::string joinToString(const std::vector<std::string>& s, const std::string& between) {
186  std::ostringstream oss;
187  bool connect = false;
188  for (const std::string& it : s) {
189  if (connect) {
190  oss << between;
191  } else {
192  connect = true;
193  }
194  oss << it;
195  }
196  return oss.str();
197  }
198 
199  inline std::string joinToString(const std::map<std::string, std::string>& m) {
200  std::ostringstream oss;
201  bool connect = false;
202  for (const auto& it : m) {
203  if (connect) {
204  oss << " ";
205  } else {
206  connect = true;
207  }
208  oss << it.first << ":" << it.second;
209  }
210  return oss.str();
211  }
212 
213 private:
215  std::string outputFileName;
216 
218  std::stringstream answerLog;
219 
220 };
221 
C++ TraCI client API implementation.
Definition: TraCIAPI.h:47
void connect(const std::string &host, int port)
Connects to the specified SUMO server.
Definition: TraCIAPI.cpp:76
A test execution class.
void readAndReportTypeDependent(tcpip::Storage &inMsg, int valueDataType)
Reads a value of the given type from the given storage and reports it.
std::string joinToString(const std::vector< std::string > &s, const std::string &between)
void commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *addData=0)
Sends and validates a GetVariable command.
void testAPI()
call all API methods once
~TraCITestClient()
Destructor.
void commandSetValue(int domID, int varID, const std::string &objID, std::ifstream &defFile)
Sends and validates a SetVariable command.
int run(std::string fileName, int port, std::string host="localhost")
Runs a test.
std::string outputFileName
The name of the file to write the results log into.
void writeResult()
Writes the results file.
int setValueTypeDependant(tcpip::Storage &into, std::ifstream &defFile, std::stringstream &msg)
Parses the next value type / value pair from the stream and inserts it into the storage.
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
void errorMsg(std::stringstream &msg)
Writes an error message.
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP.
TraCITestClient(std::string outputFileName="testclient_result.out")
Constructor.
void commandSetOrder(int order)
Sends and validates a SetOrder command.
std::stringstream answerLog
Stream containing the log.
void commandSubscribeContextVariable(int domID, const std::string &objID, double beginTime, double endTime, int domain, double range, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeContext command.
void commandSubscribeObjectVariable(int domID, const std::string &objID, double beginTime, double endTime, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeVariable command.
void commandClose()
Sends and validates a Close command.
void commandSimulationStep(double time)
Sends and validates a simulation step command.
std::string joinToString(const std::map< std::string, std::string > &m)