From 33b17231d4198eb0df46d07346a180349faf9fa5 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Wed, 15 Jul 2015 12:47:07 -0400 Subject: [PATCH] PyGazebo can now wait until Gazebo starts Added a feature to allow PyGazebo to wait until Gazebo starts up. This is useful if you want to say launch Gazebo and then wait for the network service to become available before proceeding with your PyGazebo code. --- pygazebo/pygazebo.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pygazebo/pygazebo.py b/pygazebo/pygazebo.py index 14a3990..954804e 100755 --- a/pygazebo/pygazebo.py +++ b/pygazebo/pygazebo.py @@ -783,3 +783,46 @@ def connect(address=('127.0.0.1', 11345)): """ manager = Manager(address) return manager.start() + +def wait_for_gazebo(address=('127.0.0.1', 11345), timeout=None): + """ Waits for Gazebo's network service to appear on specified port + @param timeout: in seconds, if None or 0 wait forever + @return: True of False, if timeout is None may return only True or + throw unhandled network exception + """ + import socket + import errno + + server, port = address + + s = socket.socket() + if timeout: + from time import time as now + # time module is needed to calc timeout shared between two exceptions + end = now() + timeout + + while True: + try: + if timeout: + next_timeout = end - now() + if next_timeout < 0: + return False + else: + s.settimeout(next_timeout) + s.connect((server, port)) + except socket.timeout as err: + # this exception occurs only if timeout is set + if timeout: + return False + + except socket.error as err: + # catch timeout exception from underlying network library + # this one is different from socket.timeout + if (type(err.args) != tuple or + (err[0] != errno.ETIMEDOUT and + err[0] != errno.ECONNREFUSED)): + raise err + else: + s.close() + return True +