From ed1d09a7b6c02ff9da2ceb8e665b70b364cfa014 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 12 Jun 2018 18:32:33 -0500 Subject: [PATCH] + added timer object to new communicator --- onionr/communicator2.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index 9bcb0d97..dc5140b2 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -19,18 +19,39 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sys, core, config, onionrblockapi as block, requests +import sys, core, config, onionrblockapi as block, requests, time, logger from defusedxml import minidom class OnionrCommunicatorDaemon: def __init__(self, debug, developmentMode): - self.timers = [] # list of tuples, function, time in seconds + self.timers = [] self._core = core.Core() - self.nistSaltTimestamp = 0 self.powSalt = 0 - return + self.delay = 1 + + OnionrCommunicatorTimers(self.timers, self.heartbeat, 1) + + while True: + time.sleep(self.delay) + for i in self.timers: + i.processTimer() + def heartbeat(self): + logger.debug('Communicator heartbeat') + +class OnionrCommunicatorTimers: + def __init__(self, timerList, timerFunction, frequency): + self.timerFunction = timerFunction + self.frequency = frequency + + timerList.append(self) + self.count = 0 + def processTimer(self): + self.count += 1 + if self.count == self.frequency: + self.timerFunction() + self.count = 0 shouldRun = False @@ -49,3 +70,5 @@ if shouldRun: except KeyboardInterrupt: sys.exit(1) pass + except Exception as e: + logger.error('Error occured in Communicator', error = e, timestamp = False) \ No newline at end of file