
    i                     \    d dl Z ddlmZ 	 d dlZn# e$ r dZY nw xY w G d de          ZdS )    N   )AsyncPubSubManagerc                   P     e Zd ZdZdZ	 	 d fd	Zd Zd	 Zd
 Zd Z	d Z
d Z xZS )AsyncAioPikaManagera  Client manager that uses aio_pika for inter-process messaging under
    asyncio.

    This class implements a client manager backend for event sharing across
    multiple processes, using RabbitMQ

    To use a aio_pika backend, initialize the :class:`Server` instance as
    follows::

        url = 'amqp://user:password@hostname:port//'
        server = socketio.Server(client_manager=socketio.AsyncAioPikaManager(
            url))

    :param url: The connection URL for the backend messaging queue. Example
                connection URLs are ``'amqp://guest:guest@localhost:5672//'``
                for RabbitMQ.
    :param channel: The channel name on which the server sends and receives
                    notifications. Must be the same in all the servers.
                    With this manager, the channel name is the exchange name
                    in rabbitmq
    :param write_only: If set to ``True``, only initialize to emit events. The
                       default of ``False`` initializes the class for emitting
                       and receiving. A write-only instance can be used
                       independently of the server to emit to clients from an
                       external process.
    :param logger: a custom logger to log it. If not given, the server logger
                   is used.
    :param json: An alternative JSON module to use for encoding and decoding
                 packets. Custom json modules must have ``dumps`` and ``loads``
                 functions that are compatible with the standard library
                 versions. This setting is only used when ``write_only`` is set
                 to ``True``. Otherwise the JSON module configured in the
                 server is used.
    asyncaiopika#amqp://guest:guest@localhost:5672//socketioFNc                     t           t          d          t                                          ||||           || _        t          j                    | _        d | _        d | _	        d | _
        d S )NzRaio_pika package is not installed (Run "pip install aio_pika" in your virtualenv).)channel
write_onlyloggerjson)aio_pikaRuntimeErrorsuper__init__urlasyncioLock_lockpublisher_connectionpublisher_channelpublisher_exchange)selfr   r   r   r   r   	__class__s         I/usr/local/lib/python3.11/dist-packages/socketio/async_aiopika_manager.pyr   zAsyncAioPikaManager.__init__1   s~      . / / / 	Z" 	 	$ 	$ 	$\^^
$(!!%"&    c                 D   K   t          j        | j                   d {V S N)r   connect_robustr   )r   s    r   _connectionzAsyncAioPikaManager._connection?   s+      ,TX666666666r   c                 :   K   |                                  d {V S r   )r   )r   
connections     r   _channelzAsyncAioPikaManager._channelB   s*      '')))))))))r   c                 f   K   |                     | j        t          j        j                   d {V S r   )declare_exchanger   r   ExchangeTypeFANOUT)r   r   s     r   	_exchangezAsyncAioPikaManager._exchangeE   sR      --dl.6.C.JL L L L L L L L 	Lr   c                 ~   K   |                     dddi           d {V }|                    |           d {V  |S )NFz	x-expiresi )durable	arguments)declare_queuebind)r   r   exchangequeues       r   _queuezAsyncAioPikaManager._queueI   sy      ++E7BF6K , M M M M M M M Mjj"""""""""r   c                 h  K   | j         | j        4 d {V  | j         i|                                  d {V | _         |                     | j                    d {V | _        |                     | j                   d {V | _        	 d d d           d {V  n# 1 d {V swxY w Y   d}	 	 | j                            t          j	        | j
                            |                                          t          j        j                  d           d {V  d S # t          j        $ rY |r*|                                                     d           d}n*|                                                     d           Y d S Y n*t          j        j        $ r t)          j                    w xY w)NT)bodydelivery_mode*)routing_keyz&Cannot publish to rabbitmq... retryingFz'Cannot publish to rabbitmq... giving up)r   r   r!   r$   r   r)   r   publishr   Messager   dumpsencodeDeliveryMode
PERSISTENTAMQPException_get_loggererror
exceptionsChannelInvalidStateErrorr   CancelledError)r   dataretrys      r   _publishzAsyncAioPikaManager._publishO   s     $,z        ,46:6F6F6H6H0H0H0H0H0H0HD-37==14 4 . . . . . .D* 59NN.5 5 / / / / / /D+                            	//-55$!Y__T2299;;&.&;&F   $'	 6          )    $$&&,, .8 9 9 9!EE$$&&,,AC C CEE	 E
 &? / / /,.../%	/s&   A1B
B%(B%0A/D! !A"F/'F/c           	       K   d}	 	 |                                   d {V 4 d {V }|                     |           d {V }|                    d           d {V  |                     |           d {V }|                     ||           d {V }|                                4 d {V }|2 3 d {V }|                                4 d {V  |j        W V  d}d d d           d {V  n# 1 d {V swxY w Y   R6 	 d d d           d {V  n# 1 d {V swxY w Y   d d d           d {V  n# 1 d {V swxY w Y   n# t          j	        $ rj | 
                                                    d                    |                     t          j        |           d {V  t          |dz  d          }Y n*t          j        j        $ r t          j                    w xY w)Nr   T)prefetch_countz3Cannot receive from rabbitmq... retrying in {} secs   <   )r!   r$   set_qosr)   r1   iteratorprocessr3   r   r=   r>   r?   formatr   sleepminr@   rA   rB   )r   retry_sleepr#   r   r/   r0   
queue_itermessages           r   _listenzAsyncAioPikaManager._listenq   s     	//"&"2"2"4"4444444 
0 
0 
0 
0 
0 
0 
0$(MM*$=$=======G!///;;;;;;;;;%)^^G%<%<<<<<<<H"&++gx"@"@@@@@@@E$~~// 0 0 0 0 0 0 0:-7 0 0 0 0 0 0 0''.'8'8 0 0 0 0 0 0 0 0&-l 2 2 2 2./0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .8Z0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 ) 7 7 7  ""((**0&*=*=? ? ? mK000000000!+/266&? / / /,.../'	/s    E B	D<0D3D9DC3	!D3
C==D C=DD<
D#	#D<&D#	'D<*E <
EE 	E
E A6G-'G-)r   r	   FNN)__name__
__module____qualname____doc__namer   r!   r$   r)   r1   rE   rS   __classcell__)r   s   @r   r   r      s        ! !F D@IM' ' ' ' ' '7 7 7* * *L L L   /  /  /D/ / / / / / /r   r   )r   async_pubsub_managerr   r   ImportErrorr    r   r   <module>r]      s     4 4 4 4 4 4OOOO   HHH}/ }/ }/ }/ }/, }/ }/ }/ }/ }/s    