From ee7b68c8449c20966e04ea8827383cc63a70c4a0 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 1 Jan 2020 20:07:34 -0600 Subject: [PATCH] added new daemon events files --- .../private/register_private_blueprints.py | 16 ++++++++ src/communicator/daemoneventhooks/__init__.py | 38 +++++++++++++++++++ .../__init__.py | 18 +++++---- src/onionrcommands/daemonlaunch.py | 4 ++ 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 src/communicator/daemoneventhooks/__init__.py rename src/httpapi/{daemonevents => daemoneventsapi}/__init__.py (78%) diff --git a/src/apiservers/private/register_private_blueprints.py b/src/apiservers/private/register_private_blueprints.py index 4c43d74c..b5ba6c5e 100644 --- a/src/apiservers/private/register_private_blueprints.py +++ b/src/apiservers/private/register_private_blueprints.py @@ -2,10 +2,14 @@ This file registers blueprints for the private api server """ +from gevent import spawn +from gevent import sleep + from httpapi import security, friendsapi, profilesapi, configapi, insertblock from httpapi import miscclientapi, onionrsitesapi, apiutils from httpapi import directconnections from httpapi import themeapi + """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,4 +44,16 @@ def register_private_blueprints(private_api, app): app.register_blueprint(directconnections.DirectConnectionManagement( private_api).direct_conn_management_bp) app.register_blueprint(themeapi.theme_blueprint) + + def _add_events_bp(): + while True: + try: + private_api._too_many + break + except AttributeError: + sleep(0.2) + app.register_blueprint( + private_api._too_many.get_by_string('DaemonEventsBP').flask_bp) + + spawn(_add_events_bp) return app diff --git a/src/communicator/daemoneventhooks/__init__.py b/src/communicator/daemoneventhooks/__init__.py new file mode 100644 index 00000000..b696faee --- /dev/null +++ b/src/communicator/daemoneventhooks/__init__.py @@ -0,0 +1,38 @@ +"""Onionr - Private P2P Communication. + +Hooks to handle daemon events +""" +from typing import TYPE_CHECKING + +from gevent import sleep + +if TYPE_CHECKING: + from toomanyobjs import TooMany + from communicator import OnionrCommunicatorDaemon + from httpapi.daemoneventsapi import DaemonEventsBP +""" + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +""" + + +def daemon_event_handlers(shared_state: 'TooMany'): + def _get_inst(class_name: str): + while True: + try: + return shared_state.get_by_string(class_name) + except KeyError: + sleep(0.2) + events_api: 'DaemonEventsBP' = _get_inst('DaemonEventsBP') + + diff --git a/src/httpapi/daemonevents/__init__.py b/src/httpapi/daemoneventsapi/__init__.py similarity index 78% rename from src/httpapi/daemonevents/__init__.py rename to src/httpapi/daemoneventsapi/__init__.py index 6f8aa509..31b20201 100644 --- a/src/httpapi/daemonevents/__init__.py +++ b/src/httpapi/daemoneventsapi/__init__.py @@ -21,10 +21,7 @@ import config """ -event_BP = Blueprint('event_BP', __name__) - - -class DaemonEvents: +class DaemonEventsBP: def __init__(self): """Create DaemonEvents instance, intended to be a singleton. @@ -34,13 +31,20 @@ class DaemonEvents: The callables name should match the event name _too_many: TooManyObjects instance set by external code """ + event_BP = Blueprint('event_BP', __name__) self.events = {} + self.listeners = {} self.flask_bp = event_BP event_BP = self.flask_bp - @event_BP.route('/daemon-event', methods=['POST']) - def daemon_event_handler() -> Response: - return + @event_BP.route('/daemon-event/', methods=['POST']) + def daemon_event_handler(name): + if name in self.listeners: + + + @event_BP.route('/daemon-event/bp-enabled') + def bp_enabled() -> Response: + return Response('true') def clean_old(self): """Deletes old daemon events based on their completion date.""" diff --git a/src/onionrcommands/daemonlaunch.py b/src/onionrcommands/daemonlaunch.py index d2e124ea..f5f44aef 100755 --- a/src/onionrcommands/daemonlaunch.py +++ b/src/onionrcommands/daemonlaunch.py @@ -26,6 +26,7 @@ from utils import hastor, logoheader from . import version import serializeddata import runtests +from httpapi import daemoneventsapi """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -65,6 +66,8 @@ def daemon(): shared_state = toomanyobjs.TooMany() + shared_state.get(daemoneventsapi.DaemonEventsBP) + Thread(target=shared_state.get(apiservers.ClientAPI).start, daemon=True, name='client HTTP API').start() if not offline_mode: @@ -76,6 +79,7 @@ def daemon(): shared_state.get(runtests.OnionrRunTestManager) shared_state.get(serializeddata.SerializedData) + shared_state.share_object() # share the parent object to the threads apiHost = ''