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)
This commit is contained in:
Pranav Jerry 2021-11-29 14:47:59 +05:30
parent 5e58df0b71
commit fc1f3d4216
Signed by: pranav
GPG Key ID: F1DCDC4FED0A0C5B
3 changed files with 16 additions and 21 deletions

View File

@ -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"

View File

@ -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()

View File

@ -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"]: