diff options
Diffstat (limited to 'bonafide/src/leap')
-rw-r--r-- | bonafide/src/leap/bonafide/_protocol.py (renamed from bonafide/src/leap/bonafide/service.py) | 11 | ||||
-rw-r--r-- | bonafide/src/leap/bonafide/zmq.tac | 10 | ||||
-rw-r--r-- | bonafide/src/leap/bonafide/zmq_service.py | 51 |
3 files changed, 42 insertions, 30 deletions
diff --git a/bonafide/src/leap/bonafide/service.py b/bonafide/src/leap/bonafide/_protocol.py index 24004fab..b093b9cc 100644 --- a/bonafide/src/leap/bonafide/service.py +++ b/bonafide/src/leap/bonafide/_protocol.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# service.py -# Copyright (C) 2014 LEAP +# _protocol.py +# Copyright (C) 2014-2015 LEAP # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -15,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ -Bonafide service. +Bonafide protocol. """ import os import resource @@ -36,9 +36,9 @@ from twisted.python import log COMMANDS = 'signup', 'authenticate', 'logout', 'stats' -class BonafideService(object): +class BonafideProtocol(object): """ - Expose the Bonafide Service API. + Expose the protocol that interacts with the Bonafide Service API. """ _apis = defaultdict(None) @@ -115,6 +115,7 @@ class BonafideService(object): return d def do_stats(self): + log.msg('Calculating Bonafide STATS') mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss return '[+] Bonafide service: [%s sessions] [Mem usage: %s KB]' % ( len(self._sessions), mem / 1024) diff --git a/bonafide/src/leap/bonafide/zmq.tac b/bonafide/src/leap/bonafide/zmq.tac new file mode 100644 index 00000000..d3a96253 --- /dev/null +++ b/bonafide/src/leap/bonafide/zmq.tac @@ -0,0 +1,10 @@ +# Run as: twistd -n -y zmq.tac +from twisted.application import service +from leap.bonafide.zmq_service import BonafideZMQService + +top_service = service.MultiService() +bonafide_zmq_service = BonafideZMQService() +bonafide_zmq_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 e33df3d9..df8e2344 100644 --- a/bonafide/src/leap/bonafide/zmq_service.py +++ b/bonafide/src/leap/bonafide/zmq_service.py @@ -15,23 +15,39 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ -Bonafide ZMQ Service +Bonafide ZMQ Service. """ from leap.bonafide import config -from leap.bonafide.service import BonafideService, COMMANDS +from leap.bonafide.protocol import BonafideProtocol, COMMANDS from txzmq import ZmqEndpoint, ZmqFactory, ZmqREPConnection +from twisted.application import service +from twisted.internet import reactor from twisted.python import log -class BonafideZmqREPConnection(ZmqREPConnection): +class BonafideZMQService(service.Service): - def initialize(self): - self._service = BonafideService() + def __init__(self): + self._bonafide = BonafideProtocol() + self._conn = None + + def startService(self): + zf = ZmqFactory() + e = ZmqEndpoint("bind", config.ENDPOINT) + self._conn = _BonafideZmqREPConnection(zf, e, self._bonafide) + reactor.callWhenRunning(self._conn.do_greet) + + +class _BonafideZmqREPConnection(ZmqREPConnection): + + def __init__(self, zf, e, bonafide): + ZmqREPConnection.__init__(self, zf, e) + self._bonafide = bonafide def do_greet(self): - print "[+] Bonafide service running..." + print "Bonafide service running..." def do_bye(self): print "[+] Bonafide service stopped. Have a nice day." @@ -58,40 +74,25 @@ class BonafideZmqREPConnection(ZmqREPConnection): elif cmd == 'signup': username, password = parts[1], parts[2] - d = self._service.do_signup(username, password) + d = self._bonafide.do_signup(username, password) d.addCallback(lambda response: defer_reply( 'REGISTERED -> %s' % response)) d.addErrback(log_err) elif cmd == 'authenticate': username, password = parts[1], parts[2] - d = self._service.do_authenticate(username, password) + d = self._bonafide.do_authenticate(username, password) d.addCallback(lambda response: defer_reply( 'TOKEN -> %s' % response)) d.addErrback(log_err) elif cmd == 'logout': username, password = parts[1], parts[2] - d = self._service.do_logout(username, password) + d = self._bonafide.do_logout(username, password) d.addCallback(lambda response: defer_reply( 'LOGOUT -> ok')) d.addErrback(log_err) elif cmd == 'stats': - response = self._service.do_stats() + response = self._bonafide.do_stats() defer_reply(response) - - -def get_zmq_connection(): - zf = ZmqFactory() - e = ZmqEndpoint("bind", config.ENDPOINT) - return BonafideZmqREPConnection(zf, e) - - -if __name__ == "__main__": - from twisted.internet import reactor - - s = get_zmq_connection() - reactor.callWhenRunning(s.initialize) - reactor.callWhenRunning(s.do_greet) - reactor.run() |