2020-01-02 02:07:34 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Hooks to handle daemon events
|
|
|
|
"""
|
2020-01-30 04:56:47 +00:00
|
|
|
from threading import Thread
|
|
|
|
|
2020-01-03 10:17:00 +00:00
|
|
|
from .removefrominsertqueue import remove_from_insert_queue
|
|
|
|
|
2020-01-02 02:07:34 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
from gevent import sleep
|
|
|
|
|
2020-01-04 12:13:10 +00:00
|
|
|
from communicatorutils.uploadblocks import mixmate
|
2020-01-06 12:06:27 +00:00
|
|
|
from communicatorutils import restarttor
|
2020-01-04 12:13:10 +00:00
|
|
|
|
2020-01-02 02:07:34 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from toomanyobjs import TooMany
|
2020-07-27 00:02:39 +00:00
|
|
|
from deadsimplekv import DeadSimpleKV
|
2020-01-02 02:07:34 +00:00
|
|
|
from communicator import OnionrCommunicatorDaemon
|
|
|
|
from httpapi.daemoneventsapi import DaemonEventsBP
|
2020-01-03 10:17:00 +00:00
|
|
|
from onionrtypes import BlockHash
|
2020-01-04 12:13:10 +00:00
|
|
|
from apiservers import PublicAPI
|
2020-01-02 02:07:34 +00:00
|
|
|
"""
|
|
|
|
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)
|
2020-01-03 10:17:00 +00:00
|
|
|
comm_inst = _get_inst('OnionrCommunicatorDaemon')
|
2020-01-04 12:13:10 +00:00
|
|
|
public_api: 'PublicAPI' = _get_inst('PublicAPI')
|
2020-01-02 02:07:34 +00:00
|
|
|
events_api: 'DaemonEventsBP' = _get_inst('DaemonEventsBP')
|
2020-07-27 00:02:39 +00:00
|
|
|
kv: 'DeadSimpleKV' = _get_inst('DeadSimpleKV')
|
2020-01-03 10:17:00 +00:00
|
|
|
|
|
|
|
def remove_from_insert_queue_wrapper(block_hash: 'BlockHash'):
|
|
|
|
remove_from_insert_queue(comm_inst, block_hash)
|
2020-01-04 12:13:10 +00:00
|
|
|
return "removed"
|
2020-01-03 10:17:00 +00:00
|
|
|
|
|
|
|
def print_test(text=''):
|
|
|
|
print("It works!", text)
|
|
|
|
return f"It works! {text}"
|
|
|
|
|
2020-01-04 12:13:10 +00:00
|
|
|
def upload_event(block: 'BlockHash' = ''):
|
|
|
|
if not block:
|
|
|
|
raise ValueError
|
|
|
|
public_api.hideBlocks.append(block)
|
|
|
|
try:
|
2020-07-27 00:02:39 +00:00
|
|
|
mixmate.block_mixer(kv.get('blocksToUpload'), block)
|
2020-01-04 12:13:10 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return "removed"
|
|
|
|
|
2020-01-06 12:06:27 +00:00
|
|
|
def restart_tor():
|
2020-09-15 17:08:50 +00:00
|
|
|
restarttor.restart(shared_state)
|
2020-07-27 00:12:52 +00:00
|
|
|
kv.put('offlinePeers', [])
|
2020-09-15 17:08:50 +00:00
|
|
|
kv.put('onlinePeers', [])
|
2020-01-06 12:06:27 +00:00
|
|
|
|
|
|
|
def test_runtime():
|
2020-01-30 04:56:47 +00:00
|
|
|
Thread(target=comm_inst.shared_state.get_by_string(
|
|
|
|
"OnionrRunTestManager").run_tests).start()
|
2020-01-06 12:06:27 +00:00
|
|
|
|
2020-01-03 10:17:00 +00:00
|
|
|
events_api.register_listener(remove_from_insert_queue_wrapper)
|
2020-09-15 17:08:50 +00:00
|
|
|
events_api.register_listener(restart_tor)
|
2020-01-03 10:17:00 +00:00
|
|
|
events_api.register_listener(print_test)
|
2020-01-04 12:13:10 +00:00
|
|
|
events_api.register_listener(upload_event)
|
2020-01-30 04:56:47 +00:00
|
|
|
events_api.register_listener(test_runtime)
|