call.hpp
Go to the documentation of this file.
1 #ifndef REMOTE_ROSBAG_RECORD_CALL
2 #define REMOTE_ROSBAG_RECORD_CALL
3 
4 #include <string>
5 
6 #include <ros/console.h>
7 #include <ros/master.h>
8 #include <ros/service.h>
9 #include <ros/this_node.h>
10 #include <std_srvs/Empty.h>
12 #include <xmlrpcpp/XmlRpcValue.h>
13 
14 #include <boost/regex.hpp>
15 
17 
18 // try calling services matching the given expression
19 // and return number of successfull calls
20 inline static std::size_t call(const boost::regex &expression, const bool verbose = true) {
21  std::size_t n_match(0), n_success(0);
22  try {
23  // call ros master api to get services info
24  XmlRpc::XmlRpcValue args, result, payload;
25  args[0] = ros::this_node::getName();
26  if (!ros::master::execute("getSystemState", args, result, payload,
27  /* wait_for_master = */ false)) {
28  throw XmlRpc::XmlRpcException("getSystemState");
29  }
30 
31  // call services which match the given expression
32  XmlRpc::XmlRpcValue services(payload[2]);
33  for (int i = 0; i < services.size(); ++i) {
34  const std::string name(services[i][0]);
35  if (!boost::regex_match(name, expression)) {
36  continue;
37  }
38  ++n_match;
39 
40  std_srvs::Empty srv;
41  if (!ros::service::call(name, srv)) {
42  ROS_ERROR_STREAM("Failed to call '" << name << "'");
43  continue;
44  }
45  ++n_success;
46 
47  if (verbose) {
48  ROS_INFO_STREAM("Called '" << name << "'");
49  }
50  }
51 
52  if (verbose && n_match == 0) {
53  ROS_WARN_STREAM("No service matched '" << expression << "'");
54  }
55  } catch (const XmlRpc::XmlRpcException &error) {
56  ROS_ERROR_STREAM("Error caught on calling services: " << error.getMessage());
57  }
58  return n_success;
59 }
60 
61 } // namespace remote_rosbag_record
62 
63 #endif
const std::string & getMessage() const
int size() const
bool call(const std::string &service_name, MReq &req, MRes &res)
ROSCPP_DECL const std::string & getName()
static std::size_t call(const boost::regex &expression, const bool verbose=true)
Definition: call.hpp:20
bool verbose
ROSCPP_DECL bool execute(const std::string &method, const XmlRpc::XmlRpcValue &request, XmlRpc::XmlRpcValue &response, XmlRpc::XmlRpcValue &payload, bool wait_for_master)
#define ROS_WARN_STREAM(args)
#define ROS_INFO_STREAM(args)
#define ROS_ERROR_STREAM(args)


remote_rosbag_record
Author(s):
autogenerated on Wed Jan 27 2021 03:18:31