
    hQ'                        	 d dl mZ d dlmZ d dlmZ 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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e             Z ee
       G d de             Z G d dee      Z G d de      Z G d de      Zd Z d Z!y# e$ r	  ed      w xY w# e$ r	 d dlmZ Y w xY w)    )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                       e Zd ZddZy)_StaticTestResolverc                     t        |      }|j                  |       |j                  t        dd|dk(  rdn|             |j	                          y)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
resolutions        ]/var/www/Befach/backend/env/lib/python3.12/site-packages/autobahn/twisted/testing/__init__.pyresolveHostNamez#_StaticTestResolver.resolveHostName@   sL     $H-
  ,  {Z1_E*U	
 	##%    N)r   )__name__
__module____qualname__r#    r$   r"   r   r   >   s    	&r$   r   c                   *    e Zd ZdZdZed        Zd Zy)_TestNameResolverz9
    A test version of IReactorPluggableNameResolver
    Nc                 P    | j                   t               | _         | j                   S N)	_resolverr   r   s    r"   nameResolverz_TestNameResolver.nameResolverT   s     >>!02DN~~r$   c                 ,    | j                   }|| _         |S r,   )r-   )r   resolverolds      r"   installNameResolverz%_TestNameResolver.installNameResolverZ   s    nn!
r$   )r%   r&   r'   __doc__r-   propertyr/   r3   r(   r$   r"   r*   r*   L   s%     I 
r$   r*   c                       e Zd ZdZy)r   zY
    Combine MemoryReactor, Clock and an IReactorPluggableNameResolver
    together.
    N)r%   r&   r'   r4   r(   r$   r"   r   r   `   s     	r$   r   c                       e Zd ZdZd ZddZy)_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                     || _         || _        || _        t        | j                         | _        t               | _        t               | _        y r,   )	_reactor_server_protocol_pumperr   _agentset_pumpsdict_servers)r   reactorpumperserver_protocols       r"   __init__z_TwistedWebMemoryAgent.__init__x   s<     / 34==Aer$   Nc                     |j                  d      } j                  j                  |||      }|r! j                  j                  d   \  }}}}	}
}n j                  j
                  d   \  }}}}
}t        dd|      }t        ddd      } j                         t        dd      t               _
        t        j                  d	||
      }|j                  d      }t        j                  |d||
      }|r t        |t               t        |t               t        j                   |||      } j"                  j%                  |        fd}|j'                  |       |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                 &    j                   | <   | S r,   )rA   )protor   rD   s    r"   add_mappingz0_TwistedWebMemoryAgent.open.<locals>.add_mapping   s    #2DMM% Lr$   )
startswithr=   openr:   
sslClients
tcpClientsr   r;   getattrr   rI   r
   FakeTransportbuildProtocolr   r   connectr<   addaddCallback)r   transport_configoptionsrG   	is_securereal_client_protocolhostportrI   context_factorytimeoutbindAddressserver_addressclient_addressserver_transportclientProtocolclient_transportpumprO   rD   s   `                  @r"   rQ   z_TwistedWebMemoryAgent.open   sp    %//9	  ${{//g)  0  

 IMIaIabdIeFD$';8<8P8PQS8T5D$+$UK>$UK?//1?It4<&<&>O# ..d&ND !..t4 ..U&ND -}=-}=}}-~?OQ	 	((5##r$   r,   )r%   r&   r'   r4   rE   rQ   r(   r$   r"   r8   r8   h   s    6$r$   r8   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	
_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                 X    t               | _        d| _        g | _        ddlm} || _        y )NFr   )rB   )r>   r?   _pumping_waiting_for_stoptwisted.internetrB   _global_reactor)r   global_reactors     r"   rE   z_Kalamazoo.__init__   s%    e!#>-r$   c                 :    | j                   j                  |       y)zp
        Add a new IOPump. It will be removed when both its client and
        server are disconnected.
        N)r?   rX   )r   ps     r"   rX   z_Kalamazoo.add   s    
 	r$   c                 x    | j                   ryd| _         | j                  j                  d| j                         y)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   )rl   ro   	callLater
_pump_oncer.   s    r"   startz_Kalamazoo.start   s/     ==&&q$//:r$   c                     | j                   st        | j                        r.t               }| j                  j	                  |       d| _         |S t               }|j                  d       |S )z
        :returns: a Deferred that fires when we have stopped pump()-ing

        Call from `tearDown()`, for example.
        FN)rl   lenrm   r   appendcallbackr   ds     r"   stopz_Kalamazoo.stop   sW     ==C 6 67
A""))!,!DMHJ	

4r$   c                     | j                   r7| j                          | j                  j                  d| j                         y| j
                  D ]  }|j                  d        g | _        y)z{
        flush all data from all our IOPump instances and schedule another
        iteration on the global reactor
        g?N)rl   _flushro   rt   ru   rm   rz   r{   s     r"   ru   z_Kalamazoo._pump_once   sR    
 ==KKM  **3@++

4  ,%'D"r$   c                     | j                   }t               x}| _         |D ]P  }|j                          |j                  j                  r|j
                  j                  r@|j                  |       R y)zE
        Flush all data between pending client/server pairs.
        N)r?   r>   flushclientIOdisconnectedserverIOrX   )r   	old_pumps	new_pumpsrr   s       r"   r   z_Kalamazoo._flush  sV     KK	"%%'	DKAGGIzz&&1::+B+BMM!	 r$   N)
r%   r&   r'   r4   rE   rX   rv   r}   ru   r   r(   r$   r"   rj   rj      s%    &.	;(
r$   rj   c                      t               S )z4
    return a new instance implementing IPumper
    )rj   r(   r$   r"   r   r     s     <r$   c                 ,    |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.
    )r   r8   )rB   rC   rD   s      r"   r   r     s     1!'6?CCr$   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   r8   rj   r   r   r(   r$   r"   <module>r      s   <= , 0 5 T>;  8 ? C > = 
&& 
&  
& *+  ,&	!35F 	P$2 P$fS SlDq  
G   >=>s   B& B7 &B47CC