From 3a90c55b82034cada54613c46b5fc491085180a9 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Fri, 20 Nov 2015 01:16:32 -0400 Subject: service hooks [WIP] --- bonafide/src/leap/bonafide/bonafide_cli2 | 10 ++++++- bonafide/src/leap/bonafide/zmq.tac | 8 ++++++ bonafide/src/leap/bonafide/zmq_service.py | 48 +++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/bonafide/src/leap/bonafide/bonafide_cli2 b/bonafide/src/leap/bonafide/bonafide_cli2 index 18506d9..bf05a99 100755 --- a/bonafide/src/leap/bonafide/bonafide_cli2 +++ b/bonafide/src/leap/bonafide/bonafide_cli2 @@ -47,6 +47,11 @@ parser.add_argument("--username", dest="username", help="user to operate with") parser.add_argument("--shutdown", dest="do_shutdown", action="store_true", help="shutdown the bonafide service.") + +# XXX DEBUG -------------------------------------------------------- +parser.add_argument("--debug", dest="do_debug", action="store_true", + help="debug command, can be anything") +# ------------------------------------------------------------------ ns = parser.parse_args() @@ -97,11 +102,14 @@ def send_command(): passwd = getpass.getpass() data = ("logout", ns.username, passwd) + elif ns.do_debug: + data = ("get_soledad",) + s = get_zmq_connection() try: d = s.sendMsg(*data) except zmq.error.Again: - print Fore.RED + "[ERROR] Server is down :(" + Fore.RESET + print Fore.RED + "[ERROR] Server is down" + Fore.RESET d.addCallback(cb) d.addCallback(lambda x: reactor.stop()) diff --git a/bonafide/src/leap/bonafide/zmq.tac b/bonafide/src/leap/bonafide/zmq.tac index d3a9625..f429ff5 100644 --- a/bonafide/src/leap/bonafide/zmq.tac +++ b/bonafide/src/leap/bonafide/zmq.tac @@ -1,10 +1,18 @@ # Run as: twistd -n -y zmq.tac from twisted.application import service from leap.bonafide.zmq_service import BonafideZMQService +from leap.bonafide.soledad_service import SoledadService top_service = service.MultiService() bonafide_zmq_service = BonafideZMQService() bonafide_zmq_service.setServiceParent(top_service) +# XXX DEBUG ------------------------------------- +# This SHOULD BE moved to BITMASK-CORE. +soledad_service = SoledadService() +soledad_service.setName("soledad") +soledad_service.setServiceParent(top_service) +#------------------------------------------------ + application = service.Application("bonafide") top_service.setServiceParent(application) diff --git a/bonafide/src/leap/bonafide/zmq_service.py b/bonafide/src/leap/bonafide/zmq_service.py index 38c0305..59ef47b 100644 --- a/bonafide/src/leap/bonafide/zmq_service.py +++ b/bonafide/src/leap/bonafide/zmq_service.py @@ -37,26 +37,44 @@ class BonafideZMQService(service.Service): self._bonafide = BonafideProtocol() self._conn = None + self.service_hooks = {} + def startService(self): zf = ZmqFactory() e = ZmqEndpoint("bind", config.ENDPOINT) - self._conn = _BonafideZmqREPConnection(zf, e, self._bonafide) + self._conn = _BonafideZmqREPConnection(zf, e, self._bonafide, self) reactor.callWhenRunning(self._conn.do_greet) - #def stopService(self): - # pass + def register_hook(self, kind, service): + print "REGISTERING HOOK", kind, service + self.service_hooks[kind] = service + + + # def stopService(self): + # pass class _BonafideZmqREPConnection(ZmqREPConnection): - def __init__(self, zf, e, bonafide): + def __init__(self, zf, e, bonafide, service): + # XXX passing a ref to the service, + # to be able to access sibling services ZmqREPConnection.__init__(self, zf, e) self._bonafide = bonafide + self._service = service + + def get_sibling_service(self, kind): + return self._service.parent.getServiceNamed(kind) + + def get_hooked_service(self, kind): + hooks = self._service.service_hooks + if kind in hooks: + return self.get_sibling_service(hooks[kind]) def do_greet(self): - print "Bonafide service running..." + print "Starging Bonafide service" def do_bye(self): print "Bonafide service stopped. Have a nice day." @@ -72,11 +90,13 @@ class _BonafideZmqREPConnection(ZmqREPConnection): cmd = parts[0] + # TODO split using dispatcher pattern + if cmd == "shutdown": defer_reply('ok, shutting down') reactor.callLater(1, self.do_bye) - if cmd not in COMMANDS: + if cmd not in COMMANDS + ("get_soledad",): response = 'INVALID COMMAND' defer_reply(response) @@ -88,8 +108,18 @@ class _BonafideZmqREPConnection(ZmqREPConnection): d.addErrback(log_err) elif cmd == 'authenticate': + + def activate_hook(token): + hook_service = self.get_hooked_service('on_auth') + if hook_service: + hook_service.activate_hook( + # TODO GET UUID TOO!! + 'on_auth', username=username, uuid=uuid, token=token) + return token + username, password = parts[1], parts[2] d = self._bonafide.do_authenticate(username, password) + d.addCallback(activate_hook) d.addCallback(lambda response: defer_reply( 'TOKEN -> %s' % response)) d.addErrback(log_err) @@ -104,3 +134,9 @@ class _BonafideZmqREPConnection(ZmqREPConnection): elif cmd == 'stats': response = self._bonafide.do_stats() defer_reply(response) + + # XXX DEBUG --------------------------------------------------------- + elif cmd == 'get_soledad': + response = str(self._service.parent.getServiceNamed("soledad")) + defer_reply(response) + # ------------------------------------------------------------------ -- cgit v1.2.3