added new daemon events files

This commit is contained in:
Kevin Froman 2020-01-01 20:07:34 -06:00
parent 66aafb6deb
commit ee7b68c844
4 changed files with 69 additions and 7 deletions

View File

@ -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

View File

@ -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 <https://www.gnu.org/licenses/>.
"""
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')

View File

@ -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/<name>', 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."""

View File

@ -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 = ''