a
    )¶(bÿ  ã                   @   s¢   d Z 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
 ddlmZmZ ddlmZ eeƒG d	d
„ d
ƒƒZeeeƒ eeƒG dd„ deƒƒZeeeƒ dS )z$
Utilities for Twisted.names tests.
é    )Ú	randrange)Úimplementer)ÚverifyClass)ÚIPv4Address)Úsucceed)ÚIReactorUDPÚIUDPTransport)ÚClockc                   @   sJ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
dS )ÚMemoryDatagramTransporta¬  
    This L{IUDPTransport} implementation enforces the usual connection rules
    and captures sent traffic in a list for later inspection.

    @ivar _host: The host address to which this transport is bound.
    @ivar _protocol: The protocol connected to this transport.
    @ivar _sentPackets: A C{list} of two-tuples of the datagrams passed to
        C{write} and the addresses to which they are destined.

    @ivar _connectedTo: L{None} if this transport is unconnected, otherwise an
        address to which all traffic is supposedly sent.

    @ivar _maxPacketSize: An C{int} giving the maximum length of a datagram
        which will be successfully handled by C{write}.
    c                 C   s"   || _ || _g | _d | _|| _d S ©N)Ú_hostÚ	_protocolÚ_sentPacketsÚ_connectedToÚ_maxPacketSize)ÚselfÚhostÚprotocolÚmaxPacketSize© r   ú;lib/python3.9/site-packages/twisted/names/test/test_util.pyÚ__init__&   s
    z MemoryDatagramTransport.__init__c                 C   s   t dg| j¢R Ž S )z_
        Return the address which this transport is pretending to be bound
        to.
        ZUDP)r   r   ©r   r   r   r   ÚgetHost-   s    zMemoryDatagramTransport.getHostc                 C   s    | j durtdƒ‚||f| _ dS )z>
        Connect this transport to the given address.
        NzAlready connected)r   Ú
ValueError)r   r   Úportr   r   r   Úconnect4   s    
zMemoryDatagramTransport.connectNc                 C   sH   |du r| j }|du rtdƒ‚t|ƒ| jkr4tdƒ‚| j ||f¡ dS )z*
        Send the given datagram.
        NzNeed an addresszPacket too big)r   r   Úlenr   r   Úappend)r   ZdatagramZaddrr   r   r   Úwrite<   s    zMemoryDatagramTransport.writec                 C   s   | j  ¡  tdƒS )z+
        Shut down this transport.
        N)r   ZstopProtocolr   r   r   r   r   ÚstopListeningH   s    
z%MemoryDatagramTransport.stopListeningc                 C   s   dS ©zC
        Dummy implementation to satisfy L{IUDPTransport}.
        Nr   )r   Zenabledr   r   r   ÚsetBroadcastAllowedO   s    z+MemoryDatagramTransport.setBroadcastAllowedc                 C   s   dS r!   r   r   r   r   r   ÚgetBroadcastAllowedU   s    z+MemoryDatagramTransport.getBroadcastAllowed)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   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 )
ÚMemoryReactoraO  
    An L{IReactorTime} and L{IReactorUDP} provider.

    Time is controlled deterministically via the base class, L{Clock}.  UDP is
    handled in-memory by connecting protocols to instances of
    L{MemoryDatagramTransport}.

    @ivar udpPorts: A C{dict} mapping port numbers to instances of
        L{MemoryDatagramTransport}.
    c                 C   s   t  | ¡ i | _d S r   )r	   r   ÚudpPortsr   r   r   r   r   l   s    
zMemoryReactor.__init__Ú é    c                 C   sZ   |dkr t ddƒ}|| jvrq q|| jv r2tdƒ‚t||f||ƒ}|| j|< | |¡ |S )zR
        Pretend to bind a UDP port and connect the given protocol to it.
        r   é   i   zAddress in use)r   r)   r   r
   ZmakeConnection)r   r   r   Z	interfacer   Z	transportr   r   r   Ú	listenUDPp   s    




zMemoryReactor.listenUDPN)r*   r+   )r$   r%   r&   r'   r   r-   r   r   r   r   r(   _   s   r(   N)r'   Zrandomr   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.internet.addressr   Ztwisted.internet.deferr   Ztwisted.internet.interfacesr   r   Ztwisted.internet.taskr	   r
   r(   r   r   r   r   Ú<module>   s   G
!