3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-12-22 04:32:37 +01:00

auto-t: return existing instance from HostapdCLI

This addresses the TODO where HostapdCLI was creating separate
objects each time HostapdCLI was called. This was worked around
by manually setting the important members but instead the class
can be re-worked to act as somewhat of a singleton, per-config
at least.

If there is no HostapdCLI instance for a given config one is
created and initialized. Subsequent HostapdCLI calls (for the
same config) will be returned the same object rather than a
new one.
This commit is contained in:
James Prestwood 2021-08-13 15:45:51 -07:00 committed by Denis Kenzior
parent f721f1de1e
commit 973ee269d3
2 changed files with 33 additions and 19 deletions

View File

@ -28,10 +28,28 @@ chan_freq_map = [
ctrl_count = 0
mainloop = GLib.MainLoop()
class HostapdCLI:
def _init_hostapd(self, config=None):
class HostapdCLI(object):
_instances = {}
def __new__(cls, config=None, *args, **kwargs):
hapd = ctx.hostapd[config]
if not config:
config = hapd.config
if not config in cls._instances.keys():
cls._instances[config] = object.__new__(cls, *args, **kwargs)
cls._instances[config]._initialized = False
return cls._instances[config]
def _init_hostapd(self, config):
global ctrl_count
interface = None
if self._initialized:
return
self._initialized = True
self.ctrl_sock = None
if not ctx.hostapd:
@ -45,21 +63,6 @@ class HostapdCLI:
if not hasattr(self, '_hostapd_restarted'):
self._hostapd_restarted = False
#
# TODO: In theory some type of instance singleton could be created for
# HostapdCLI where test-runner initializes but any subsequent
# call to HostapdCLI (with the same config) returns the same
# object that test-runner has. This would avoid setting these
# variables.
#
if hapd.cli:
self.ifname = hapd.cli.ifname
self.ctrl_sock = hapd.cli.ctrl_sock
self.cmdline = hapd.cli.cmdline
self.interface = hapd.intf
self.config = hapd.config
return
self.interface = hapd.intf
self.config = hapd.config
@ -83,7 +86,7 @@ class HostapdCLI:
ctrl_count = ctrl_count + 1
def __init__(self, config=None):
def __init__(self, config=None, *args, **kwargs):
self._init_hostapd(config)
def _poll_event(self, event):
@ -137,6 +140,13 @@ class HostapdCLI:
def __del__(self):
self._del_hostapd()
HostapdCLI._instances[self.config] = None
# Check if this is the final instance
destroy = len([hapd for hapd in HostapdCLI._instances.values() if hapd is not None]) == 0
if destroy:
HostapdCLI._instances = {}
def set_value(self, key, value):
cmd = self.cmdline + ['set', key, value]
ctx.start_process(cmd, wait=True)

View File

@ -545,6 +545,10 @@ class Hostapd:
except:
print("Failed to remove %s" % self.global_ctrl_iface)
for hapd in self.instances:
hapd.cli._instances[hapd.config] = None
hapd.cli = None
self.instances = None
# Hostapd may have already been stopped