a
    ©ˆcÞ  ã                   @   sH   d Z ddlZddlmZ ddlmZ G dd„ deƒZG dd	„ d	eƒZdS )
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_callbackc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚLinkProtocolzu
    Subclass this to communicate with wslink clients. LinkProtocol
    objects provide rpc and pub/sub actions.
    c                 C   s   dd„ | _ dd„ | _d | _d S )Nc                 S   s   d S ©N© )ÚxÚyr   r   úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/wslink/websocket.pyÚ<lambda>   ó    z'LinkProtocol.__init__.<locals>.<lambda>c                 S   s   d S r   r   )r   r   r   r
   r      r   )ÚpublishÚaddAttachmentÚ
coreServer©Úselfr   r   r
   Ú__init__   s    

zLinkProtocol.__init__c                 C   s   || _ || _|| _d S r   ©r   r   Ú
stopServer©r   r   r   r   r   r   r
   Úinit    s    zLinkProtocol.initc                 C   s   | j r| j  |¡S d S r   )r   ÚgetSharedObject©r   Úkeyr   r   r
   r   %   s    zLinkProtocol.getSharedObjectc                 C   s   dS ©zÿCalled 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_idr   r   r
   Ú	onConnect*   s    	zLinkProtocol.onConnectc                 C   s   dS ©z-Called when a websocket connection is closed.Nr   ©r   r   r   r   r
   ÚonClose5   s    zLinkProtocol.onCloseN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r!   r   r   r   r
   r      s   r   c                   @   sŠ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zedƒdd„ ƒZedƒd dd„ƒZdS )!ÚServerProtocolz�
    Defines the core server protocol for wslink. Gathers a list of LinkProtocol
    objects that provide rpc and publish functionality.
    c                 C   s   g | _ d | _|  ¡  d S r   )ÚlinkProtocolsÚsecretÚ
initializer   r   r   r
   r   H   s    zServerProtocol.__init__c                 C   s   || _ || _|| _d S r   r   r   r   r   r
   r   M   s    zServerProtocol.initc                 C   s   dS )zh
        Let sub classes define what they need to do to properly initialize
        themselves.
        Nr   r   r   r   r
   r)   R   s    zServerProtocol.initializec                 C   s:   t | dƒsi | _|d kr,|| jv r,| j|= n
|| j|< d S )NÚsharedObjects)Úhasattrr*   )r   r   Zsharedr   r   r
   ÚsetSharedObjectY   s
    

zServerProtocol.setSharedObjectc                 C   s   || j v r| j | S d S d S r   )r*   r   r   r   r
   r   a   s    

zServerProtocol.getSharedObjectc                 C   s$   t |tƒsJ ‚| |_| j |¡ d S r   )Ú
isinstancer   r   r'   Úappend)r   Úprotocolr   r   r
   ÚregisterLinkProtocolg   s    z#ServerProtocol.registerLinkProtocolc              
   C   sX   t |tƒsJ ‚d |_z| j |¡ W n. tyR } zt d¡ W Y d }~n
d }~0 0 d S )Nz+Link protocol missing from registered list.)r-   r   r   r'   ÚremoveÚ
ValueErrorÚloggingÚerror)r   r/   Úer   r   r
   ÚunregisterLinkProtocolo   s    z%ServerProtocol.unregisterLinkProtocolc                 C   s   | j S r   )r'   r   r   r   r
   ÚgetLinkProtocolsw   s    zServerProtocol.getLinkProtocolsc                 C   s
   || _ d S r   )r(   )r   Z	newSecretr   r   r
   ÚupdateSecretz   s    zServerProtocol.updateSecretc                 C   s   dS r   r   r   r   r   r
   r   }   s    	zServerProtocol.onConnectc                 C   s   dS r   r   r    r   r   r
   r!   ˆ   s    zServerProtocol.onClosezapplication.exitc                 C   s   |   ¡  dS )zRPC callback to exitN)r   r   r   r   r
   Úexit�   s    zServerProtocol.exitzapplication.exit.lateré<   c                 C   s    t d|› d�ƒ t|| jƒ dS )z(RPC callback to exit after a short delayzschedule exit for z seconds from nowN)Úprintr   r   )r   ZsecondsLaterr   r   r
   Ú	exitLater’   s    zServerProtocol.exitLaterN)r:   )r"   r#   r$   r%   r   r   r)   r,   r   r0   r6   r7   r8   r   r!   Ú	exportRpcr9   r<   r   r   r   r
   r&   B   s    
r&   )	r%   r3   Ú r   r=   r   Úobjectr   r&   r   r   r   r
   Ú<module>   s
   
.