commit 6304bfc15f786f2d27a56b750edf6ad7926bebd7
Author: Robert Haschke <rhaschke@techfak.uni-bielefeld.de>
Date:   Sat Jun 4 11:15:09 2016 +0200

    python client: timeout for ROS connection

diff --git a/client/python/hlrc_client/MiddlewareROS.py b/client/python/hlrc_client/MiddlewareROS.py
index e8c7de7..a8a8377 100644
--- a/client/python/hlrc_client/MiddlewareROS.py
+++ b/client/python/hlrc_client/MiddlewareROS.py
@@ -26,12 +26,10 @@ Excellence Initiative.
 """
 
 from Middleware import *
-import errno
 
 import rospy
 import actionlib
 from hlrc_server.msg import *
-from geometry_msgs.msg import PointStamped
 from actionlib_msgs.msg import GoalStatus
 
 class MiddlewareROS(Middleware):
@@ -39,14 +37,15 @@ class MiddlewareROS(Middleware):
     ROS_ACTION_CALL_TIMEOUT = 30.0
 
     #######################################################################
-    def __init__(self, scope, loglevel=logging.WARNING):
+    def __init__(self, scope, loglevel=logging.WARNING, timeout=None):
         """initialise
         :param scope: base scope we want to listen on
         """
         #init base settings
         Middleware.__init__(self,scope,loglevel)
-        #call mw init
-        self.init_middleware()
+        #ros-specific initialization
+        if timeout is None: timeout = rospy.Duration()
+        self.init_middleware(timeout)
 
     def __del__(self):
         """destructor
@@ -54,7 +53,7 @@ class MiddlewareROS(Middleware):
         self.logger.debug("destructor of MiddlewareROS called")
 
     #######################################################################
-    def init_middleware(self):
+    def init_middleware(self, timeout):
         """initialise middleware
         """
         self.logger.info("initialising ROS middleware connection on scope %s" % (self.base_scope))
@@ -62,37 +61,33 @@ class MiddlewareROS(Middleware):
         try:
             rospy.init_node('hlrc_client', anonymous=True)
         except rospy.exceptions.ROSException as e:
-            print e
+            self.logger.info(str(e))
 
         self.logger.info("creating action clients")
 
-        self.logger.debug("creating speech action client")
-        self.speech_client = actionlib.SimpleActionClient(self.base_scope + "/set/speech", speechAction)
-        self.speech_client.wait_for_server()
-
-        self.logger.debug("creating default emotion action client")
-        self.default_emotion_client = actionlib.SimpleActionClient(self.base_scope + "/set/defaultEmotion", emotionstateAction)
-        self.default_emotion_client.wait_for_server()
-
-        self.logger.debug("creating current emotion action client")
-        self.current_emotion_client = actionlib.SimpleActionClient(self.base_scope + "/set/currentEmotion", emotionstateAction)
-        self.current_emotion_client.wait_for_server()
-
-        self.logger.debug("creating animation action client")
-        self.animation_client = actionlib.SimpleActionClient(self.base_scope + "/set/animation", animationAction)
-        self.animation_client.wait_for_server()
-
-        self.logger.debug("creating gazetarget action client")
-        self.gazetarget_client = actionlib.SimpleActionClient(self.base_scope + "/set/gaze", gazetargetAction)
-        self.gazetarget_client.wait_for_server()
-
-        self.logger.debug("creating lookattarget action client")
-        self.lookattarget_client = actionlib.SimpleActionClient(self.base_scope + "/set/lookat", lookattargetAction)
-        self.lookattarget_client.wait_for_server()
-
-        self.logger.debug("creating mouthtarget action client")
-        self.mouthtarget_client = actionlib.SimpleActionClient(self.base_scope + "/set/mouth", mouthtargetAction)
-        self.mouthtarget_client.wait_for_server()
+        start = time.time()
+        def create_client(sub_scope, action, name):
+            if not timeout.is_zero():
+                t = rospy.Duration.from_sec(timeout.to_sec() - (time.time() - start)) # time left
+                if t <= rospy.Duration():
+                    raise Exception("timeout while connecting to actionlib servers")
+            else:
+                t = timeout
+
+            self.logger.debug("creating %s client" % name)
+
+            client = actionlib.SimpleActionClient(self.base_scope + sub_scope, action)
+            if not client.wait_for_server(t):
+                raise Exception("timeout while creating %s client" % name)
+            return client
+
+        self.speech_client = create_client("/set/speech", speechAction, "speech action")
+        self.default_emotion_client = create_client("/set/defaultEmotion", emotionstateAction, "default emotion")
+        self.current_emotion_client = create_client("/set/currentEmotion", emotionstateAction, "current emotion")
+        self.animation_client = create_client("/set/animation", animationAction, "animation")
+        self.gazetarget_client = create_client("/set/gaze", gazetargetAction, "gazetarget")
+        self.lookattarget_client = create_client("/set/lookat", lookattargetAction, "lookattarget")
+        self.mouthtarget_client = create_client("/set/mouth", mouthtargetAction, "mouthtarget")
 
         self.logger.info("MiddlewareROS initialised")
 
