Working rosjava service server/client example?
Hi,
Is there any working service server/client example code like the AddTwoInts tutorial for rospy and roscpp? I am able to run publisher/subscriber in rosjava, but struggling making a rosjava service server node works.
What I am trying is a simple service server which takes empty request and print something on the screen.
import com.google.common.base.Preconditions;
import org.ros.node.DefaultNodeFactory;
import org.ros.node.Node;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMain;
import org.ros.node.service.ServiceServer;
import org.ros.internal.node.service.ServiceResponseBuilder;
import org.ros.service.std_srvs.Empty;
public class TestServiceServer implements NodeMain {
private static final String SERVICE_NAME = "/test_service";
private static final String SERVICE_TYPE = "std_srvs/Empty";
private Node node;
@Override
public void main(NodeConfiguration configuration) {
Preconditions.checkState(node == null);
Preconditions.checkNotNull(configuration);
try {
System.out.println("Starting a Testing Service Node........");
node = new DefaultNodeFactory().newNode("test_service_server", configuration);
ServiceServer<Empty.Request, Empty.Response> server =
node.newServiceServer(SERVICE_NAME, SERVICE_TYPE,
new ServiceResponseBuilder<Empty.Request, Empty.Response>() {
@Override
public Empty.Response build(Empty.Request request) {
Empty.Response response = new Empty.Response();
System.out.println("called!!");
return response;
}
});
//server.awaitRegistration();
} catch (Exception e) {
if (node != null) {
node.getLog().fatal(e);
} else {
e.printStackTrace();
}
}
@Override
public void shutdown() {
node.shutdown();
node = null;
}
}
When I call the service:
rosservice call /test_service
These error shows up:
Traceback (most recent call last):
File "/opt/ros/electric/ros/bin/rosservice", line 46, in <module>
rosservice.rosservicemain()
File "/opt/ros/electric/stacks/ros_comm/tools/rosservice/src/rosservice.py", line 731, in rosservicemain
_rosservice_cmd_call(argv)
File "/opt/ros/electric/stacks/ros_comm/tools/rosservice/src/rosservice.py", line 586, in _rosservice_cmd_call
service_class = get_service_class_by_name(service_name)
File "/opt/ros/electric/stacks/ros_comm/tools/rosservice/src/rosservice.py", line 357, in get_service_class_by_name
service_type = get_service_type(service_name)
File "/opt/ros/electric/stacks/ros_comm/tools/rosservice/src/rosservice.py", line 141, in get_service_type
return get_service_headers(service_name, service_uri).get('type', None)
File "/opt/ros/electric/stacks/ros_comm/tools/rosservice/src/rosservice.py", line 113, in get_service_headers
return roslib.network.read_ros_handshake_header(s, cStringIO.StringIO(), 2048)
File "/opt/ros/electric/ros/core/roslib/src/roslib/network.py", line 367, in read_ros_handshake_header
raise ROSHandshakeException("connection from sender terminated before handshake header received. %s bytes were received. Please check sender for additional details."%b.tell())
roslib.network.ROSHandshakeException: connection from sender terminated before handshake header received. 0 bytes were received. Please check sender for additional details.
Could someone point what's the problem? Thanks.