ui: Create a glib class and simplify the shutdown logic.

This commit is contained in:
Damir Jelić 2019-05-07 10:26:13 +02:00
parent 57f3a00a78
commit e961ff6035
2 changed files with 51 additions and 45 deletions

View File

@ -28,8 +28,7 @@ from pantalaimon.client import PanClient
from pantalaimon.log import logger
from pantalaimon.store import ClientInfo, PanStore
from pantalaimon.ui import (
glib_loop,
shutdown_glib_loop,
GlibT,
DeviceVerifyMessage,
DeviceUnverifyMessage,
ExportKeysMessage,
@ -707,7 +706,6 @@ async def init(homeserver, http_proxy, ssl, send_queue, recv_queue):
web.post("/_matrix/client/r0/user/{user_id}/filter", proxy.filter),
])
app.router.add_route("*", "/" + "{proxyPath:.*}", proxy.router)
app.on_shutdown.append(proxy.shutdown)
return proxy, app
@ -904,18 +902,21 @@ def start(
))
data_dir = user_data_dir("pantalaimon", "")
glib_thread = GlibT(pan_queue.sync_q, ui_queue.sync_q, data_dir)
fut = loop.run_in_executor(
None,
glib_loop,
pan_queue.sync_q,
ui_queue.sync_q,
data_dir
glib_thread.run
)
kill_glib = partial(shutdown_glib_loop, fut, pan_queue.async_q)
async def wait_for_glib(glib_thread, fut, app):
glib_thread.stop()
await fut
stop_glib = partial(wait_for_glib, glib_thread, fut)
app.on_shutdown.append(proxy.shutdown)
app.on_shutdown.append(kill_glib)
app.on_shutdown.append(stop_glib)
home = os.path.expanduser("~")
os.chdir(home)

View File

@ -9,21 +9,17 @@ from gi.repository import GLib
from queue import Empty
from nio.store import TrustState
from pantalaimon.store import PanStore
from pantalaimon.log import logger
DBusGMainLoop(set_as_default=True)
@attr.s
class Message:
pass
@attr.s
class ShutDownMessage(Message):
pass
@attr.s
class InfoMessage(Message):
string = attr.ib()
@ -203,39 +199,47 @@ class Control(dbus.service.Object):
pass
def glib_loop(receive_queue, send_queue, data_dir):
DBusGMainLoop(set_as_default=True)
loop = GLib.MainLoop()
@attr.s
class GlibT:
receive_queue = attr.ib()
send_queue = attr.ib()
data_dir = attr.ib()
bus_name = dbus.service.BusName("org.pantalaimon",
bus=dbus.SessionBus(),
do_not_queue=True)
loop = attr.ib(init=False)
bus_name = attr.ib(init=False)
store = attr.ib(init=False)
users = attr.ib(init=False)
devices = attr.ib(init=False)
control_bus = attr.ib(init=False)
device_bus = attr.ib(init=False)
store = PanStore(data_dir)
users = store.load_all_users()
devices = store.load_all_devices()
def __attrs_post_init__(self):
self.loop = None
control_bus = Control(bus_name, send_queue, users)
device_bus = Devices(bus_name, send_queue, devices)
self.bus_name = dbus.service.BusName("org.pantalaimon",
bus=dbus.SessionBus(),
do_not_queue=True)
def message_callback():
self.store = PanStore(self.data_dir)
self.users = self.store.load_all_users()
self.devices = self.store.load_all_devices()
self.control_bus = Control(self.bus_name, self.send_queue, self.users)
self.device_bus = Devices(self.bus_name, self.send_queue, self.devices)
def message_callback(self):
try:
message = receive_queue.get_nowait()
message = self.receive_queue.get_nowait()
except Empty:
return True
logger.debug(f"UI loop received message {message}")
if isinstance(message, ShutDownMessage):
receive_queue.task_done()
loop.quit()
return False
elif isinstance(message, DevicesMessage):
device_bus.update_devices(message)
if isinstance(message, DevicesMessage):
self.device_bus.update_devices(message)
elif isinstance(message, DeviceAuthStringMessage):
device_bus.sas_show(
self.device_bus.sas_show(
message.pan_user,
message.user_id,
message.device_id,
@ -243,16 +247,17 @@ def glib_loop(receive_queue, send_queue, data_dir):
)
elif isinstance(message, InfoMessage):
control_bus.info(message.string)
self.control_bus.info(message.string)
receive_queue.task_done()
self.receive_queue.task_done()
return True
GLib.timeout_add(100, message_callback)
def run(self):
self.loop = GLib.MainLoop()
GLib.timeout_add(100, self.message_callback)
self.loop.run()
loop.run()
async def shutdown_glib_loop(future, queue, app):
await queue.put(ShutDownMessage())
await future
def stop(self):
if self.loop:
self.loop.quit()
self.loop = None