U
    •uÄcQ'  ã                   @   sZ  zd dl mZ W n ek
r,   edƒ‚Y nX 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k
r’   d dlmZ Y nX 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   újc:\users\psant\downloads\hotstarbackend\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 krtƒ | _ | j S ©N)Ú	_resolverr   ©r   r   r   r   ÚnameResolverT   s    
z_TestNameResolver.nameResolverc                 C   s   | j }|| _ |S r    )r!   )r   Z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krˆt	ƒ ˆ_
tjˆd	||d
�}| d¡}tj|d||d
�}|rÎt|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    )r0   )Úproto©r   r3   r   r   Úadd_mapping´   s    
z0_TwistedWebMemoryAgent.open.<locals>.add_mapping)Ú
startswithr,   Úopenr)   Z
sslClientsZ
tcpClientsr   r*   Úgetattrr   r7   r	   ZFakeTransportZbuildProtocolr
   r   Úconnectr+   ÚaddZaddCallback)r   Ztransport_configÚoptionsr5   Z	is_secureZreal_client_protocolÚhostÚportr7   Zcontext_factoryÚtimeoutZbindAddressZserver_addressZclient_addressZserver_transportZclientProtocolZclient_transportZpumpr:   r   r9   r   r<   ‚   sP    
 þ  þ
  þ

   ÿ
z_TwistedWebMemoryAgent.open)N)r   r   r   r&   r4   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   )r1   )r-   r.   Ú_pumpingÚ_waiting_for_stopÚtwisted.internetr1   Ú_global_reactor)r   Zglobal_reactorr   r   r   r4   Ï   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   )rE   rH   Ú	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)rE   ÚlenrF   r   ÚappendÚcallback©r   Údr   r   r   Ústopè   s    
z_Kalamazoo.stopc                 C   s@   | j r |  ¡  | j d| j¡ n| 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)rE   Ú_flushrH   rJ   rK   rF   rO   rP   r   r   r   rK   ÷   s    
z_Kalamazoo._pump_oncec                 C   sD   | j }tƒ  }| _ |D ](}| ¡  |jjr4|jjr4q| |¡ qdS )zE
        Flush all data between pending client/server pairs.
        N)r.   r-   ÚflushZclientIOÚdisconnectedZserverIOr?   )r   Z	old_pumpsZ	new_pumpsrI   r   r   r   rS     s    z_Kalamazoo._flushN)
r   r   r   r&   r4   r?   rL   rR   rK   rS   r   r   r   r   rD   »   s   rD   c                   C   s   t ƒ S )z4
    return a new instance implementing IPumper
    )rD   r   r   r   r   r     s    r   c                 C   s   |dk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(   )r1   r2   r3   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(   rD   r   r   r   r   r   r   Ú<module>   s:   ÿ
SV