def testInjection(self): f = open('scrutator/tests/tool.fakepy', 'r') content = f.read() bus = EventManager() bus.bind(FileContent().getType(),FileContentListener()) #bus.bind('all',PrintListener()) bus.push(FileContent(filename="hello/world/nimp/__init__.py",content="print \"init\"")) bus.push(FileContent(filename="fake/__init__.py",content="print \"init\"")) bus.push(FileContent(filename="fake/fake.py",content=content)) bus.push(DieEvent(test="hello")) import fake.fake
class BasicServerBrain(BasicBrain): transport_event = ContainerEvent delay_first_init = 60 launched = False def __init__(self): super(BasicServerBrain, self).__init__() self.hostDict = dict() self.localbus = EventManager() self.localbus.bind(PingEvent().getType(), self.onPing) reactor.callLater(self.delay_first_init, self.onLaunch) def garbageAgent(self): for agentName in self.hostDict.keys(): agent = self.hostDict[agentName] if agent.getLastPing() > 360: self.onLooseAgent(agentName) del(self.hostDict[agentName]) def onLaunch(self): launched = True def onPing(self, eventObj, evtMgr): source = eventObj.source if not self.hostDict.has_key(source): self.hostDict[source] = Agent() self.onFirstPing(eventObj, evtMgr) if self.launched: self.onFirstInit(eventObj, evtMgr) self.hostDict[source].update() def onFirstPing(self, eventObj, evtMgr): pass def onFirstInit(self, eventObj, evtMgr): pass def onInit(self): super(BasicServerBrain, self).onInit() from scrutator.core.callback import ToBasicBrainLocalbusCallback callback = ToBasicBrainLocalbusCallback() gate_listener = GateListener(self.localbus, callback.callback) self.bus.bind(self.transport_event().getType(), gate_listener) self.localbus.bind('all', PrintListener()) #garbage agent which not respond from twisted.internet.task import LoopingCall lc = LoopingCall(self.garbageAgent) lc.start(60) def sendTo(self, to, msg): self.bus.getMessageBoxManager().push(SimpleEvent(to=to, msg=msg)) def getAgentList(self): return self.hostDict def getHostList(self): return self.hostDict.keys()