o
    >hk2                     @   s  d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZmZ d dlmZmZmZ d	ZG d
d deZee dedeee ee f fddZG dd deZG dd deZG dd deZ G dd de Z!G dd de Z"dS )    N)OptionalDictTuple)hlval)protocol)ConnectionDenyConnectionRequestConnectionResponse)TransportDetails)
ITransportISession)ProtocolErrorSerializationErrorTransportLost)WampWebSocketServerProtocolWampWebSocketClientProtocolWampWebSocketServerFactoryWampWebSocketClientFactoryc                   @   s   e Zd ZU dZdZee ed< dZee	 ed< dde
dee fddZd	d
 Zdede
dee fddZdedefddZdd Zdd Zedee	 fddZdd Zdd ZdS )WampWebSocketProtocolz>
    Base class for WAMP-over-WebSocket transport mixins.
    N_session_transport_detailscodereasonc                 C   s"   | j jd||d | || d S )NzEFailing WAMP-over-WebSocket transport: code={code}, reason="{reason}")r   r   )logdebug_fail_connection)selfr   r    r   w/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/wamp/websocket.py_bailout5   s   
zWampWebSocketProtocol._bailoutc              
   C   s~   z| j  | _| | j_| j|  W dS  ty> } z| jjdt	 d d
|}| jtjj|d W Y d}~dS d}~ww )z^
        Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onOpen`
        {tb}tbWAMP Internal Error ({0})r   N)factory_factoryr   
_transportonOpen	Exceptionr   critical	traceback
format_excformatr   r   WebSocketProtocol CLOSE_STATUS_CODE_INTERNAL_ERROR)r   er   r   r   r   r(   :   s   
 zWampWebSocketProtocol.onOpenwasCleanc                 C   sj   || _ | jdur3z| jjd|||d | j| W n ty-   | jjdt d Y nw d| _dS dS )z_
        Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onClose`
        NzWWAMP-over-WebSocket transport lost: wasClean={wasClean}, code={code}, reason="{reason}")r1   r   r   r    r!   )	_onclose_reasonr   r   r   onCloser)   r*   r+   r,   )r   r1   r   r   r   r   r   r3   J   s   

zWampWebSocketProtocol.onClosepayloadisBinaryc                 C   s>  zI| j ||D ]>}| jjdtdddd| jjr!t| jjdddnd| jjr/t| jjdddndtddddtd	ddd|d
 | j| qW dS  t	yt } z| jj
dt d d|}| jtjj|d W Y d}~dS d}~w ty } z| jj
dt d d|}| jtjj|d W Y d}~dS d}~ww )za
        Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessage`
        <
{action1}{session}, {authid}{action2}
  {message}
{action3}zWAMP-Receive(greenTcolorboldF-z) <<z<<action1authidsessionaction2action3messager    r!   zWAMP Protocol Error ({0})r$   Nr#   )_serializerunserializer   tracer   r   _authid_session_id	onMessager   r*   r+   r,   r-   r   r   r.    CLOSE_STATUS_CODE_PROTOCOL_ERRORr)   r/   )r   r4   r5   msgr0   r   r   r   r   rH   ]   s.   

 
 zWampWebSocketProtocol.onMessagec                 C   s   |   riz@| jjdtdddd| jjrt| jjdddnd| jjr*t| jjdddndtddddtd	ddd|d
 | j|\}}W n t	y` } z| j
d| td|d}~ww | || dS t )zM
        Implements :func:`autobahn.wamp.interfaces.ITransport.send`
        r6   zWAMP-Transmit(redTr8   Fr;   z) >>z>>r<   z$WAMP message serialization error: {}z%WAMP message serialization error: {0}N)isOpenr   rE   r   r   rF   rG   rC   	serializer)   errorr-   r   sendMessager   )r   rJ   r4   r5   r0   r   r   r   sendv   s$   zWampWebSocketProtocol.sendc                 C   s
   | j duS )zO
        Implements :func:`autobahn.wamp.interfaces.ITransport.isOpen`
        N)r   r   r   r   r   rL      s   
zWampWebSocketProtocol.isOpenreturnc                 C   s   | j S )zZ
        Implements :func:`autobahn.wamp.interfaces.ITransport.transport_details`
        )r   rQ   r   r   r   transport_details   s   z'WampWebSocketProtocol.transport_detailsc                 C       |   r| tjj dS t )zN
        Implements :func:`autobahn.wamp.interfaces.ITransport.close`
        N)rL   	sendCloser   r.   CLOSE_STATUS_CODE_NORMALr   rQ   r   r   r   close      zWampWebSocketProtocol.closec                 C   rT   )zN
        Implements :func:`autobahn.wamp.interfaces.ITransport.abort`
        N)rL   r   r   r.   CLOSE_STATUS_CODE_GOING_AWAYr   rQ   r   r   r   abort   rX   zWampWebSocketProtocol.abortN)__name__
__module____qualname____doc__r   r   r   __annotations__r   r
   intstrr   r(   boolr3   bytesrH   rP   rL   propertyrS   rW   rZ   r   r   r   r   r   -   s   
 	r   subprotocolrR   c                 C   s^   z(|  d}|d dkrtd|d t|d }d|dd  }||fW S    Y dS )N.r   wampzFWAMP WebSocket subprotocol identifier must start with "wamp", not "{}"      )NN)splitr)   r-   ra   join)rf   sversionserializer_idr   r   r   parseSubprotocolIdentifier   s   

rp   c                   @   s:   e Zd ZdZdZdedeee e	eef f fddZ
dS )r   z:
    Mixin for WAMP-over-WebSocket server transports.
    TrequestrR   c                 C   s   i }|j D ]$}t|\}}|dkr)|| jj v r)t| jj| | _||f  S q| jr;ttj	d
d| jj t| jjd | _d|fS )a
        Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onConnect`
        rj   z1This server only speaks WebSocket subprotocols {}, jsonN)	protocolsrp   r%   _serializerskeyscopyrC   STRICT_PROTOCOL_NEGOTIATIONr   BAD_REQUESTr-   rl   )r   rq   headersrf   rn   serializerIdr   r   r   	onConnect   s   

z%WampWebSocketServerProtocol.onConnectN)r\   r]   r^   r_   ry   r   r   r   rb   r   r}   r   r   r   r   r      s    *r   c                   @   s"   e Zd ZdZdZdefddZdS )r   z:
    Mixin for WAMP-over-WebSocket client transports.
    Tresponsec                 C   sX   |j | jjvr| jrtdd| jjd}nt|j \}}t| jj	| | _
dS )rr   zLThe server does not speak any of the WebSocket subprotocols {} we requested.rs   rt   N)r   r%   ru   ry   r)   r-   rl   rp   rx   rv   rC   )r   r~   ro   rn   r   r   r   r}      s   z%WampWebSocketClientProtocol.onConnectN)r\   r]   r^   r_   ry   r	   r}   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdddZdS )WampWebSocketFactoryzF
    Base class for WAMP-over-WebSocket transport factory mixins.
    Nc                    sf  t  r | _n fdd| _|du rg }zddlm} ||dd ||  W n	 ty4   Y nw zddlm} ||dd ||  W n	 tyT   Y nw zdd	lm} ||dd ||  W n	 tyt   Y nw zdd
lm} ||dd ||  W n	 ty   Y nw |st	di | _
|D ]}|| j
|j< qdd |D | _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 default
           serializers). Serializers must implement
           :class:`autobahn.wamp.interfaces.ISerializer`.
        :type serializers: list
        c                      s    S r[   r   r   r%   r   r   <lambda>  s    z/WampWebSocketFactory.__init__.<locals>.<lambda>Nr   )CBORSerializerT)batched)MsgPackSerializer)UBJSONSerializer)JsonSerializerz$Could not import any WAMP serializerc                 S   s   g | ]}d  |jqS )z	wamp.2.{})r-   SERIALIZER_ID).0serr   r   r   
<listcomp>2  s    z1WampWebSocketFactory.__init__.<locals>.<listcomp>)callabler&   autobahn.wamp.serializerr   appendImportErrorr   r   r   r)   rv   r   
_protocols)r   r%   serializersr   r   r   r   r   r   r   r   __init__   sN   zWampWebSocketFactory.__init__r[   )r\   r]   r^   r_   r   r   r   r   r   r      s    r   c                   @      e Zd ZdZdS )r   zC
    Mixin for WAMP-over-WebSocket server transport factories.
    Nr\   r]   r^   r_   r   r   r   r   r   5      r   c                   @   r   )r   zC
    Mixin for WAMP-over-WebSocket client transport factories.
    Nr   r   r   r   r   r   ;  r   r   )#rx   r+   typingr   r   r   autobahn.utilr   autobahn.websocketr   autobahn.websocket.typesr   r   r	   autobahn.wamp.typesr
   autobahn.wamp.interfacesr   r   autobahn.wamp.exceptionr   r   r   __all__objectr   registerrb   ra   rp   r   r   r   r   r   r   r   r   r   <module>   s&    
"C