
    afQ'                        	 d dl mZ n# e$ r  ed          w xY w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 n# e$ r	 d dlmZ Y nw xY 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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!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                       e Zd ZddZdS )_StaticTestResolverr   c                     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
resolutions        S/var/www/html/env/lib/python3.11/site-packages/autobahn/twisted/testing/__init__.pyresolveHostNamez#_StaticTestResolver.resolveHostName@   sr     $H--
  ,,,  {Z1__EE*UU	
 	
 	
 	##%%%%%    N)r   )__name__
__module____qualname__r#    r$   r"   r   r   >   s(        	& 	& 	& 	& 	& 	&r$   r   c                   2    e Zd ZdZdZed             Zd ZdS )_TestNameResolverz9
    A test version of IReactorPluggableNameResolver
    Nc                 D    | j         t                      | _         | j         S N)	_resolverr   r   s    r"   nameResolverz_TestNameResolver.nameResolverT   s    >!022DN~r$   c                 "    | j         }|| _         |S r,   )r-   )r   resolverolds      r"   installNameResolverz%_TestNameResolver.installNameResolverZ   s    n!
r$   )r%   r&   r'   __doc__r-   propertyr/   r3   r(   r$   r"   r*   r*   L   sM          I  X
    r$   r*   c                       e Zd ZdZdS )r   zY
    Combine MemoryReactor, Clock and an IReactorPluggableNameResolver
    together.
    N)r%   r&   r'   r4   r(   r$   r"   r   r   `   s          	Dr$   r   c                        e Zd ZdZd Z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                     || _         || _        || _        t          | j                   | _        t                      | _        t                      | _        d S r,   )	_reactor_server_protocol_pumperr   _agentset_pumpsdict_servers)r   reactorpumperserver_protocols       r"   __init__z_TwistedWebMemoryAgent.__init__x   sE     / 34=AAeer$   Nc                     |                     d          } j                            |||          }|r j        j        d         \  }}}}	}
}n j        j        d         \  }}}}
}t          dd|          }t          ddd          }                                 t          dd          t                      _
        t          j        d	||
          }|                    d          }t          j        |d||
          }|r*t          |t                     t          |t                     t          j        |||          } j                            |            f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                     j         | <   | S r,   )rA   )protor   rD   s    r"   add_mappingz0_TwistedWebMemoryAgent.open.<locals>.add_mapping   s    #2DM% 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   s    %//99	  ${//g)  0  
  

  	UIMIabdIeFD$';;8<8PQS8T5D$+$UK>>$UK??//11?It44<&<&>&>O# .d&ND D D !..t44 .U&ND D D  	>-}===-}===}-~?OQ Q	 	 	 	 	 	 	((555##r$   r,   )r%   r&   r'   r4   rE   rQ   r(   r$   r"   r8   r8   h   sA           6$ 6$ 6$ 6$ 6$ 6$r$   r8   c                   6    e Zd ZdZd Zd Zd Zd Zd Z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                 b    t                      | _        d| _        g | _        ddlm} || _        d S )NFr   )rB   )r>   r?   _pumping_waiting_for_stoptwisted.internetrB   _global_reactor)r   global_reactors     r"   rE   z_Kalamazoo.__init__   s<    ee!#>>>>>>-r$   c                 :    | j                             |           dS )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                 f    | 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   )rl   ro   	callLater
_pump_oncer.   s    r"   startz_Kalamazoo.start   s;     = 	F&&q$/:::::r$   c                     | j         st          | j                  r1t                      }| j                            |           d| _         |S t                      }|                    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   sl     = 	C 677 	

A"))!,,,!DMHJJ	

4r$   c                     | j         r6|                                  | j                            d| j                   dS | j        D ]}|                    d           g | _        dS )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   sq    
 = 	(KKMMM **3@@@@@+ ! !

4    %'D"""r$   c                     | j         }t                      x}| _         |D ]D}|                                 |j        j        r|j        j        r/|                    |           EdS )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  sr     K	"%%%'	DK 	 	AGGIIIz& 1:+B MM!		 	r$   N)
r%   r&   r'   r4   rE   rX   rv   r}   ru   r   r(   r$   r"   rj   rj      sx         &. . .  	; 	; 	;  ( ( (
 
 
 
 
r$   rj   c                      t                      S )z4
    return a new instance implementing IPumper
    )rj   r(   r$   r"   r   r     s     <<r$   c                 6    |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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  <=======   
+G  
 , + + + + + 0 0 0 0 0 0 5 5 5 5 5 5 T T T T T T T T>;;;;;;; > > >========>       8 8 8 8 8 8 8 8 ? ? ? ? ? ? C C C C C C > > > > > > = = = = = = 
& 
& 
& 
& 
&& 
& 
&  
& *++       ,+&	 	 	 	 	!35F 	 	 	P$ P$ P$ P$ P$2 P$ P$ P$fS S S S S S S Sl  D D D D Ds   	 ? AA