Eclipse SUMO - Simulation of Urban MObility
tracitestclient_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Main method for TraCITestClient
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <iostream>
28 #include <string>
29 #include <cstdlib>
30 #include "TraCITestClient.h"
31 
32 
33 // ===========================================================================
34 // method definitions
35 // ===========================================================================
36 int main(int argc, char* argv[]) {
37  std::string defFile = "";
38  std::string outFileName = "testclient_out.txt";
39  int port = -1;
40  std::string host = "localhost";
41 
42  if ((argc == 1) || (argc % 2 == 0)) {
43  std::cout << "Usage: TraCITestClient -def <definition_file> -p <remote port>"
44  << "[-h <remote host>] [-o <outputfile name>]" << std::endl;
45  return 0;
46  }
47 
48  for (int i = 1; i < argc; i++) {
49  std::string arg = argv[i];
50  if (arg.compare("-def") == 0) {
51  defFile = argv[i + 1];
52  i++;
53  } else if (arg.compare("-o") == 0) {
54  outFileName = argv[i + 1];
55  i++;
56  } else if (arg.compare("-p") == 0) {
57  port = atoi(argv[i + 1]);
58  i++;
59  } else if (arg.compare("-h") == 0) {
60  host = argv[i + 1];
61  i++;
62  } else {
63  std::cerr << "unknown parameter: " << argv[i] << std::endl;
64  return 1;
65  }
66  }
67 
68  if (port == -1) {
69  std::cerr << "Missing port" << std::endl;
70  return 1;
71  }
72  if (defFile.compare("") == 0) {
73  std::cerr << "Missing definition file" << std::endl;
74  return 1;
75  }
76 
77  try {
78  TraCITestClient client(outFileName);
79  return client.run(defFile, port, host);
80  } catch (tcpip::SocketException& e) {
81  std::cerr << "Socket error running the test client: " << e.what();
82  return 1;
83  } catch (libsumo::TraCIException& e) {
84  std::cerr << "TraCI error running the test client: " << e.what();
85  return 1;
86  }
87 }
int run(std::string fileName, int port, std::string host="localhost")
Runs a test.
A test execution class.
int main(int argc, char *argv[])