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

    )absolute_importdivisionprint_functionunicode_literalsN)gen   )ServerSession   )ProtocolError)ProtocolHandlerc               @   s4   e Zd ZdZdd Zejdd Zejdd ZdS )	r   a   A Bokeh server may be expected to receive any of the following protocol
    messages:

    * ``EVENT``
    * ``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``.

    c             C   sH   t  | _tj| jd< tj| jd< tj| jd< | j| jd< tj| jd< d S )NzPULL-DOC-REQzPUSH-DOCz	PATCH-DOCzSERVER-INFO-REQZEVENT)dict	_handlersr   ZpullpushZpatch_server_info_reqZevent)self r   <lib/python3.7/site-packages/bokeh/server/protocol_handler.py__init__B   s    zProtocolHandler.__init__c          
   c   s   | j |j|jf}|dkr*| j |j}|dkr>td| y|||V }W nV tk
r } z8td|| tjd|j	|j
dd ||t|}W dd}~X Y nX t|d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 %r: %rz  message header %r content %rr   )exc_info)r   getZmsgtypeZrevisionr
   	ExceptionlogerrordebugheaderZcontentreprr   Return)r   message
connectionZhandlerZworker   r   r   handleK   s    "zProtocolHandler.handlec             C   s   t |jd|jd d S )NzSERVER-INFO-REPLYZmsgid)r   r   ZprotocolZcreater   )r   r   r   r   r   r   r   k   s    z ProtocolHandler._server_info_reqN)	__name__
__module____qualname____doc__r   r   	coroutiner    r   r   r   r   r   r   -   s   	 r   )r$   Z
__future__r   r   r   r   ZloggingZ	getLoggerr!   r   Ztornador   Zsessionr   Zprotocol.exceptionsr
   __all__objectr   r   r   r   r   <module>
   s   
	