o
    j3                     @   st   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZ dd	lmZmZmZmZ G d
d deZdS )
    )sleepN   )get_config_parameter   )SyncStrategy)LDAPSocketOpenErrorLDAPOperationResultLDAPMaximumRetriesErrorLDAPStartTLSError)loglog_enabledERRORBASICc                   @   sb   e Zd Zdd ZdddZdddZdd
dZdd Zdd Zdd Z	dd Z
dd Zdd Zd	S )RestartableStrategyc                 C   sf   t | | d| _d| _d| _d| _td| _td| _d| _	d | _
d | _d | _d | _d | _g | _d S )NTFRESTARTABLE_SLEEPTIMERESTARTABLE_TRIES)r   __init__syncno_real_dsapooled
can_streamr   restartable_sleep_timerestartable_tries_restarting_last_bind_controls_current_message_type_current_request_current_controls_restart_tlsexception_history)selfldap_connection r#   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/strategy/restartable.pyr   %   s   


zRestartableStrategy.__init__FTc                 C   s   t | || d S N)r   open)r!   reset_usageread_server_infor#   r#   r$   r&   5   s   zRestartableStrategy.openc              
   C   s  zt | ||| |   W dS  ty8 } zttr.ttd|| j | t	|t
| W Y d}~nd}~ww | jsJd| _| j}|dkr*ttrWttd| j| | j t| j | jjsz| j  W n4 tjtfys   Y n) ty } zttrttd|| j | t	|t
| W Y d}~nd}~ww zF| jjr| jj| j}| jj|kr|| j_| jjr| jj jd7  _t | ||| | jjr| jj jd7  _d| j_d| _|   W dS  ty } z+ttrttd|| j | t	|t
| | jjr| jj jd7  _W Y d}~nd}~ww t| jts&|d8 }|dksHd| _d| j_ttr@ttd	| jj| j t | jj| j!| jdS )
z
        Try to open and connect a socket to a Server
        raise LDAPExceptionError if unable to open or connect socket
        if connection is restartable tries for the number of restarting requested or forever
        N<%s> while restarting <%s>Tr   z+try #%d to open Restartable connection <%s>r   Fz;restartable connection strategy failed while opening socket<%s> for <%s>)"r   _open_socket_reset_exception_history	Exceptionr   r   r   
connection_add_exception_to_historytypestrr   r   r   r   r   closedunbindsocketerrorr   server_pool
get_serverserverusage_usageservers_from_poolrestartable_successesrestartable_failures
isinstancebool
last_errorr
   r    )r!   addressuse_sslunix_socketecounter
new_serverr#   r#   r$   r+   8   sv   

"
#
z RestartableStrategy._open_socketNc           
   
   C   s  || _ || _|| _| js| jj| _|dkr|| _zt| |||}| 	  |W S  t
yP } zttr;ttd|| j | t|t| W Y d }~nd }~ww | jsd| _| j}|dkrttrottd| j| | j t| j | jjsz| j  W n4 tjtfy   Y n) t
y } zttrttd|| j | t|t| W Y d }~nd }~ww d}z\| jjddd | jr| jjddsd	| jjrd
| jj nd }ttrttd||  | j  t||dkr| jjd| jd | jj j!s
| jj j"s
| j#  n| jj$dd W n- t
y? } z ttr(ttd|| j | t|t| d}W Y d }~nd }~ww |sz | j|||}	| jj%rZ| jj& j'd7  _'d| _| 	  |	W S  t
y } z ttryttd|| j | t|t| d}W Y d }~nd }~ww |r| jj%r| jj& j(d7  _(t)| jt*s|d8 }|dks`d| _d| j_ttrttd| jj| j t+| jj| j,| j)NbindRequestr)   Tr   z.try #%d to send in Restartable connection <%s>F)r'   r(   )r(   z)restart tls in restartable not successfulz -  z%s for <%s>)r(   controls)	read_infor   z%restartable connection failed to sendr*   )-r   r   r   r   r.   tls_startedr   r   sendr,   r-   r   r   r   r/   r0   r1   r   r   r   r   r   r2   r3   r4   r5   r   r&   	start_tlsr@   r   bindr8   schemainforefresh_server_info_fire_deferredr9   r:   r<   r=   r>   r?   r
   r    )
r!   message_typerequestrI   
message_idrD   rE   failurer5   	ret_valuer#   r#   r$   rL   s   s   
"

"



7
zRestartableStrategy.sendc              
   C   s  zt | |}|   |W S  ty6 } zttr!ttd|| j | t	|t
| W Y d }~nd }~ww zt | | | j| j| j}|   |W S  ty } z/ttr`ttd|| j | t	|t
| t|tstd| j_ttrttd| jj| j  d }~ww )Nr)   zCrestartable connection strategy failed in post_send_single_responser*   )r   post_send_single_responser,   r-   r   r   r   r.   r/   r0   r1   rL   r   r   r   r>   r	   r@   r!   rU   rW   rD   r#   r#   r$   rX      s2   "
z-RestartableStrategy.post_send_single_responsec              
   C   s  zt | |}|   |W S  ty6 } zttr!ttd|| j | t	|t
| W Y d }~nd }~ww zt | | j| j| j| j}|   |W S  ty } z1ttrattd|| j | t	|t
| t|tsv|j| j_ttrttd| jj| j |d }~ww )Nr)   r*   )r   post_send_searchr,   r-   r   r   r   r.   r/   r0   r1   rL   r   r   r   r>   r	   argsr@   rY   r#   r#   r$   rZ      s2   "

z$RestartableStrategy.post_send_searchc                 C   s.   t | jtst |ts| j| d S d S d S r%   )r>   r   r?   r
   r    append)r!   excr#   r#   r$   r/      s
   
z-RestartableStrategy._add_exception_to_historyc                 C   s   | j rg | _ d S d S r%   )r    r!   r#   r#   r$   r,      s   
z,RestartableStrategy._reset_exception_historyc                 C      t r%   NotImplementedErrorr^   r#   r#   r$   
get_stream      zRestartableStrategy.get_streamc                 C   r_   r%   r`   )r!   valuer#   r#   r$   
set_stream  rc   zRestartableStrategy.set_stream)FT)FFr%   )__name__
__module____qualname__r   r&   r+   rL   rX   rZ   r/   r,   rb   re   r#   r#   r#   r$   r   $   s    


;Rr   )__doc__timer   r4   rH   r   r   r   core.exceptionsr   r	   r
   r   	utils.logr   r   r   r   r   r#   r#   r#   r$   <module>   s    