+ added timer object to new communicator

master
Kevin Froman 2018-06-12 18:32:33 -05:00
parent b6da3385cb
commit ed1d09a7b6
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
1 changed files with 27 additions and 4 deletions

View File

@ -19,18 +19,39 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import sys, core, config, onionrblockapi as block, requests import sys, core, config, onionrblockapi as block, requests, time, logger
from defusedxml import minidom from defusedxml import minidom
class OnionrCommunicatorDaemon: class OnionrCommunicatorDaemon:
def __init__(self, debug, developmentMode): def __init__(self, debug, developmentMode):
self.timers = [] # list of tuples, function, time in seconds self.timers = []
self._core = core.Core() self._core = core.Core()
self.nistSaltTimestamp = 0 self.nistSaltTimestamp = 0
self.powSalt = 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 shouldRun = False
@ -49,3 +70,5 @@ if shouldRun:
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(1) sys.exit(1)
pass pass
except Exception as e:
logger.error('Error occured in Communicator', error = e, timestamp = False)