From bb1d982669c44a990ffc926f4666b6aa72237619 Mon Sep 17 00:00:00 2001 From: Thomas B Thompson Date: Mon, 10 Jan 2011 22:23:49 -0500 Subject: Worked on getting the threading stuff consolidated in ThreadedSmootCoreObject. Also set up a decent system for SmootCoreObjects to kill the whole application in a managed fashion. --- operationscore/Input.py | 14 ++++++++------ operationscore/Renderer.py | 1 - operationscore/SmootCoreObject.py | 27 +++++++++++++++++++++++++-- operationscore/ThreadedSmootCoreObject.py | 8 ++------ 4 files changed, 35 insertions(+), 15 deletions(-) (limited to 'operationscore') diff --git a/operationscore/Input.py b/operationscore/Input.py index 3dd74cf..2ee3c3c 100644 --- a/operationscore/Input.py +++ b/operationscore/Input.py @@ -11,28 +11,28 @@ import pdb class Input(ThreadedSmootCoreObject): #Event scope is a function pointer the function that will get called when #an Parent is raised. - def __init__(self, argDict): + def init(self): self.eventQueue = [] - self.parentScope = argDict['parentScope'] - self.argDict = argDict - if not 'RefreshInterval' in argDict: + if not 'RefreshInterval' in self.argDict: print 'RefreshInterval not defined. Defaulting to .5s.' self.argDict['RefreshInterval'] = 500 + self.parentScope = self.argDict['parentScope'] self.inputInit() - threading.Thread.__init__(self) - self.daemon = True #This kills this thread when the main thread stops + def respond(self, eventDict): #if eventDict != []: self.parentScope.lock.acquire() self.parentScope.processResponse(self.argDict, eventDict) self.parentScope.lock.release() time.sleep(.001) + def parentAlive(self): try: parentAlive = self.parentScope.alive() return parentAlive except: return False + def run(self): while 1: try: @@ -43,8 +43,10 @@ class Input(ThreadedSmootCoreObject): self.acquireLock() self.sensingLoop() self.releaseLock() + def sensingLoop(self): pass + def inputInit(self): pass diff --git a/operationscore/Renderer.py b/operationscore/Renderer.py index 88da606..ed88a8c 100644 --- a/operationscore/Renderer.py +++ b/operationscore/Renderer.py @@ -7,7 +7,6 @@ from operationscore.ThreadedSmootCoreObject import * class Renderer(ThreadedSmootCoreObject): def init(self): self.initRenderer() - threading.Thread.__init__(self) def render(lightSystem): pass def initRenderer(self): diff --git a/operationscore/SmootCoreObject.py b/operationscore/SmootCoreObject.py index c481776..8b36f4d 100644 --- a/operationscore/SmootCoreObject.py +++ b/operationscore/SmootCoreObject.py @@ -2,39 +2,62 @@ import pdb import threading import thread import util.Config as configGetter + class SmootCoreObject(object): def __init__(self, argDict, skipValidation = False): + self.dieListeners = [] self.argDict = argDict self.validateArgs(self.className()+'.params') self.lock = thread.allocate_lock() - self.init() #call init of inheriting class #put everything into attributes for speed for key in argDict: setattr(self, key, argDict[key]) + self.init() #call init of inheriting class # self.__setitem__ = self.argDict.__setitem__ # self.__getitem__ = self.argDict.__getitem__ + def init(self): pass + def acquireLock(self): - self.lock = thread.allocate_lock() #TODO: fix. + self.lock = thread.allocate_lock() #TODO: fix. -- investigate this, it should only have to be run once in the initialization. self.lock.acquire() + def releaseLock(self): self.lock.release() + def className(self): return self.__class__.__name__ + def __setitem__(self,k, item): self.argDict[k] = item + def __getitem__(self, item): if item in self.argDict: return self.argDict[item] else: return None + def __getiter__(self): return self.argDict.__getiter__() + def validateArgs(self, argFileName): self.validateArgDict(configGetter.loadParamRequirementDict(argFileName))#util #caches for us, woo! + def validateArgDict(self, validationDict): for item in validationDict: if not item in self.argDict: raise Exception(validationDict[item]) + + def addDieListener(self, listener): + if listener not in self.dieListeners: + self.dieListeners.append(listener) + + def removeDieListener(self, listener): + if listener in self.dieListeners: + self.dieListeners.remove(listener) + + def die(self): + for listener in self.dieListeners: + listener.handleDie(self) diff --git a/operationscore/ThreadedSmootCoreObject.py b/operationscore/ThreadedSmootCoreObject.py index 90611bc..967ee35 100644 --- a/operationscore/ThreadedSmootCoreObject.py +++ b/operationscore/ThreadedSmootCoreObject.py @@ -5,10 +5,6 @@ import util.Config as configGetter from operationscore.SmootCoreObject import SmootCoreObject class ThreadedSmootCoreObject(SmootCoreObject, threading.Thread): def __init__(self, argDict, skipValidation = False): - self.argDict = argDict - self.validateArgs(self.className()+'.params') - self.lock = thread.allocate_lock() + SmootCoreObject.__init__(self, argDict, skipValidation) threading.Thread.__init__(self) - self.init() #call init of inheriting class - # self.__setitem__ = self.argDict.__setitem__ - # self.__getitem__ = self.argDict.__getitem__ + self.daemon = True #This kills this thread when the main thread stops -- cgit v1.2.3