U
    /e                     @  sd   d Z ddlmZ ddlZeeZddlmZm	Z	m
Z
 ddlmZ ddlmZ d	ZG d
d dZdS )zR Encapsulate handling of all Bokeh Protocol messages a Bokeh server may
receive.

    )annotationsN)AnyCallableDict   )ProtocolError   )ServerSession)ProtocolHandlerc                   @  s8   e Zd ZU dZded< ddddZdd	 Zd
d ZdS )r
   a   A Bokeh server may be expected to receive any of the following protocol
    messages:

    * ``PATCH-DOC``
    * ``PULL-DOC-REQ``
    * ``PUSH-DOC``
    * ``SERVER-INFO-REQ``

    The job of ``ProtocolHandler`` is to direct incoming messages to the right
    specialized handler for each message type. When the server receives a new
    message on a connection it will call ``handler`` with the message and the
    connection that the message arrived on. Most messages are ultimately
    handled by the ``ServerSession`` class, but some simpler messages types
    such as ``SERVER-INFO-REQ`` may be handled directly by ``ProtocolHandler``.

    Any unexpected messages will result in a ``ProtocolError``.

    zDict[str, Callable[..., Any]]	_handlersNone)returnc                 C  s:   i | _ tj| j d< tj| j d< tj| j d< | j| j d< d S )NzPULL-DOC-REQzPUSH-DOCz	PATCH-DOCzSERVER-INFO-REQ)r   r	   pullpushpatch_server_info_req)self r   A/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/server/protocol_handler.py__init__A   s
    zProtocolHandler.__init__c              
     s   | j |j}|dkr$| j |j}|dkr8td| z|||I dH }W nD tk
r } z&tjd||dd ||t|}W 5 d}~X Y nX |S )aI   Delegate a received message to the appropriate handler.

        Args:
            message (Message) :
                The message that was receive that needs to be handled

            connection (ServerConnection) :
                The connection that received this message

        Raises:
            ProtocolError

        Nz%s not expected on serverz/error handling message
 message: %r 
 error: %rT)exc_info)r   getZmsgtyper   	Exceptionlogerrorrepr)r   message
connectionhandlerZworker   r   r   handleI   s      "zProtocolHandler.handlec                   s   |j d|jd S )NzSERVER-INFO-REPLYmsgid)protocolcreateheader)r   r   r   r   r   r   r   h   s    z ProtocolHandler._server_info_reqN)__name__
__module____qualname____doc____annotations__r   r    r   r   r   r   r   r
   +   s
   
r
   )r(   
__future__r   logging	getLoggerr%   r   typingr   r   r   Zprotocol.exceptionsr   sessionr	   __all__r
   r   r   r   r   <module>   s   
