
    i*                        d Z ddlZddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
Z
ddlmZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZ 	 ddlmZ dZ n# e!$ rZ"dZ e"Z#Y dZ"["ndZ"["ww xY w ej$        e%          Z&d Z' G d dej(                  Z) G d de*          Z+ ee           G d de*                      Z, edd           G d de                      Z-dS )zV
Module that implements an event loop based on twisted
( https://twistedmatrix.com ).
    N)
deprecated)partial)ThreadLock)reactorprotocol)connectProtocolTCP4ClientEndpointSSL4ClientEndpoint)IOpenSSLClientConnectionCreator)Failure)implementer)
ConnectionConnectionShutdownTimerTimerManagerConnectionException)SSLTFc                 b    	  |                                               d S # t          $ r Y d S w xY wN)_cleanupReferenceError)cleanup_weakrefs    m/Users/user/workspace/sujinbaek/cqa-test-app/venv/lib/python3.11/site-packages/cassandra/io/twistedreactor.pyr   r   -   sI    ""$$$$$   s     
..c                   *    e Zd ZdZd Zd Zd Zd ZdS )TwistedConnectionProtocolz[
    Twisted Protocol class for handling data received and connection
    made events.
    c                     || _         d S r   )
connection)selfr   s     r   __init__z"TwistedConnectionProtocol.__init__:   s    $    c                 v    | j         j                            |           | j                                          dS )z
        Callback function that is called when data has been received
        on the connection.

        Reaches back to the Connection object and queues the data for
        processing.
        N)r   _iobufwritehandle_readr   datas     r   dataReceivedz&TwistedConnectionProtocol.dataReceived=   s7     	$$T***##%%%%%r!   c                 D    | j                             | j                   dS )z
        Callback function that is called when a connection has succeeded.

        Reaches back to the Connection object and confirms that the connection
        is ready.
        N)r   client_connection_made	transportr   s    r   connectionMadez(TwistedConnectionProtocol.connectionMadeH   s"     	..t~>>>>>r!   c                 z    t                               d|           | j                            |j                   d S )NzConnect lost: %s)logdebugr   defunctvalue)r   reasons     r   connectionLostz(TwistedConnectionProtocol.connectionLostQ   s5    		$f--------r!   N)__name__
__module____qualname____doc__r    r(   r-   r4    r!   r   r   r   4   sZ         
% % %	& 	& 	&? ? ?. . . . .r!   r   c                   H    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d Zd Zd ZdS )	TwistedLoopNc                 R    t                      | _        t                      | _        d S r   )r   _lockr   _timersr,   s    r   r    zTwistedLoop.__init__^   s    VV
#~~r!   c           	      `   | j         5  t          j        st          t          j        dddi          | _        d| j        _        | j                                         t          j	        t          t          t          j        |                                d d d            d S # 1 swxY w Y   d S )N#cassandra_driver_twisted_event_loopinstallSignalHandlersF)targetnamekwargsT)r=   r   runningr   run_threaddaemonstartatexitregisterr   r   weakrefrefr,   s    r   maybe_startzTwistedLoop.maybe_startb   s    Z 	F 	F? F%W[+P.Eu-M O  O  O '+#""$$$'+d2C2C D DEEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs   BB##B'*B'c                     t           j        S r   )r   _stoppedr,   s    r   _reactor_stoppedzTwistedLoop._reactor_stoppedl   s    r!   c                 $   | j         rt          j        t          j                   | j                             d           | j                                         rt                              d           t                              d           d S d S )Ng      ?timeoutzrEvent loop thread could not be joined, so shutdown may not be clean. Please call Cluster.shutdown() to avoid this.zEvent loop thread was joined)	rG   r   callFromThreadstopjoinis_aliver/   warningr0   r,   s    r   r   zTwistedLoop._cleanupo   s    < 	6"7<000Lc***|$$&& A @ A A A II455555	6 	6r!   c                 x    | j                             |           t          j        | j        |j                   d S r   )r>   	add_timerr   rU   _schedule_timeoutend)r   timers     r   r[   zTwistedLoop.add_timery   s8    u%%% 	t5uyAAAAAr!   c                 B   |rt          |t          j                    z
  d          }| j        rI| j                                        r0|| j        k     r#| j                            |           || _        d S d S t          j        || j                  | _        || _        d S d S )Nr   )	maxtime_timeout_taskactive_timeoutresetr   	callLater_on_loop_timer)r   next_timeoutdelays      r   r\   zTwistedLoop._schedule_timeout   s     	-ty{{2A66E! -d&8&?&?&A&A -$-//&,,U333$0DMMM 0/ &-%6ud>Q%R%R" ,	- 	-r!   c                 v    | j                                          |                     | j         j                   d S r   )r>   service_timeoutsr\   rh   r,   s    r   rg   zTwistedLoop._on_loop_timer   s5    %%'''t|899999r!   )r5   r6   r7   r=   rG   rb   rd   r    rN   rQ   r   r[   r\   rg   r9   r!   r   r;   r;   W   s        EGMH& & &F F F     6 6 6B B B	- 	- 	-: : : : :r!   r;   c                   &    e Zd Zd Zd Zd Zd ZdS )_SSLCreatorc                 T   || _         || _        || _        || _        |r|| _        nt          j        t
          j                  | _        d| j        v r%| j                            | j        d                    d| j        v r%| j        	                    | j        d                    d| j        v r%| j        
                    | j        d                    d| j        v r,| j                            | j        d         | j                   | j                            | j                   d S )Ncertfilekeyfileca_certs	cert_reqs)callback)endpointssl_optionscheck_hostnamerT   contextr   ContextTLSv1_METHODuse_certificate_fileuse_privatekey_fileload_verify_locations
set_verifyverify_callbackset_info_callbackinfo_callback)r   rt   ssl_contextru   rv   rT   s         r   r    z_SSLCreator.__init__   s(    &, 	&DLL;s'788DLT---11$2B:2NOOOD,,,001A)1LMMMT---2243CJ3OPPPd...''$[1!1 (    	&&t'9:::::r!   c                     |S r   r9   )r   r   x509errnumerrdepthoks         r   r~   z_SSLCreator.verify_callback   s    	r!   c                 B   |t           j        z  r| j        r| j        j        |                                                                j        k    rO|                                }|	                    t          t          d| j                                       d S d S d S d S )NzHostname verification failed)r   SSL_CB_HANDSHAKE_DONErv   rt   addressget_peer_certificateget_subject
commonNameget_app_datafailVerificationr   r   )r   r   whereretr+   s        r   r   z_SSLCreator.info_callback   s    3,, 	x" xt}'<
@_@_@a@a@m@m@o@o@z'z'z&3355	**73FGegkgt3u3u+v+vwwwww	x 	xx x'z'zr!   c                     t          j        | j        d           }|                    |           | j        r<d| j        v r3|                    | j        d                             d                     |S )Nserver_hostnameascii)r   r   rw   set_app_dataru   set_tlsext_host_nameencode)r   tlsProtocolr   s      r   clientConnectionForTLSz"_SSLCreator.clientConnectionForTLS   su    ^DL$77
,,, 	a 1T5E E E++D,<=N,O,V,VW^,_,_```r!   N)r5   r6   r7   r    r~   r   r   r9   r!   r   rm   rm      sS        ; ; ;.  x x x    r!   rm   z3.30.0zWThe Twisted event loop is deprecated and will be removed in 3.31.0.  See CASSPYTHON-12.)versionr3   c                   l    e Zd ZdZdZed             Zed             Zd Zd Z	d Z
d Zd	 Zd
 Zd ZdS )TwistedConnectionz]
    An implementation of :class:`.Connection` that utilizes the
    Twisted event loop.
    Nc                 >    | j         st                      | _         d S d S r   )_loopr;   )clss    r   initialize_reactorz$TwistedConnection.initialize_reactor   s%    y 	&#CIII	& 	&r!   c                 Z    t          ||          }| j                            |           |S r   )r   r   r[   )r   rT   rs   r^   s       r   create_timerzTwistedConnection.create_timer   s,    gx((	E"""r!   c                     t          j        | g|R i | d| _        d| _        d| _        t          j        | j                   | j        	                                 dS )a  
        Initialization method.

        Note that we can't call reactor methods directly here because
        it's not thread-safe, so we schedule the reactor/connection
        stuff to be run from the event loop thread when it gets the
        chance.
        TN)
r   r    	is_closed	connectorr+   r   rU   add_connectionr   rN   )r   argsrD   s      r   r    zTwistedConnection.__init__   sh     	D242226222t2333
     r!   c                 |    | j         s| j        r-t          s$t          t	          t
                    dz             d S d S )NzO, pyOpenSSL must be installed to enable SSL support with the Twisted event loop)r   ru   _HAS_SSLImportErrorstrimport_exceptionr,   s    r   _check_pyopensslz"TwistedConnection._check_pyopenssl   sZ     	t/ 	 !())ef   	 	r!   c                    | j                                         \  }}| j        s| j        rh|                                  t          | j         | j        r| j        nd| j        | j        | j                  }t          t          |||| j                  }nt          t          ||| j                  }t          |t          |                      dS )z\
        Convenience function to connect and store the resulting
        connector.
        N)sslContextFactoryrT   rS   )rt   resolver   ru   r   rm   _check_hostnameconnect_timeoutr   r   r
   r	   r   )r   hostportssl_connection_creatorrt   s        r   r   z TwistedConnection.add_connection   s    
 ]**,,
d 	t/ 	 !!###%0$($4>  $ $$& &" *"8,  HH *,	  H 	";D"A"ABBBBBr!   c                     | j         5  d| _        ddd           n# 1 swxY w Y   || _        |                                  dS )z]
        Called by twisted protocol when a connection attempt has
        succeeded.
        FN)lockr   r+   _send_options_message)r   r+   s     r   r*   z(TwistedConnection.client_connection_made	  s    
 Y 	# 	#"DN	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#"""$$$$$s     c                    | j         5  | j        r	 ddd           dS d| _        ddd           n# 1 swxY w Y   t                              dt	          |           | j                   t          j        | j        j	        j
                   t                              d| j                   | j        sE|                     t          d| j        z                       | j                                         dS dS )z8
        Disconnect and error-out all requests.
        NTzClosing connection (%s) to %szClosed socket to %szConnection to %s was closed)r   r   r/   r0   idrt   r   rU   r+   r   
disconnect
is_defuncterror_all_requestsr   connected_eventsetr,   s    r   closezTwistedConnection.close  sO    Y 	" 	"~ 	" 	" 	" 	" 	" 	" 	" 	" "DN	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"
 			12d88T]KKKt~7BCCC		'777 	'##"#@4=#PQQS S S  $$&&&&&		' 	's   	1155c                 .    |                                   dS )z3
        Process the incoming data buffer.
        N)process_io_bufferr,   s    r   r%   zTwistedConnection.handle_read&  s     	     r!   c                 D    t          j        | j        j        |           dS )a  
        This function is called when outgoing data should be queued
        for sending.

        Note that we can't call transport.write() directly because
        it is not thread-safe, so we schedule it to run from within
        the event loop when it gets the chance.
        N)r   rU   r+   r$   r&   s     r   pushzTwistedConnection.push,  s"     	t~3T:::::r!   )r5   r6   r7   r8   r   classmethodr   r   r    r   r   r*   r   r%   r   r9   r!   r   r   r      s         
 E& & [&   [
! ! !$  !C !C !CF% % %' ' '&! ! !	; 	; 	; 	; 	;r!   r   ).r8   rJ   r   loggingra   	functoolsr   	threadingr   r   rL   twisted.internetr   r   twisted.internet.endpointsr	   r
   r   twisted.internet.interfacesr   twisted.python.failurer   zope.interfacer   cassandra.connectionr   r   r   r   r   OpenSSLr   r   r   er   	getLoggerr5   r/   r   Protocolr   objectr;   rm   r   r9   r!   r   <module>r      sy      ! ! ! ! ! !         " " " " " " " "  . . . . . . . . ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ G G G G G G * * * * * * & & & & & & i i i i i i i i i i i i i iHH   H g!!   .  .  .  .  . 1  .  .  .F5: 5: 5: 5: 5:& 5: 5: 5:p ,--& & & & && & & .-&P H%~|; |; |; |; |;
 |; |; @|; |; |;s   A# #A6(A11A6