def connectToODMChannel(self): self.odmListener = ODMListener() self.odmListener.connect(self.domManager) self.odmListener.deviceManagerAdded.addListener(weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.deviceManagerRemoved.addListener(weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.applicationAdded.addListener(weakobj.boundmethod(self.applicationODMEvent)) self.odmListener.applicationRemoved.addListener(weakobj.boundmethod(self.applicationODMEvent)) self.odmListener.applicationFactoryAdded.addListener(weakobj.boundmethod(self.unusedODMEvent)) self.odmListener.applicationFactoryRemoved.addListener(weakobj.boundmethod(self.unusedODMEvent)) self.odmListener.deviceAdded.addListener(weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.deviceRemoved.addListener(weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.serviceAdded.addListener(weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.serviceRemoved.addListener(weakobj.boundmethod(self.devMgrODMEvent))
def connect(self, domain): log.trace('Connecting %s', self.CHANNEL_NAME) # Generate a unique identifier for each event channel connection to prevent # collisions if multiple instances are running. self.__identifier = str(uuid4()) self.__consumer = events.GenericEventConsumer(weakobj.boundmethod(self.push), keep_structs=True) self.__domainManager = domain self.__domainManager.registerWithEventChannel(self.__consumer._this(), self.__identifier, self.CHANNEL_NAME)
def __init__(self, profile, spd, scd, prf, instanceName, refid, impl=None, idmListener=None): DomainComponent.__init__(self, profile, spd, scd, prf, instanceName, refid, impl) # NB: The model.Device class is not directly in the hierarchy, but any # concrete subclass should inherit from Device or one of its subclasses. model.Device._buildAPI(self) self.__adminState = CorbaAttribute(weakobj.boundmethod(self._get_adminState), weakobj.boundmethod(self._set_adminState)) self.__adminState.changed.addListener(weakobj.boundmethod(self.adminStateChanged)) self.__operationalState = CorbaAttribute(weakobj.boundmethod(self._get_operationalState)) self.__operationalState.changed.addListener(weakobj.boundmethod(self.operationalStateChanged)) self.__usageState = CorbaAttribute(weakobj.boundmethod(self._get_usageState)) self.__usageState.changed.addListener(weakobj.boundmethod(self.usageStateChanged)) self.__idmListener = idmListener # If the domain IDM channel is available, listen for state changes. if self.__idmListener: # Only receive notifications for this device by filtering based on # the identifier. identifier = self._get_identifier() def matcher(deviceId, stateChangeFrom, stateChangeTo): return deviceId == identifier weakobj.addListener(self.__idmListener.administrativeStateChanged, self.__adminStateChangeEvent, matcher) weakobj.addListener(self.__idmListener.operationalStateChanged, self.__operationalStateChangeEvent, matcher) weakobj.addListener(self.__idmListener.usageStateChanged, self.__usageStateChangeEvent, matcher)
def test_WeakBoundMethod(self): """ Test that weakly-bound methods can be called like a regular bound method, and are invalidated on object destruction. """ obj = TestClass() foo = weakobj.boundmethod(obj.foo) # Check that the weakly-bound method has the same behavior. self.assertEqual(obj.foo(), foo()) # Check that weakly-bound notifications behave correctly. self._notificationTest(weakobj.boundmethod(obj.bar)) # Delete the object and ensure that the method is invalidated. del obj try: foo() except weakref.ReferenceError: pass else: self.fail('Weak bound method should be invalidated')
def connectToODMChannel(self): self.odmListener = ODMListener() self.odmListener.connect(self.domManager) self.odmListener.deviceManagerAdded.addListener( weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.deviceManagerRemoved.addListener( weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.applicationAdded.addListener( weakobj.boundmethod(self.applicationODMEvent)) self.odmListener.applicationRemoved.addListener( weakobj.boundmethod(self.applicationODMEvent)) self.odmListener.applicationFactoryAdded.addListener( weakobj.boundmethod(self.unusedODMEvent)) self.odmListener.applicationFactoryRemoved.addListener( weakobj.boundmethod(self.unusedODMEvent)) self.odmListener.deviceAdded.addListener( weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.deviceRemoved.addListener( weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.serviceAdded.addListener( weakobj.boundmethod(self.devMgrODMEvent)) self.odmListener.serviceRemoved.addListener( weakobj.boundmethod(self.devMgrODMEvent))
def test_WeakBoundMethodCallback(self): """ Check that weakly-bound method finalizer callbacks are called as expected. """ children = [] # Add a weakly-bound member function that will remove itself from the # list when the object is destroyed. obj = TestClass() children.append(weakobj.boundmethod(obj.foo, children.remove)) self.assertEqual(len(children), 1) # Add the same member function but with a different object. obj2 = TestClass() children.append(weakobj.boundmethod(obj2.foo, children.remove)) self.assertEqual(len(children), 2) # Add a notification function from a third object. obj3 = TestClass() children.append(weakobj.boundmethod(obj3.bar, children.remove)) self.assertEqual(len(children), 3) # Delete the second object and check that the callback removed only its # weak bound method from the list del obj2 self.assertEqual(len(children), 2) self.assertEqual(children[0], weakobj.boundmethod(obj.foo)) # Delete the first object, which should leave only the third object's # notification method del obj self.assertEqual(len(children), 1) self.assertEqual(children[0], weakobj.boundmethod(obj3.bar)) del obj3 self.assertEqual(len(children), 0)
def __init__(self, profile, spd, scd, prf, instanceName, refid, impl=None, idmListener=None): DomainComponent.__init__(self, profile, spd, scd, prf, instanceName, refid, impl) # NB: The model.Device class is not directly in the hierarchy, but any # concrete subclass should inherit from Device or one of its subclasses. model.Device._buildAPI(self) self.__adminState = CorbaAttribute( weakobj.boundmethod(self._get_adminState), weakobj.boundmethod(self._set_adminState)) self.__adminState.changed.addListener( weakobj.boundmethod(self.adminStateChanged)) self.__operationalState = CorbaAttribute( weakobj.boundmethod(self._get_operationalState)) self.__operationalState.changed.addListener( weakobj.boundmethod(self.operationalStateChanged)) self.__usageState = CorbaAttribute( weakobj.boundmethod(self._get_usageState)) self.__usageState.changed.addListener( weakobj.boundmethod(self.usageStateChanged)) self.__idmListener = idmListener # If the domain IDM channel is available, listen for state changes. if self.__idmListener: # Only receive notifications for this device by filtering based on # the identifier. identifier = self._get_identifier() def matcher(deviceId, stateChangeFrom, stateChangeTo): return deviceId == identifier weakobj.addListener(self.__idmListener.administrativeStateChanged, self.__adminStateChangeEvent, matcher) weakobj.addListener(self.__idmListener.operationalStateChanged, self.__operationalStateChangeEvent, matcher) weakobj.addListener(self.__idmListener.usageStateChanged, self.__usageStateChangeEvent, matcher)