From fc1f3d4216e691db93684e7539002a49d40bb0ad Mon Sep 17 00:00:00 2001 From: Pranav Jerry Date: Mon, 29 Nov 2021 14:47:59 +0530 Subject: [PATCH] another fix for #25 Or, to be more precise, a fix for the fix preceding the current fix for issue #25 that just didn't fix things right. If your head spins, see the previous commit, or call an ambulance (that is, if you are privileged enough to access a phone and know your local helpline number) --- naxalnet/__init__.py | 2 +- naxalnet/network.py | 30 +++++++++++------------------- naxalnet/scripts.py | 5 ++++- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/naxalnet/__init__.py b/naxalnet/__init__.py index 8eccc68..fc0d289 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.dev6" +__version__ = "0.5.1a0.dev7" diff --git a/naxalnet/network.py b/naxalnet/network.py index 00563d9..4a59616 100644 --- a/naxalnet/network.py +++ b/naxalnet/network.py @@ -28,10 +28,9 @@ examples. """ import subprocess -from multiprocessing import Process from pathlib import Path from dasbus.connection import SystemMessageBus -from dasbus.loop import EventLoop +from dasbus.loop import EventLoop, GLib NETWORKD_BUS = "org.freedesktop.network1" @@ -143,7 +142,6 @@ class NetworkLoop(NetworkD): self.waitfor = None self.wait_function = None self.loop = EventLoop() - self.timeout = 0 def start_loop(self): """start the dasbus loop""" @@ -151,28 +149,16 @@ class NetworkLoop(NetworkD): print("waitfor", self.waitfor) print("waitfor func", self.wait_function) self.proxy.PropertiesChanged.connect(self.on_properties_changed) - process = Process(target=self.loop.run) - process.start() - - # wait until timeout - if self.timeout != 0: - process.join(self.timeout) - else: - process.join() - - # When the process is timed out, it is not killed. We have to kill - # it manually - if process.is_alive(): - self.on_timeout() + self.loop.run() def wait_until_routable(self, timeout=0): """ - Wait until timeout in seconds and returns True when any + 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 """ print("wait until routable") - self.timeout = timeout + self.setup_timeout(timeout) self.wait_for_change("AddressState", self.on_addressstate_change) return self.is_routable() @@ -196,7 +182,13 @@ class NetworkLoop(NetworkD): if self.waitfor in data: return self.wait_function() + def setup_timeout(self, timeout): + """setup a timeout""" + print("setup timeout") + if timeout != 0: + GLib.timeout_add(timeout, self.on_timeout) + def on_timeout(self): - """called when a timeout occurs""" + """called by dasbus when a timeout occurs""" print("on timeout") self.loop.quit() diff --git a/naxalnet/scripts.py b/naxalnet/scripts.py index 609ab53..8d24c76 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) + # timeout = 10 seconds + routable = networkd.wait_until_routable(10 * 1000) networkd.remove_all_configs() return routable @@ -243,8 +244,10 @@ def main(): notify("STATUS=Checking for internet") # If any interface is routable, set gateway mode to server if any_interface_is_routable(): + logger.info("Network is routable. Setting gw_mode to server") gateway_mode = "server" else: + logger.info("Network is not routable. Setting gw_mode to client") gateway_mode = "client" logger.info("gateway_mode set to %s", gateway_mode) elif args.gateway_mode in ["server", "client", "off"]: