mvsim-cli-topic.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/core/exceptions.h>
11 #include <mvsim/Comms/Client.h>
12 
13 #include "mvsim-cli.h"
14 
15 static int printCommandsTopic(bool showErrorMsg);
16 static int topicList();
17 
18 static const std::map<std::string, cmd_t> cliTopicCommands = {
19  {"list", cmd_t(&topicList)},
20 };
21 
23 {
24  const auto& lstCmds = argCmd.getValue();
25  if (argHelp.isSet()) return printCommandsTopic(false);
26  if (lstCmds.size() != 2) return printCommandsTopic(true);
27 
28  // Take second unlabeled argument:
29  const std::string subcommand = lstCmds.at(1);
30  auto itSubcmd = cliTopicCommands.find(subcommand);
31 
32  if (itSubcmd == cliTopicCommands.end()) return printCommandsTopic(true);
33 
34  // Execute command:
35  return (itSubcmd->second)();
36 
37  return 0;
38 }
39 
40 int topicList()
41 {
43 
44  client.setMinLoggingLevel(
45  mrpt::typemeta::TEnumType<mrpt::system::VerbosityLevel>::name2value(
46  argVerbosity.getValue()));
47 
48  std::cout << "# Connecting to server...\n";
49  client.connect();
50  std::cout << "# Connected.\n";
51  std::cout << "# Querying list of topics to server...\n";
52  const auto lstTopics = client.requestListOfTopics();
53 
54  std::cout << "# Done. Found " << lstTopics.size() << " topics:\n";
55 
56  for (const auto& n : lstTopics)
57  {
58  if (argDetails.isSet())
59  {
60  std::cout << "- name: \"" << n.name << "\"\n"
61  << " type: \"" << n.type << "\"\n"
62  << " publishers:\n";
63 
64  ASSERT_EQUAL_(n.endpoints.size(), n.publishers.size());
65 
66  for (size_t i = 0; i < n.endpoints.size(); i++)
67  {
68  std::cout << " - endpoint: \"" << n.endpoints[i] << "\"\n"
69  << " - publisherNode: \"" << n.publishers[i]
70  << "\"\n";
71  }
72  }
73  else
74  {
75  std::cout << n.name << " [" << n.type << "] from node ";
76  if (n.publishers.size() != 1)
77  std::cout << n.publishers.size() << " publishers.\n";
78  else
79  std::cout << n.publishers.at(0) << "\n";
80  }
81  }
82  return 0;
83 }
84 
85 int printCommandsTopic(bool showErrorMsg)
86 {
87  if (showErrorMsg)
88  {
90  std::cerr << "Error: missing or unknown subcommand.\n";
92  }
93 
94  fprintf(
95  stderr,
96  R"XXX(Usage:
97 
98  mvsim topic --help Show this help
99  mvsim topic list [--details] List all advertised topics in the server.
100 
101 )XXX");
102 
103  return showErrorMsg ? 1 : 0;
104 }
#define ASSERT_EQUAL_(__A, __B)
static const std::map< std::string, cmd_t > cliTopicCommands
void BASE_IMPEXP setConsoleColor(TConsoleColor color, bool changeStdErr=false)
std::function< int(void)> cmd_t
Definition: mvsim-cli.h:23
TCLAP::SwitchArg argHelp("h","help","Shows more detailed help for command", cmd)
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
static int printCommandsTopic(bool showErrorMsg)
GLsizei n
TCLAP::ValueArg< std::string > argVerbosity("v","verbose","Verbosity level", false,"INFO","ERROR|WARN|INFO|DEBUG", cmd)
bool isSet() const
std::vector< InfoPerTopic > requestListOfTopics()
TCLAP::UnlabeledMultiArg< std::string > argCmd("command","Command to run. Run 'mvsim help' to list commands.", false,"", cmd)
TCLAP::SwitchArg argDetails("","details","Shows details in the specified subcommand", cmd)
static int topicList()
int commandTopic()


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51