1b2732e9dSimarom"""ZAP Authenticator integrated with the tornado IOLoop.
2b2732e9dSimarom
3b2732e9dSimarom.. versionadded:: 14.1
4b2732e9dSimarom"""
5b2732e9dSimarom
6b2732e9dSimarom# Copyright (C) PyZMQ Developers
7b2732e9dSimarom# Distributed under the terms of the Modified BSD License.
8b2732e9dSimarom
9b2732e9dSimaromfrom zmq.eventloop import ioloop, zmqstream
10b2732e9dSimaromfrom .base import Authenticator
11b2732e9dSimarom
12b2732e9dSimarom
13b2732e9dSimaromclass IOLoopAuthenticator(Authenticator):
14b2732e9dSimarom    """ZAP authentication for use in the tornado IOLoop"""
15b2732e9dSimarom
16b2732e9dSimarom    def __init__(self, context=None, encoding='utf-8', log=None, io_loop=None):
17b2732e9dSimarom        super(IOLoopAuthenticator, self).__init__(context)
18b2732e9dSimarom        self.zap_stream = None
19b2732e9dSimarom        self.io_loop = io_loop or ioloop.IOLoop.instance()
20b2732e9dSimarom
21b2732e9dSimarom    def start(self):
22b2732e9dSimarom        """Start ZAP authentication"""
23b2732e9dSimarom        super(IOLoopAuthenticator, self).start()
24b2732e9dSimarom        self.zap_stream = zmqstream.ZMQStream(self.zap_socket, self.io_loop)
25b2732e9dSimarom        self.zap_stream.on_recv(self.handle_zap_message)
26b2732e9dSimarom
27b2732e9dSimarom    def stop(self):
28b2732e9dSimarom        """Stop ZAP authentication"""
29b2732e9dSimarom        if self.zap_stream:
30b2732e9dSimarom            self.zap_stream.close()
31b2732e9dSimarom            self.zap_stream = None
32b2732e9dSimarom        super(IOLoopAuthenticator, self).stop()
33b2732e9dSimarom
34b2732e9dSimarom__all__ = ['IOLoopAuthenticator']
35