a
    õ£Beü  ã                   @   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__© úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/zmq/auth/thread.pyr      s
    
zAuthenticationThread.__init__©r   c                 C   s^   t  ¡ }z0| |  ¡ ¡ W | jr0| j ¡  d| _| ¡  n | jrP| j ¡  d| _| ¡  0 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	 
¡  t| j ¡ I d H ƒ}| j|v rr| j ¡ }|  |¡rrd S | jj|v 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   Ú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
   Úidr;   r<   )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�sŒtdƒ‚dS )zStart the authentication thread)Zsocket_classr   )r   r
   é
   )Útimeoutz$Authenticator thread failed to startN)r   Ústartr?   Úsocketr    ZPAIRr3   r
   ZlingerÚbindr;   Úconnectr	   r<   r   ÚwaitÚRuntimeError)r   Zthread_piper   r   r   rD   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_aliver<   Újoinr   r   Ústop©r   r   r   r   rL   x   s    

zThreadAuthenticator.stopc                 C   s   t | jo| j ¡ ƒS )z$Is the ZAP thread currently running?)r7   r<   rJ   rM   r   r   r   rJ   ƒ   s    zThreadAuthenticator.is_alivec                 C   s   |   ¡  d S r   )rL   rM   r   r   r   Ú__del__‡   s    zThreadAuthenticator.__del__)Nr=   N)r/   r0   r1   r2   r4   r;   Ústrr	   r   r   r   rD   rL   r7   rJ   rN   r8   r   r   r   r   r9   U   s    
   üür9   )r2   r   Ú	threadingr   r   Útypingr   r   r   r    Zzmq.asyncioÚbaser   r	   r9   Ú__all__r   r   r   r   Ú<module>   s   B6