
    f                         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e      Z G d	 d
e      Zd
gZy)z>ZAP Authenticator in a Python Thread.

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

    This is run in the background by ThreadAuthenticator
    pipeloopauthenticatorNpollerreturnc                     t         |   d       || _        |j                  | _        || _        t               | _        y )NT)daemon)super__init__r   logr   r   started)selfr   r   	__class__s      J/var/www/cvtools/html/venv/lib/python3.12/site-packages/zmq/auth/thread.pyr   zAuthenticationThread.__init__   s:    
 	%* $$	w    c                 j   t        j                         }	 |j                  | j                                | j                  r!| j                  j                          d| _        |j                          y# | j                  r!| j                  j                          d| _        |j                          w xY w)z*Start the Authentication Agent thread taskN)asyncionew_event_looprun_until_complete_runr   close)r   r   s     r   runzAuthenticationThread.run*   sy     %%'	##DIIK0yy		! 	JJL	 yy		! 	JJLs   A3 3?B2c                   K   t         j                  j                         | _        | j                  j	                  | j
                  t         j                         | j                  j	                  | j                  j                  t         j                         | j                  j                          	 t        | j                  j                          d {         }| j
                  |v r,| j
                  j                         }| j                  |      ry | j                  j                  |v rG| j                  j                  j                         }| j                  j                  |       d {    7 7 wN)zmqr   Pollerr   registerr   POLLINr   
zap_socketr   setdictpollrecv_multipart_handle_pipe_messagehandle_zap_message)r   eventsmsgs      r   r   zAuthenticationThread._run7   s    kk((*TYY

3T//::CJJG 0 0 223FyyF"ii..0,,S1!!,,6((33BBD((;;C@@@ 2 As%   CE<E8BE<2E:3E<:E<r0   c                     |d   }| j                   j                  d|       |dk(  ry| j                   j                  d|       | j                  j	                  d       y)Nr   zauth received API command %r	   TERMINATETz!Invalid auth command from API: %rs   ERRORF)r   debugerrorr   send)r   r0   commands      r   r-   z)AuthenticationThread._handle_pipe_messageG   sQ    a&5w?l" HHNN>HIINN8$r   r   N)__name__
__module____qualname____doc__r$   Socket__annotations__r   AbstractEventLoopr	   r   r   r%   r   r!   r   r   bytesboolr-   __classcell__r   s   @r   r   r      s    
 **

#
##  +/FHS[[''(/
$
 jj
 
	
A U  r   r   c                        e Zd ZU dZded<   dZeed<   eed<   	 	 	 dded   d	ed
e	f fdZ
d fdZd fdZdefdZddZ xZS )ThreadAuthenticatorz-Run ZAP authentication in a background threadz
zmq.Socketr    pipe_endpointthreadcontextzzmq.Contextencodingr   c                 n    t         |   |||       d | _        dt        |        d| _        d | _        y )N)rH   rI   r   z	inproc://z.inproc)r   r   r   idrF   rG   )r   rH   rI   r   r   s       r   r   zThreadAuthenticator.__init__\   s<     	8E	(D
':r   r   c                 z   t         |           | j                  j                  t        j
                  t        j                        | _        d| j                  _        | j                  j                  | j                         | j                  j                  t        j
                  t        j                        }d|_        |j                  | j                         t        | |      | _        | j                  j                          | j                  j                  j                  d      st!        d      y)zStart the authentication thread)socket_classr   )r   r   
   )timeoutz$Authenticator thread failed to startN)r   startrH   socketr$   PAIRr<   r   lingerbindrF   connectr   rG   r   waitRuntimeError)r   thread_piper   s     r   rP   zThreadAuthenticator.startg   s     	 LL''szz'J					t))*ll))#(()LD../*KP{{""'''3EFF 4r   c                    | j                   rm| j                   j                  d       | j                         r| j                  j	                          d| _        | j                   j                          d| _         t        |           y)zStop the authentication threadr2   N)r   r5   is_aliverG   joinr    r   stop)r   r   s    r   r\   zThreadAuthenticator.stopx   sX    99IINN<(}}  "DKIIOODIr   c                 d    t        | j                  xr | j                  j                               S )z$Is the ZAP thread currently running?)r@   rG   rZ   r   s    r   rZ   zThreadAuthenticator.is_alive   s#    DKK:DKK$8$8$:;;r   c                 $    | j                          y r#   )r\   r^   s    r   __del__zThreadAuthenticator.__del__   s    		r   )Nzutf-8Nr7   )r8   r9   r:   r;   r=   rF   strr   r   r   r   rP   r\   r@   rZ   r`   rA   rB   s   @r   rD   rD   U   si    7
M3   ,0		-(	 	 		G"	<$ <r   rD   )r;   r   	threadingr   r   typingr   r   r   r$   zmq.asynciobaser	   r   rD   __all__ r   r   <module>rh      sE     # & & 
  ?6 ?D3- 3l !
!r   