o
    ¯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   ú>/usr/lib/python3/dist-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.
        Ú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rtdƒ‚| j ||f¡ dS )z*
        Send the given datagram.
        NzNeed an addresszPacket too big)r   r   Úlenr   r   Úappend)r   ÚdatagramÚaddrr   r   r   Úwrite<   s   zMemoryDatagramTransport.writec                 C   s   | j  ¡  tdƒS )z+
        Shut down this transport.
        N)r   ÚstopProtocolr   r   r   r   r   ÚstopListeningH   s   
z%MemoryDatagramTransport.stopListeningc                 C   ó   dS ©zC
        Dummy implementation to satisfy L{IUDPTransport}.
        Nr   )r   Úenabledr   r   r   ÚsetBroadcastAllowedO   ó   z+MemoryDatagramTransport.setBroadcastAllowedc                 C   r%   r&   r   r   r   r   r   ÚgetBroadcastAllowedU   r)   z+MemoryDatagramTransport.getBroadcastAllowedr   )Ú__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   s\   |dkr	 t ddƒ}|| jvrnq|| jv rtdƒ‚t||f||ƒ}|| j|< | |¡ |S )zR
        Pretend to bind a UDP port and connect the given protocol to it.
        r   Té   i   zAddress in use)r   r0   r   r
   ÚmakeConnection)r   r   r   Ú	interfacer   Ú	transportr   r   r   Ú	listenUDPp   s   

ý


zMemoryReactor.listenUDPN)r1   r2   )r+   r,   r-   r.   r   r7   r   r   r   r   r/   _   s    r/   N)r.   Úrandomr   Úzope.interfacer   Úzope.interface.verifyr   Útwisted.internet.addressr   Útwisted.internet.deferr   Útwisted.internet.interfacesr   r   Útwisted.internet.taskr	   r
   r/   r   r   r   r   Ú<module>   s   
G!