From 183ce16eca46bec56916a78ef3dc0c79cd3bb1d4 Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Sun, 14 Nov 2021 19:57:35 +0530 Subject: [PATCH] added timeout This doesn't work yet --- naxalnet/__init__.py | 2 +- naxalnet/network.py | 12 ++++++++++-- naxalnet/scripts.py | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/naxalnet/__init__.py b/naxalnet/__init__.py index 623d2e4..2a38d30 100644 --- a/naxalnet/__init__.py +++ b/naxalnet/__init__.py @@ -42,4 +42,4 @@ given below. # # In case you forgot to change the version, skip the number # and put the next number in the next commit. -__version__ = "0.5.1a0.dev3" +__version__ = "0.5.1a0.dev4" diff --git a/naxalnet/network.py b/naxalnet/network.py index caa1e80..d1787f0 100644 --- a/naxalnet/network.py +++ b/naxalnet/network.py @@ -29,6 +29,7 @@ examples. import subprocess from pathlib import Path +from gi.repository import GObject from dasbus.connection import SystemMessageBus from dasbus.loop import EventLoop @@ -145,10 +146,12 @@ class NetworkLoop(NetworkD): def wait_until_routable(self, timeout=0): """ - wait until timeout in milliseconds and returns True when any - network interface is shown routable by networkd + Wait until timeout in milliseconds and returns True when any + network interface is shown routable by networkd. Does not wait + for timeout if timeout==0 """ self.wait_for_change("AddressState", self.on_addressstate_change) + self.setup_timeout(timeout) return self.is_routable() def wait_for_change(self, name, function): @@ -167,6 +170,11 @@ class NetworkLoop(NetworkD): if self.waitfor in data: return self.wait_function() + def setup_timeout(self, timeout): + """setup a timeout""" + if timeout != 0: + GObject.timeout_add(timeout, self.on_timeout) + def on_timeout(self): """called by dasbus when a timeout occurs""" self.loop.quit() diff --git a/naxalnet/scripts.py b/naxalnet/scripts.py index 4dbcd20..64ec06d 100644 --- a/naxalnet/scripts.py +++ b/naxalnet/scripts.py @@ -63,7 +63,8 @@ def any_interface_is_routable(): logger.debug("Adding temporary config %s", i) networkd.add_config(i) - routable = networkd.wait_until_routable() + # timeout = 10 seconds + routable = networkd.wait_until_routable(10 * 1000) networkd.remove_all_configs() return routable