12#ifndef RTCTK_CLIENT_CLIENTLIB_HPP
13#define RTCTK_CLIENT_CLIENTLIB_HPP
20#include <boost/program_options.hpp>
28boost::program_options::variables_map
ParseOptions(
int argc,
char* argv[]);
33template <
typename CommandRequestorType = rtctk::componentFramework::CommandRequestor>
36 std::is_base_of_v<rtctk::componentFramework::CommandRequestor, CommandRequestorType>,
37 "'CommandRequestorType must extend 'rtctk::componentFramework::CommandRequestor'");
41 using std::chrono::duration;
42 using std::chrono::duration_cast;
43 using std::chrono::milliseconds;
44 using std::chrono::seconds;
51 string cid = args[
"cid"].as<
string>();
52 string cmd = args[
"cmd"].as<
string>();
53 string cmd_arg = args[
"cmd_arg"].as<
string>();
54 Uri sde = args[
"sde"].as<Uri>();
57 std::optional<milliseconds> timeout;
58 if (args.count(
"timeout")) {
59 timeout = duration_cast<milliseconds>(duration<double>(args[
"timeout"].as<double>()));
63 if (cmd ==
"Listen") {
64 Uri subscribe_uri = serv_disc.GetPubSubEndpoint();
65 LOG4CPLUS_DEBUG(logger,
"Subscribe URI is '" << subscribe_uri <<
"'.");
68 [&](
const taiclock::TaiClock::time_point timestamp,
70 const string& state) {
71 LOG4CPLUS_INFO(logger,
72 name <<
" changed state to: " << state);
76 this_thread::sleep_for(seconds(1));
80 Uri request_uri = serv_disc.GetReqRepEndpoint();
81 LOG4CPLUS_DEBUG(logger,
"Request URI is '" << request_uri <<
"'.");
83 CommandRequestorType requestor(request_uri, timeout);
84 auto reply = requestor.SendCommandSync(cmd, cmd_arg);
85 cout << reply << endl;
87 }
catch (
const elt::mal::TimeoutException& e) {
88 LOG4CPLUS_ERROR(logger,
"Request timed out.");
90 }
catch (
const std::exception& e) {
91 LOG4CPLUS_FATAL(logger, e.what());
94 LOG4CPLUS_FATAL(logger,
"Unknown exception");
101boost::program_options::variables_map
ParseOptions(
int argc,
char* argv[]) {
103 namespace bpo = boost::program_options;
108 bpo::positional_options_description pos_opts_desc;
109 pos_opts_desc.add(
"cid", 1);
110 pos_opts_desc.add(
"cmd", 1);
111 pos_opts_desc.add(
"cmd_arg", -1);
113 bpo::options_description opts_desc(
"Options");
115 opts_desc.add_options()
116 (
"help,h",
"Print help messages")
117 (
"timeout,t", bpo::value<double>()->default_value(30.0),
"timeout [sec]")
118 (
"cid,i", bpo::value<string>(),
"component identity")
119 (
"cmd,c", bpo::value<string>(),
"command name")
120 (
"cmd_arg,a", bpo::value<string>()->default_value(
""),
"command arguments")
121 (
"sde,s", bpo::value<Uri>()->default_value(Uri(
"consul://localhost:8500")),
122 "service discovery endpoint");
126 bpo::variables_map vm;
129 bpo::command_line_parser(argc, argv).options(opts_desc).positional(pos_opts_desc).run(),
134 if (vm.count(
"help")) {
135 cout << opts_desc << endl;
138 if (vm.count(
"cid")) {
139 LOG4CPLUS_DEBUG(logger,
"Component identity is '" << vm[
"cid"].as<string>() <<
"'.");
141 LOG4CPLUS_FATAL(logger,
"Component identity not specified !");
144 if (vm.count(
"cmd")) {
145 LOG4CPLUS_DEBUG(logger,
"Command is '" << vm[
"cmd"].as<string>() <<
"'.");
147 LOG4CPLUS_FATAL(logger,
"Command name not specified !");
150 if (vm.count(
"cmd_arg")) {
151 LOG4CPLUS_DEBUG(logger,
"Command Arg is '" << vm[
"cmd_arg"].as<string>() <<
"'.");
153 if (vm.count(
"sde")) {
154 LOG4CPLUS_DEBUG(logger,
155 "Service Discovery Endpoint is '" << vm[
"sde"].as<Uri>() <<
"'.");
157 LOG4CPLUS_FATAL(logger,
"Service Discovery Endpoint not specified !");
160 if (vm.count(
"timeout")) {
161 LOG4CPLUS_DEBUG(logger,
162 "Request reply timeout is " << vm[
"timeout"].as<double>() <<
"s.");
167 }
catch (bpo::error& e) {
168 LOG4CPLUS_FATAL(logger, e.what() << endl << endl);
169 LOG4CPLUS_INFO(logger, opts_desc << endl);
RAII class to clean-up logging without leaking memory.
Definition logger.hpp:27
Class that implements a very basic service discovery mechanism.
Definition serviceDiscovery.hpp:32
Class used to subscribe to state-changed-topic using MAL.
Definition stateSubscriber.hpp:40
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
int RunClient(int argc, char **argv)
Definition clientLib.hpp:34
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
void LogConfigureTool(const std::string &app_name, log4cplus::LogLevel default_log_level=log4cplus::ERROR_LOG_LEVEL, bool log_to_file=false)
Performs default logging configuration for Client Applications and Standalone Tools.
Definition logger.cpp:144
Logging Support Library based on log4cplus.
Definition clientLib.hpp:26
boost::program_options::variables_map ParseOptions(int argc, char *argv[])
Definition clientLib.hpp:101
Definition commandReplier.cpp:21
Definition ddsSub.hpp:155
Class that implements a very basic service discover mechanism.
Subscribes to stdif state topic via MAL.