B
    (d                 @   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__ \/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/zmq/auth/thread.pyr      s
    
zAuthenticationThread.__init__)r   c             C   s@   t  }z||   W d| jr2| j  d| _|  X dS )z*Start the Authentication Agent thread taskN)asyncionew_event_looprun_until_complete_runr
   close)r   r   r   r   r   run*   s    
zAuthenticationThread.runc                s   t j | _| j| jt j | j| jjt j | j	
  xdt| j I d H }| j|krt| j }| |rtd S | jj|kr>| jj }| j|I d H  q>W d S )N)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 %rs	   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   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 )N)r:   r;   r   z	inproc://z.inproc)r   r   r
   idr7   r8   )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PAIRr/   r
   Zlingerbindr7   connectr	   r8   r   waitRuntimeError)r   Zthread_pipe)r   r   r   r?   g   s    

zThreadAuthenticator.startc                sH   | j r:| j d |  r$| j  d| _| j   d| _ t   dS )zStop the authentication threads	   TERMINATEN)r
   r)   is_aliver8   joinr   r   stop)r   )r   r   r   rG   x   s    

zThreadAuthenticator.stopc             C   s   t | jo| j S )z$Is the ZAP thread currently running?)r3   r8   rE   )r   r   r   r   rE      s    zThreadAuthenticator.is_alivec             C   s   |    d S )N)rG   )r   r   r   r   __del__   s    zThreadAuthenticator.__del__)Nr9   N)r+   r,   r-   r.   r0   r7   strr	   r   r   r   r?   rG   r3   rE   rH   r4   r   r   )r   r   r5   U   s   
  r5   )r.   r   	threadingr   r   typingr   r   r   r   Zzmq.asynciobaser   r	   r5   __all__r   r   r   r   <module>   s   B6