From 6e967a1d75141c649dfe8cc1743dea43256f1930 Mon Sep 17 00:00:00 2001
From: Ernesto Corbellini <ecorbellini@ekumenlabs.com>
Date: Fri, 22 Jan 2016 17:39:21 -0300
Subject: [PATCH] Added timeout to the wait for server.

---
 .../rosjava_actionlib/ActionClient.java       | 29 +++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ActionClient.java b/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ActionClient.java
index 50c43ea..00861f8 100644
--- a/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ActionClient.java
+++ b/src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ActionClient.java
@@ -25,6 +25,8 @@ import org.ros.node.topic.SubscriberListener;
 import org.ros.internal.node.topic.PublisherIdentifier;
 import org.ros.node.topic.DefaultSubscriberListener;
 import org.ros.message.MessageListener;
+import org.ros.message.Duration;
+import org.ros.message.Time;
 import org.ros.internal.message.Message;
 import java.util.concurrent.TimeUnit;
 import java.util.List;
@@ -332,20 +334,37 @@ public class ActionClient<T_ACTION_GOAL extends Message,
 
   /**
    * Wait for an actionlib server to connect.
+   * @param timeout The maximum amount of time to wait for an action server. If
+   * this value is less than or equal to zero, it will wait forever until a
+   * server is detected.
+   * @return True if the action server was detected before the timeout and
+   * false otherwise.
    */
-  public boolean waitForActionServerToStart() {
+  public boolean waitForActionServerToStart(Duration timeout) {
     boolean res = false;
+    boolean gotTime = true;
+    Time finalTime = node.getCurrentTime().add(timeout);
 
-    while (!res) {
+    while (!res && gotTime) {
       res = goalPublisher.hasSubscribers() &&
         cancelPublisher.hasSubscribers() &&
         feedbackPublisherFlag &&
         resultPublisherFlag &&
         statusReceivedFlag;
+      if (timeout.isPositive()) {
+        gotTime = (node.getCurrentTime().compareTo(finalTime) < 0);
+      }
     }
     return res;
   }
 
+  /**
+   * Wait indefinately until an actionlib server is connected.
+   */
+  public void waitForActionServerToStart() {
+    waitForActionServerToStart(new Duration(0));
+  }
+
   @Override
   public void onNewPublisher(Subscriber subscriber, PublisherIdentifier publisherIdentifier) {
   //public void onNewFeedbackPublisher(Subscriber<T_ACTION_FEEDBACK> subscriber, PublisherIdentifier publisherIdentifier) {
@@ -360,9 +379,15 @@ public class ActionClient<T_ACTION_GOAL extends Message,
     }
   }
 
+  /**
+   * Get the current state of the action goal as being tracked by the client.
+   * @return The state of the goal.
+   * @see ClientStateMachine.ClientStates
+   */
   public int getGoalState() {
     return goalManager.getGoalState();
   }
+
   /**
    * Finish the action client. Unregister publishers and listeners.
    */
-- 
GitLab