This is the situation:
Node A has a service client which calls a service server defined in node B for all the time.
In the meanwhile, node B also has a service client which calls a service server defined in node A for all the time.
In other words, there are a service client and server in node A, and there are also a service server and client in node B. And they call each other all the time.
Now comes the problem, the program hangs there and no call is successful. It seems there is a deadlock. Any one could give me a detailed explanation? And any one could provide me with a solution? Thanks!
Node A:
#include "ros/ros.h"
#include "roscpp_tutorials/TwoInts.h"
#include <cstdlib>
bool test(roscpp_tutorials::TwoInts::Request &req,
roscpp_tutorials::TwoInts::Response &res )
{
res.sum = req.a + req.b;
ROS_INFO("test : x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO(" test sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_client");
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<roscpp_tutorials::TwoInts>("add_two_ints");
ros::ServiceServer server = n.advertiseService("test", test);
ros::Rate rate(10);
roscpp_tutorials::TwoInts srv;
srv.request.a = 1;
srv.request.b = 2;
while(ros::ok())
{
if (client.call(srv))
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
}
ros::spinOnce();
rate.sleep();
}
return 0;
}
Node B:
#include "ros/ros.h"
#include "roscpp_tutorials/TwoInts.h"
bool add(roscpp_tutorials::TwoInts::Request &req,
roscpp_tutorials::TwoInts::Response &res )
{
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO(" sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_server");
ros::NodeHandle n;
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
ros::ServiceClient client = n.serviceClient<roscpp_tutorials::TwoInts>("test");
ros::Rate rate(10);
roscpp_tutorials::TwoInts srv;
srv.request.a = 1;
srv.request.b = 2;
while(ros::ok())
{
if (client.call(srv))
{
ROS_INFO("test: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service test");
}
ros::spinOnce();
rate.sleep();
}
//ros::spin();
return 0;
}
Originally posted by Winston on ROS Answers with karma: 180 on 2016-08-28
Post score: 0