
    i"                         S SK r S SKrS SKJr   S SKrS SKJr   S SKrS SK	J
r
  SSKJr  \ R                  " S5      rS r " S	 S
\5      rg! \ a    SrSr N>f = f! \ a    SrSr
 NCf = f)    N)urlparse)
RedisError)ValkeyError   )PubSubManagersocketioc                    [        U 5      nUR                  S;  a  [        S5      e/ nUR                  R	                  S5      S   R	                  S5       H3  nUR                  SS5      u  pEUR                  U[        U5      45        M5     0 nUR                  (       a  UR                  US'   UR                  (       a  UR                  US	'   S
nUR                  (       aa  UR                  R	                  S5      n[        U5      S:  a  US   S:w  a  [        US   5      US'   [        U5      S:  a  US   S:w  a  US   nX'U4$ )zuParse a Redis Sentinel URL with the format:
redis+sentinel://[:password]@host1:port1,host2:port2,.../db/service_name
   redis+sentinelvalkey+sentinelzInvalid Redis Sentinel URL@,:r   usernamepasswordN/    db   )r   scheme
ValueErrornetlocsplitrsplitappendintr   r   pathlen)	url
parsed_url	sentinels	host_porthostportkwargsservice_namepartss	            S/home/admin/cozy_coffee/venv/lib/python3.13/site-packages/socketio/redis_manager.pyparse_redis_sentinel_urlr+      s,    #J EE566I&&,,S1"5;;C@	%%c1-
$D	*+ A F'00z'00zL%%c*u:?uQx2~uQx=F4Lu:?uQx2~ 8LF**    c                   b   ^  \ rS rSrSrSr  SU 4S jjrU 4S jrS rS r	S r
S	 rS
 rSrU =r$ )RedisManager2   a  Redis based client manager.

This class implements a Redis backend for event sharing across multiple
processes. Only kept here as one more example of how to build a custom
backend, since the kombu backend is perfectly adequate to support a Redis
message queue.

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

    url = 'redis://hostname:port/0'
    server = socketio.Server(client_manager=socketio.RedisManager(url))

:param url: The connection URL for the Redis server. For a default Redis
            store running on the same host, use ``redis://``.  To use a
            TLS connection, use ``rediss://``. To use Redis Sentinel, use
            ``redis+sentinel://`` with a comma-separated list of hosts
            and the service name after the db in the URL path. Example:
            ``redis+sentinel://user:pw@host1:1234,host2:2345/0/myredis``.
: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 redis_options: additional keyword arguments to be passed to
                      ``Redis.from_url()`` or ``Sentinel()``.
redisc                 z   > [         TU ]  X#UUS9  Xl        U=(       d    0 U l        SU l        S U l        S U l        g )N)channel
write_onlyloggerjsonF)super__init__	redis_urlredis_options	connectedr0   pubsub)selfr!   r2   r3   r4   r5   r9   	__class__s          r*   r7   RedisManager.__init__Z   sC    " 	 	$*0b
r,   c                   > [         TU ]  5         SnU R                  R                  S:X  a  SSKJn  U" S5      nO(SU R                  R                  ;   a  SSKJn  U" S5      nU(       d"  [        SU R                  R                  -   5      eg )	NTeventletr   )is_monkey_patchedsocketgevent)is_module_patchedz<Redis requires a monkey patched socket library to work with )	r6   
initializeserver
async_modeeventlet.patcherrA   gevent.monkeyrD   RuntimeError)r<   monkey_patchedrA   rD   r=   s       r*   rE   RedisManager.initialized   s}    ;;!!Z/:.x8N///7.x8N++0012 2 r,   c                    [        U R                  5      nUR                  R                  SS5      S   R	                  5       nUS;   a%  [
        b  [        c  [        S5      e[
        [        4$ US;   a%  [        b  [        c  [        S5      e[        [        4$ US:X  a?  [
        b  [        c%  [        b  [        c  [        S	5      e[        [        4$ [
        [        4$ S
U 3n[        U5      e)N+r   r   )r0   redisszLRedis package is not installed (Run "pip install redis" in your virtualenv).)valkeyvalkeyszNValkey package is not installed (Run "pip install valkey" in your virtualenv).unixzdRedis package is not installed (Run "pip install redis" or "pip install valkey" in your virtualenv).zUnsupported Redis URL scheme: )r   r8   r   r   lowerr0   r   rJ   rP   r   r   )r<   r"   r   	error_msgs       r*   _get_redis_module_and_error(RedisManager._get_redis_module_and_errors   s    dnn-
""((a0399;((}
 2" $: ; ; *$$**~!4" $: ; ; ;&&V}
 2>[%8& (> ? ?
 ";..j((4VH=	##r,   c                     U R                  5       u  p[        U R                  5      nUR                  S;   a|  [	        U R                  5      u  pEnU R
                  nUR                  U5        UR                  R                  " U40 UD6nUR                  U=(       d    U R                  5      U l        O6UR                  R                  " U R                  40 U R
                  D6U l        U R                  R                  SS9U l        SU l        g )Nr
   T)ignore_subscribe_messages)rU   r   r8   r   r+   r9   updatesentinelSentinel
master_forr2   r0   Redisfrom_urlr;   r:   )	r<   module_r"   r#   r(   connection_kwargsr'   rZ   s	            r*   _redis_connectRedisManager._redis_connect   s    446	dnn-
 EE(8 7I%6''FMM+,//	DVDH!,,\-IT\\JDJ..t~~ E151C1CEDJjj''$'Gr,   c           	         U R                  5       u  p#[        SSS5       Hd  n U R                  (       d  U R                  5         U R                  R                  U R                  U R                  R                  U5      5      s  $    g ! U a\  nUS:  a,  [        R                  SS[        U5      0S9  SU l         S nAM  [        R                  SS[        U5      0S9   S nA  g S nAff = f)	Nr   r   r   z#Cannot publish to redis... retryingredis_exceptionextraFz$Cannot publish to redis... giving up)rU   ranger:   rb   r0   publishr2   r5   dumpsr4   errorstr)r<   datar`   rk   retries_leftexcs         r*   _publishRedisManager._publish   s    335!!R,L~~'')zz))$,,		8MNN	 -
  !#LL=0#c(; !  &+DNNLL>0#c(; !  s   AB		C+,C&C&&C+c              #     #    U R                  5       u  pSnSn  U(       d7  U R                  5         U R                  R                  U R                  5        SnU R                  R                  5        S h  vN   Mc   N! U aR  n[        R                  SU S3S[        U5      0S9  Sn[        R                  " U5        US-  nUS:  a  Sn S nANWS nAff = f7f)	Nr   Fz)Cannot receive from redis... retrying in z secsre   rf   r   <   )rU   rb   r;   	subscriber2   listenr4   rk   rl   timesleep)r<   r`   rk   retry_sleep
subscribedro   s         r*   _redis_listen_with_retries'RedisManager._redis_listen_with_retries   s     335
%!'')KK))$,,7"#K;;--///  0 % ,,7=?$5s3x#@  B #


;'q #"$K%sB   CAA? 6A=7A? ;C=A? ?CACCCCc              #     #    U R                   R                  S5      nU R                  5        H(  nUS   U:X  d  M  US   S:X  d  M  SU;   d  M!  US   v   M*     U R                  R	                  U R                   5        g 7f)Nzutf-8r2   typemessagerm   )r2   encoderz   r;   unsubscribe)r<   r2   r~   s      r*   _listenRedisManager._listen   sn     ,,%%g.668Gy!W,FOy0Vw5Ffo% 9 	-s   6B B B 1B )r:   r;   r0   r9   r8   )zredis://localhost:6379/0r   FNNN)__name__
__module____qualname____firstlineno____doc__namer7   rE   rU   rb   rp   rz   r   __static_attributes____classcell__)r=   s   @r*   r.   r.   2   s>    $J D?IIM2$: *%*. .r,   r.   )loggingrv   urllib.parser   r0   redis.exceptionsr   ImportErrorrP   valkey.exceptionsr   pubsub_managerr   	getLoggerr4   r+   r.    r,   r*   <module>r      s      !+
-
 *			:	&+4^.= ^.U  EJ  FKs"   
A 
A 
AA
A)(A)