From a768cf71405484ce03228ac9ea022d49465452de Mon Sep 17 00:00:00 2001 From: xingjl6280 Date: Tue, 12 Jan 2021 10:32:37 +0800 Subject: [PATCH] Modify AddTwoInts client to wait for service This example is not working properly, as the request gets triggered too early before client fully up. --- .../ros2/rcljava/examples/client/AddTwoIntsClient.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/client/AddTwoIntsClient.java b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/client/AddTwoIntsClient.java index 22cff0a..46bc5c0 100644 --- a/rcljava_examples/src/main/java/org/ros2/rcljava/examples/client/AddTwoIntsClient.java +++ b/rcljava_examples/src/main/java/org/ros2/rcljava/examples/client/AddTwoIntsClient.java @@ -32,6 +32,16 @@ public static void main(final String[] args) throws InterruptedException, Except Client client = node.createClient( example_interfaces.srv.AddTwoInts.class, "add_two_ints"); + + // Wait for client to fully initiliased and find the service + while(!client.waitForService(Duration.ofSeconds(1))) { + if(!RCLJava.ok()) { + System.out.println("client interrupted while waiting for service to appear."); + return; + } + + System.out.println("waiting for service to appear..."); + } example_interfaces.srv.AddTwoInts_Request request = new example_interfaces.srv.AddTwoInts_Request();