auto-t: fix wait logic for the devices

In addition, it makes InterfacesAdded\Removed handlers
a little more generic.
This commit is contained in:
Tim Kourt 2018-06-27 16:33:14 -07:00 committed by Denis Kenzior
parent 28c6a37db0
commit 395a70d652
1 changed files with 28 additions and 12 deletions

View File

@ -604,13 +604,15 @@ class PSKAgent(dbus.service.Object):
class DeviceList(collections.Mapping): class DeviceList(collections.Mapping):
def __init__(self, iwd, objects): def __init__(self, iwd):
self._dict = {} self._dict = {}
iwd._object_manager.connect_to_signal("InterfacesAdded", iwd._object_manager.connect_to_signal("InterfacesAdded",
self._interfaces_added_handler, IWD_DEVICE_INTERFACE) self._interfaces_added_handler)
iwd._object_manager.connect_to_signal("InterfacesRemoved", iwd._object_manager.connect_to_signal("InterfacesRemoved",
self._interfaces_removed_handler, IWD_DEVICE_INTERFACE) self._interfaces_removed_handler)
objects = iwd._object_manager.GetManagedObjects()
for path in objects: for path in objects:
for interface in objects[path]: for interface in objects[path]:
@ -630,10 +632,12 @@ class DeviceList(collections.Mapping):
self._dict.pop(key).remove() self._dict.pop(key).remove()
def _interfaces_added_handler(self, path, interfaces): def _interfaces_added_handler(self, path, interfaces):
if IWD_DEVICE_INTERFACE in interfaces:
self._dict[path] = Device(path, interfaces[IWD_DEVICE_INTERFACE]) self._dict[path] = Device(path, interfaces[IWD_DEVICE_INTERFACE])
def _interfaces_removed_handler(self, path, interfaces): def _interfaces_removed_handler(self, path, interfaces):
del _dict[path] if IWD_DEVICE_INTERFACE in interfaces:
del self._dict[path]
class IWD(AsyncOpAbstract): class IWD(AsyncOpAbstract):
@ -684,16 +688,22 @@ class IWD(AsyncOpAbstract):
tries += 1 tries += 1
time.sleep(0.1) time.sleep(0.1)
self._devices = DeviceList(self, self._devices = DeviceList(self)
self._object_manager.GetManagedObjects())
def __del__(self): def __del__(self):
if self._iwd_proc is None: if self._iwd_proc is None:
return return
self._object_manager_if = None
self._agent_manager_if = None
self._known_network_manager_if = None
self._devices = None
self._iwd_proc.terminate() self._iwd_proc.terminate()
self._iwd_proc.wait() self._iwd_proc.wait()
self._iwd_proc = None
@property @property
def _object_manager(self): def _object_manager(self):
if self._object_manager_if is None: if self._object_manager_if is None:
@ -766,17 +776,23 @@ class IWD(AsyncOpAbstract):
assert not os.path.isabs(source) assert not os.path.isabs(source)
shutil.copy(source, IWD_STORAGE_DIR) shutil.copy(source, IWD_STORAGE_DIR)
def list_devices(self, wait_to_appear = False): def list_devices(self, wait_to_appear = False, max_wait = 15):
if not wait_to_appear: if not wait_to_appear:
return list(self._devices.values()) return list(self._devices.values())
tries = 0 self._wait_timed_out = False
def wait_timeout_cb():
self._wait_timed_out = True
return False
timeout = GLib.timeout_add_seconds(max_wait, wait_timeout_cb)
context = mainloop.get_context()
while len(self._devices) == 0: while len(self._devices) == 0:
if tries > 100: context.iteration(may_block=True)
if self._wait_timed_out:
raise TimeoutError('IWD has no associated devices') raise TimeoutError('IWD has no associated devices')
tries += 1 GLib.source_remove(timeout)
time.sleep(0.2)
return list(self._devices.values()) return list(self._devices.values())