diff options
Diffstat (limited to 'staslib')
-rw-r--r-- | staslib/avahi.py | 50 | ||||
-rw-r--r-- | staslib/gutil.py | 20 |
2 files changed, 60 insertions, 10 deletions
diff --git a/staslib/avahi.py b/staslib/avahi.py index 84a0b2a..f91e489 100644 --- a/staslib/avahi.py +++ b/staslib/avahi.py @@ -71,6 +71,29 @@ def fmt_service_str(interface, protocol, name, stype, domain, flags): # pylint: ) +class ValueRange: + '''Implement a range of values with ceiling. Once the ceiling has been + reached, then any further request for a new value will return the + ceiling (i.e last value).''' + + def __init__(self, values: list): + self._values = values + self._index = 0 + + def get_next(self): + '''Get the next value (or last value if ceiling was reached)''' + value = self._values[self._index] + if self._index >= 0: + self._index += 1 + if self._index >= len(self._values): + self._index = -1 + return value + + def reset(self): + '''Reset the range to start from the beginning''' + self._index = 0 + + # ****************************************************************************** class Service: # pylint: disable=too-many-instance-attributes '''Object used to keep track of the services discovered from the avahi-daemon''' @@ -109,6 +132,11 @@ class Service: # pylint: disable=too-many-instance-attributes self._interface_id, self._protocol_id, self._name, self._stype, self._domain, self._flags ) + self._connect_check_retry_tmo = ValueRange([2, 5, 10, 30, 60, 300, 600]) + self._connect_check_retry_tmr = gutil.GTimer( + self._connect_check_retry_tmo.get_next(), self._on_connect_check_retry + ) + self._ip = None self._resolver = None self._data = {} @@ -151,8 +179,17 @@ class Service: # pylint: disable=too-many-instance-attributes self._identified_cback() return + self._connect_check(verbose=True) # Enable verbosity on first attempt + + def _connect_check(self, verbose=False): self._reachable = False - connect_checker = gutil.TcpChecker(traddr, trsvcid, host_iface, self._tcp_connect_check_cback) + connect_checker = gutil.TcpChecker( + self._data['traddr'], + self._data['trsvcid'], + self._data['host-iface'], + verbose, + self._tcp_connect_check_cback, + ) try: connect_checker.connect() @@ -168,7 +205,16 @@ class Service: # pylint: disable=too-many-instance-attributes self._connect_checker.close() self._connect_checker = None self._reachable = connected - self._identified_cback() + + if self._reachable: + self._identified_cback() + else: + # Restart the timer but with incremented timeout + self._connect_check_retry_tmr.start(self._connect_check_retry_tmo.get_next()) + + def _on_connect_check_retry(self): + self._connect_check() + return GLib.SOURCE_REMOVE def set_resolver(self, resolver): '''Set the resolver object''' diff --git a/staslib/gutil.py b/staslib/gutil.py index 9aef347..7bdc117 100644 --- a/staslib/gutil.py +++ b/staslib/gutil.py @@ -423,7 +423,9 @@ class Deferred: class TcpChecker: # pylint: disable=too-many-instance-attributes '''@brief Verify that a TCP connection can be established with an enpoint''' - def __init__(self, traddr, trsvcid, host_iface, user_cback, *user_data): + def __init__( + self, traddr, trsvcid, host_iface, verbose, user_cback, *user_data + ): # pylint: disable=too-many-arguments self._user_cback = user_cback self._host_iface = host_iface self._user_data = user_data @@ -432,6 +434,7 @@ class TcpChecker: # pylint: disable=too-many-instance-attributes self._cancellable = None self._gio_sock = None self._native_sock = None + self._verbose = verbose def connect(self): '''Attempt to connect''' @@ -502,13 +505,14 @@ class TcpChecker: # pylint: disable=too-many-instance-attributes if err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.CANCELLED): logging.debug('TcpChecker._connect_async_cback() - %s', err.message) # pylint: disable=no-member else: - logging.info( - 'Unable to verify TCP connectivity - (%-10s %-14s %s): %s', - self._host_iface + ',', - self._traddr.compressed + ',', - self._trsvcid, - err.message, # pylint: disable=no-member - ) + if self._verbose: + logging.info( + 'Unable to verify TCP connectivity - (%-10s %-14s %s): %s', + self._host_iface + ',', + self._traddr.compressed + ',', + self._trsvcid, + err.message, # pylint: disable=no-member + ) self.close() |