+
    DfjpT                       R t ^ RIHt ^ RIHtHt ^ RIHt ^ RIH	t	H
t
HtHtHt ^ RIHt ^ RIHtHt ^ RIHt ^ RIHtHtHtHtHt ^ R	IHtHtHtH t H!t!H"t"H#t# ^ R
I$H%t% ^ RI&H't' ]! R4      t(R R lt) ! R R]4      t*]! ]4       ! R R4      4       t+]! ] 4       ! R R4      4       t,R R lt-]-! ],4       ]-! ]+4       ] ! R R4      4       t.R R lt/RRR]3R R llt0]0! 4       t1]/! 4       t2 ! R  R!]4      t3R"# )#z
Implementation of L{twisted.application.internet.ClientService}, particularly
its U{automat <https://automat.readthedocs.org/>} state machine.
)annotations)	dataclassfield)random)CallableOptionalProtocolTypeVarUnion)implementer)TypeMachineBuilderpep614)Service)CancelledErrorDeferredfailmaybeDeferredsucceed)IAddressIDelayedCall	IProtocolIProtocolFactoryIReactorTimeIStreamClientEndpoint
ITransport)Logger)FailureTc                    V ^8  d   QhRRRR/# )   maybeReactorzOptional[T]returnr    )formats   "E/usr/lib/python3/dist-packages/twisted/application/_client_service.py__annotate__r%   )   s     	 	k 	a 	    c                    V f	   ^ RI Hp V# V # )zJ
@return: the argument, or the global reactor if the argument is L{None}.
)reactor)twisted.internetr(   )r    r(   s   & r$   _maybeGlobalReactorr*   )   s     ,r&   c                  v    ] tR t^5tR R ltR R ltR R ltR R ltRR
 R lltRR R llt	RR R llt
RtR	# )_Clientc                   V ^8  d   QhRR/# r   r!   Noner"   )r#   s   "r$   r%   _Client.__annotate__6   s      t r&   c                    R# )D
Start this L{ClientService}, initiating the connection retry loop.
Nr"   selfs   &r$   start_Client.start6       r&   c                   V ^8  d   QhRR/# r   r!   Deferred[None]r"   )r#   s   "r$   r%   r0   ;   s      n r&   c                    R# )z
Stop trying to connect and disconnect any current connection.

@return: a L{Deferred} that fires when all outstanding connections are
    closed and all in-progress connection attempts halted.
Nr"   r3   s   &r$   stop_Client.stop;   r7   r&   c                    V ^8  d   QhRRRR/# )r   protocol_ReconnectingProtocolProxyr!   r/   r"   )r#   s   "r$   r%   r0   C   s      (B t r&   c                    R# )zO
A connection has been made.

@param protocol: The protocol of the connection.
Nr"   )r4   r?   s   &&r$   _connectionMade_Client._connectionMadeC   r7   r&   c                    V ^8  d   QhRRRR/# )r   failurer   r!   r/   r"   )r#   s   "r$   r%   r0   J   s       T r&   c                    R# )z
Deliver connection failures to any L{ClientService.whenConnected}
L{Deferred}s that have met their failAfterFailures threshold.

@param failure: the Failure to fire the L{Deferred}s with.
Nr"   r4   rE   s   &&r$   _connectionFailed_Client._connectionFailedJ   r7   r&   Nc                    V ^8  d   QhRRRR/# r   rE   Optional[Failure]r!   r/   r"   )r#   s   "r$   r%   r0   R   s      "3 t r&   c                    R# )z/
The wait between connection attempts is done.
Nr"   rG   s   &&r$   
_reconnect_Client._reconnectR   r7   r&   c                    V ^8  d   QhRRRR/# rK   r"   )r#   s   "r$   r%   r0   W   s      +<  r&   c                    R# )z/
The current connection has been disconnected.
Nr"   rG   s   &&r$   _clientDisconnected_Client._clientDisconnectedW   r7   r&   c                    V ^8  d   QhRRRR/# r   failAfterFailuresOptional[int]r!   Deferred[IProtocol]r"   )r#   s   "r$   r%   r0   \   s      $1	r&   c                   R# )a  
Retrieve the currently-connected L{Protocol}, or the next one to
connect.

@param failAfterFailures: number of connection failures after which the
    Deferred will deliver a Failure (None means the Deferred will only
    fail if/when the service is stopped).  Set this to 1 to make the
    very first connection failure signal an error.  Use 2 to allow one
    failure but signal an error if the subsequent retry then fails.

@return: a Deferred that fires with a protocol produced by the factory
    passed to C{__init__}.  It may:

        - fire with L{IProtocol}

        - fail with L{CancelledError} when the service is stopped

        - fail with e.g.
          L{DNSLookupError<twisted.internet.error.DNSLookupError>} or
          L{ConnectionRefusedError<twisted.internet.error.ConnectionRefusedError>}
          when the number of consecutive failed connection attempts
          equals the value of "failAfterFailures"
Nr"   r4   rV   s   "&r$   whenConnected_Client.whenConnected\   r7   r&   r"   N)__name__
__module____qualname____firstlineno__r5   r<   rB   rH   rN   rR   r[   __static_attributes__r"   r&   r$   r,   r,   5   s*    


 r&   r,   c                  V    ] tR t^xtRtR R ltR R ltR R ltR R	 ltR
 R lt	Rt
R# )r@   z
A proxy for a Protocol to provide connectionLost notification to a client
connection service, in support of reconnecting when connections are lost.
c               $    V ^8  d   QhRRRRRR/# )r   r?   r   lostNotificationCallable[[Failure], None]r!   r/   r"   )r#   s   "r$   r%   '_ReconnectingProtocolProxy.__annotate__   s$     2 2!25N2	2r&   c                    Wn         W n        R# )a  
Create a L{_ReconnectingProtocolProxy}.

@param protocol: the application-provided L{interfaces.IProtocol}
    provider.
@type protocol: provider of L{interfaces.IProtocol} which may
    additionally provide L{interfaces.IHalfCloseableProtocol} and
    L{interfaces.IFileDescriptorReceiver}.

@param lostNotification: a 1-argument callable to invoke with the
    C{reason} when the connection is lost.
N)	_protocol_lostNotification)r4   r?   re   s   &&&r$   __init__#_ReconnectingProtocolProxy.__init__   s     "!1r&   c                    V ^8  d   QhRRRR/# )r   	transportr   r!   r/   r"   )r#   s   "r$   r%   rg      s     1 1
 1t 1r&   c                	H    Wn         V P                  P                  V4       R # r]   )
_transportri   makeConnection)r4   rn   s   &&r$   rq   )_ReconnectingProtocolProxy.makeConnection   s    #%%i0r&   c                    V ^8  d   QhRRRR/# )r   reasonr   r!   r/   r"   )r#   s   "r$   r%   rg      s     + +W + +r&   c                     V P                   P                  V4      V P                  V4       #   T P                  T4       i ; i)z
The connection was lost.  Relay this information.

@param reason: The reason the connection was lost.

@return: the underlying protocol's result
)ri   connectionLostrj   )r4   rt   s   &&r$   rv   )_ReconnectingProtocolProxy.connectionLost   s6    	+>>008""6*D""6*s	   . Ac                    V ^8  d   QhRRRR/# r   itemstrr!   objectr"   )r#   s   "r$   r%   rg      s     - - - -r&   c                	.    \        V P                  V4      # r]   )getattrri   r4   rz   s   &&r$   __getattr__&_ReconnectingProtocolProxy.__getattr__   s    t~~t,,r&   c                   V ^8  d   QhRR/# r   r!   r{   r"   )r#   s   "r$   r%   rg      s     J J# Jr&   c                	R    R V P                   P                   RV P                  : R2# )<z
 wrapping >)	__class__r^   ri   r3   s   &r$   __repr__#_ReconnectingProtocolProxy.__repr__   s'    4>>**+:dnn5GqIIr&   )rj   ri   rp   N)r^   r_   r`   ra   __doc__rk   rq   rv   r   r   rb   r"   r&   r$   r@   r@   x   s'    
2$1+-J Jr&   r@   c                  J    ] tR t^tRtR R ltR R ltR R ltR R	 ltR
t	R# )_DisconnectFactoryzz
A L{_DisconnectFactory} is a proxy for L{IProtocolFactory} that catches
C{connectionLost} notifications and relays them.
c               $    V ^8  d   QhRRRRRR/# )r   protocolFactoryr   protocolDisconnectedrf   r!   r/   r"   )r#   s   "r$   r%   _DisconnectFactory.__annotate__   s(     : :): 8: 
	:r&   c                	    Wn         W n        R # r]   )_protocolFactory_protocolDisconnected)r4   r   r   s   &&&r$   rk   _DisconnectFactory.__init__   s    
 !0%9"r&   c                    V ^8  d   QhRRRR/# )r   addrr   r!   zOptional[IProtocol]r"   )r#   s   "r$   r%   r      s     M M( M/B Mr&   c                n    V P                   P                  V4      pVf   R# \        W P                  4      # )a  
Create a L{_ReconnectingProtocolProxy} with the disconnect-notification
callback we were called with.

@param addr: The address the connection is coming from.

@return: a L{_ReconnectingProtocolProxy} for a protocol produced by
    C{self._protocolFactory}
N)r   buildProtocolr@   r   )r4   r   builts   && r$   r    _DisconnectFactory.buildProtocol   s3     %%33D9=)%1K1KLLr&   c                    V ^8  d   QhRRRR/# ry   r"   )r#   s   "r$   r%   r      s     4 4 4 4r&   c                	.    \        V P                  V4      # r]   )r~   r   r   s   &&r$   r   _DisconnectFactory.__getattr__   s    t,,d33r&   c                   V ^8  d   QhRR/# r   r"   )r#   s   "r$   r%   r      s     
 
# 
r&   c                	b    R P                  V P                  P                  V P                  4      # )z<{} wrapping {!r}>)r#   r   r^   r   r3   s   &r$   r   _DisconnectFactory.__repr__   s*    #**NN##T%:%:
 	
r&   )r   r   N)
r^   r_   r`   ra   r   rk   r   r   r   rb   r"   r&   r$   r   r      s!    
:M4
 
r&   r   c                    V ^8  d   QhRRRR/# )r   or|   r!   r/   r"   )r#   s   "r$   r%   r%      s        F  t  r&   c                .    R F  p\        W4       K  	  R# )z
Remove the special runtime attributes set by L{implementer} so that a class
can proxy through those attributes with C{__getattr__} and thereby forward
optionally-provided interfaces by the delegated class.
N)__providedBy____provides____implemented__)delattr)r   zopeSpecials   & r$   _deinterfacer      s     M Mr&   c                      ] tR t^t$ RtR]R&   R]R&   R]R&   R]R	&   R
]R&   ]! ]R7      tR]R&   ]! ]R7      t	R]R&   ^ t
R]R&   ]! 4       tR]R&   R R ltR R ltR R ltR R ltRtR# )_Corez.
Shared core for ClientService state machine.
r   endpointr   factoryCallable[[int], float]timeoutForAttemptr   clock'Optional[Callable[[IProtocol], object]]prepareConnection)default_factoryzlist[Deferred[None]]stopWaitersz/list[tuple[Deferred[IProtocol], Optional[int]]]awaitingConnectedintfailedAttemptsr   logc                   V ^8  d   QhRR/# r9   r"   )r#   s   "r$   r%   _Core.__annotate__   s     $ $^ $r&   c                	n    V P                   P                  \        4       4       V P                   R,          # )   )r   appendr   r3   s   &r$   waitForStop_Core.waitForStop   s)    
+##r&   c                    V ^8  d   QhRRRR/# )r   valuezUnion[IProtocol, Failure]r!   r/   r"   )r#   s   "r$   r%   r      s      6 4 r&   c                	f    . V P                   uV n         pV F  w  r4VP                  V4       K  	  R # r]   )r   callback)r4   r   waitingw	remainings   &&   r$   unawait_Core.unawait   s/    *,d.D.D'#LAJJu $r&   c                   V ^8  d   QhRR/# r.   r"   )r#   s   "r$   r%   r      s     0 0d 0r&   c                	J    V P                  \        \        4       4      4       R # r]   )r   r   r   r3   s   &r$   cancelConnectWaiters_Core.cancelConnectWaiters   s    W^-./r&   c                   V ^8  d   QhRR/# r.   r"   )r#   s   "r$   r%   r      s       r&   c                	b    . V P                   uV n         pV F  pVP                  R 4       K  	  R # r]   )r   r   )r4   r   r   s   &  r$   finishStopping_Core.finishStopping   s-    $&(8(8!'AJJt r&   )r   r   N)r^   r_   r`   ra   r   __annotations__r   listr   r   r   r   r   r   r   r   r   rb   r"   r&   r$   r   r      s    
 $#-->> ).d(CK%CINJF  NC(C$
0 r&   r   c                   V ^8  d   QhRR/# )r   r!   zCallable[[_Core], _Client]r"   )r#   s   "r$   r%   r%     s     b b/ br&   c            	        \        \        \        4      p R&R R llpR R lpR&R R llpV P                  R4      pV P                  RV4      pV P                  R	4      pV P                  R
V4      pV P                  RV4      pV P                  R4      p	V P                  R4      p
V P                  R	4      pVP	                  \        P
                  4      P                  V4      P                  R 4       VP	                  \        P
                  4      P                  4       P                  R 4       VP	                  \        P                  4      P                  V4      P                  R 4       VP	                  \        P
                  4      P                  4       P                  R 4       VP	                  \        P                  4      P                  V4      P                  R 4       VP	                  \        P                  4      P                  V4      P                  R 4       VP	                  \        P
                  4      P                  4       P                  R 4       VP	                  \        P                  4      P                  V4      P                  R 4       V	P	                  \        P
                  4      P                  V
4      P                  R 4       V
P	                  \        P
                  4      P                  V
4      P                  R 4       VP	                  \        P
                  4      P                  V4      P                  R 4       \        VP	                  \        P                  4      P                  V4      4      \        VP	                  \        P                  4      P                  V4      4      R R l4       4       p\        VP	                  \        P                  4      P                  V	4      4      R R l4       p\        VP	                  \        P                  RR7      P                  V4      4      R R l4       p\        VP	                  \        P                  4      P                  V4      4      R R l4       p\        VP	                  \        P                  4      P                  V	4      4      R R l4       p\        VP	                  \        P                  4      P                  4       4      R&R R ll4       p\        V	P	                  \        P                  4      P                  4       4      \        V
P	                  \        P                  4      P                  V	4      4      R R l4       4       p\        V	P	                  \        P                  4      P                  V4      4      \        V	P	                  \        P                  4      P                  V4      4      R&R R ll4       4       p\        VP	                  \        P                  RR7      P                  4       4      \        VP	                  \        P                  RR7      P                  4       4      \        VP	                  \        P                  4      P                  V4      4      \        V
P	                  \        P                  4      P                  V
4      4      \        V	P	                  \        P                  4      P                  V	4      4      R&R  R! ll4       4       4       4       4       p\        V
P	                  \        P                  4      P                  V4      4      R&R" R# ll4       p\        VP	                  \        P                  4      P                  V4      4      R&R$ R% ll4       pV P!                  4       # )'Nc               (    V ^8  d   QhRRRRRRRR/# )	r   cr,   sr   rE   rL   r!   r   r"   )r#   s   "r$   r%   !makeMachine.<locals>.__annotate__  s,     6 666'86	6r&   c                   V;P                   ^,          un         VP                  VP                   4      pVP                  P                  RVP                   VP                  VR7       VP
                  P                  W0P                  4      # )r   zDScheduling retry {attempt} to connect {endpoint} in {delay} seconds.)attemptr   delay)r   r   r   infor   r   	callLaterrN   )r   r   rE   r   s   &&& r$   waitForRetry!makeMachine.<locals>.waitForRetry  sq     	
A##A$4$45	

R$$ZZ	 	 	
 ww  55r&   c               (    V ^8  d   QhRRRRRRRR/# )r   r   r,   r   r   r?   r@   r!   r"   )r#   s   "r$   r%   r     s,      (B	#r&   c                J    ^ Vn         VP                  VP                  4       V# )    )r   r   ri   )r   r   r?   s   &&&r$   rememberConnection'makeMachine.<locals>.rememberConnection  s#     			($$%r&   c               (    V ^8  d   QhRRRRRRRR/# )	r   r   r,   r   r   rE   rL   r!   $Deferred[_ReconnectingProtocolProxy]r"   )r#   s   "r$   r%   r     s,      '8	-r&   c                  a \        SP                  V P                  4      pSP                  P	                  V4      pR  V3R llpTpVP                  V4      P                  V P                  4      P                  V P                  4       V# )c                    V ^8  d   QhRRRR/# )r   r?   r@   r!   r   r"   )r#   s   "r$   r%   <makeMachine.<locals>.attemptConnection.<locals>.__annotate__"  s     	% 	%0	%1	%r&   c                   <a  SP                   e*   \        SP                   S 4      P                  V 3R l4      # \        S 4      # )Nc                   < S# r]   r"   )_r?   s   &r$   <lambda>ImakeMachine.<locals>.attemptConnection.<locals>.prepare.<locals>.<lambda>'  s    hr&   )r   r   addCallbackr   )r?   r   s   fr$   prepare7makeMachine.<locals>.attemptConnection.<locals>.prepare"  sA     "".$Q%8%8(COO&  8$$r&   )	r   r   rR   r   connectr   rB   
addErrbackrH   )r   r   rE   factoryProxy
connectingr   connectingProxys   &f&    r$   attemptConnection&makeMachine.<locals>.attemptConnection  st     *!))Q5J5JK*+***<*<\*J
	% 	% %''0[**+Z++,r&   Init
ConnectingStoppedWaiting	ConnectedDisconnecting
Restartingc               $    V ^8  d   QhRRRRRR/# r   r   r,   r   r   r!   r:   r"   )r#   s   "r$   r%   r   Q  s!       U ~ r&   c                    \        R 4      # r]   )r   r   r   s   &&r$   immediateStop"makeMachine.<locals>.immediateStopO  s     t}r&   c               (    V ^8  d   QhRRRRRRRR/# )	r   r   r,   r   r   r   r   r!   r:   r"   )r#   s   "r$   r%   r   U  s,      'K	r&   c                F    VP                  4       pVP                  4        V# r]   )r   cancel)r   r   r   waiteds   &&& r$   connectingStop#makeMachine.<locals>.connectingStopT  s     r&   T)nodatac               (    V ^8  d   QhRRRRRRRR/# )	r   r   r,   r   r   rE   r   r!   r/   r"   )r#   s   "r$   r%   r   ]  s(          E  G    r&   c                   . p. pVP                    FP  w  rVVf   VP                  WV34       K  V^8:  d   VP                  V4       K7  VP                  WV^,
          34       KR  	  WAn         V F  pVP                  V4       K  	  R # r]   )r   r   r   )r   r   rE   readynotReadyr   r   s   &&&    r$   failedWhenConnecting)makeMachine.<locals>.failedWhenConnecting\  su    DF//LA /aQM 23 0 'AJJw r&   c               (    V ^8  d   QhRRRRRRRR/# )	r   r   r,   r   r   futureRetryr   r!   r:   r"   )r#   s   "r$   r%   r   l  s(       E   r&   c                    VP                  4       pVP                  4        VP                  4        VP                  4        V# r]   )r   r   r	  r   )r   r   r  r
  s   &&& r$   r<   makeMachine.<locals>.stopk  s6    	 	r&   c               (    V ^8  d   QhRRRRRRRR/# )	r   r   r,   r   r   r?   r@   r!   r:   r"   )r#   s   "r$   r%   r   t  s,      (B	r&   c                Z    VP                  4       pVP                  P                  4        V# r]   )r   rp   loseConnection)r   r   r?   r
  s   &&& r$   stopWhileConnected'makeMachine.<locals>.stopWhileConnecteds  s&     **,r&   c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r   r   r,   r   r   r?   r@   rV   rW   r!   rX   r"   )r#   s   "r$   r%   r   |  s:     + +++ -+ )	+
 
+r&   c                ,    \        VP                  4      # r]   )r   ri   )r   r   r?   rV   s   &&&&r$   whenConnectedWhenConnected/makeMachine.<locals>.whenConnectedWhenConnected{  s     x))**r&   c               $    V ^8  d   QhRRRRRR/# r  r"   )r#   s   "r$   r%   r     s!      W  > r&   c                "    VP                  4       # r]   )r   r  s   &&r$   	discoStopmakeMachine.<locals>.discoStop  s     }}r&   c               (    V ^8  d   QhRRRRRRRR/# 	r   r   r,   r   r   rE   rL   r!   r/   r"   )r#   s   "r$   r%   r     s,      '8	r&   c                F    VP                  4        VP                  4        R # r]   )r   r   r   r   rE   s   &&&r$   disconnectingFinished*makeMachine.<locals>.disconnectingFinished  s    
 	
 	r&   c               (    V ^8  d   QhRRRRRRRR/# 	r   r   r,   r   r   rV   rW   r!   rX   r"   )r#   s   "r$   r%   r     s,      1>	r&   c                R    \        4       pVP                  P                  W234       V# r]   )r   r   r   )r   r   rV   results   &&& r$   awaitingConnection'makeMachine.<locals>.awaitingConnection  s'     '/j	""F#>?r&   c               (    V ^8  d   QhRRRRRRRR/# r&  r"   )r#   s   "r$   r%   r     s*      w 5 3D PT r&   c                &    VP                  4        R # r]   )r   r(  s   &&&r$   restartDone makeMachine.<locals>.restartDone  s    	r&   c               (    V ^8  d   QhRRRRRRRR/# r,  r"   )r#   s   "r$   r%   r     s,     & &&&1>&	&r&   c                (    \        \        4       4      # r]   )r   r   )r   r   rV   s   &&&r$   notGoingToConnect&makeMachine.<locals>.notGoingToConnect  s     N$%%r&   r]   )r   r,   r   stateuponr5   toreturnslooprB   rN   rH   rR   r   r<   r[   build)machiner   r   r   r   r   r   r   r   r   r   r  r  r  r<   r  r  r#  r)  r/  r3  r7  s                         r$   makeMachiner@    s    %0G68 == D|->?JmmI&GmmI|4Gk+=>IMM/2M|,JmmI&G 	IIgmm
+33D9OOGMM"'')11$7OOG++,//	:BB4HLL$$&..t4LL##$''
3;;DANN7,,-009AA$GNN7==!&&(006NN7../227;CCDIw}}%((4<<TBOOGMM"%%j199$?LL"":.66t< DIIgll#&&w/0GLL&))'23 4 1 JOOGLL),,];< = JOOG55dOCFFwOP  Q  GLL&))'23 4 INN7<<(++M:; < INN70016689+ :+ Mw||,1134JOOGLL),,];< = 5 Mw889<<WEFMw::;>>wGH I G JOOG11$O?DDFGGLL..tL<AACDDIIg++,//56JOOG11255jABMw44588GH I C 7 E H
 JOOG778;;JGH I GLL../227;<& =&
 ==?r&   g      ?g      N@g      ?c          
     ,    V ^8  d   QhRRRRRRRRRR/# )	r   initialDelayfloatmaxDelayfactorjitterzCallable[[], float]r!   r   r"   )r#   s   "r$   r%   r%     s:     + +++ +  	+
 +r&   c                &   a aaa R VV VV3R llpV# )aL  
A timeout policy for L{ClientService} which computes an exponential backoff
interval with configurable parameters.

@since: 16.1.0

@param initialDelay: Delay for the first reconnection attempt (default
    1.0s).
@type initialDelay: L{float}

@param maxDelay: Maximum number of seconds between connection attempts
    (default 60 seconds, or one minute).  Note that this value is before
    jitter is applied, so the actual maximum possible delay is this value
    plus the maximum possible result of C{jitter()}.
@type maxDelay: L{float}

@param factor: A multiplicative factor by which the delay grows on each
    failed reattempt.  Default: 1.5.
@type factor: L{float}

@param jitter: A 0-argument callable that introduces noise into the delay.
    By default, C{random.random}, i.e. a pseudorandom floating-point value
    between zero and one.
@type jitter: 0-argument callable returning L{float}

@return: a 1-argument callable that, given an attempt count, returns a
    floating point number; the number of seconds to delay.
@rtype: see L{ClientService.__init__}'s C{retryPolicy} argument.
c                    V ^8  d   QhRRRR/# )r   r   r   r!   rC  r"   )r#   s   "r$   r%   #backoffPolicy.<locals>.__annotate__  s            r&   c                   <  \        SS\        ^dV 4      ,          ,          S4      pVS! 4       ,           #   \         d    Sp Li ; i)d   )minOverflowError)r   r   rE  rB  rF  rD  s   & r$   policybackoffPolicy.<locals>.policy  sJ    	#c72C(CDhOE vx  	E	s   $5 AAr"   )rB  rD  rE  rF  rN  s   ffff r$   backoffPolicyrP    s    H    Mr&   c                  r   a  ] tR tRtRt]! 4       tRR R lltRR R lltR V 3R llt	R	 V 3R
 llt
RtV ;t# )ClientServicei  z
A L{ClientService} maintains a single outgoing connection to a client
endpoint, reconnecting after a configurable timeout when a connection
fails, either before or after connecting.

@since: 16.1.0
c          
     ,    V ^8  d   QhRRRRRRRRR	R
/# )r   r   r   r   r   retryPolicyz Optional[Callable[[int], float]]r   zOptional[IReactorTime]r   r   r"   )r#   s   "r$   r%   ClientService.__annotate__  s=     :
 :
':
 ":
 6	:

 &:
 C:
r&   c                    \        V4      pVf   \        MTp\        \        VVVVVV P                  R7      4      V n        R# )a  
@param endpoint: A L{stream client endpoint
    <interfaces.IStreamClientEndpoint>} provider which will be used to
    connect when the service starts.

@param factory: A L{protocol factory <interfaces.IProtocolFactory>}
    which will be used to create clients for the endpoint.

@param retryPolicy: A policy configuring how long L{ClientService} will
    wait between attempts to connect to C{endpoint}; a callable taking
    (the number of failed connection attempts made in a row (L{int}))
    and returning the number of seconds to wait before making another
    attempt.

@param clock: The clock used to schedule reconnection.  It's mainly
    useful to be parametrized in tests.  If the factory is serialized,
    this attribute will not be serialized, and the default value (the
    reactor) will be restored when deserialized.

@param prepareConnection: A single argument L{callable} that may return
    a L{Deferred}.  It will be called once with the L{protocol
    <interfaces.IProtocol>} each time a new connection is made.  It may
    call methods on the protocol to prepare it for use (e.g.
    authenticate) or validate it (check its health).

    The C{prepareConnection} callable may raise an exception or return
    a L{Deferred} which fails to reject the connection.  A rejected
    connection is not used to fire an L{Deferred} returned by
    L{whenConnected}.  Instead, L{ClientService} handles the failure
    and continues as if the connection attempt were a failure
    (incrementing the counter passed to C{retryPolicy}).

    L{Deferred}s returned by L{whenConnected} will not fire until any
    L{Deferred} returned by the C{prepareConnection} callable fire.
    Otherwise its successful return value is consumed, but ignored.

    Present Since Twisted 18.7.0
N)r   r   )r*   _defaultPolicyClientMachiner   _log_machine)r4   r   r   rT  r   r   s   &&&&&&r$   rk   ClientService.__init__  sE    \ $E*(3(;n!."3II	"
r&   c                    V ^8  d   QhRRRR/# rU   r"   )r#   s   "r$   r%   rU  #  s     > >!.>	>r&   c                8    V P                   P                  V4      # )a  
Retrieve the currently-connected L{Protocol}, or the next one to
connect.

@param failAfterFailures: number of connection failures after which
    the Deferred will deliver a Failure (None means the Deferred will
    only fail if/when the service is stopped).  Set this to 1 to make
    the very first connection failure signal an error.  Use 2 to
    allow one failure but signal an error if the subsequent retry
    then fails.
@type failAfterFailures: L{int} or None

@return: a Deferred that fires with a protocol produced by the
    factory passed to C{__init__}
@rtype: L{Deferred} that may:

    - fire with L{IProtocol}

    - fail with L{CancelledError} when the service is stopped

    - fail with e.g.
      L{DNSLookupError<twisted.internet.error.DNSLookupError>} or
      L{ConnectionRefusedError<twisted.internet.error.ConnectionRefusedError>}
      when the number of consecutive failed connection attempts
      equals the value of "failAfterFailures"
)rZ  r[   rZ   s   &&r$   r[   ClientService.whenConnected#  s    : }}**+<==r&   c                   V ^8  d   QhRR/# r.   r"   )r#   s   "r$   r%   rU  B  s      d r&   c                   < V P                   '       d   V P                  P                  R4       R# \        SV `  4        V P
                  P                  4        R# )r2   z1Duplicate ClientService.startService {log_source}N)runningrY  warnsuperstartServicerZ  r5   r4   r   s   &r$   rd  ClientService.startServiceB  s=     <<<IINNNOr&   c                   V ^8  d   QhRR/# r9   r"   )r#   s   "r$   r%   rU  L  s     $ $^ $r&   c                T   < \         SV `  4        V P                  P                  4       # )z
Stop attempting to reconnect and close any existing connections.

@return: a L{Deferred} that fires when all outstanding connections are
    closed and all in-progress connection attempts halted.
)rc  stopServicerZ  r<   re  s   &r$   ri  ClientService.stopServiceL  s#     	}}!!##r&   )rZ  )NNNr]   )r^   r_   r`   ra   r   r   rY  rk   r[   rd  ri  rb   __classcell__)r   s   @r$   rR  rR    s0     8D:
x>> $ $r&   rR  N)4r   
__future__r   dataclassesr   r   r   _goodEnoughRandomtypingr   r   r   TypingProtocolr	   r
   zope.interfacer   automatr   r   twisted.application.servicer   twisted.internet.deferr   r   r   r   r   twisted.internet.interfacesr   r   r   r   r   r   r   twisted.loggerr   twisted.python.failurer   r   r*   r,   r@   r   r   r   r@  rP  rW  rX  rR  r"   r&   r$   <module>rx     s  
 # ( . Q Q & . /    " *CL	@n @F Y-J -J -J` #
 #
 #
L     ' ( # # #LbL "3	+\ x$G x$r&   