0
$\begingroup$

Rosanswers logo

I'm writing a library where I'd like to be able to get the number of publishers and subscribers on a topic, without subscribing to that topic.

Are there any functions in roscpp which can make this query directly, or do I need to make the xmlrpc calls and interpret the results myself?


Originally posted by ahendrix on ROS Answers with karma: 47576 on 2015-01-22

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I ended up doing this by calling the master API directly.

std::string node_name = ros::this_node::getName();
XmlRpc::XmlRpcValue request(node_name);
XmlRpc::XmlRpcValue response;
XmlRpc::XmlRpcValue payload;
bool success = ros::master::execute("getSystemState", request,
    response, payload, false);

ROS_ASSERT(payload.getType() == XmlRpc::XmlRpcValue::TypeArray);
ROS_ASSERT(payload.size() == 3);

bool published = false;
bool subscribed = false;

XmlRpc::XmlRpcValue & publishers = payload[0];
ROS_ASSERT(publishers.getType() == XmlRpc::XmlRpcValue::TypeArray);
for(int i=0; i<publishers.size(); i++) {
  XmlRpc::XmlRpcValue & topic = publishers[i];
  ROS_ASSERT(topic.getType() == XmlRpc::XmlRpcValue::TypeArray);
  ROS_ASSERT(topic[0].getType() == XmlRpc::XmlRpcValue::TypeString);
  std::string topic_name = topic[0];
  if(topic_name == response_topic_) {
    published = true;
  }
}

XmlRpc::XmlRpcValue & subscribers = payload[1];
ROS_ASSERT(subscribers.getType() == XmlRpc::XmlRpcValue::TypeArray);
for(int i=0; i<subscribers.size(); i++) {
  XmlRpc::XmlRpcValue & topic = subscribers[i];
  ROS_ASSERT(topic.getType() == XmlRpc::XmlRpcValue::TypeArray);
  ROS_ASSERT(topic[0].getType() == XmlRpc::XmlRpcValue::TypeString);
  std::string topic_name = topic[0];
  if(topic_name == request_topic_) {
    subscribed = true;
  }
}

Originally posted by ahendrix with karma: 47576 on 2015-01-26

This answer was ACCEPTED on the original site

Post score: 4

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.