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.dev/sumo
3 // Copyright (C) 2001-2024 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 <config.h>
25 #include <string>
26 #include <sstream>
27 #include <vector>
28 
29 #include <foreign/tcpip/socket.h>
30 #include <utils/traci/TraCIAPI.h>
31 
32 
33 // ===========================================================================
34 // class definitions
35 // ===========================================================================
42 class TraCITestClient : public TraCIAPI {
43 public:
47  TraCITestClient(std::string outputFileName = "testclient_result.out");
48 
49 
52 
53 
59  int run(std::string fileName, int port, std::string host = "localhost");
60 
61 
62 protected:
65 
69  void commandSimulationStep(double time);
70 
71 
74  void commandClose();
75 
76 
79  void commandSetOrder(int order);
80 
81 
88  void commandGetVariable(int domID, int varID, const std::string& objID, tcpip::Storage* addData = 0);
89 
90 
97  void commandSetValue(int domID, int varID, const std::string& objID, std::ifstream& defFile);
98 
99 
108  void commandSubscribeObjectVariable(int domID, const std::string& objID, double beginTime, double endTime, int varNo, std::ifstream& defFile);
109 
110 
121  void commandSubscribeContextVariable(int domID, const std::string& objID, double beginTime, double endTime, int domain, double range, int varNo, std::ifstream& defFile);
123 
124 
125 
126 private:
129 
132  void writeResult();
133 
134 
138  void errorMsg(std::stringstream& msg);
140 
141 
142 
145 
151 
152 
159 
160 
161 
164 
172  int setValueTypeDependant(tcpip::Storage& into, std::ifstream& defFile, std::stringstream& msg);
173 
174 
179  void readAndReportTypeDependent(tcpip::Storage& inMsg, int valueDataType);
181 
182 
184  void testAPI();
185 
186  inline std::string joinToString(const std::vector<std::string>& s, const std::string& between) {
187  std::ostringstream oss;
188  bool connect = false;
189  for (const std::string& it : s) {
190  if (connect) {
191  oss << between;
192  } else {
193  connect = true;
194  }
195  oss << it;
196  }
197  return oss.str();
198  }
199 
200  inline std::string joinToString(const std::map<std::string, std::string>& m) {
201  std::ostringstream oss;
202  bool connect = false;
203  for (const auto& it : m) {
204  if (connect) {
205  oss << " ";
206  } else {
207  connect = true;
208  }
209  oss << it.first << ":" << it.second;
210  }
211  return oss.str();
212  }
213 
214  inline std::string joinToString(const std::vector<double>& v, const std::string& between) {
215  std::ostringstream oss;
216  bool connect = false;
217  for (double it : v) {
218  if (connect) {
219  oss << between;
220  } else {
221  connect = true;
222  }
223  oss << it;
224  }
225  return oss.str();
226  }
227 
228 private:
230  std::string outputFileName;
231 
233  std::stringstream answerLog;
234 
235 };
C++ TraCI client API implementation.
Definition: TraCIAPI.h:48
void connect(const std::string &host, int port)
Connects to the specified SUMO server.
Definition: TraCIAPI.cpp:68
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)
std::string joinToString(const std::vector< double > &v, 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)