split oout processManager

This commit is contained in:
chrys 2017-08-03 22:04:47 +02:00
parent 6c7277ec12
commit cd5e042768
6 changed files with 89 additions and 110 deletions

View File

@ -0,0 +1,26 @@
#!/bin/python
import time
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from core import debug
import time
class command():
def __init__(self):
pass
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
def getDescription(self):
return 'No Description found'
def run(self):
print(time.time())
def setCallback(self, callback):
pass

View File

@ -14,30 +14,14 @@ from ctypes import c_bool
class eventManager():
def __init__(self):
self._mainLoopRunning = Value(c_bool, True)
self._eventProcesses = []
self.running = Value(c_bool, True)
self._eventQueue = Queue() # multiprocessing.Queue()
self.cleanEventQueue()
def initialize(self, environment):
self.env = environment
self.addSimpleEventThread(fenrirEventType.HeartBeat, self.heartBeatTimer)
def shutdown(self):
self.terminateAllProcesses()
self.cleanEventQueue()
def heartBeatTimer(self):
try:
time.sleep(0.5)
except:
pass
#self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')
return time.time()
def terminateAllProcesses(self):
time.sleep(1)
for proc in self._eventProcesses:
try:
proc.terminate()
except Exception as e:
print(e)
def proceedEventLoop(self):
event = self._eventQueue.get()
st = time.time()
@ -53,7 +37,6 @@ class eventManager():
return
elif event['Type'] == fenrirEventType.StopMainLoop:
self.handleStopMainLoop(event)
return
elif event['Type'] == fenrirEventType.ScreenUpdate:
self.env['runtime']['fenrirManager'].handleScreenUpdate(event)
elif event['Type'] == fenrirEventType.KeyboardInput:
@ -71,36 +54,19 @@ class eventManager():
elif event['Type'] == fenrirEventType.ExecuteCommand:
self.env['runtime']['fenrirManager'].handleExecuteCommand(event)
def isMainEventLoopRunning(self):
return self._mainLoopRunning.value == 1
return self.running.value == 1
def startMainEventLoop(self):
self._mainLoopRunning.value = 1
self.running.value = 1
while( self.isMainEventLoopRunning()):
self.proceedEventLoop()
def handleStopMainLoop(self, event):
self._mainLoopRunning.value = 0
self.running.value = 0
time.sleep(0.1)
def stopMainEventLoop(self, Force = False):
if Force:
self._mainLoopRunning.value = 0
self.running.value = 0
self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
def addCustomEventThread(self, function, pargs = None, multiprocess=False):
self._mainLoopRunning.value = 1
if multiprocess:
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs))
else:# thread not implemented yet
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs))
self._eventProcesses.append(t)
t.start()
def addSimpleEventThread(self, event, function, pargs = None, multiprocess=False, runOnce = False):
self._mainLoopRunning.value = 1
if multiprocess:
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs))
self._eventProcesses.append(t)
else:# thread not implemented yet
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs))
t.start()
def cleanEventQueue(self):
if self._eventQueue.empty():
return
@ -109,39 +75,12 @@ class eventManager():
self._eventQueue.get_nowait()
except Empty:
pass
def getEventQueue(self):
return self._eventQueue
def getMainLoopRunning(self):
return self.running
def putToEventQueue(self,event, data):
if not isinstance(event, fenrirEventType):
return False
self._eventQueue.put({"Type":event,"Data":data})
return True
def customEventWorkerThread(self, eventQueue, function, args):
#if not isinstance(eventQueue, Queue):
# return
if not callable(function):
return
while self.isMainEventLoopRunning():
try:
if args:
function(self._mainLoopRunning, eventQueue, args)
else:
function(self._mainLoopRunning, eventQueue)
except Exception as e:
print(e)
def simpleEventWorkerThread(self, event, function, args, runOnce = False):
if not isinstance(event, fenrirEventType):
return
if not callable(function):
return
while self.isMainEventLoopRunning():
Data = None
try:
if args != None:
Data = function(self._mainLoopRunning, args)
else:
Data = function()
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('eventManager:simpleEventWorkerThread:function():' + st(e),debug.debugLevel.ERROR)
self.putToEventQueue(event, Data)
if runOnce:
break

View File

@ -5,74 +5,86 @@
# By Chrys, Storm Dragon, and contributers.
from core import debug
from core.eventData import fenrirEventType
import time
from threading import Thread
from multiprocessing import Process, Queue
from multiprocessing.sharedctypes import Value
from ctypes import c_bool
class eventManager():
class processManager():
def __init__(self):
self._mainLoopRunning = Value(c_bool, True)
self._eventProcesses = []
self._Processes = []
self._Threads = []
def initialize(self, environment):
self.env = environment
self.running = self.env['runtime']['eventManager'].getMainLoopRunning()
self.addSimpleEventThread(fenrirEventType.HeartBeat, self.heartBeatTimer, multiprocess=False)
def shutdown(self):
self.terminateAllProcesses()
self.cleanEventQueue()
def heartBeatTimer(self, active):
try:
time.sleep(0.5)
except:
pass
return time.time()
def terminateAllProcesses(self):
time.sleep(1)
for proc in self._eventProcesses:
for proc in self._Processes:
try:
proc.terminate()
except Exception as e:
print(e)
def addCustomEventThread(self, function, pargs = None, multiprocess=False):
self._mainLoopRunning.value = 1
def addCustomEventThread(self, function, pargs = None, multiprocess = False, runOnce = False):
eventQueue = self.env['runtime']['eventManager'].getEventQueue()
if multiprocess:
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs))
t = Process(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce))
self._Processes.append(t)
else:# thread not implemented yet
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs))
self._eventProcesses.append(t)
t = Thread(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce))
self._Threads.append(t)
t.start()
def addSimpleEventThread(self, event, function, pargs = None, multiprocess=False, runOnce = False):
self._mainLoopRunning.value = 1
def addSimpleEventThread(self, event, function, pargs = None, multiprocess = False, runOnce = False):
if multiprocess:
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs))
self._eventProcesses.append(t)
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce))
self._Processes.append(t)
else:# thread not implemented yet
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs))
t = Thread(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce))
self._Threads.append(t)
t.start()
def customEventWorkerThread(self, eventQueue, function, args):
def customEventWorkerThread(self, eventQueue, function, pargs = None, runOnce = False):
#if not isinstance(eventQueue, Queue):
# return
if not callable(function):
return
while self.isMainEventLoopRunning():
while self.running.value:
try:
if args:
function(self._mainLoopRunning, eventQueue, args)
if pargs:
function(self.running, eventQueue, pargs)
else:
function(self._mainLoopRunning, eventQueue)
function(self.running, eventQueue)
except Exception as e:
print(e)
if runOnce:
break
def simpleEventWorkerThread(self, event, function, args, runOnce = False):
def simpleEventWorkerThread(self, event, function, pargs = None, runOnce = False):
if not isinstance(event, fenrirEventType):
return
if not callable(function):
return
while self.isMainEventLoopRunning():
while self.running.value:
Data = None
try:
if args != None:
Data = function(self._mainLoopRunning, args)
if pargs:
Data = function(self.running, pargs)
else:
Data = function()
Data = function(self.running)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('eventManager:simpleEventWorkerThread:function():' + st(e),debug.debugLevel.ERROR)
self.putToEventQueue(event, Data)
self.env['runtime']['debug'].writeDebugOut('processManager:simpleEventWorkerThread:function():' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['eventManager'].putToEventQueue(event, Data)
if runOnce:
break

View File

@ -9,6 +9,7 @@ currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(in
fenrirPath = os.path.dirname(currentdir)
from configparser import ConfigParser
from core import processManager
from core import eventManager
from core import inputManager
from core import outputManager
@ -321,6 +322,8 @@ class settingsManager():
environment['runtime']['fenrirManager'] = fenrirManager
environment['runtime']['eventManager'] = eventManager.eventManager()
environment['runtime']['eventManager'].initialize(environment)
environment['runtime']['processManager'] = processManager.processManager()
environment['runtime']['processManager'].initialize(environment)
environment['runtime']['inputManager'] = inputManager.inputManager()
environment['runtime']['inputManager'].initialize(environment)
environment['runtime']['outputManager'] = outputManager.outputManager()

View File

@ -52,10 +52,10 @@ class driver():
return
self.updateInputDevices()
if _udevAvailable:
self.env['runtime']['eventManager'].addCustomEventThread(self.plugInputDeviceWatchdogUdev)
self.env['runtime']['processManager'].addCustomEventThread(self.plugInputDeviceWatchdogUdev)
else:
self.env['runtime']['eventManager'].addSimpleEventThread(fenrirEventType.PlugInputDevice, self.plugInputDeviceWatchdogTimer)
self.env['runtime']['eventManager'].addSimpleEventThread(fenrirEventType.KeyboardInput, self.inputWatchdog, {'dev':self.iDevicesFD})
self.env['runtime']['processManager'].addSimpleEventThread(fenrirEventType.PlugInputDevice, self.plugInputDeviceWatchdogTimer)
self.env['runtime']['processManager'].addSimpleEventThread(fenrirEventType.KeyboardInput, self.inputWatchdog, {'dev':self.iDevicesFD})
def plugInputDeviceWatchdogUdev(self,active , eventQueue):
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
@ -64,12 +64,11 @@ class driver():
while active:
devices = monitor.poll(2)
if devices:
print('drin')
eventQueue.put({"Type":fenrirEventType.PlugInputDevice,"Data":''})
#self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')
return time.time()
def plugInputDeviceWatchdogTimer(self):
def plugInputDeviceWatchdogTimer(self, active):
time.sleep(2.5)
return time.time()
def shutdown(self):

View File

@ -38,7 +38,7 @@ class driver():
self.hichar = None
def initialize(self, environment):
self.env = environment
self.env['runtime']['eventManager'].addCustomEventThread(self.updateWatchdog)
self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog)
def shutdown(self):
pass
def getCurrScreen(self):