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