2020-07-07 13:37:23 +00:00
|
|
|
"""
|
|
|
|
Onionr - Private P2P Communication.
|
2019-02-28 03:02:44 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
This file contains timer control for the communicator
|
|
|
|
"""
|
2019-09-12 19:50:06 +00:00
|
|
|
from __future__ import annotations # thank you python, very cool
|
2020-07-07 13:37:23 +00:00
|
|
|
import uuid
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import onionrexceptions
|
|
|
|
import logger
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from typing import Callable, NewType, Iterable
|
|
|
|
if TYPE_CHECKING:
|
2020-07-26 02:36:48 +00:00
|
|
|
from deadsimplekv import DeadSimpleKV
|
2020-07-07 13:37:23 +00:00
|
|
|
from communicator import OnionrCommunicatorDaemon
|
|
|
|
"""
|
2019-02-28 03:02:44 +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/>.
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
2019-09-12 19:50:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
CallFreqSeconds = NewType('CallFreqSeconds', int)
|
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
|
2019-02-28 03:02:44 +00:00
|
|
|
class OnionrCommunicatorTimers:
|
2020-03-20 08:50:48 +00:00
|
|
|
def __init__(self, daemon_inst: OnionrCommunicatorDaemon,
|
2020-07-07 13:37:23 +00:00
|
|
|
timer_function: Callable, frequency: CallFreqSeconds,
|
|
|
|
make_thread: bool = True,
|
|
|
|
thread_amount: int = 1, max_threads: int = 5,
|
|
|
|
requires_peer: bool = False, my_args: Iterable = []):
|
2019-09-12 19:50:06 +00:00
|
|
|
self.timer_function = timer_function
|
2019-02-28 03:02:44 +00:00
|
|
|
self.frequency = frequency
|
2019-09-12 19:50:06 +00:00
|
|
|
self.thread_amount = thread_amount
|
|
|
|
self.make_thread = make_thread
|
|
|
|
self.requires_peer = requires_peer
|
|
|
|
self.daemon_inst = daemon_inst
|
|
|
|
self.max_threads = max_threads
|
|
|
|
self.args = my_args
|
2020-07-26 02:36:48 +00:00
|
|
|
self.kv: "DeadSimpleKV" = daemon_inst.shared_state.get_by_string(
|
|
|
|
"DeadSimpleKV")
|
2019-09-12 19:50:06 +00:00
|
|
|
|
|
|
|
self.daemon_inst.timers.append(self)
|
2019-02-28 03:02:44 +00:00
|
|
|
self.count = 0
|
|
|
|
|
|
|
|
def processTimer(self):
|
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
# mark # of instances of a thread we have (decremented at thread end)
|
2019-02-28 03:02:44 +00:00
|
|
|
try:
|
2019-09-12 19:50:06 +00:00
|
|
|
self.daemon_inst.threadCounts[self.timer_function.__name__]
|
2019-02-28 03:02:44 +00:00
|
|
|
except KeyError:
|
2019-09-12 19:50:06 +00:00
|
|
|
self.daemon_inst.threadCounts[self.timer_function.__name__] = 0
|
2019-02-28 03:02:44 +00:00
|
|
|
|
2020-07-07 13:37:23 +00:00
|
|
|
# execute timer's func, if we are not missing *required* online peer
|
2020-07-26 02:36:48 +00:00
|
|
|
if self.count == self.frequency and not self.kv.get('shutdown'):
|
2019-02-28 03:02:44 +00:00
|
|
|
try:
|
2020-07-07 13:37:23 +00:00
|
|
|
if self.requires_peer and \
|
|
|
|
len(self.daemon_inst.onlinePeers) == 0:
|
2019-02-28 03:02:44 +00:00
|
|
|
raise onionrexceptions.OnlinePeerNeeded
|
|
|
|
except onionrexceptions.OnlinePeerNeeded:
|
2019-08-07 02:50:15 +00:00
|
|
|
return
|
2019-02-28 03:02:44 +00:00
|
|
|
else:
|
2019-09-12 19:50:06 +00:00
|
|
|
if self.make_thread:
|
|
|
|
for i in range(self.thread_amount):
|
2020-07-07 13:37:23 +00:00
|
|
|
"""
|
|
|
|
Log if a timer has max num of active threads
|
|
|
|
If this logs frequently it is indicative of a bug
|
|
|
|
or need for optimization
|
|
|
|
"""
|
|
|
|
if self.daemon_inst.threadCounts[
|
|
|
|
self.timer_function.__name__] >= \
|
|
|
|
self.max_threads:
|
|
|
|
logger.debug(
|
|
|
|
f'{self.timer_function.__name__} is currently using the maximum number of threads, not starting another.') # noqa
|
|
|
|
# if active number of threads for timer not reached yet
|
2019-02-28 03:02:44 +00:00
|
|
|
else:
|
2020-07-07 13:37:23 +00:00
|
|
|
self.daemon_inst.threadCounts[
|
|
|
|
self.timer_function.__name__] += 1
|
|
|
|
newThread = threading.Thread(
|
|
|
|
target=self.timer_function, args=self.args,
|
|
|
|
daemon=True,
|
|
|
|
name=self.timer_function.__name__ + ' - ' +
|
|
|
|
str(uuid.uuid4()))
|
2019-02-28 03:02:44 +00:00
|
|
|
newThread.start()
|
|
|
|
else:
|
2019-09-12 19:50:06 +00:00
|
|
|
self.timer_function()
|
2020-07-07 13:37:23 +00:00
|
|
|
self.count = -1 # negative 1 because its incremented at bottom
|
2019-02-28 03:02:44 +00:00
|
|
|
self.count += 1
|