2019-02-28 03:02:44 +00:00
|
|
|
'''
|
2019-06-12 06:44:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-02-28 03:02:44 +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
|
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/>.
|
|
|
|
'''
|
2019-09-12 19:50:06 +00:00
|
|
|
|
|
|
|
import uuid
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import onionrexceptions, logger
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from typing import Callable, NewType, Iterable
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from communicator import OnionrCommunicatorDaemon
|
|
|
|
|
|
|
|
CallFreqSeconds = NewType('CallFreqSeconds', int)
|
|
|
|
|
2019-02-28 03:02:44 +00:00
|
|
|
class OnionrCommunicatorTimers:
|
2019-09-12 19:50:06 +00:00
|
|
|
def __init__(self, daemon_inst: OnionrCommunicatorDaemon,
|
|
|
|
timer_function: Callable, frequency: CallFreqSeconds,
|
|
|
|
make_thread:bool=True, thread_amount:int=1, max_threads:int=5,
|
|
|
|
requires_peer:bool=False, my_args:Iterable=[]):
|
|
|
|
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
|
|
|
|
|
|
|
|
self.daemon_inst.timers.append(self)
|
2019-02-28 03:02:44 +00:00
|
|
|
self.count = 0
|
|
|
|
|
|
|
|
def processTimer(self):
|
|
|
|
|
|
|
|
# mark how many instances of a thread we have (decremented at thread end)
|
|
|
|
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
|
|
|
|
|
|
|
# execute thread if it is time, and we are not missing *required* online peer
|
2019-09-12 19:50:06 +00:00
|
|
|
if self.count == self.frequency and not self.daemon_inst.shutdown:
|
2019-02-28 03:02:44 +00:00
|
|
|
try:
|
2019-09-12 19:50:06 +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):
|
|
|
|
if self.daemon_inst.threadCounts[self.timer_function.__name__] >= self.max_threads:
|
|
|
|
logger.debug('%s is currently using the maximum number of threads, not starting another.' % self.timer_function.__name__, terminal=True)
|
2019-02-28 03:02:44 +00:00
|
|
|
else:
|
2019-09-12 19:50:06 +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()
|
2019-02-28 03:02:44 +00:00
|
|
|
self.count = -1 # negative 1 because its incremented at bottom
|
|
|
|
self.count += 1
|