Persistent Service initialization
I am changing a ROS service to be persistent. I experiment with checking the connection, but the initialization puzzles me. Do I really have to initialize the client twice like this:
ros::ServiceClient client;
// I need to create a client before I can check for the servers existence
client = m_nh.serviceClient<SomeServiceType>("ServiceName", true);
client.waitForExistence();
// if the server came up during the "waitForExistence", the client is not valid at this point:
ROS_WARN_COND(!client.isValid(), "Client not valid");
// so I have to create it again!!!
client = m_nh.serviceClient<SomeServiceType>("ServiceName", true);
Is this really the right way?
no. That should not be needed.
What are you trying to do? And what gave you the impression that this was needed?
I conclude this based on the
isValid
check. It returns false right afterwaitForInstance()
. I ended up with this example because AFAIK, when using a persistent service connection, I have to check the validity of the client connection before usingclient.call(...)
.Ok. In that case: yes, you do need to do that:
(or some variant thereof).
As with all remoting, there's going to be opportunity for race conditions, sadly.
The waitForService that you pointed out in your now deleted answer seems to be the answer for this question.
I'm not sure. If you use
waitForService(..)
, it'll tell you whether the service exists.client.isValid()
tells you whether the client instance you created is valid. Unless validity is determined only by checking whether the service exists, those would seem to not be necessarily equivalent.