Skip to content
Snippets Groups Projects
Commit cedaf7c2 authored by Ramin Yaghoubzadeh's avatar Ramin Yaghoubzadeh
Browse files
parents 40af8667 2348549a
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python from notifier import ComponentNotifier
# -*- coding: utf-8 -*-
'''
This file intentionally left blank (for now)
@author: ryaghoub
'''
...@@ -8,30 +8,30 @@ Created on Dec 20, 2012 ...@@ -8,30 +8,30 @@ Created on Dec 20, 2012
from __future__ import with_statement from __future__ import with_statement
import threading import threading
from ipaaca import IUEventType import ipaaca
from ipaaca import Message
NotificationState = ipaaca.enum(
NEW = 'new',
OLD = 'old',
DOWN = 'down'
)
class ComponentNotifier(object): class ComponentNotifier(object):
'''
classdocs
'''
NOTIFY_CATEGORY = "componentNotify"; NOTIFY_CATEGORY = "componentNotify"
SEND_CATEGORIES = "send_categories"; SEND_CATEGORIES = "send_categories"
RECEIVE_CATEGORIES = "recv_categories"; RECEIVE_CATEGORIES = "recv_categories"
STATE = "state"; STATE = "state"
NAME = "name"; NAME = "name"
FUNCTION = "function"; FUNCTION = "function"
def __init__(self, componentName, componentFunction, sendCategories, receiveCategories, outBuffer, inBuffer): def __init__(self, componentName, componentFunction, sendCategories, receiveCategories, outBuffer=None, inBuffer=None):
'''
Constructor
'''
self.componentName = componentName self.componentName = componentName
self.componentFunction = componentFunction self.componentFunction = componentFunction
self.sendCategories = frozenset(sendCategories) self.sendCategories = frozenset(sendCategories)
self.receiveCategories = frozenset(receiveCategories) self.receiveCategories = frozenset(receiveCategories)
self.inBuffer = inBuffer self.inBuffer = inBuffer if inBuffer is not None else ipaaca.InputBuffer(componentName + 'Notifier')
self.outBuffer = outBuffer self.outBuffer = outBuffer if outBuffer is not None else ipaaca.OutputBuffer(componentName + 'Notifier')
self.initialized = False self.initialized = False
self.notificationHandlers = [] self.notificationHandlers = []
self.initializeLock = threading.Lock() self.initializeLock = threading.Lock()
...@@ -40,17 +40,17 @@ class ComponentNotifier(object): ...@@ -40,17 +40,17 @@ class ComponentNotifier(object):
def _submit_notify(self, isNew): def _submit_notify(self, isNew):
with self.submitLock: with self.submitLock:
notifyIU = Message(ComponentNotifier.NOTIFY_CATEGORY) notifyIU = ipaaca.Message(ComponentNotifier.NOTIFY_CATEGORY)
notifyIU.payload[ComponentNotifier.NAME] = self.componentName notifyIU.payload = {
notifyIU.payload[ComponentNotifier.FUNCTION] = self.componentFunction ComponentNotifier.NAME: self.componentName,
notifyIU.payload[ComponentNotifier.SEND_CATEGORIES] = ",".join(self.sendCategories) ComponentNotifier.FUNCTION: self.componentFunction,
notifyIU.payload[ComponentNotifier.RECEIVE_CATEGORIES] = ",".join(self.receiveCategories) ComponentNotifier.SEND_CATEGORIES: ",".join(self.sendCategories),
notifyIU.payload[ComponentNotifier.STATE] = "new" if isNew else "old" ComponentNotifier.RECEIVE_CATEGORIES: ",".join(self.receiveCategories),
ComponentNotifier.STATE: NotificationState.NEW if isNew else NotificationState.OLD,
}
self.outBuffer.add(notifyIU) self.outBuffer.add(notifyIU)
def _handle_iu_event(self, iu, event_type, local): 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: if iu.payload[ComponentNotifier.NAME] == self.componentName:
return return
with self.notificationHandlerLock: with self.notificationHandlerLock:
...@@ -67,8 +67,8 @@ class ComponentNotifier(object): ...@@ -67,8 +67,8 @@ class ComponentNotifier(object):
def initialize(self): def initialize(self):
with self.initializeLock: with self.initializeLock:
if (not self.initialized): if (not self.initialized):
self.inBuffer.register_handler(self._handle_iu_event, [IUEventType.MESSAGE], ComponentNotifier.NOTIFY_CATEGORY) self.inBuffer.register_handler(self._handle_iu_event, ipaaca.IUEventType.MESSAGE, ComponentNotifier.NOTIFY_CATEGORY)
self._submit_notify(True) self._submit_notify(isNew=True)
self.initialized = True self.initialized = True
...@@ -3,7 +3,12 @@ Created on Dec 20, 2012 ...@@ -3,7 +3,12 @@ Created on Dec 20, 2012
@author: hvanwelbergen @author: hvanwelbergen
''' '''
import os
import time
import unittest import unittest
from mockito import mock from mockito import mock
from mockito import verify from mockito import verify
from mockito import any from mockito import any
...@@ -14,9 +19,7 @@ from ipaaca import IUEventType ...@@ -14,9 +19,7 @@ from ipaaca import IUEventType
from ipaaca import Message from ipaaca import Message
from ipaaca import InputBuffer from ipaaca import InputBuffer
from ipaaca import OutputBuffer from ipaaca import OutputBuffer
from ipaaca.util.notifier import ComponentNotifier from ipaaca.util import ComponentNotifier
import time
import os
class IUCategoryMatcher(BaseMatcher): class IUCategoryMatcher(BaseMatcher):
def __init__(self, expected_cat): 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