Skip to content

Commit

Permalink
Clean up a tiny bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ketok4321 committed Jan 15, 2024
1 parent 14da7ed commit f02658e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
11 changes: 6 additions & 5 deletions src/fetch_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

class FetchWorker(threading.Thread):
def __init__(self, app):
super().__init__(name="SpeedtestWorker", daemon=True)
super().__init__(name="FetchWorker", daemon=True)

self.stop_event = threading.Event()
self.app = app
self.servers = []

def run(self):
event_loop = asyncio.new_event_loop()
Expand All @@ -31,12 +32,12 @@ async def do_run(self):
GLib.idle_add(self.app.win.set_view, self.app.win.loading_view)

try:
self.app.servers = []
while len(self.app.servers) == 0:
self.servers = []
while len(self.servers) == 0:
print("Trying to fetch servers...")
self.app.servers = await self.app.backend.get_servers()
self.servers = await self.app.backend.get_servers()

GLib.idle_add(self.app.win.start_view.server_selector.set_model, Gtk.StringList.new(list(map(lambda s: s.name, self.app.servers))))
GLib.idle_add(self.app.win.start_view.server_selector.set_model, Gtk.StringList.new(list(map(lambda s: s.name, self.servers))))
GLib.idle_add(self.app.win.main_view.pop)
GLib.idle_add(self.app.win.set_view, self.app.win.main_view)
except Exception as e:
Expand Down
16 changes: 8 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
class SpeedtestApplication(Adw.Application):
def __init__(self):
super().__init__(application_id=APP_ID, flags=Gio.ApplicationFlags.DEFAULT_FLAGS)

self.servers = None

self.win = None
self.settings = Gio.Settings("xyz.ketok.Speedtest")
self.backend = None
self.fetch_worker = None
self.test_worker = None

Expand All @@ -37,12 +37,12 @@ def do_activate(self):
self.win = self.props.active_window
if not self.win:
self.win = SpeedtestWindow(application=self)
self.win.on_test_end = lambda: self.test_worker.stop_event.set()
self.win.on_test_end = lambda: self.test_worker.stop_event.set()

self.settings.bind("width", self.win, "default-width", Gio.SettingsBindFlags.DEFAULT)
self.settings.bind("height", self.win, "default-height", Gio.SettingsBindFlags.DEFAULT)
self.settings.bind("width", self.win, "default-width", Gio.SettingsBindFlags.DEFAULT)
self.settings.bind("height", self.win, "default-height", Gio.SettingsBindFlags.DEFAULT)

self.win.present()
self.win.present()

self.load_backend()

Expand Down Expand Up @@ -83,12 +83,12 @@ def on_preferences_action(self, widget, _):
def on_start_action(self, widget, _):
self.win.start_test()

server = self.servers[self.win.start_view.server_selector.get_selected()]
server = self.fetch_worker.servers[self.win.start_view.server_selector.get_selected()]

self.win.test_view.reset()
self.win.test_view.server = server.name

self.test_worker = TestWorker(self.backend, self.win, server, self.settings)
self.test_worker = TestWorker(self, server)
self.test_worker.start()

def on_retry_connect_action(self, widget, _):
Expand Down
26 changes: 12 additions & 14 deletions src/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ class TestResults:
total_up: int = 0

class TestWorker(threading.Thread):
def __init__(self, backend, win, server, settings):
super().__init__(name="SpeedtestWorker", daemon=True)
def __init__(self, app, server):
super().__init__(name="TestWorker", daemon=True)

self.stop_event = threading.Event()
self.backend = backend
self.win = win
self.app = app
self.server = server
self.settings = settings

def run(self):
event_loop = asyncio.new_event_loop()
Expand Down Expand Up @@ -52,7 +50,7 @@ async def run_async(self):

async def do_run(self):
try:
view = self.win.test_view
view = self.app.win.test_view

timeout = None

Expand Down Expand Up @@ -85,32 +83,32 @@ def on_event(type):
GLib.source_remove(timeout)
view.upload.remove_css_class("active")

GLib.idle_add(self.win.test_view.progress.set_visible, True)
GLib.idle_add(self.app.win.test_view.progress.set_visible, True)
self.results = TestResults()
await self.backend.start(self.server, self.results, lambda type: GLib.idle_add(on_event, type))
GLib.idle_add(self.win.test_view.progress.set_visible, False)
await self.app.backend.start(self.server, self.results, lambda type: GLib.idle_add(on_event, type))
GLib.idle_add(self.app.win.test_view.progress.set_visible, False)
except Exception as e:
print(e)
GLib.idle_add(self.win.set_view, self.win.offline_view)
GLib.idle_add(self.app.win.set_view, self.app.win.offline_view)

def update(self, gauge, total, part_two):
view = self.win.test_view
view = self.app.win.test_view

current_duration = time.time() - self.start_time
value = total * OVERHEAD_COMPENSATION / current_duration

if current_duration > 1:
speedMb = round(value / 125_000, 1)
gauge.value = str(speedMb) + "Mbps"
gauge.fill = min(speedMb / self.settings.get_int("gauge-scale"), 1.0)
gauge.fill = min(speedMb / self.app.settings.get_int("gauge-scale"), 1.0)

view.progress.set_fraction(current_duration / DURATION * 0.5 + (0.5 if part_two else 0.0))

return not self.stop_event.is_set()

async def perform_test(self, test, streams):
GLib.idle_add(self.win.test_view.progress.set_visible, True)
GLib.idle_add(self.app.win.test_view.progress.set_visible, True)

self.start_time = time.time()

GLib.idle_add(self.win.test_view.progress.set_visible, False)
GLib.idle_add(self.app.win.test_view.progress.set_visible, False)

0 comments on commit f02658e

Please sign in to comment.