o
    ¨Ýj*  ã                   @   sV   d dl Z zd dlZW n ey   dZY nw ddlmZ e  d¡ZG dd„ deƒZdS )é    Né   )ÚPubSubManagerÚsocketioc                       sB   e Zd ZdZdZ		d‡ fdd„	Zd	d
„ Zdd„ Zdd„ Z‡  Z	S )ÚKafkaManagera}  Kafka based client manager.

    This class implements a Kafka backend for event sharing across multiple
    processes.

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

        url = 'kafka://hostname:port'
        server = socketio.Server(client_manager=socketio.KafkaManager(url))

    :param url: The connection URL for the Kafka server. For a default Kafka
                store running on the same host, use ``kafka://``. For a highly
                available deployment of Kafka, pass a list with all the
                connection URLs available in your cluster.
    :param channel: The channel name (topic) 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.
    Úkafkaúkafka://localhost:9092r   FNc                    sp   t d u rtdƒ‚tƒ j||||d� t|tƒr|gn|}dd„ |D ƒ| _t j| jd�| _t j	| j
| jd�| _d S )NzZkafka-python package is not installed (Run "pip install kafka-python" in your virtualenv).)ÚchannelÚ
write_onlyÚloggerÚjsonc                 S   s$   g | ]}|d kr|dd… nd‘qS )zkafka://é   Nzlocalhost:9092© )Ú.0Úurlr   r   úQ/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/kafka_manager.pyÚ
<listcomp>;   s    ÿz)KafkaManager.__init__.<locals>.<listcomp>)Zbootstrap_servers)r   ÚRuntimeErrorÚsuperÚ__init__Ú
isinstanceÚstrZ
kafka_urlsZKafkaProducerÚproducerZKafkaConsumerr   Úconsumer)Úselfr   r   r	   r
   r   Úurls©Ú	__class__r   r   r   0   s   ÿÿÿzKafkaManager.__init__c                 C   s(   | j j| j| j |¡d� | j  ¡  d S )N)Úvalue)r   Úsendr   r   ÚdumpsÚflush)r   Údatar   r   r   Ú_publishA   s   zKafkaManager._publishc                 c   s   � | j E d H  d S ©N)r   )r   r   r   r   Ú_kafka_listenE   s   €zKafkaManager._kafka_listenc                 c   s(   � |   ¡ D ]}|j| jkr|jV  qd S r#   )r$   Ztopicr   r   )r   Úmessager   r   r   Ú_listenH   s   €€þzKafkaManager._listen)r   r   FNN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r"   r$   r&   Ú__classcell__r   r   r   r   r      s     ÿr   )Úloggingr   ÚImportErrorZpubsub_managerr   Ú	getLoggerr
   r   r   r   r   r   Ú<module>   s    ÿ
