summaryrefslogtreecommitdiff
path: root/src/leap/common/events/component.py
diff options
context:
space:
mode:
authorTomas Touceda <chiiph@riseup.net>2013-06-19 14:31:49 -0300
committerTomas Touceda <chiiph@riseup.net>2013-06-19 14:31:49 -0300
commitc84521f7881dc7b4197f57300bfe0511aa47dac2 (patch)
tree0ec1938b79b527ed94e9d54a21122ce88c0dab39 /src/leap/common/events/component.py
parentdbbf33d27268266088ee1ab15fc04cef33259250 (diff)
parenta08b198d889396d25182bb9716817311bcc3be47 (diff)
Merge remote-tracking branch 'drebs/feature/2696-implement-unregister-in-events-2' into develop
Diffstat (limited to 'src/leap/common/events/component.py')
-rw-r--r--src/leap/common/events/component.py55
1 files changed, 53 insertions, 2 deletions
diff --git a/src/leap/common/events/component.py b/src/leap/common/events/component.py
index 9932190..029d1ac 100644
--- a/src/leap/common/events/component.py
+++ b/src/leap/common/events/component.py
@@ -56,6 +56,7 @@ class CallbackAlreadyRegistered(Exception):
"""
Raised when trying to register an already registered callback.
"""
+ pass
def ensure_component_daemon():
@@ -111,7 +112,7 @@ def register(signal, callback, uid=None, replace=False, reqcbk=None,
callback identified by the given uid and replace is False.
:return: the response from server for synch calls or nothing for asynch
- calls
+ calls.
:rtype: leap.common.events.events_pb2.EventsResponse or None
"""
ensure_component_daemon() # so we can receive registered signals
@@ -140,6 +141,56 @@ def register(signal, callback, uid=None, replace=False, reqcbk=None,
str(request)[:40])
return service.register(request, callback=reqcbk, timeout=timeout)
+def unregister(signal, uid=None, reqcbk=None, timeout=1000):
+ """
+ Unregister a callback.
+
+ If C{uid} is specified, unregisters only the callback identified by that
+ unique id. Otherwise, unregisters all callbacks
+
+ :param signal: the signal that causes the callback to be launched
+ :type signal: int (see the `events.proto` file)
+ :param uid: a unique id for the callback
+ :type uid: int
+ :param reqcbk: a callback to be called when a response from server is
+ received
+ :type reqcbk: function
+ callback(leap.common.events.events_pb2.EventResponse)
+ :param timeout: the timeout for synch calls
+ :type timeout: int
+
+ :return: the response from server for synch calls or nothing for asynch
+ calls or None if no callback is registered for that signal or uid.
+ :rtype: leap.common.events.events_pb2.EventsResponse or None
+ """
+ if signal not in registered_callbacks or not registered_callbacks[signal]:
+ logger.warning("No callback registered for signal %d." % signal)
+ return None
+ # unregister callback locally
+ cbklist = registered_callbacks[signal]
+ if uid is not None:
+ if filter(lambda (cbkuid, _): cbkuid == uid, cbklist) == []:
+ logger.warning("No callback registered for uid %d." % st)
+ return None
+ registered_callbacks[signal] = filter(lambda(x, y): x != uid, cbklist)
+ else:
+ # exclude all callbacks for given signal
+ registered_callbacks[signal] = []
+ # unregister port in server if there are no more callbacks for this signal
+ if not registered_callbacks[signal]:
+ request = proto.UnregisterRequest()
+ request.event = signal
+ request.port = EventsComponentDaemon.get_instance().get_port()
+ request.mac_method = mac_auth.MacMethod.MAC_NONE
+ request.mac = ""
+ service = RpcService(proto.EventsServerService_Stub,
+ server.SERVER_PORT, 'localhost')
+ logger.info(
+ "Sending unregistration request to server on port %s: %s",
+ server.SERVER_PORT,
+ str(request)[:40])
+ return service.unregister(request, callback=reqcbk, timeout=timeout)
+
def signal(signal, content="", mac_method="", mac="", reqcbk=None,
timeout=1000):
@@ -168,7 +219,7 @@ def signal(signal, content="", mac_method="", mac="", reqcbk=None,
:type timeout: int
:return: the response from server for synch calls or nothing for asynch
- calls
+ calls.
:rtype: leap.common.events.events_pb2.EventsResponse or None
"""
request = proto.SignalRequest()