
    wgt                     b    d dl Z d dlZd dlZ	 d dlZddlmZ  G d de      Zy# e$ r dZY w xY w)    N   )PubSubManagerc                   ^     e Zd ZdZdZ	 	 	 	 d fd	Z fdZd Zd Zd Z	d Z
d	 Zd
 Z xZS )KombuManagera  Client manager that uses kombu for inter-process messaging.

    This class implements a client manager backend for event sharing across
    multiple processes, using RabbitMQ, Redis or any other messaging mechanism
    supported by `kombu <http://kombu.readthedocs.org/en/latest/>`_.

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

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

    :param url: The connection URL for the backend messaging queue. Example
                connection URLs are ``'amqp://guest:guest@localhost:5672//'``
                and ``'redis://localhost:6379/'`` for RabbitMQ and Redis
                respectively. Consult the `kombu documentation
                <http://kombu.readthedocs.org/en/latest/userguide                /connections.html#urls>`_ for more on how to construct
                connection URLs.
    :param channel: The channel name on which the server sends and receives
                    notifications. Must be the same in all the servers.
    :param write_only: If set to ``True``, only initialize to emit events. The
                       default of ``False`` initializes the class for emitting
                       and receiving.
    :param connection_options: additional keyword arguments to be passed to
                               ``kombu.Connection()``.
    :param exchange_options: additional keyword arguments to be passed to
                             ``kombu.Exchange()``.
    :param queue_options: additional keyword arguments to be passed to
                          ``kombu.Queue()``.
    :param producer_options: additional keyword arguments to be passed to
                             ``kombu.Producer()``.
    kombuc	                     t         t        d      t        	|   |||       || _        |xs i | _        |xs i | _        |xs i | _        |xs i | _        | j                         | _
        y )NzLKombu package is not installed (Run "pip install kombu" in your virtualenv).)channel
write_onlylogger)r   RuntimeErrorsuper__init__urlconnection_optionsexchange_optionsqueue_optionsproducer_options_connectionpublisher_connection)
selfr   r	   r
   r   r   r   r   r   	__class__s
            [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/kombu_manager.pyr   zKombuManager.__init__1   s~     =  . / / 	ZO"4": 0 6B*0b 0 6B$($4$4$6!    c                 
   t         |           d}| j                  j                  dk(  rddlm}  |d      }n&d| j                  j                  v rddlm}  |d      }|s"t        d| j                  j                  z         y )	NTeventletr   )is_monkey_patchedsocketgevent)is_module_patchedz<Kombu requires a monkey patched socket library to work with )	r   
initializeserver
async_modeeventlet.patcherr   gevent.monkeyr   r   )r   monkey_patchedr   r   r   s       r   r    zKombuManager.initializeA   s}    ;;!!Z/:.x8N///7.x8N++0012 2 r   c                 V    t        j                  | j                  fi | j                  S )N)r   
Connectionr   r   )r   s    r   r   zKombuManager._connectionP   s"    DD,C,CDDr   c                     ddd}|j                  | j                         t        j                  | j                  fi |S )NfanoutF)typedurable)updater   r   Exchanger	   )r   optionss     r   	_exchangezKombuManager._exchangeS   s6    #6t,,-~~dll6g66r   c                     dt        t        j                               z   }dddid}|j                  | j                         t        j                  || j                         fi |S )Nzpython-socketio.Fz	x-expiresi )r+   queue_arguments)struuiduuid4r,   r   r   Queuer/   )r   
queue_namer.   s      r   _queuezKombuManager._queueX   sU    '#djjl*;;
#f8MNt))*{{:t~~'7C7CCr   c                      |j                   dd| j                         i| j                  }|j                  ||j                        S )Nexchange )Producerr/   r   ensurepublish)r   
connectionproducers      r   _producer_publishzKombuManager._producer_publish^   sL    &:&& @0@ @)-)>)>@  8+;+;<<r   c                 V   d}	 	 | j                  | j                        } |t        j                  |             y # t        t
        j                  j                  f$ rH |r"| j                         j                  d       d}n!| j                         j                  d       Y y Y nw xY w)NTz&Cannot publish to rabbitmq... retryingFz'Cannot publish to rabbitmq... giving up)
r@   r   pickledumpsOSErrorr   
exceptions
KombuError_get_loggererror)r   dataretryproducer_publishs       r   _publishzKombuManager._publishc   s    #'#9#9--$/  d!34U--889 $$&,, .8 9!E$$&,,AC	 	 s   6< A&B'&B'c              #     K   | j                         }d}	 	 | j                         5 }|j                  |      5 }	 |j                  d      }|j	                          |j
                   d}3# 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY wn{# t        t        j                  j                  f$ rU | j                         j                  dj                  |             t        j                  |       t        |dz  d      }Y nw xY ww)Nr   T)blockz3Cannot receive from rabbitmq... retrying in {} secs   <   )r7   r   SimpleQueuegetackpayloadrD   r   rE   rF   rG   rH   formattimesleepmin)r   reader_queueretry_sleepr>   queuemessages         r   _listenzKombuManager._listenu   s     {{}7%%' ,:#//= ,"&+iidi&;G#KKM")//1*+K	 #, , ,, , , U--889 7  "((**0&*=? 

;'!+/267 sQ   DB B5A..A7	3B;	B B	B DA5D	DD		D)z#amqp://guest:guest@localhost:5672//socketioFNNNNN)__name__
__module____qualname____doc__namer   r    r   r/   r7   r@   rL   r]   __classcell__)r   s   @r   r   r      sF     B D@>B;?6:7 2E7
D=
$7r   r   )rB   rV   r3   r   ImportErrorpubsub_managerr   r   r:   r   r   <module>rg      s?       *y7= y7  Es   $ ..