ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

Call getPid to obtain node pid via XMLRPC in C++

asked 2019-02-14 16:50:46 -0500

timwheeler gravatar image

I am trying to get the process IDs for all ROS nodes via the getPid ROS Slave API. I have been able to run the following:

XmlRpc::XmlRpcValue args, result, payload;
args[0] = "name of some node";
ros::master::execute("getPid", args, result, payload, true);

Unfortunately, this snippet appears to return the pid of rosmaster, and is independent of the args value I pass in.

I tried creating my own XmlRpc::XmlRpcClient instance with master host and port but different URIs. I cannot figure out how to get access to node URIs. If I do get them, I am not convinced that my approach works

std::string method = "getPid";
XmlRpc::XmlRpcClient* c = ros::XMLRPCManager::instance()->getXMLRPCClient(master_host, master_port, uri);
c->execute(method.c_str(), request, response);

Has anyone been successful here in the past? I found the following Python approach, but would very much like to do it in C++. I am using kinetic.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-02-15 04:55:16 -0500

gvdhoorn gravatar image

updated 2019-03-04 10:06:29 -0500

I would think the result you receive from the master makes total sense right now, as you're asking it (ie: the master) to return its pid.

Haven't done this myself in C++, but I would imagine the control flow being similar to Python:

  1. retrieve all node names from the master
  2. retrieve URIs for all returned names
  3. construct clients for all URIs
  4. invoke getPid on all clients

Edit:

"I cannot figure out how to get access to node URIs" - do you know how to do step (2) of what you suggest?

You can use lookupNode in the Master API.

edit flag offensive delete link more

Comments

"I cannot figure out how to get access to node URIs" - do you know how to do step (2) of what you suggest?

timwheeler gravatar image timwheeler  ( 2019-02-15 11:07:59 -0500 )edit

Perfect, thank you.

timwheeler gravatar image timwheeler  ( 2019-02-19 10:50:27 -0500 )edit
1

Could someone please write working template of this working? I get the URI, but the "getPID" call fails:

// Get URI of the node
XmlRpc::XmlRpcValue args, result, payload;
args.setSize(2);
args[0] = "caller_name";
args[1] = "node_name";
ros::master::execute("lookupNode", args, result, payload, true);

// Make new client of node
std::string uri = result[2];
XmlRpc::XmlRpcClient* client =
ros::XMLRPCManager::instance()->getXMLRPCClient(ros::master::getHost(), ros::master::getPort(), uri.c_str());

// Get PID of the node
XmlRpc::XmlRpcValue request, response;
request.setSize(1);
request[0] = "node_name";
client->execute("getPid", request, response);

The client execute call (last line) always returns false, am I missing something here?

marinamrr gravatar image marinamrr  ( 2020-09-28 11:59:11 -0500 )edit

Be sure to close the client created by the XmlRpc::XmlRpcClient, since it opens a socket for every client created. In case you run this in loop, you could get "Too many files opened" error or "Error:24" especially in linux. Since for each client a socket is created and sockets are defined as file descriptors in linux. This will be a memory leak. So always close an opened client after processing. Releasing the client connection is recommended.

ros::XMLRPCManager::instance()->releaseXMLRPCClient(xclient);
aravindsaiUR gravatar image aravindsaiUR  ( 2021-05-19 06:28:02 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2019-02-14 16:48:58 -0500

Seen: 641 times

Last updated: Mar 04 '19