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