12 #ifndef RTCTK_CLIENT_CLIENTLIB_HPP
13 #define RTCTK_CLIENT_CLIENTLIB_HPP
20 #include <boost/program_options.hpp>
28 boost::program_options::variables_map
ParseOptions(
int argc,
char *argv[]);
30 template<
typename CommandRequestorType = rtctk::componentFramework::CommandRequestor>
33 static_assert(std::is_base_of_v<rtctk::componentFramework::CommandRequestor, CommandRequestorType>,
34 "'CommandRequestorType must extend 'rtctk::componentFramework::CommandRequestor'");
38 using std::chrono::duration;
39 using std::chrono::duration_cast;
40 using std::chrono::milliseconds;
41 using std::chrono::seconds;
45 MakeLogger(
"client", log4cplus::INFO_LOG_LEVEL,
false,
false);
49 string cid = args[
"cid"].as<
string>();
50 string cmd = args[
"cmd"].as<
string>();
51 string cmd_arg = args[
"cmd_arg"].as<
string>();
52 Uri sde = args[
"sde"].as<Uri>();
55 std::optional<milliseconds> timeout;
56 if (args.count(
"timeout")) {
57 timeout = duration_cast<milliseconds>(duration<double>(args[
"timeout"].as<double>()));
61 if (cmd ==
"Listen") {
63 Uri subscribe_uri = serv_disc.GetPubSubEndpoint();
64 LOG4CPLUS_DEBUG(
GetLogger(),
"Subscribe URI is '" << subscribe_uri <<
"'.");
67 [](
const double timestamp,
const string&
name,
const string&
state) {
71 while(
true) { this_thread::sleep_for(seconds(1)); }
75 Uri request_uri = serv_disc.GetReqRepEndpoint();
76 LOG4CPLUS_DEBUG(
GetLogger(),
"Request URI is '" << request_uri <<
"'.");
78 CommandRequestorType requestor(request_uri, timeout);
79 auto reply = requestor.SendCommandSync(cmd, cmd_arg);
80 cout << reply << endl;
82 }
catch (elt::mal::TimeoutException
const& e) {
83 LOG4CPLUS_ERROR(
GetLogger(),
"Request timed out.");
85 }
catch (std::exception
const& e) {
89 LOG4CPLUS_FATAL(
GetLogger(),
"Unknown exception");
96 boost::program_options::variables_map
ParseOptions(
int argc,
char *argv[])
99 namespace bpo = boost::program_options;
103 bpo::positional_options_description pos_opts_desc;
104 pos_opts_desc.add(
"cid", 1);
105 pos_opts_desc.add(
"cmd", 1);
106 pos_opts_desc.add(
"cmd_arg", -1);
108 bpo::options_description opts_desc(
"Options");
109 opts_desc.add_options()
110 (
"help,h",
"Print help messages")
111 (
"timeout,t", bpo::value<double>()->default_value(30.0),
"timeout [sec]")
112 (
"cid,i", bpo::value<string>(),
"component identity")
113 (
"cmd,c", bpo::value<string>(),
"command name")
114 (
"cmd_arg,a", bpo::value<string>()->default_value(
""),
"command arguments")
115 (
"sde,s", bpo::value<Uri>(),
"service discovery endpoint");
119 bpo::variables_map vm;
122 bpo::command_line_parser(argc, argv)
124 .positional(pos_opts_desc)
129 if (vm.count(
"help")) {
130 cout << opts_desc << endl;
133 if (vm.count(
"cid")) {
134 LOG4CPLUS_DEBUG(
GetLogger(),
"Component identity is '" << vm[
"cid"].as<string>() <<
"'.");
136 LOG4CPLUS_FATAL(
GetLogger(),
"Component identity not specified !");
139 if (vm.count(
"cmd")) {
140 LOG4CPLUS_DEBUG(
GetLogger(),
"Command is '" << vm[
"cmd"].as<string>() <<
"'.");
142 LOG4CPLUS_FATAL(
GetLogger(),
"Command name not specified !");
145 if (vm.count(
"cmd_arg")) {
146 LOG4CPLUS_DEBUG(
GetLogger(),
"Command Arg is '" << vm[
"cmd_arg"].as<string>() <<
"'.");
148 if (vm.count(
"sde")) {
149 LOG4CPLUS_DEBUG(
GetLogger(),
"Service Discovery Endpoint is '" << vm[
"sde"].as<Uri>() <<
"'.");
151 LOG4CPLUS_FATAL(
GetLogger(),
"Service Discovery Endpoint not specified !");
154 if (vm.count(
"timeout")) {
155 LOG4CPLUS_DEBUG(
GetLogger(),
"Request reply timeout is " << vm[
"timeout"].as<double>()
161 }
catch (bpo::error& e) {
162 LOG4CPLUS_FATAL(
GetLogger(), e.what() << endl << endl );
163 LOG4CPLUS_INFO(
GetLogger(), opts_desc << endl );