diff --git a/rosjava/src/main/java/org/ros/node/ServiceClientNode.java b/rosjava/src/main/java/org/ros/node/ServiceClientNode.java index 708680a7..1eace99b 100644 --- a/rosjava/src/main/java/org/ros/node/ServiceClientNode.java +++ b/rosjava/src/main/java/org/ros/node/ServiceClientNode.java @@ -23,6 +23,9 @@ import org.ros.namespace.GraphName; import org.ros.node.service.ServiceClient; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + /** * A {@link NodeMain} which provides a service client * @@ -30,6 +33,7 @@ */ public class ServiceClientNode extends AbstractNodeMain { private final GraphName graphName; + private CountDownLatch countDownLatch = new CountDownLatch(1); /** * Getter for serviceClient @@ -60,6 +64,7 @@ public void onStart(final ConnectedNode connectedNode) { } catch (final ServiceNotFoundException exception) { throw new RuntimeException(exception); } + this.countDownLatch.countDown(); } @Override @@ -73,6 +78,35 @@ public void onShutdown(Node node) { } } + /** + * Awaits for the Node to start. + * The thread however can be interrupted or it can return if the Node has shutdown. + * + * @return true if the Node is started and the service client is connected + * + * @throws InterruptedException + */ + public boolean awaitConnection() throws InterruptedException { + this.countDownLatch.await(); + return this.serviceClient != null && this.serviceClient.isConnected(); + } + + /** + * Awaits for the Node to start. + * The thread however can be interrupted or it can return if the Node has shutdown. + * + * @param time + * @param unit + * + * @return true if the Node is started and the service client is connected + * + * @throws InterruptedException + */ + public boolean awaitConnection(final long time, final TimeUnit unit) throws InterruptedException { + return this.countDownLatch.await(time, unit) && + this.serviceClient != null && this.serviceClient.isConnected(); + } + @Override public void onShutdownComplete(Node node) { }