
    2Bf                     N    d dl mZ d dlZd dlmZ d dlZddlmZ  G d de      Zy)    )partialN)json   )BaseManagerc                        e Zd ZdZdZd fd	Z fdZ	 	 d fd	Z fdZddZ	d Z
d	 Z fd
Zd Zd Zd Z fdZd Z xZS )PubSubManagera=  Manage a client list attached to a pub/sub backend.

    This is a base class that enables multiple servers to share the list of
    clients, with the servers communicating events through a pub/sub backend.
    The use of a pub/sub backend also allows any client connected to the
    backend to emit events addressed to Socket.IO clients.

    The actual backends must be implemented by subclasses, this class only
    provides a pub/sub generic framework.

    :param channel: The channel name on which the server sends and receives
                    notifications.
    pubsubc                     t         t        |           || _        || _        t        j                         j                  | _        || _	        y N)
superr   __init__channel
write_onlyuuiduuid4hexhost_idlogger)selfr   r   r   	__class__s       W/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/socketio/pubsub_manager.pyr   zPubSubManager.__init__   s8    mT+-$zz|''    c                     t         t        |           | j                  s*| j                  j                  | j                        | _        | j                         j                  | j                  dz          y )Nz backend initialized.)r   r   
initializer   serverstart_background_task_threadthread_get_loggerinfoname)r   r   s    r   r   zPubSubManager.initialize!   sR    mT-/++;;DLLIDK		,C CDr   c                 .   |j                  d      rt        t        |   ||||||      S |xs d}|<| j                  t        d      |t        d      | j                  ||      }|||f}nd}| j                  d||||||| j                  d       y)	a/  Emit a message to a single client, a room, or all the clients
        connected to the namespace.

        This method takes care or propagating the message to all the servers
        that are connected through the message queue.

        The parameters are the same as in :meth:`.Server.emit`.
        ignore_queue	namespaceroomskip_sidcallback/Nz:Callbacks can only be issued from the context of a server.z'Cannot use callback without a room set.emit)methodeventdatar%   r&   r'   r(   r   )
getr   r   r*   r   RuntimeError
ValueError_generate_ack_id_publishr   )
r   r,   r-   r%   r&   r'   r(   kwargsidr   s
            r   r*   zPubSubManager.emit'   s     ::n%2tyth! 3 # # $	{{"" $: ; ;| !JKK&&tX6Bi,HH%$-t#+"&,,0 	1r   c                 |    | j                  ||      rt        | 	  ||      S | j                  d||xs dd       y )N
disconnectr)   )r+   sidr%   )is_connectedr   can_disconnectr2   )r   r7   r%   r   s      r   r9   zPubSubManager.can_disconnectE   sD    S),7)#y99 MM\#(1(8S: ;r   c                 6    | j                  d||xs dd       y )N
close_roomr)   )r+   r&   r%   r2   )r   r&   r%   s      r   r;   zPubSubManager.close_roomN   s    t$-$46 	7r   c                     t        d      )zPublish a message on the Socket.IO channel.

        This method needs to be implemented by the different subclasses that
        support pub/sub backends.
        .This method must be implemented in a subclass.NotImplementedError)r   r-   s     r   r2   zPubSubManager._publishR   s     " #. / 	/r   c                     t        d      )zReturn the next message published on the Socket.IO channel,
        blocking until a message is available.

        This method needs to be implemented by the different subclasses that
        support pub/sub backends.
        r>   r?   )r   s    r   _listenzPubSubManager._listen[   s     " #. / 	/r   c           	      6   |j                  d      }|j                  d      }|&t        |      dk(  rt        | j                  |g| }nd }t        t
        |   |d   |d   |j                  d      |j                  d      |j                  d      |	       y )
Nr(   r      r,   r-   r%   r&   r'   r$   )r.   lenr   _return_callbackr   r   r*   )r   messageremote_callbackremote_host_idr(   r   s        r   _handle_emitzPubSubManager._handle_emite   s     "++j1 Y/&3+?1+Dt44n 1 /1H HmT'(8'&/29++k2J-4[[-@18Z1H19	 	( 	;r   c                     | j                   |j                  d      k(  r$	 |d   }|d   }|d   }| j                  |||       y y # t        $ r Y y w xY w)Nr   r7   r4   args)r   r.   KeyErrortrigger_callback)r   rG   r7   r4   rL   s        r   _handle_callbackzPubSubManager._handle_callbackv   sd    <<7;;y11enT]v !!#r40 2
  s   A 	AAc           	      4    | j                  d|||||d       y )Nr(   )r+   r   r7   r%   r4   rL   r<   )r   r   r7   r%   callback_idrL   s         r   rF   zPubSubManager._return_callback   s#     	!	#% 	&r   c                 |    | j                   j                  |j                  d      |j                  d      d       y )Nr7   r%   T)r7   r%   r#   )r   r6   r.   )r   rG   s     r   _handle_disconnectz PubSubManager._handle_disconnect   s3    7;;u#5)0[)A,0 	 	2r   c                 l    t         t        |   |j                  d      |j                  d             y )Nr&   r%   )r&   r%   )r   r   r;   r.   )r   rG   r   s     r   _handle_close_roomz PubSubManager._handle_close_room   s/    mT-V$K0H 	. 	Jr   c                 2   | j                         D ]  }d }t        |t              r|}n>t        |t              r	 t	        j
                  |      }|	 t        j
                  |      }|sYd|v s^| j                         j                  dj                  |d                |d   dk(  r| j                  |       |d   dk(  r| j                  |       |d   dk(  r| j                  |       |d   dk(  s| j                  |        y #  Y xY w#  Y xY w)Nr+   zpubsub message: {}r*   r(   r6   r;   )rB   
isinstancedictbytespickleloadsr   r   r    formatrJ   rO   rS   rU   )r   rG   r-   s      r   r   zPubSubManager._thread   s   ||~ 	2GD'4(gu-%||G4 <#zz'2 D(  "''(<(C(CN)$ %>V+%%d+(^z1))$/(^|3++D1(^|3++D13	2s   DDDD)socketioFN)NNNNr   )__name__
__module____qualname____doc__r!   r   r   r*   r9   r;   r2   rB   rJ   rO   rF   rS   rU   r   __classcell__)r   s   @r   r   r   
   s\     DE EI1<;7//;"1&2
J2r   r   )		functoolsr   r   engineior   rZ   base_managerr   r    r   r   <module>rg      s"        %`2K `2r   