
    Yfk                        d Z ddlZddlZddlZddl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 ddlmZ dd	lmZ dd
lmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlm Z  	 ddl!m"Z" n# e#$ r  G d de$          Z"Y nw xY wdZ%d&                                Z'dZ( ej)        d          Z* ej)        d          Z+dedZ,ddddZ- G d de          Z.dS )zAMQP Connections.    N)array)	monotonic)ensure_promise   )__version__saslspec)AbstractChannel)Channel)	AMQPDeprecationWarningChannelErrorConnectionErrorConnectionForcedMessageNackedRecoverableChannelErrorRecoverableConnectionErrorResourceErrorerror_for_code)frame_handlerframe_writer	Transport)SSLErrorc                       e Zd ZdS )r   N)__name__
__module____qualname__     A/var/www/html/env/lib/python3.11/site-packages/amqp/connection.pyr   r      s        r   r   zThe .{attr} attribute on the connection was accessed before
the connection was established.  This is supported for now, but will
be deprecated in amqp 2.2.0.

Since amqp 2.0 you have to explicitly call Connection.connect()
before using the connection.
zP
Start from server, version: %d.%d, properties: %s, mechanisms: %s, locales: %s
)
Connectionamqpz)amqp.connection.Connection.heartbeat_tickzpy-amqp)productproduct_versionT)consumer_cancel_notifyzconnection.blockedauthentication_failure_closec                   T    e Zd ZdZeZeZeZdZ	dZ
dZdZdZdZdZdZdZ ej        ej        j        d           ej        ej        j                   ej        ej        j        d           ej        ej        j        d           ej        ej        j        d           ej        ej        j                   ej        ej        j                   ej        ej        j                  hZd eD             Zej        j        ej        j        fZee j!        e"e#fZ$e%fZ&e'e(e j!        e"e#fZ)e*fZ+d	d
d
ddddddddddddddddddde,e-f fd	Z.d Z/d Z0d Z1d;dZ2d Z3d;dZ4d Z5e6d             Z7e7j8        d             Z7e6d             Z9e9j8        d             Z9e6d             Z-e-j8        d             Z-	 d<dZ:d Z;d=d Z<d>d"Z=d# Z>	 	 d?d$Z?e6d%             Z@d& ZAd' ZBd( ZCd@d)ZDd* ZEd;d+ZFd;d,ZGd- ZH	 	 dAd0ZId1 ZJd2 ZKd3 ZLd4 ZMd5 ZNd6 ZOdBd8ZPe6d9             ZQe6d:             ZR xZSS )Cr!   a  AMQP Connection.

    The connection class provides methods for a client to establish a
    network connection to a server, and for both peers to operate the
    connection thereafter.

    GRAMMAR::

        connection          = open-connection *use-connection close-connection
        open-connection     = C:protocol-header
                              S:START C:START-OK
                              *challenge
                              S:TUNE C:TUNE-OK
                              C:OPEN S:OPEN-OK
        challenge           = S:SECURE C:SECURE-OK
        use-connection      = *channel
        close-connection    = C:CLOSE S:CLOSE-OK
                            / S:CLOSE C:CLOSE-OK
    Create a connection to the specified host, which should be
    a 'host[:port]', such as 'localhost', or '1.2.3.4:5672'
    (defaults to 'localhost', if a port is not specified then
    5672 is used)

    Authentication can be controlled by passing one or more
    `amqp.sasl.SASL` instances as the `authentication` parameter, or
    setting the `login_method` string to one of the supported methods:
    'GSSAPI', 'EXTERNAL', 'AMQPLAIN', or 'PLAIN'.
    Otherwise authentication will be performed using any supported method
    preferred by the server. Userid and passwords apply to AMQPLAIN and
    PLAIN authentication, whereas on GSSAPI only userid will be used as the
    client name. For EXTERNAL authentication both userid and password are
    ignored.

    The 'ssl' parameter may be simply True/False, or
    a dictionary of options to pass to :class:`ssl.SSLContext` such as
    requiring certain certificates. For details, refer ``ssl`` parameter of
    :class:`~amqp.transport.SSLTransport`.

    The "socket_settings" parameter is a dictionary defining tcp
    settings which will be applied as socket options.

    When "confirm_publish" is set to True, the channel is put to
    confirm mode. In this mode, each published message is
    confirmed using Publisher confirms RabbitMQ extension.
    Nr   ooFSSsBlBBsBBc                     i | ]
}|j         |S r   )
method_sig.0ms     r    
<dictcomp>zConnection.<dictcomp>   s    222Aa222r   zlocalhost:5672guestr   /en_USFc                 4   t          j                    j        | _        |pd}|pd}|r&t	          |t
          j                  r|f}|| _        n||dk    rt          j        |          }n|dk    rt          j	                    }n|dk    r)||t          d          t          j        ||          }nW|dk    r)||t          d          t          j        ||          }n(|t          j        ||          }nt          d|          |f| _        nVt          j        |d	
          t          j	                    t          j        ||          t          j        ||          f| _        t          | j        fi |	pi | _        || _        || _        || _        t)          |          | _        || _        || _        d| _        i | _        t5                                          | d           d | _        d | _        d | _        || _        || _         || _!        || _"        |
| _#        || _$        || _%        || _&        || _'        || _(        t)          |          | _)        tU          d          | _+        d| _,        d| _-        i | _.        g | _/        g | _0        || _1        d S )Ni  i   GSSAPIEXTERNALAMQPLAINz-Must supply authentication or userid/passwordPLAINzInvalid login methodT)	fail_softFr   H)2uuiduuid4hex_connection_id
isinstancer   SASLauthenticationr6   r7   
ValueErrorr8   r9   RAWdictlibrary_propertiesclient_propertieslocalehostvirtual_hostr   
on_tune_okframe_handler_clsframe_writer_cls_handshake_completechannelssuper__init___frame_writer_on_inbound_frame
_transportchannel_max	frame_maxclient_heartbeatconfirm_publishsslread_timeoutwrite_timeoutsocket_settings
on_blockedon_unblockedon_openr   _used_channel_idsversion_majorversion_minorserver_properties
mechanismslocalesconnect_timeout)selfrI   useridpasswordlogin_methodlogin_responserB   rJ   rH   rG   rY   rf   rU   rV   	heartbeatr_   r]   r^   rX   rK   rZ   r[   r\   r   r   kwargsauth	__class__s                              r    rQ   zConnection.__init__   s    #jll.!*U'	 	A.$)44 3"0!2"0D%x''{6**++}++>X%5$GI I I}VX66((>X%5$GI I Iz&(33+xn== !7FFF#''D#';v#F#F#F#'=??#'=#B#B#':fh#?#?#AD
 "&#"
 "
'8'>B"
 "
 	((44!. ,#( q!!!!!% '" ).(*. %(%g..!&s !#.r   c           
          | j         r,d| j         d| j         d| j          dt          |           dd	S d| j         d| j         dt          |           ddS )Nz<AMQP Connection: r3   z using z at z#x>z (disconnected) at )rT   rI   rJ   idrg   s    r    __repr__zConnection.__repr__  s    ? 	7@	 @ @D4E @ @ O@ @13D?@ @ @ @7	 7 7D4E 7 7(*467 7 7 7r   c                 .    |                                   | S N)connectrs   s    r    	__enter__zConnection.__enter__"  s    r   c                 .    |                                   d S rv   )close)rg   eargss     r    __exit__zConnection.__exit__&  s    

r   c                 8    | j                             ||          S rv   )r_   then)rg   
on_successon_errors      r    r~   zConnection.then)  s    |  X666r   c                    | j                             t          j        j        | j        t          j        j        | j        t          j        j        | j	        t          j        j
        | j        t          j        j        | j        t          j        j        | j        t          j        j        | j        t          j        j        | j        i           d S rv   )
_callbacksupdater	   r!   Start	_on_startOpenOk_on_open_okSecure
_on_secureTune_on_tuneClose	_on_closeBlocked_on_blocked	Unblocked_on_unblockedCloseOk_on_close_okrs   s    r    _setup_listenerszConnection._setup_listeners,  s    O!4>O"D$4O"DOO $-O!4>O#T%5O%t'9O#T%6	 
 		 		 		 		 		r   c                 
   | j         r|r
 |            nd S 	 |                     | j        | j        | j        | j        | j        | j                  | _        | j        	                                 | 
                    | | j                  | _        |                     | | j                  | _        | j        s$|                     | j                   | j        "d S d S # t"          t$          f$ r |                                   w xY w)N)r\   )timeout)	connectedr   rI   rf   rY   rZ   r[   r\   	transportrw   rL   on_inbound_methodon_inbound_framerM   r   rN   drain_eventsOSErrorr   collect)rg   callbacks     r    rw   zConnection.connect8  s/    > 	4!)388:::t3	!^^	4/!4#5 $ 4 ,  DN
 N""$$$$($:$:d,%. %.D! $ 5 5dDN K KD. @!!$*>!??? . @ @ @ @ @ " 	 	 	LLNNN	s   C C 'Dc                 z    t          j        t          t                              |                               d S )N)attr)warningswarnr   W_FORCE_CONNECTformat)rg   r   s     r    _warn_force_connectzConnection._warn_force_connectP  sA    ,"""--/ / 	0 	0 	0 	0 	0r   c                 p    | j         )|                     d           |                                  | j         S )Nr   )rT   r   rw   rs   s    r    r   zConnection.transportT  s3    ?"$$[111LLNNNr   c                     || _         d S rv   )rT   )rg   r   s     r    r   zConnection.transport[  s    #r   c                 p    | j         )|                     d           |                                  | j         S )Nr   )rS   r   rw   rs   s    r    r   zConnection.on_inbound_frame_  s6    !)$$%7888LLNNN%%r   c                     || _         d S rv   )rS   )rg   r   s     r    r   zConnection.on_inbound_framef  s    !1r   c                 p    | j         )|                     d           |                                  | j         S )Nr   )rR   r   rw   rs   s    r    r   zConnection.frame_writerj  s5    %$$^444LLNNN!!r   c                     || _         d S rv   )rR   )rg   r   s     r    r   zConnection.frame_writerq  s    )r   FsSsc                 J   | j         }|| _        || _        || _        t	          |t
                    r|                    d          }|                    d          | _        |                    d          | _	        t                              t          | j        | j        | j        | j        | j	                   |                    d          pi |                    di           }|                    fd| j                                        D                        |s|                    dd            | j        D ]0}	|	j        | j        v r |	                    |           }
|
t,          ur n1t/          d                    d                    d | j        D                                                       d                    | j                                                                      |                     t8          j        j        |||	j        |
| j        f           d S )	Nzutf-8     capabilitiesc                 F    i | ]\  }}                     |          ||S r   )get)r/   
wanted_cap
enable_capscaps      r    r1   z(Connection._on_start.<locals>.<dictcomp>  sA     
 
 
&
Jxx
##


 
 
r   zGCouldn't find appropriate auth mechanism (can offer: {}; available: {})s   , c              3   2   K   | ]}|j         	|j         V  d S rv   )	mechanismr.   s     r    	<genexpr>z'Connection._on_start.<locals>.<genexpr>  s?       / /#$"#+/q{ / / / / / /r   ) rG   ra   rb   rc   r@   strencodesplitrd   re   AMQP_LOGGERdebugSTART_DEBUG_FMTr   
setdefaultr   negotiate_capabilitiesitemspoprB   r   startNotImplementedr   r   joindecodesend_methodr	   r!   StartOkrH   )rg   ra   rb   rc   rd   re   argsigrG   caprB   rk   r   s              @r    r   zConnection._on_startu  sE    2**!2j#&& 	4#**733J$**400}}S)) 2"DOT\	
 	
 	
 !$$^44:**>2>>

 
 
 
 
*.*E*K*K*M*M
 
 
 	 	 	
  	8 !!.$777"1 	; 	;N'4?::!/!5!5d!;!;!77E!117JJ / /(,(;/ / / / //5vxxJJt//6688	2: 2:; ; ; 	O#V 8T[*	
 	
 	
 	
 	
r   c                     d S rv   r   )rg   	challenges     r    r   zConnection._on_secure  s    r   c                    | j         pd}|p| j        | _        |p| j        | _        |pd| _        | j        dk    s|dk    rt	          | j        |          | _        nt          | j        |          | _        | j         sd| _        |                     t          j	        j
        || j        | j        | j        f| j                   d S )Nr   r   )rW   rU   rV   server_heartbeatmaxrl   minr   r	   r!   TuneOk_on_tune_sent)rg   rU   rV   r   r   rW   s         r    r   zConnection._on_tune  s    05A&:$*:"4dn 0 5A  A%%)9Q)>)> !68HIIDNN !68HIIDN $ 	DNO"Ft~t~>' 	 	
 	
 	
 	
 	
r   ssbc                 b    |                      t          j        j        || j        ddf           d S )N F)r   r	   r!   OpenrJ   )rg   r   s     r    r   zConnection._on_tune_sent  s:    O &4+<b%*H	
 	
 	
 	
 	
r   c                 >    d| _         |                     |            d S )NT)rN   r_   rs   s    r    r   zConnection._on_open_ok  s"    #' Tr   c           	      (    t          |f|||||d|S )N)rf   rY   rZ   r[   r\   r   )rg   rI   rf   rY   rZ   r[   r\   rm   s           r    r   zConnection.Transport  s:     7"1s%]+7 7 067 7 	7r   c                 (    | j         o| j         j        S rv   )rT   r   rs   s    r    r   zConnection.connected  s    <4?#<<r   c                       j         r j                                           j        r> fd j                                        D             }|D ]}|                                 d x _         x _         _        d S )Nc                     g | ]}|u|	S r   r   )r/   chrg   s     r    
<listcomp>z&Connection.collect.<locals>.<listcomp>  s*       T>> !>>r   )rT   rz   rO   valuesr   
connection)rg   rO   r   s   `  r    r   zConnection.collect  s    ? 	$O!!###= 		   !]1133  H
   

<@@@$/DMMMr   c                 8   t          | j                  }t          d| j        dz             D ]$}||vr| j                            |           |c S %t          d                    t          | j                  | j                  t          j
        j                  )Nr   z/No free channel ids, current={}, channel_max={})setr`   rangerU   appendr   r   lenrO   r	   r   r   )rg   used_channel_ids
channel_ids      r    _get_free_channel_idzConnection._get_free_channel_id  s    t5664#3a#788 	" 	"J!111&--j999!!!! 2 =DDDM""D$46 67;|7HJ J 	Jr   c                 r    || j         v rt          d|d          | j                             |           |S )NzChannel z already open)r`   r   r   )rg   r   s     r    _claim_channel_idzConnection._claim_channel_id  sG    ///!"HZ"H"H"HIII"))*555r   c                     | j         t          d          	 | j         |         S # t          $ r1 |                     | ||          }|                                 |cY S w xY w)zCreate new channel.

        Fetch a Channel object identified by the numeric channel_id, or
        create that object if it doesn't already exist.
        NzConnection already closed.)r_   )rO   r   KeyErrorr   open)rg   r   r   channels       r    r   zConnection.channel  su     = ,-IJJJ	=,, 	 	 	ll4XlFFGLLNNNNNN	s   % 8A A c                      t          d          )NzUse AMQP heartbeats)NotImplementedErrorrs   s    r    is_alivezConnection.is_alive	  s    !"7888r   c                 `    |                      |          s	 |                      |          d S d S rv   )blocking_read)rg   r   s     r    r   zConnection.drain_events  sG    $$W-- 	 $$W-- 	 	 	 	 	r   c                     | j                             |          5  | j                                         }d d d            n# 1 swxY w Y   |                     |          S rv   )r   having_timeout
read_framer   )rg   r   frames      r    r   zConnection.blocking_read  s    ^**733 	0 	0N--//E	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0$$U+++s   AAAc                 r    | j         t          d          | j         |                             |||          S )NzConnection already closed)rO   r   dispatch_method)rg   r   r-   payloadcontents        r    r   zConnection.on_inbound_method  s?    = ,-HIII}Z(88
 
 	
r   r   r   r   c           	      ,   | j         dS 	 d| _        |                     t          j        j        ||||d         |d         ft          j        j                  d| _        S # t          t          f$ r | 	                                  w xY w# d| _        w xY w)  Request a connection close.

        This method indicates that the sender wants to close the
        connection. This may be due to internal conditions (e.g. a
        forced shut-down) or due to an error handling a specific
        method, i.e. an exception.  When a close is due to an
        exception, the sender provides the class and method id of the
        method which caused the exception.

        RULE:

            After sending this method any received method except the
            Close-OK method MUST be discarded.

        RULE:

            The peer sending this method MAY use a counter or timeout
            to detect failure of the other peer to respond correctly
            with the Close-OK method.

        RULE:

            When a server receives the Close method from a client it
            MUST delete all server-side resources associated with the
            client's context.  A client CANNOT reconnect to a context
            after sending or receiving a Close method.

        PARAMETERS:
            reply_code: short

                The reply code. The AMQ reply codes are defined in AMQ
                RFC 011.

            reply_text: shortstr

                The localised reply text.  This text can be logged as an
                aid to resolving issues.

            class_id: short

                failing method class

                When the close is provoked by a method exception, this
                is the class of the method.

            method_id: short

                failing method ID

                When the close is provoked by a method exception, this
                is the ID of the method.
        NTr   r   )waitF)
rT   
is_closingr   r	   r!   r   r   r   r   r   )rg   
reply_code
reply_textr-   r   s        r    rz   zConnection.close  s    l ?"F	$"DO##%vZA
1F_, $   $DOO " 	 	 	LLNNN	
 $DO####s   AA   'BB
 
	Bc                 \    |                                   t          ||||ft                    )r   )_x_close_okr   r   )rg   r  r  class_id	method_ids        r    r   zConnection._on_closef  s:    j 	Z&	2OE E 	Er   c                 \    |                      t          j        j        | j                   dS )ah  Confirm a connection close.

        This method confirms a Connection.Close method and tells the
        recipient that it is safe to release resources for the
        connection and close the socket.

        RULE:
            A peer that detects a socket closure without having
            received a Close-Ok handshake method SHOULD log the error.
        r   N)r   r	   r!   r   r   rs   s    r    r  zConnection._x_close_ok  s+     	04;LMMMMMr   c                 .    |                                   dS )ai  Confirm a connection close.

        This method confirms a Connection.Close method and tells the
        recipient that it is safe to release resources for the
        connection and close the socket.

        RULE:

            A peer that detects a socket closure without having
            received a Close-Ok handshake method SHOULD log the error.
        N)r   rs   s    r    r   zConnection._on_close_ok  s     	r   c                 B    d}| j         r|                      |          S dS )zlCallback called when connection blocked.

        Notes:
            This is an RabbitMQ Extension.
        z#connection blocked, see broker logsN)r]   )rg   reasons     r    r   zConnection._on_blocked  s/     7? 	+??6***	+ 	+r   c                 <    | j         r|                                  S d S rv   )r^   rs   s    r    r   zConnection._on_unblocked  s)     	'$$&&&	' 	'r   c                 8    |                      ddd d d            d S )N   r   )r   rs   s    r    send_heartbeatzConnection.send_heartbeat  s$    !QdD11111r      c                    t                               d| j                   | j        sdS |dk    rd}| j        }| j        }| j        | j        |k    rt                      | _        | j	        | j	        |k    rt                      | _
        t                      }t                               d| j        | j	        |||| j        | j        | j        	  	         ||c| _        | _	        || j        | j        |z  z   k    rGt                               d| j                   |                                  t                      | _        d| j        z  }| j
        |z   }|t                      k     }| j
        r|rt          d          dS dS )a  Send heartbeat packets if necessary.

        Raises:
            ~amqp.exceptions.ConnectionForvced: if none have been
                received recently.

        Note:
            This should be called frequently, on the order of
            once per second.

        Keyword Arguments:
            rate (int): Number of heartbeat frames to send during the heartbeat
                        timeout
        z"heartbeat_tick : for connection %sNr   r  zheartbeat_tick : Prev sent/recv: %s/%s, now - %s/%s, monotonic - %s, last_heartbeat_sent - %s, heartbeat int. - %s for connection %sz3heartbeat_tick: sending heartbeat for connection %szToo many heartbeats missed)AMQP_HEARTBEAT_LOGGERr   r?   rl   
bytes_sent
bytes_recv	prev_sentr   last_heartbeat_sent	prev_recvlast_heartbeat_receivedr  r   )rg   ratesent_nowrecv_nownowtwo_heartbeatstwo_heartbeats_intervalheartbeats_misseds           r    heartbeat_tickzConnection.heartbeat_tick  s    	##$H$($7	9 	9 	9~ 	F 199D ??>!T^x%?%?'0{{D$>!T^x%?%?+4;;D(kk##  NDNh$N
	
 
	
 
	
 *28& )DNT,AAAA!''E#% % % !!!'0{{D$ T^+"&">"O3ikkA' 	A,= 	A"#?@@@	A 	A 	A 	Ar   c                     | j         j        S rv   )r   sockrs   s    r    r!  zConnection.sock
  s    ~""r   c                 :    | j                             d          pi S )Nr   )rc   r   rs   s    r    server_capabilitieszConnection.server_capabilities  s    %)).99?R?r   rv   )r   )r*   )r   )FNNN)NN)r   r   r   r+   )r  )Tr   r   r   __doc__r   NEGOTIATE_CAPABILITIESr   LIBRARY_PROPERTIESrF   rl   rW   r   r  r  r  r  r  r  r	   methodr!   r   r   r   r   r   r   r   r   _METHODS_ALLOWED_METHODS_WHEN_CLOSINGr   socketerrorIOErrorr   connection_errorsr   channel_errorsr   r   recoverable_connection_errorsr   recoverable_channel_errorsr   r   rQ   rt   rx   r|   r~   r   rw   r   propertyr   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   r   r  r   r   r   r  r  r!  r#  __classcell__)ro   s   @r    r!   r!   ?   s*       , ,\ G 4 , I      J J I I 	DO)733DO*++DO*C00DO(%00DO)622DO+,,DO-..DO+,,	H 32222H 	t6%!
 		 #_N"%! 	 " -Ww"4 "!'TDd1dt"E t4!%]*Y/ Y/ Y/ Y/ Y/ Y/v7 7 7    7 7 7 7
 
 
   00 0 0   X $ $ $ & & X& 2 2 2 " " X" * * * /5/
 /
 /
 /
b  
 
 
 
.
 
 
 

  
 ?C"&7 7 7 7 = = X=A A A J J J      9 9 9   
, , , ,

 
 
 =CF$ F$ F$ F$P7E 7E 7ErN N N  + + +' ' '2 2 2=A =A =A =A~ # # X# @ @ X@ @ @ @ @r   r!   )/r$  loggingr*  r<   r   r   timer   viner   r   r   r   r	   abstract_channelr
   r   r   
exceptionsr   r   r   r   r   r   r   r   r   method_framingr   r   r   r   rY   r   ImportError	Exceptionr   stripr   __all__	getLoggerr   r  r&  r%  r!   r   r   r    <module>r?     sl                           % % % % % % % % % % - - - - - -      ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 8 7 7 7 7 7 7 7                   9     	EGG  g''))/   "   #$(  Q@ Q@ Q@ Q@ Q@ Q@ Q@ Q@ Q@ Q@s    A' 'A=<A=