
    sj                         d Z ddlZddlZddlmZ ddlmZ ddlmZ  ej                  e
      Zd Z G d d      Z G d	 d
      Z G d d      Zy)z
This module implements the core RPC and publish APIs. Developers can extend
LinkProtocol to provide additional RPC callbacks for their web-applications. Then extend
ServerProtocol to hook all the needed LinkProtocols together.
    N)register)schedule_callback)EventEmitterc                       y N )___s     =/DATA/.local/lib/python3.12/site-packages/wslink/websocket.pynoopr      s    C    c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)LinkProtocolzu
    Subclass this to communicate with wslink clients. LinkProtocol
    objects provide rpc and pub/sub actions.
    c                 >    t         | _        t         | _        d | _        y r   )r   publishaddAttachment
coreServerselfs    r   __init__zLinkProtocol.__init__!   s    !r   c                 .    || _         || _        || _        y r   r   r   
stopServerr   r   r   r   s       r   initzLinkProtocol.init'       *$r   c                 R    | j                   r| j                   j                  |      S y r   )r   getSharedObjectr   keys     r   r   zLinkProtocol.getSharedObject,   s!    ????22377r   c                      yzCalled when a new websocket connection is established.

        request is the HTTP request header, and client_id an opaque string that
        identifies the connection.  The default implementation is a noop. A
        subclass may redefine it.

        Nr   r   request	client_ids      r   	onConnectzLinkProtocol.onConnect1       r   c                      yz-Called when a websocket connection is closed.Nr   r   r%   s     r   onClosezLinkProtocol.onClose:   r'   r   N)	__name__
__module____qualname____doc__r   r   r   r&   r+   r   r   r   r   r      s     
%

<r   r   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)NetworkMonitorz
    Provide context manager for increase/decrease pending request
    either synchronously or asynchronously.

    The Asynchronous version also await completion.
    c                 D    d| _         t        j                         | _        y )Nr   )pendingasyncioEventeventr   s    r   r   zNetworkMonitor.__init__M   s    ]]_
r   c                 8    | j                   j                          y)zTrigger completion eventN)r6   setr   s    r   network_call_completedz%NetworkMonitor.network_call_completedR   s    

r   c                 .    | xj                   dz  c_         y)zIncrease pending request   N)r3   r   r	   r
   s      r   on_enterzNetworkMonitor.on_enterV   s    r   c                     | xj                   dz  c_         | j                   dk(  r6| j                  j                         s| j                  j                          yyy)zSDecrease pending request and trigger completion event if we reach 0 pending requestr;   r   N)r3   r6   is_setr8   r<   s      r   on_exitzNetworkMonitor.on_exitZ   sA    <<1TZZ%6%6%8JJNN &9r   c                 &    | j                          | S r   r=   r   s    r   	__enter__zNetworkMonitor.__enter__a   s    r   c                 $    | j                          y r   )r@   )r   exc_type	exc_valueexc_tracebacks       r   __exit__zNetworkMonitor.__exit__e   s    r   c                 .   K   | j                          | S wr   rB   r   s    r   
__aenter__zNetworkMonitor.__aenter__i   s     s   c                 `   K   | j                          | j                          d {    y 7 wr   )r@   
completion)r   exc_texc_vexc_tbs       r   	__aexit__zNetworkMonitor.__aexit__m   s     oos   $.,.c                    K   | j                   rJ| j                  j                          | j                  j                          d{    | j                   rIyy7 w)z/Await completion of any pending network requestN)r3   r6   clearwaitr   s    r   rL   zNetworkMonitor.completionq   s=     llJJ**//### ll#s   AAAAAN)r,   r-   r.   r/   r   r9   r=   r@   rC   rH   rJ   rP   rL   r   r   r   r1   r1   E   s4    %
 $r   r1   c                       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 ed      d        Z ed      dd       Zy)ServerProtocolz
    Defines the core server protocol for wslink. Gathers a list of LinkProtocol
    objects that provide rpc and publish functionality.
    c                     t               | _        t        g d      | _        g | _        d | _        | j                          y )N)	exceptionerrorcriticalinfodebug)allowed_events)r1   network_monitorr   log_emitterlinkProtocolssecret
initializer   s    r   r   zServerProtocol.__init__~   s8    -/'N
  r   c                 .    || _         || _        || _        y r   r   r   s       r   r   zServerProtocol.init   r   r   c                      y)zh
        Let sub classes define what they need to do to properly initialize
        themselves.
        Nr   r   s    r   ra   zServerProtocol.initialize   r'   r   c                     t        | d      si | _        ||| j                  v r| j                  |= y || j                  |<   y )NsharedObjects)hasattrre   )r   r    shareds      r   setSharedObjectzServerProtocol.setSharedObject   sD    t_-!#D>cT%7%77""3'&,Ds#r   c                 >    || j                   v r| j                   |   S y r   )re   r   s     r   r   zServerProtocol.getSharedObject   s$    $$$$%%c**r   c                 l    t        |t              sJ | |_        | j                  j	                  |       y r   )
isinstancer   r   r_   append)r   protocols     r   registerLinkProtocolz#ServerProtocol.registerLinkProtocol   s.    (L111"!!(+r   c                     t        |t              sJ d |_        	 | j                  j	                  |       y # t
        $ r, d}t        j                  |       | j                  d|       Y y w xY w)Nz+Link protocol missing from registered list.rX   )	rk   r   r   r_   remove
ValueErrorloggerrX   r^   )r   rm   error_messages      r   unregisterLinkProtocolz%ServerProtocol.unregisterLinkProtocol   sc    (L111"	5%%h/ 	5IMLL'Wm4	5s   7 2A,+A,c                     | j                   S r   )r_   r   s    r   getLinkProtocolszServerProtocol.getLinkProtocols   s    !!!r   c                     || _         y r   )r`   )r   	newSecrets     r   updateSecretzServerProtocol.updateSecret   s	    r   c                      yr"   r   r#   s      r   r&   zServerProtocol.onConnect   r'   r   c                      yr)   r   r*   s     r   r+   zServerProtocol.onClose   r'   r   zapplication.exitc                 $    | j                          y)zRPC callback to exitN)r   r   s    r   exitzServerProtocol.exit   s     	r   zapplication.exit.laterc                 \    t         j                  d|       t        || j                         y)z(RPC callback to exit after a short delayz%schedule exit for %s seconds from nowN)rr   rZ   r   r   )r   secondsLaters     r   	exitLaterzServerProtocol.exitLater   s!     	;\J,8r   N)<   )r,   r-   r.   r/   r   r   ra   rh   r   rn   rt   rv   ry   r&   r+   	exportRpcr}   r   r   r   r   rU   rU   x   sr    
%
-
,5" < !" # '(9 )9r   rU   )r/   r4   loggingwslinkr   r   r   wslink.emitterr   	getLoggerr,   rr   r   r   r1   rU   r   r   r   <module>r      sT      ( $ '			8	$  <  <T0$ 0$fT9 T9r   