o
    >hQ'                     @   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   resolutionBeganaddressResolvedr   resolutionComplete)selfreceiverhostName
portNumber
resolution r   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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_classr   r   r   factoryNT)isServerhostAddresspeerAddressFc                    s    j | < | S r&   )r7   )protor   r:   r   r    add_mapping   s   
z0_TwistedWebMemoryAgent.open.<locals>.add_mapping)
startswithr3   openr0   
sslClients
tcpClientsr   r1   getattrr   r>   r	   FakeTransportbuildProtocolr
   r   connectr2   addaddCallback)r   transport_configoptionsr<   	is_securereal_client_protocolhostportr>   context_factorytimeoutbindAddressserver_addressclient_addressserver_transportclientProtocolclient_transportpumprD   r   rC   r    rF      s@   




z_TwistedWebMemoryAgent.openr&   )r"   r#   r$   r-   r;   rF   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   )r8   )r4   r5   _pumping_waiting_for_stoptwisted.internetr8   _global_reactor)r   global_reactorr   r   r    r;      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)r5   rM   )r   pr   r   r    rM      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   )r_   rb   	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)r_   lenr`   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)r_   _flushrb   re   rf   r`   rj   rk   r   r   r    rf      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)r5   r4   flushclientIOdisconnectedserverIOrM   )r   	old_pumps	new_pumpsrd   r   r   r    rn     s   z_Kalamazoo._flushN)
r"   r#   r$   r-   r;   rM   rg   rm   rf   rn   r   r   r   r    r^      s    r^   c                   C   s   t  S )z4
    return a new instance implementing IPumper
    )r^   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/   )r8   r9   r:   r   r   r    r     s   r   N)"twisted.internet.interfacesr   ImportErrortwisted.internet.deferr   twisted.internet.addressr   twisted.internet._resolverr   r   r   twisted.internet.testingr   twisted.test.proto_helperstwisted.testr	   zope.interfacer
   r   autobahn.websocket.interfacesr   autobahn.twisted.websocketr   r   r   __all__objectr   r%   r   r/   r^   r   r   r   r   r   r    <module>   s@   SV