
    2Bf1                     ^    d dl Z d dlZd dlmZmZ  ej                  d      Z G d de      Zy)    N)bidictValueDuplicationErrorsocketioc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd ZddZd Zd Zd Z	 	 ddZd Zd Zd Zy)BaseManagera]  Manage client connections.

    This class keeps track of all the clients and the rooms they are in, to
    support the broadcasting of messages. The data used by this class is
    stored in a memory structure, making it appropriate only for single process
    services. More sophisticated storage backends can be implemented by
    subclasses.
    c                 X    d | _         d | _        i | _        i | _        i | _        i | _        y N)loggerserverrooms
eio_to_sid	callbackspending_disconnectselfs    U/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/socketio/base_manager.py__init__zBaseManager.__init__   s-    
"$    c                     || _         y r	   )r   )r   r   s     r   
set_serverzBaseManager.set_server   s	    r   c                      y)zqInvoked before the first request is received. Subclasses can add
        their initialization code here.
        N r   s    r   
initializezBaseManager.initialize   s     	r   c                 6    | j                   j                         S )z3Return an iterable with the active namespace names.)r   keysr   s    r   get_namespaceszBaseManager.get_namespaces#   s    zz  r   c              #     K   | j                   |   }t        |d      rht        |t              sX|d   |v r ||d      j                  j                         ni }|dd D ]&  }|j                  ||v r||   j                  ni        ( n#||v r||   j                  j                         ni }|j                         D ]  \  }}||f  yw)z:Return an iterable with the active participants in a room.__len__r      N)r   hasattr
isinstancestr_fwdmcopyupdateitems)r   	namespaceroomnsparticipantsrsideio_sids           r   get_participantszBaseManager.get_participants'   s     ZZ	"4#JtS,A7;Aw"}2d1g;,,113"L!"X D##17BqEKKCD 59BJ2d8>>..0BL(..0 	LCw,	s   CCc                     | j                   j                  j                         }	 | j                  ||d|       | j                  ||||       |S # t        $ r Y yw xY w)z,Register a client connection to a namespace.N)r-   )r   eiogenerate_id
enter_roomr   )r   r-   r'   r,   s       r   connectzBaseManager.connect3   s`    kkoo))+	OOCD'OB 	YW=
	 % 		s   A 	AAc                     || j                   v r|| j                   |   v ry	 | j                  |   d    |   d uS # t        $ r Y y w xY w)NF)r   r   KeyErrorr   r,   r'   s      r   is_connectedzBaseManager.is_connected>   s[    ///t..y99	::i(.s34?? 		s   9 	AAc                 `    	 | j                   |   d    j                  |   S # t        $ r Y y w xY wr	   )r   _invmr5   )r   r-   r'   s      r   sid_from_eio_sidzBaseManager.sid_from_eio_sidH   s8    	::i(.44W== 		s   ! 	--c                 b    || j                   v r!| j                   |   d    j                  |      S y r	   )r   getr6   s      r   eio_sid_from_sidzBaseManager.eio_sid_from_sidN   s2    

"::i(.22377 #r   c                 &    | j                  ||      S r	   )r7   r6   s      r   can_disconnectzBaseManager.can_disconnectR   s      i00r   c                     || j                   vrg | j                   |<   | j                   |   j                  |       | j                  |   d   j                  |      S )zPut the client in the to-be-disconnected list.

        This allows the client data structures to be present while the
        disconnect handler is invoked, but still recognize the fact that the
        client is soon going away.
        N)r   appendr   r<   r6   s      r   pre_disconnectzBaseManager.pre_disconnectU   sZ     D33313D##I.	*11#6zz)$T*..s33r   c                    || j                   vryg }| j                   |   j                         j                         D ]  \  }}||v s|j                  |        |D ]  }| j	                  |||        || j
                  v r| j
                  |= || j                  v rZ|| j                  |   v rH| j                  |   j                  |       t        | j                  |         dk(  r| j                  |= yyyy)z.Register a client disconnect from a namespace.Nr   )	r   r$   r&   rA   
leave_roomr   r   removelen)r   r,   r'   r   	room_namer(   s         r   
disconnectzBaseManager.disconnecta   s    DJJ&#zz)499;AAC 	(OItd{Y'	(  	2DOOCD1	2$.. s#///t..y99##I.55c:4**956!;++I6 < : 0r   Nc                 "   ||| j                   vrt        d      || j                   vri | j                   |<   || j                   |   vrt               | j                   |   |<   || j                   |   d   |   }|| j                   |   |   |<   y)zAdd a client to a room.Nz+sid is not connected to requested namespace)r   
ValueErrorr   )r   r,   r'   r(   r-   s        r   r2   zBaseManager.enter_rooms   s    ?y

:JKKDJJ&$&DJJy!tzz),,*0(DJJy!$'?jj+D1#6G+2

9d#C(r   c                     	 | j                   |   |   |= t        | j                   |   |         dk(  r:| j                   |   |= t        | j                   |         dk(  r| j                   |= yyy# t        $ r Y yw xY w)zRemove a client from a room.r   N)r   rF   r5   )r   r,   r'   r(   s       r   rD   zBaseManager.leave_room   s    	

9%d+C04::i(./14JJy)$/tzz),-2

9- 3 5  		s   A)A. .	A:9A:c                 ~    	 | j                  ||      D ]  \  }}| j                  |||        y# t        $ r Y yw xY w)z$Remove all participants from a room.N)r.   rD   r5   )r   r(   r'   r,   _s        r   
close_roomzBaseManager.close_room   sI    	//	4@ 6QY56 		s   -0 	<<c                     g }	 | j                   |   j                         D ]  \  }}|	||v s|j                  |         	 |S # t        $ r Y |S w xY w)z Return the rooms a client is in.)r   r&   rA   r5   )r   r,   r'   r+   rG   r(   s         r   	get_roomszBaseManager.get_rooms   si    	#'::i#8#>#>#@ (	4(SD[HHY'(
   		s   %A A A 	AAc                     || j                   vryt        |t              s|g}| j                  ||      D ]@  \  }}	||vs|| j	                  ||      }
nd}
| j
                  j                  |	||||
       B y)zaEmit a message to a single client, a room, or all the clients
        connected to the namespace.N)r   r!   listr.   _generate_ack_idr   _emit_internal)r   eventdatar'   r(   skip_sidcallbackkwargsr,   r-   ids              r   emitzBaseManager.emit   s     DJJ&(D) zH 11)TB 	PLC("'..sH=BB**7E4BO	Pr   c                     d}	 | j                   |   |   }| j                   |   |= | ||  yy# t        $ r" | j                         j                  d       Y 3w xY w)zInvoke an application callback.Nz$Unknown callback received, ignoring.)r   r5   _get_loggerwarning)r   r,   rZ   rV   rX   s        r   trigger_callbackzBaseManager.trigger_callback   so    	(~~c*2.H
 s#B'dO    	O&&'MN	Os   / (AAc                     || j                   vr$dt        j                  d      i| j                   |<   t        | j                   |   d         }|| j                   |   |<   |S )z/Generate a unique identifier for an ACK packet.r   r   )r   	itertoolscountnext)r   r,   rX   rZ   s       r   rS   zBaseManager._generate_ack_id   sZ    dnn$#$iooa&8"9DNN3$..%a()"*sB	r   c                     | j                   r| j                   S | j                  r| j                  j                   S t        S )zlGet the appropriate logger

        Prevents uninitialized servers in write-only mode from failing.
        )r
   r   default_loggerr   s    r   r]   zBaseManager._get_logger   s2     ;;;;[[;;%%%!!r   r	   )NNN)__name__
__module____qualname____doc__r   r   r   r   r.   r3   r7   r:   r=   r?   rB   rH   r2   rD   rN   rP   r[   r_   rS   r]   r   r   r   r   r   	   sv    %!
	81
47$
3		 @DP "r   r   )ra   loggingr   r   	getLoggerre   objectr   r   r   r   <module>rm      s.      0""":.D"& D"r   