o
    ¨Ýjµ  ã                   @   sT   d dl Z d dlZzd dlZW n ey   dZY nw ddlmZ G dd„ deƒZdS )é    Né   )ÚPubSubManagerc                       sj   e Zd ZdZdZ				d‡ fdd„	Z‡ fd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡  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. 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.
    :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()``.
    Úkombuú#amqp://guest:guest@localhost:5672//ÚsocketioFNc
           
         s`   t d u rtdƒ‚tƒ j||||d� || _|pi | _|pi | _|p"i | _|	p'i | _|  	¡ | _
d S )NzLKombu package is not installed (Run "pip install kombu" in your virtualenv).)ÚchannelÚ
write_onlyÚloggerÚjson)r   ÚRuntimeErrorÚsuperÚ__init__ÚurlÚconnection_optionsÚexchange_optionsÚqueue_optionsÚproducer_optionsÚ_connectionÚpublisher_connection)
Úselfr   r   r	   r
   r   r   r   r   r   ©Ú	__class__© úQ/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/kombu_manager.pyr   :   s   ÿ



zKombuManager.__init__c                    sh   t ƒ  ¡  d}| jjdkrddlm} |dƒ}nd| jjv r(ddlm} |dƒ}|s2td| jj ƒ‚d S )	NTZeventletr   )Úis_monkey_patchedÚsocketZgevent)Úis_module_patchedz<Kombu requires a monkey patched socket library to work with )	r   Ú
initializeÚserverZ
async_modeZeventlet.patcherr   Zgevent.monkeyr   r   )r   Zmonkey_patchedr   r   r   r   r   r   K   s   

ÿÿÿzKombuManager.initializec                 C   s   t j| jfi | j¤ŽS )N)r   Ú
Connectionr   r   )r   r   r   r   r   Z   s   zKombuManager._connectionc                 C   s*   dddœ}|  | j¡ tj| jfi |¤ŽS )NZfanoutF)ÚtypeÚdurable)Úupdater   r   ZExchanger   )r   Úoptionsr   r   r   Ú	_exchange]   s   
zKombuManager._exchangec                 C   sB   dt t ¡ ƒ }dddidœ}| | j¡ tj||  ¡ fi |¤ŽS )Nzpython-socketio.Fz	x-expiresià“ )r"   Zqueue_arguments)ÚstrÚuuidÚuuid4r#   r   r   ÚQueuer%   )r   Z
queue_namer$   r   r   r   Ú_queueb   s   zKombuManager._queuec                 C   s(   |j dd|  ¡ i| j¤Ž}| ||j¡S )NZexchanger   )ZProducerr%   r   ZensureÚpublish)r   Ú
connectionZproducerr   r   r   Ú_producer_publishh   s   ÿzKombuManager._producer_publishc              
   C   sœ   d}	 z|   | j¡}|| j |¡ƒ W d S  tyL } z,|r.|  ¡ jddt|ƒid� d}n|  ¡ jddt|ƒid� W Y d }~d S W Y d }~nd }~ww q)NTz&Cannot publish to rabbitmq... retryingÚrabbitmq_exception©ÚextraFz'Cannot publish to rabbitmq... giving up)r-   r   r   ÚdumpsÚ	ExceptionÚ_get_loggerÚerrorr&   )r   ÚdataÚretryZproducer_publishÚexcr   r   r   Ú_publishm   s0   ÿ
þ
þû€ûúzKombuManager._publishc              
   c   sÞ   � d}	 z:|   ¡ }|  ¡ �(}| |¡�}	 |jdd�}| ¡  |jV  d}q1 s)w   Y  W d   ƒ n1 s8w   Y  W n/ tym } z#|  ¡ jd|› d�dt	|ƒid� t
 |¡ t|d d	ƒ}W Y d }~nd }~ww q)
Nr   T)Úblockz,Cannot receive from rabbotmq... retrying in z secsr.   r/   é   é<   )r*   r   ÚSimpleQueueÚgetZackÚpayloadr2   r3   r4   r&   ÚtimeÚsleepÚmin)r   Zretry_sleepZreader_queuer,   ÚqueueÚmessager7   r   r   r   Ú_listen�   s8   €
üÿÿ€ÿ
ý
€úözKombuManager._listen)	r   r   FNNNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r   r   r%   r*   r-   r8   rD   Ú__classcell__r   r   r   r   r      s    +ýr   )r?   r'   r   ÚImportErrorZpubsub_managerr   r   r   r   r   r   Ú<module>   s    ÿ