U
    luf                     @   sv   d Z ddlZddlmZmZ ddlmZmZmZ ddl	Z	ddl
Z	ddlmZ G dd deZG d	d
 d
eZd
gZdS )z>ZAP Authenticator in a Python Thread.

.. versionadded:: 14.1
    N)EventThread)AnyListOptional   )Authenticatorc                       s   e Zd ZU dZejed< ejed< e	ed< dZ
eejj ed< e	ejdd fdd	Zdd
ddZdd Zee edddZ  ZS )AuthenticationThreadzgA Thread for running a zmq Authenticator

    This is run in the background by ThreadAuthenticator
    pipeloopauthenticatorNpoller)r   r
   returnc                    s*   t    || _|j| _|| _t | _d S N)super__init__r   logr
   r   started)selfr   r
   	__class__ 3/tmp/pip-unpacked-wheel-h6ekxre8/zmq/auth/thread.pyr      s
    
zAuthenticationThread.__init__r   c                 C   s@   t  }z||   W 5 | jr2| j  d| _|  X dS )z*Start the Authentication Agent thread taskN)asyncioZnew_event_loopr
   closeZrun_until_complete_run)r   r   r   r   r   run*   s    
zAuthenticationThread.runc                    s   t j | _| j| jt j | j| jjt j | j	
  t| j I d H }| j|krr| j }| |rrd S | jj|kr<| jj }| j|I d H  q<d S r   )zmqr   Pollerr   registerr
   POLLINr   Z
zap_socketr   setdictpollZrecv_multipart_handle_pipe_messageZhandle_zap_message)r   eventsmsgr   r   r   r   7   s    



zAuthenticationThread._run)r'   r   c                 C   s@   |d }| j d| |dkr"dS | j d| | jd dS )Nr   zauth received API command %r	   TERMINATETz!Invalid auth command from API: %rs   ERRORF)r   debugerrorr
   send)r   r'   commandr   r   r   r%   G   s    z)AuthenticationThread._handle_pipe_message)__name__
__module____qualname____doc__r   Socket__annotations__r   ZAbstractEventLoopr   r   r   r   r   r   r   r   bytesboolr%   __classcell__r   r   r   r   r	      s   


r	   c                       s   e Zd ZU dZded< dZeed< eed< ded	 ee	d
 fddZ
dd fddZdd fddZedddZddddZ  ZS )ThreadAuthenticatorz-Run ZAP authentication in a background threadz
zmq.Socketr
    pipe_endpointthreadNutf-8zzmq.Contextcontextencodingr   c                    s4   t  j|||d d | _dt|  d| _d | _d S )Nr;   z	inproc://z.inproc)r   r   r
   idr8   r9   )r   r<   r=   r   r   r   r   r   \   s    zThreadAuthenticator.__init__r   c                    s   t    | jjtjtjd| _d| j_| j	| j
 | jjtjtjd}d|_|| j
 t| |d| _| j  | jjjddstddS )zStart the authentication thread)Zsocket_classr   )r   r
   
   )timeoutz$Authenticator thread failed to startN)r   startr<   socketr   ZPAIRr1   r
   Zlingerbindr8   connectr	   r9   r   waitRuntimeError)r   Zthread_piper   r   r   rA   g   s    

zThreadAuthenticator.startc                    sH   | j r:| j d |  r$| j  d| _| j   d| _ t   dS )zStop the authentication threadr(   N)r
   r+   is_aliver9   joinr   r   stopr   r   r   r   rI   x   s    

zThreadAuthenticator.stopc                 C   s   t | jo| j S )z$Is the ZAP thread currently running?)r4   r9   rG   rJ   r   r   r   rG      s    zThreadAuthenticator.is_alivec                 C   s   |    d S r   )rI   rJ   r   r   r   __del__   s    zThreadAuthenticator.__del__)Nr:   N)r-   r.   r/   r0   r2   r8   strr	   r   r   r   rA   rI   r4   rG   rK   r5   r   r   r   r   r6   U   s    
   r6   )r0   r   	threadingr   r   typingr   r   r   r   Zzmq.asynciobaser   r	   r6   __all__r   r   r   r   <module>   s   B6