Skip to content

Commit

Permalink
Can wait for start
Browse files Browse the repository at this point in the history
  • Loading branch information
SpyrosKou committed Jun 18, 2022
1 parent c1d5b73 commit 7b12f34
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions rosjava/src/main/java/org/ros/node/ServiceClientNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
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
*
* @author Spyros Koukas
*/
public class ServiceClientNode<T extends Message, S extends Message> extends AbstractNodeMain {
private final GraphName graphName;
private CountDownLatch countDownLatch = new CountDownLatch(1);

/**
* Getter for serviceClient
Expand Down Expand Up @@ -60,6 +64,7 @@ public void onStart(final ConnectedNode connectedNode) {
} catch (final ServiceNotFoundException exception) {
throw new RuntimeException(exception);
}
this.countDownLatch.countDown();
}

@Override
Expand All @@ -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) {
}
Expand Down

0 comments on commit 7b12f34

Please sign in to comment.