From 2348549afd0259114a2c2640516b31bfb10112cc Mon Sep 17 00:00:00 2001
From: Hendrik Buschmeier <hbuschme@uni-bielefeld.de>
Date: Thu, 23 May 2013 14:08:15 +0200
Subject: [PATCH] Added Python ComponentNotifier to __init__.

---
 ipaacalib/python/src/ipaaca/util/__init__.py |  9 +---
 ipaacalib/python/src/ipaaca/util/notifier.py | 54 ++++++++++----------
 ipaacalib/python/test/src/testnotifier.py    |  9 ++--
 3 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/ipaacalib/python/src/ipaaca/util/__init__.py b/ipaacalib/python/src/ipaaca/util/__init__.py
index 09ce39d..7962f53 100644
--- a/ipaacalib/python/src/ipaaca/util/__init__.py
+++ b/ipaacalib/python/src/ipaaca/util/__init__.py
@@ -1,9 +1,2 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-'''
-This file intentionally left blank (for now)
-
-@author: ryaghoub
-'''
-
+from notifier import ComponentNotifier
 
diff --git a/ipaacalib/python/src/ipaaca/util/notifier.py b/ipaacalib/python/src/ipaaca/util/notifier.py
index 227b90c..2782c8d 100644
--- a/ipaacalib/python/src/ipaaca/util/notifier.py
+++ b/ipaacalib/python/src/ipaaca/util/notifier.py
@@ -8,30 +8,30 @@ Created on Dec 20, 2012
 from __future__ import with_statement
 import threading
 
-from ipaaca import IUEventType
-from ipaaca import Message
+import ipaaca
+
+NotificationState = ipaaca.enum(
+    NEW = 'new',
+    OLD = 'old',
+    DOWN = 'down'
+)
+
 class ComponentNotifier(object):
-    '''
-    classdocs
-    '''
 
-    NOTIFY_CATEGORY = "componentNotify";
-    SEND_CATEGORIES = "send_categories";
-    RECEIVE_CATEGORIES = "recv_categories";
-    STATE = "state";
-    NAME = "name";
-    FUNCTION = "function";    
+    NOTIFY_CATEGORY = "componentNotify"
+    SEND_CATEGORIES = "send_categories"
+    RECEIVE_CATEGORIES = "recv_categories"
+    STATE = "state"
+    NAME = "name"
+    FUNCTION = "function"
     
-    def __init__(self, componentName, componentFunction, sendCategories, receiveCategories, outBuffer, inBuffer):
-        '''
-        Constructor
-        '''
+    def __init__(self, componentName, componentFunction, sendCategories, receiveCategories, outBuffer=None, inBuffer=None):
         self.componentName = componentName
         self.componentFunction = componentFunction
         self.sendCategories = frozenset(sendCategories)
         self.receiveCategories = frozenset(receiveCategories)
-        self.inBuffer = inBuffer
-        self.outBuffer = outBuffer
+        self.inBuffer = inBuffer if inBuffer is not None else ipaaca.InputBuffer(componentName + 'Notifier')
+        self.outBuffer = outBuffer if outBuffer is not None else ipaaca.OutputBuffer(componentName + 'Notifier')
         self.initialized = False
         self.notificationHandlers = []
         self.initializeLock = threading.Lock()
@@ -40,17 +40,17 @@ class ComponentNotifier(object):
     
     def _submit_notify(self, isNew):
         with self.submitLock:
-            notifyIU = Message(ComponentNotifier.NOTIFY_CATEGORY)
-            notifyIU.payload[ComponentNotifier.NAME] = self.componentName
-            notifyIU.payload[ComponentNotifier.FUNCTION] = self.componentFunction
-            notifyIU.payload[ComponentNotifier.SEND_CATEGORIES] = ",".join(self.sendCategories)
-            notifyIU.payload[ComponentNotifier.RECEIVE_CATEGORIES] =  ",".join(self.receiveCategories)
-            notifyIU.payload[ComponentNotifier.STATE] = "new" if isNew else "old"                
-            
+            notifyIU = ipaaca.Message(ComponentNotifier.NOTIFY_CATEGORY)
+            notifyIU.payload = {
+                ComponentNotifier.NAME: self.componentName,
+                ComponentNotifier.FUNCTION: self.componentFunction,
+                ComponentNotifier.SEND_CATEGORIES: ",".join(self.sendCategories),
+                ComponentNotifier.RECEIVE_CATEGORIES:  ",".join(self.receiveCategories),
+                ComponentNotifier.STATE: NotificationState.NEW if isNew else NotificationState.OLD,
+            }          
             self.outBuffer.add(notifyIU)
     
     def _handle_iu_event(self, iu, event_type, local):
-        #print("handle, iuname:"+iu.payload[ComponentNotifier.NAME]+" component name: "+self.componentName+" state "+iu.payload[ComponentNotifier.STATE])
         if iu.payload[ComponentNotifier.NAME] == self.componentName:
             return
         with self.notificationHandlerLock:
@@ -67,8 +67,8 @@ class ComponentNotifier(object):
     def initialize(self):
         with self.initializeLock:
             if (not self.initialized):
-                self.inBuffer.register_handler(self._handle_iu_event, [IUEventType.MESSAGE], ComponentNotifier.NOTIFY_CATEGORY)
-                self._submit_notify(True)
+                self.inBuffer.register_handler(self._handle_iu_event, ipaaca.IUEventType.MESSAGE, ComponentNotifier.NOTIFY_CATEGORY)
+                self._submit_notify(isNew=True)
                 self.initialized = True
 
 
diff --git a/ipaacalib/python/test/src/testnotifier.py b/ipaacalib/python/test/src/testnotifier.py
index 6d13d58..5a84f5e 100644
--- a/ipaacalib/python/test/src/testnotifier.py
+++ b/ipaacalib/python/test/src/testnotifier.py
@@ -3,7 +3,12 @@ Created on Dec 20, 2012
 
 @author: hvanwelbergen
 '''
+
+import os
+import time
 import unittest
+
+
 from mockito import mock
 from mockito import verify
 from mockito import any
@@ -14,9 +19,7 @@ from ipaaca import IUEventType
 from ipaaca import Message
 from ipaaca import InputBuffer
 from ipaaca import OutputBuffer
-from ipaaca.util.notifier import ComponentNotifier
-import time
-import os
+from ipaaca.util import ComponentNotifier
 
 class IUCategoryMatcher(BaseMatcher):
     def __init__(self, expected_cat):
-- 
GitLab