Skip to content
Snippets Groups Projects
Commit 2348549a authored by Hendrik Buschmeier's avatar Hendrik Buschmeier
Browse files

Added Python ComponentNotifier to __init__.

parent e44a5214
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
This file intentionally left blank (for now)
@author: ryaghoub
'''
from notifier import ComponentNotifier
......@@ -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
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment