U
    •uÄc®.  ã                   @   sr  d dl Z d dl mZ d dl mZ d dlmZ d dlmZ d dlZe ¡  d dl	m
Z
mZ d dlmZmZ d dlmZ d d	lmZ d
Zdd„ ZG dd„ de jƒZe
G dd„ deejƒƒZe
G dd„ deejƒƒZG dd„ deƒZe
G dd„ deejƒƒZe
G dd„ deejƒƒZe
G dd„ dejeƒƒZe
G dd„ dejeƒƒZe
G dd„ dejeƒƒZe
G dd „ d ej eƒƒZ dS )!é    N)Úiscoroutine)ÚFuture)Údeque)ÚOptional)ÚpublicÚhltype)Úcreate_transport_detailsÚtransport_channel_id)Ú	websocket)Úprotocol)ÚWebSocketServerProtocolÚWebSocketClientProtocolÚWebSocketServerFactoryÚWebSocketClientFactoryÚWampWebSocketServerProtocolÚWampWebSocketClientProtocolÚWampWebSocketServerFactoryÚWampWebSocketClientFactoryc                 C   s   t | tƒpt| ƒS )z�
    Returns ``True`` iff the value yields.

    .. seealso:: http://stackoverflow.com/questions/20730248/maybedeferred-analog-with-asyncio
    )Ú
isinstancer   r   )Úvalue© r   ú^/var/www/html/CISBACKEND/school/venv/lib/python3.8/site-packages/autobahn/asyncio/websocket.pyÚyields5   s    r   c                   @   sÌ   e Zd ZU dZe ¡ ZdZee	 e
d< dZee e
d< dd„ Zdd„ Zd	d
„ Zdd„ Zd*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d$d%„ Zd&d'„ Zd(d)„ ZdS )+ÚWebSocketAdapterProtocolzP
    Adapter class for asyncio-based WebSocket client and server protocols.
    NÚpeerÚ	is_serverc                 C   sT   | j jdt| jƒ|d� || _t| j| jƒ| _| jj| _t	ƒ | _
|  ¡  |  ¡  d S )Nz{func}(transport={transport}))ÚfuncÚ	transport)ÚlogÚdebugr   Úconnection_mader   r   r   Ú_transport_detailsr   r   Úreceive_queueÚ_consumeZ_connectionMade)Úselfr   r   r   r   r    G   s    ÿ
z(WebSocketAdapterProtocol.connection_madec                 C   s&   |   |¡ |d k	r| j ¡  d | _d S ©N)Z_connectionLostr   Úclose)r$   Úexcr   r   r   Úconnection_lostZ   s    

z(WebSocketAdapterProtocol.connection_lostc                    s4   t ˆ jjptjjd�ˆ _‡ fdd„}ˆ j |¡ d S )N)Úloopc                    s.   ˆ j r"ˆ j  ¡ }ˆ jr ˆ  |¡ q ˆ  ¡  d S r%   )r"   Úpopleftr   Z_dataReceivedr#   )Ú_Údata©r$   r   r   Úprocesse   s
    
z2WebSocketAdapterProtocol._consume.<locals>.process)r   Úfactoryr)   ÚtxaioÚconfigÚwaiterÚadd_done_callback)r$   r.   r   r-   r   r#   b   s    z!WebSocketAdapterProtocol._consumec                 C   s&   | j  |¡ | j ¡ s"| j d ¡ d S r%   )r"   Úappendr2   ÚdoneÚ
set_result)r$   r,   r   r   r   Údata_receivedn   s    
z&WebSocketAdapterProtocol.data_receivedFc                 C   s*   |rt | jdƒr| j ¡  n
| j ¡  d S )NÚabort)Úhasattrr   r8   r&   )r$   r8   r   r   r   Ú_closeConnections   s    z)WebSocketAdapterProtocol._closeConnectionc                 C   sD   | j jr&dt| j| j jdƒi}|| j _|  ¡ }t|ƒr@t 	|¡ d S )Nz
tls-unique)
r!   Z	is_securer	   r   r   Ú
channel_idZonOpenr   ÚasyncioÚensure_future)r$   r;   Úresr   r   r   Ú_onOpeny   s     ÿz WebSocketAdapterProtocol._onOpenc                 C   s    |   |¡}t|ƒrt |¡ d S r%   )ZonMessageBeginr   r<   r=   )r$   ÚisBinaryr>   r   r   r   Ú_onMessageBegin†   s    
z(WebSocketAdapterProtocol._onMessageBeginc                 C   s    |   |¡}t|ƒrt |¡ d S r%   )ZonMessageFrameBeginr   r<   r=   )r$   Úlengthr>   r   r   r   Ú_onMessageFrameBegin‹   s    
z-WebSocketAdapterProtocol._onMessageFrameBeginc                 C   s    |   |¡}t|ƒrt |¡ d S r%   )ZonMessageFrameDatar   r<   r=   ©r$   Úpayloadr>   r   r   r   Ú_onMessageFrameData�   s    
z,WebSocketAdapterProtocol._onMessageFrameDatac                 C   s   |   ¡ }t|ƒrt |¡ d S r%   )ZonMessageFrameEndr   r<   r=   ©r$   r>   r   r   r   Ú_onMessageFrameEnd•   s    z+WebSocketAdapterProtocol._onMessageFrameEndc                 C   s    |   |¡}t|ƒrt |¡ d S r%   )ZonMessageFramer   r<   r=   rD   r   r   r   Ú_onMessageFrameš   s    
z(WebSocketAdapterProtocol._onMessageFramec                 C   s   |   ¡ }t|ƒrt |¡ d S r%   )ZonMessageEndr   r<   r=   rG   r   r   r   Ú_onMessageEndŸ   s    z&WebSocketAdapterProtocol._onMessageEndc                 C   s"   |   ||¡}t|ƒrt |¡ d S r%   )Z	onMessager   r<   r=   )r$   rE   r@   r>   r   r   r   Ú
_onMessage¤   s    z#WebSocketAdapterProtocol._onMessagec                 C   s    |   |¡}t|ƒrt |¡ d S r%   )ZonPingr   r<   r=   rD   r   r   r   Ú_onPing©   s    
z WebSocketAdapterProtocol._onPingc                 C   s    |   |¡}t|ƒrt |¡ d S r%   )ZonPongr   r<   r=   rD   r   r   r   Ú_onPong®   s    
z WebSocketAdapterProtocol._onPongc                 C   s$   |   |||¡}t|ƒr t |¡ d S r%   )ZonCloser   r<   r=   )r$   ZwasCleanÚcodeÚreasonr>   r   r   r   Ú_onClose³   s    z!WebSocketAdapterProtocol._onClosec                 C   s   t dƒ‚d S )Nznot implemented©Ú	Exception)r$   ZproducerZ	streamingr   r   r   ÚregisterProducer¸   s    z)WebSocketAdapterProtocol.registerProducerc                 C   s   d S r%   r   r-   r   r   r   ÚunregisterProducer»   s    z+WebSocketAdapterProtocol.unregisterProducer)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   Úmake_loggerr   r   r   ÚstrÚ__annotations__r   Úboolr    r(   r#   r7   r:   r?   rA   rC   rF   rH   rI   rJ   rK   rL   rM   rP   rS   rT   r   r   r   r   r   >   s,   

r   c                   @   s   e Zd ZdZe ¡ ZdS )r   z“
    Base class for asyncio-based WebSocket server protocols.

    Implements:

    * :class:`autobahn.websocket.interfaces.IWebSocketChannel`
    N©rU   rV   rW   rX   r0   rY   r   r   r   r   r   r   Ä   s   r   c                   @   s(   e Zd ZdZe ¡ Zdd„ Zdd„ ZdS )r   z“
    Base class for asyncio-based WebSocket client protocols.

    Implements:

    * :class:`autobahn.websocket.interfaces.IWebSocketChannel`
    c                 C   s8   |   |¡}| jjdt| jƒ|d� t|ƒr4t |¡ d S )Nz{func}: {res})r   r>   )Z	onConnectr   r   r   Ú
_onConnectr   r<   r=   )r$   Úresponser>   r   r   r   r^   Ý   s    
z"WebSocketClientProtocol._onConnectc                 C   s   t dƒ‚d S )Nz)WSS over explicit proxies not implementedrQ   r-   r   r   r   ÚstartTLSã   s    z WebSocketClientProtocol.startTLSN)	rU   rV   rW   rX   r0   rY   r   r^   r`   r   r   r   r   r   Ñ   s   r   c                   @   s    e Zd ZdZe ¡ Zdd„ ZdS )ÚWebSocketAdapterFactoryzP
    Adapter class for asyncio-based WebSocket client and server factories.
    c                 C   s   |   ¡ }| |_|S r%   )r   r/   )r$   Úprotor   r   r   Ú__call__í   s    z WebSocketAdapterFactory.__call__N)rU   rV   rW   rX   r0   rY   r   rc   r   r   r   r   ra   ç   s   ra   c                   @   s$   e Zd ZdZe ¡ ZeZdd„ Z	dS )r   z 
    Base class for asyncio-based WebSocket server factories.

    Implements:

    * :class:`autobahn.websocket.interfaces.IWebSocketServerChannelFactory`
    c                 O   s2   |  dd¡}|pt ¡ | _tjj| f|ž|Ž dS )a  
        .. note::
            In addition to all arguments to the constructor of
            :meth:`autobahn.websocket.interfaces.IWebSocketServerChannelFactory`,
            you can supply a ``loop`` keyword argument to specify the
            asyncio event loop to be used.
        r)   N)Úpopr<   Úget_event_loopr)   r   r   Ú__init__©r$   ÚargsÚkwargsr)   r   r   r   rf     s    zWebSocketServerFactory.__init__N)
rU   rV   rW   rX   r0   rY   r   r   r   rf   r   r   r   r   r   ó   s   r   c                   @   s    e Zd ZdZe ¡ Zdd„ ZdS )r   z 
    Base class for asyncio-based WebSocket client factories.

    Implements:

    * :class:`autobahn.websocket.interfaces.IWebSocketClientChannelFactory`
    c                 O   s2   |  dd¡}|pt ¡ | _tjj| f|ž|Ž dS )a  

        .. note::
            In addition to all arguments to the constructor of
            :meth:`autobahn.websocket.interfaces.IWebSocketClientChannelFactory`,
            you can supply a ``loop`` keyword argument to specify the
            asyncio event loop to be used.
        r)   N)rd   r<   re   r)   r   r   rf   rg   r   r   r   rf     s    	zWebSocketClientFactory.__init__N)rU   rV   rW   rX   r0   rY   r   rf   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZe ¡ ZdS )r   z�
    asyncio-based WAMP-over-WebSocket server protocol.

    Implements:

    * :class:`autobahn.wamp.interfaces.ITransport`
    Nr]   r   r   r   r   r   *  s   r   c                   @   s$   e Zd ZdZe ¡ ZeZdd„ Z	dS )r   z;
    asyncio-based WAMP-over-WebSocket server factory.
    c                 O   s<   |  dd¡}tj | ||¡ | j|d< tj| f|ž|Ž dS )až  

        :param factory: A callable that produces instances that implement
            :class:`autobahn.wamp.interfaces.ITransportHandler`
        :type factory: callable

        :param serializers: A list of WAMP serializers to use (or ``None``
            for all available serializers).
        :type serializers: list of objects implementing
            :class:`autobahn.wamp.interfaces.ISerializer`
        ÚserializersNÚ	protocols)rd   r
   r   rf   Ú
_protocolsr   ©r$   r/   rh   ri   rj   r   r   r   rf   A  s    
z#WampWebSocketServerFactory.__init__N)
rU   rV   rW   rX   r0   rY   r   r   r   rf   r   r   r   r   r   7  s   r   c                   @   s   e Zd ZdZe ¡ ZdS )r   z‚
    asyncio-based WAMP-over-WebSocket client protocols.

    Implements:

    * :class:`autobahn.wamp.interfaces.ITransport`
    Nr]   r   r   r   r   r   X  s   r   c                   @   s$   e Zd ZdZe ¡ ZeZdd„ Z	dS )r   z;
    asyncio-based WAMP-over-WebSocket client factory.
    c                 O   s<   |  dd¡}tj | ||¡ | j|d< tj| f|ž|Ž dS )aÜ  

        :param factory: A callable that produces instances that implement
            :class:`autobahn.wamp.interfaces.ITransportHandler`
        :type factory: callable

        :param serializer: The WAMP serializer to use (or ``None`` for
           "best" serializer, chosen as the first serializer available from
           this list: CBOR, MessagePack, UBJSON, JSON).
        :type serializer: object implementing :class:`autobahn.wamp.interfaces.ISerializer`
        rj   Nrk   )rd   r
   r   rf   rl   r   rm   r   r   r   rf   o  s    
z#WampWebSocketClientFactory.__init__N)
rU   rV   rW   rX   r0   rY   r   r   r   rf   r   r   r   r   r   e  s   r   )!r<   r   r   Úcollectionsr   Útypingr   r0   Zuse_asyncioZautobahn.utilr   r   Zautobahn.asyncio.utilr   r	   Zautobahn.wampr
   Zautobahn.websocketr   Ú__all__r   ÚProtocolr   r   r   Úobjectra   r   r   r   r   r   r   r   r   r   r   Ú<module>   s>   	  