glib: Create a new module to deal with all things glib

- Chmod -x dbus.py. It appears that the file was accidentally set to permissions
0o755 instead of 0o644.

glib module will contain:

- Code to deal with glib main loop.

- Use glib as a way to schedule timely events instead of creating long running
  threads.

- Other mechanisms to help with asynchronous I/O until we start using asyncio.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-01-28 15:59:26 -08:00 committed by James Valleroy
parent f150528379
commit 2e534db168
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 65 additions and 36 deletions

View File

@ -21,7 +21,7 @@ import importlib
import logging
import sys
from . import (__version__, cfg, dbus, frontpage, log, menu, module_loader,
from . import (__version__, cfg, frontpage, glib, log, menu, module_loader,
setup, utils, web_framework, web_server)
if utils.is_axes_old():
@ -130,7 +130,7 @@ def adapt_config(arguments):
def on_web_server_stop():
"""Stop all other threads since web server is trying to exit."""
setup.stop()
dbus.stop()
glib.stop()
def main():
@ -178,7 +178,7 @@ def main():
setup.run_setup_in_background()
dbus.run()
glib.run()
web_server.init()
web_server.run(on_web_server_stop)

36
plinth/dbus.py Executable file → Normal file
View File

@ -23,16 +23,13 @@ import threading
from plinth.utils import import_from_gi
from . import network, setup
from . import setup
glib = import_from_gi('GLib', '2.0')
gio = import_from_gi('Gio', '2.0')
logger = logging.getLogger(__name__)
_thread = None
_server = None
_main_loop = None
class PackageHandler():
@ -127,35 +124,8 @@ class DBusServer():
# service again.
def run():
"""Run a glib main loop forever in a thread."""
global _thread
_thread = threading.Thread(target=_run)
_thread.start()
def stop():
"""Exit glib main loop and end the thread."""
if _main_loop:
logger.info('Exiting main loop for D-Bus services')
_main_loop.quit()
def _run():
"""Connect to D-Bus and run main loop."""
logger.info('Started new thread for D-Bus services')
def init():
"""Connect to D-Bus service. Must be run from glib thread."""
global _server
_server = DBusServer()
_server.connect()
# Initialize all other modules that glib main loop
# XXX: Refactor this code into separate 'glib' module later
network.init()
global _main_loop
_main_loop = glib.MainLoop()
_main_loop.run()
_main_loop = None
logger.info('D-Bus services thread exited.')

59
plinth/glib.py Normal file
View File

@ -0,0 +1,59 @@
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import logging
import threading
from plinth import dbus, network
from plinth.utils import import_from_gi
glib = import_from_gi('GLib', '2.0')
_thread = None
_main_loop = None
logger = logging.getLogger(__name__)
def run():
"""Run a glib main loop forever in a thread."""
global _thread
_thread = threading.Thread(target=_run)
_thread.start()
def stop():
"""Exit glib main loop and end the thread."""
if _main_loop:
logger.info('Exiting glib main loop')
_main_loop.quit()
def _run():
"""Connect to D-Bus and run main loop."""
logger.info('Started new thread for glib main loop.')
# Initialize all modules that use glib main loop
dbus.init()
network.init()
global _main_loop
_main_loop = glib.MainLoop()
_main_loop.run()
_main_loop = None
logger.info('Glib main loop thread exited.')