o
    èó·iQ'  ã                   @   sR  zd dl mZ W n ey   edƒ‚w d dlmZ d dlmZ d dlmZ d dl m	Z	m
Z
 zd dlmZ W n eyE   d dlmZ Y nw d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZ d dlmZ dZeeƒG dd„ deƒƒZee
ƒG dd„ deƒƒZG dd„ deeƒZG dd„ deƒZG dd„ deƒZdd„ Z dd„ Z!dS )é    )ÚIHostnameResolverz=Twisted 17.1.0 or later required for autobahn.twisted.testing)ÚDeferred)ÚIPv4Address)ÚHostResolution)ÚISSLTransportÚIReactorPluggableNameResolver)ÚMemoryReactorClock)Úiosim)ÚdirectlyProvidesÚimplementer)ÚIWebSocketClientAgent)Ú_TwistedWebSocketClientAgent)ÚWebSocketServerProtocol)ÚWebSocketServerFactory)Úcreate_pumperÚcreate_memory_agentÚMemoryReactorClockResolverc                   @   s   e Zd Zddd„ZdS )Ú_StaticTestResolverr   c                 C   s<   t |ƒ}| |¡ | tdd|dkrdn|ƒ¡ | ¡  dS )zR
        Implement IHostnameResolver which always returns 127.0.0.1:31337
        ÚTCPú	127.0.0.1r   éiz  N)r   ZresolutionBeganZaddressResolvedr   ZresolutionComplete)ÚselfZreceiverZhostNameZ
portNumberÚ
resolution© r   úgC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\autobahn/twisted/testing/__init__.pyÚresolveHostName@   s   
ÿz#_StaticTestResolver.resolveHostNameN)r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   >   s    r   c                   @   s(   e Zd ZdZdZedd„ ƒZdd„ ZdS )Ú_TestNameResolverz9
    A test version of IReactorPluggableNameResolver
    Nc                 C   s   | j d u r	tƒ | _ | j S ©N)Ú	_resolverr   ©r   r   r   r   ÚnameResolverT   s   
z_TestNameResolver.nameResolverc                 C   s   | j }|| _ |S r    )r!   )r   ÚresolverÚoldr   r   r   ÚinstallNameResolverZ   s   z%_TestNameResolver.installNameResolver)r   r   r   Ú__doc__r!   Úpropertyr#   r&   r   r   r   r   r   L   s    
r   c                   @   s   e Zd ZdZdS )r   zY
    Combine MemoryReactor, Clock and an IReactorPluggableNameResolver
    together.
    N)r   r   r   r'   r   r   r   r   r   `   s    r   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )Ú_TwistedWebMemoryAgentaä  
    A testing agent which will hook up an instance of
    `server_protocol` for every client that is created via the `open`
    API call.

    :param reactor: the reactor to use for tests (usually an instance
        of MemoryReactorClockResolver)

    :param pumper: an implementation IPumper (e.g. as returned by
        `create_pumper`)

    :param server_protocol: the server-side WebSocket protocol class
        to instantiate (e.g. a subclass of `WebSocketServerProtocol`
    c                 C   s2   || _ || _|| _t| j ƒ| _tƒ | _tƒ | _d S r    )	Ú_reactorÚ_server_protocolÚ_pumperr   Ú_agentÚsetÚ_pumpsÚdictÚ_servers)r   ÚreactorÚpumperÚserver_protocolr   r   r   Ú__init__x   s   z_TwistedWebMemoryAgent.__init__Nc                    s  |  d¡}ˆ jj|||d�}|rˆ jjd \}}}}	}
}nˆ jjd \}}}}
}tdd|ƒ}tdddƒ}ˆ  ¡ ‰tˆddƒdu rDt	ƒ ˆ_
tjˆd	||d
�}| d¡}tj|d||d
�}|rgt|tƒ t|tƒ t ˆ|||¡}ˆ j |¡ ‡ ‡fdd„}| |¡ |S )a}  
        Implement IWebSocketClientAgent with in-memory transports.

        :param transport_config: a string starting with 'wss://' or
            'ws://'

        :param options: a dict containing options

        :param protocol_class: the client protocol class to
            instantiate (or `None` for defaults, which is to use
            `WebSocketClientProtocol`)
        zwss://)Úprotocol_classéÿÿÿÿr   r   r   ÚfactoryNT)ZisServerZhostAddressZpeerAddressFc                    s   ˆˆ j | < | S r    )r1   )Úproto©r   r4   r   r   Úadd_mapping´   s   
z0_TwistedWebMemoryAgent.open.<locals>.add_mapping)Ú
startswithr-   Úopenr*   Z
sslClientsZ
tcpClientsr   r+   Úgetattrr   r8   r	   ZFakeTransportZbuildProtocolr
   r   Úconnectr,   ÚaddZaddCallback)r   Ztransport_configÚoptionsr6   Z	is_secureZreal_client_protocolÚhostÚportr8   Zcontext_factoryÚtimeoutZbindAddressÚserver_addressÚclient_addressZserver_transportZclientProtocolZclient_transportZpumpr;   r   r:   r   r=   ‚   s@   
þþ
þ

ÿ
z_TwistedWebMemoryAgent.openr    )r   r   r   r'   r5   r=   r   r   r   r   r)   h   s    
r)   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú
_Kalamazooaµ  
    Feeling whimsical about class names, see https://en.wikipedia.org/wiki/Handcar

    This is 'an IOPump pumper', an object which causes a series of
    IOPumps it is monitoring to do their I/O operations
    periodically. This needs the 'real' reactor which trial drives,
    because reasons:

     - so @inlineCallbacks / async-def functions work
       (if I could explain exactly why here, I would)

     - we need to 'break the loop' of synchronous calls somewhere and
       polluting the tests themselves with that is bad

     - get rid of e.g. .flush() calls in tests themselves (thus
      'teaching' the tests about details of I/O scheduling that they
      shouldn't know).
    c                 C   s*   t ƒ | _d| _g | _ddlm} || _d S )NFr   )r2   )r.   r/   Ú_pumpingÚ_waiting_for_stopZtwisted.internetr2   Ú_global_reactor)r   Zglobal_reactorr   r   r   r5   Ï   s
   
z_Kalamazoo.__init__c                 C   s   | j  |¡ dS )zp
        Add a new IOPump. It will be removed when both its client and
        server are disconnected.
        N)r/   r@   )r   Úpr   r   r   r@   Ö   s   z_Kalamazoo.addc                 C   s$   | j rdS d| _ | j d| j¡ dS )zÃ
        Begin triggering I/O in all IOPump instances we have. We will keep
        periodically 'pumping' our IOPumps until `.stop()` is
        called. Call from `setUp()` for example.
        NTr   )rH   rJ   Ú	callLaterÚ
_pump_oncer"   r   r   r   ÚstartÝ   s   z_Kalamazoo.startc                 C   s@   | j st| jƒrtƒ }| j |¡ d| _ |S tƒ }| d¡ |S )z
        :returns: a Deferred that fires when we have stopped pump()-ing

        Call from `tearDown()`, for example.
        FN)rH   ÚlenrI   r   ÚappendÚcallback©r   Údr   r   r   Ústopè   s   
z_Kalamazoo.stopc                 C   sB   | j r|  ¡  | j d| j¡ dS | jD ]}| d¡ qg | _dS )z{
        flush all data from all our IOPump instances and schedule another
        iteration on the global reactor
        gš™™™™™¹?N)rH   Ú_flushrJ   rL   rM   rI   rQ   rR   r   r   r   rM   ÷   s   

z_Kalamazoo._pump_oncec                 C   sD   | j }tƒ  }| _ |D ]}| ¡  |jjr|jjrq| |¡ qdS )zE
        Flush all data between pending client/server pairs.
        N)r/   r.   ÚflushZclientIOÚdisconnectedZserverIOr@   )r   Z	old_pumpsZ	new_pumpsrK   r   r   r   rU     s   üz_Kalamazoo._flushN)
r   r   r   r'   r5   r@   rN   rT   rM   rU   r   r   r   r   rG   »   s    rG   c                   C   s   t ƒ S )z4
    return a new instance implementing IPumper
    N)rG   r   r   r   r   r     s   r   c                 C   s   |du rt }t| ||ƒS )a)  
    return a new instance implementing `IWebSocketClientAgent`.

    connection attempts will be satisfied by traversing the Upgrade
    request path starting at `resource` to find a `WebSocketResource`
    and then exchange data between client and server using purely
    in-memory buffers.
    N)r   r)   )r2   r3   r4   r   r   r   r     s   r   N)"Ztwisted.internet.interfacesr   ÚImportErrorZtwisted.internet.deferr   Ztwisted.internet.addressr   Ztwisted.internet._resolverr   r   r   Ztwisted.internet.testingr   Ztwisted.test.proto_helpersZtwisted.testr	   Zzope.interfacer
   r   Zautobahn.websocket.interfacesr   Zautobahn.twisted.websocketr   r   r   Ú__all__Úobjectr   r   r   r)   rG   r   r   r   r   r   r   Ú<module>   s@   ÿÿÿSV