
    oi                     :    d dl Z d dlmZ d dlmZ  G d de      Zy)    N)Dict)WebSocketHandlerc                   N    e Zd ZdZi Zi ZdeddfdZd
dZe	dd de
ddfd	       Zy)RequestInfoSocketHandlera]  A websocket handler used to provide the request info
    associated with kernel ids in preheat kernel mode.

    Class variables
    ---------------
    - _waiters : A dictionary which holds the `websocket` connection
    associated with the kernel id.

    - cache : A dictionary which holds the request info associated
    with the kernel id.
    	kernel_idreturnNc                     | t         j                  |<   || j                  v r| j                  | j                  |          yy)zCreate a new websocket connection, this connection is
        identified by the kernel id.

        Args:
            kernel_id (str): Kernel id used by the notebook when it opens
            the websocket connection.
        N)r   _waiters_cachewrite_message)selfr   s     b/home/ubuntu/docker-apps/notebooks/venv/lib/python3.12/site-packages/voila/request_info_handler.pyopenzRequestInfoSocketHandler.open   s=     8< )))4#t{{956 $    c                     t         j                  j                         D ]  \  }}|| k(  s n t         j                  = y )N)r   r
   items)r   k_idwaiters      r   on_closez!RequestInfoSocketHandler.on_close#   s>    4==CCE 	LD&~	 %--d3r   clsmsgc                     |d   }|d   }| j                   j                  |d      }|	 |j                  |       y|| j                  |<   y# t        $ r t	        j
                  dd       Y yw xY w)a*  Class method used to dispatch the request info to the waiting
        notebook. This method is called in `VoilaHandler` when the request
        info becomes available.
        If this method is called before the opening of websocket connection,
        `msg` is stored in `_cache` and the message will be dispatched when
        a notebook with corresponding kernel id is connected.

        Args:
            - msg (Dict): this dictionary contains the `kernel_id` to identify
            the waiting notebook and `payload` is the request info.
        r   payloadNzError sending messageT)exc_info)r
   getr   	Exceptionloggingerrorr   )r   r   r   r   r   s        r   send_updatesz%RequestInfoSocketHandler.send_updates)   sy     $	i.!!)T2F$$W- %,CJJy!  F5EFs   A  A/.A/)r   N)__name__
__module____qualname____doc__r
   r   strr   r   classmethodr   r    r   r   r   r      sU    
 HF
7c 
7d 
74 ,4 ,4 ,D , ,r   r   )r   typingr   tornado.websocketr   r   r&   r   r   <module>r)      s      .8,/ 8,r   