From b4c8608cb480c40f50383d277a4bd70d6ce4450e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Ba=C5=9Faran?= Date: Fri, 4 Mar 2016 18:01:47 +0200 Subject: [PATCH] added event manager --- opt/ahenk/ahenkd.py | 11 +++++++++++ opt/ahenk/base/Scope.py | 7 +++++++ opt/ahenk/base/event/EventBase.py | 19 +++++++++++++++++++ opt/ahenk/base/event/EventManager.py | 14 ++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 opt/ahenk/base/event/EventBase.py create mode 100644 opt/ahenk/base/event/EventManager.py diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index 107077d..88e8b8d 100644 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -10,6 +10,7 @@ from base.messaging.MessageReceiver import MessageReceiver from base.messaging.MessageSender import MessageSender from base.registration.Registration import Registration from base.messaging.MessageResponseQueue import MessageResponseQueue +from base.event.EventManager import EventManager from base.plugin.PluginManager import PluginManager from base.task.TaskManager import TaskManager from multiprocessing import Process @@ -20,6 +21,10 @@ import sys,logging,queue,time class AhenkDeamon(BaseDaemon): """docstring for AhenkDeamon""" + def reload(self,msg): + # reload service here + pass + def run(self): print ("merhaba dunya") @@ -39,6 +44,12 @@ class AhenkDeamon(BaseDaemon): logger.info("this is info log") globalscope.setLogger(logger) + eventManager = EventManager() + globalscope.setEventManager(eventManager) + + eventManager.register_event('registration_ok',self.reload) + + pluginManager = PluginManager() pluginManager.loadPlugins() globalscope.setPluginManager(pluginManager) diff --git a/opt/ahenk/base/Scope.py b/opt/ahenk/base/Scope.py index b5e98f9..a8bbe41 100644 --- a/opt/ahenk/base/Scope.py +++ b/opt/ahenk/base/Scope.py @@ -17,6 +17,7 @@ class Scope(object): self.pluginManager=None self.taskManager=None self.responseQueue=None + self.eventManager=None @staticmethod def getInstance(): @@ -71,3 +72,9 @@ class Scope(object): def setResponseQueue(self,responseQueue): self.responseQueue=responseQueue + + def getEventManager(self): + return self.eventManager + + def setEventManager(self,eventManager): + self.eventManager=eventManager diff --git a/opt/ahenk/base/event/EventBase.py b/opt/ahenk/base/event/EventBase.py new file mode 100644 index 0000000..86bd198 --- /dev/null +++ b/opt/ahenk/base/event/EventBase.py @@ -0,0 +1,19 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# @author: İsmail BAŞARAN + +class EventBase(): + listeners = [] + def __init__(self): + self.listeners.append(self) + self.listener_events = [] + def register_event(self, event_name, callback_func): + self.listener_events.append({'event_name' : event_name, 'callback_func' : callback_func}) + + +class Event(): + def __init__(self, event_name, *callback_args): + for listener in EventBase.listeners: + for listener_cls in listener.listener_events: + if listener_cls['event_name'] == event_name: + listener_cls['callback_func'](*callback_args) diff --git a/opt/ahenk/base/event/EventManager.py b/opt/ahenk/base/event/EventManager.py new file mode 100644 index 0000000..7130240 --- /dev/null +++ b/opt/ahenk/base/event/EventManager.py @@ -0,0 +1,14 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# @author: İsmail BAŞARAN + +from base.event.EventBase import EventBase,Event + +class EventManager(EventBase): + """docstring for EventManager""" + def __init__(self): + EventBase.__init__(self) + + + def fireEvent(self,event_name,*args): + Event(event_name,*args)