U
    
W[K  ã                   @   s´   d 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dlmZ ddlmZ dd	lmZmZ eeƒG d
d„ deƒƒZe	eeƒ eeƒG dd„ deƒƒZe	eeƒ dS )z$
Utilities for Twisted.names tests.
é    )ÚdivisionÚabsolute_import)Ú	randrange)Úimplementer)ÚverifyClass)ÚIPv4Address)Úsucceed)ÚClock)ÚIReactorUDPÚIUDPTransportc                   @   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| jžŽ S )z_
        Return the address which this transport is pretending to be bound
        to.
        ÚUDP)r   )r   r   ©r   r   r   r   ÚgetHost/   s    zMemoryDatagramTransport.getHostc                 C   s    | j dk	rtdƒ‚||f| _ dS )z>
        Connect this transport to the given address.
        NzAlready connected)r   Ú
ValueError)r   r   Úportr   r   r   Úconnect7   s    
zMemoryDatagramTransport.connectNc                 C   sH   |dkr| j }|dk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   ÚstopListeningM   s    
z%MemoryDatagramTransport.stopListeningc                 C   s   dS ©zC
        Dummy implementation to satisfy L{IUDPTransport}.
        Nr   )r   Úenabledr   r   r   ÚsetBroadcastAllowedU   s    z+MemoryDatagramTransport.setBroadcastAllowedc                 C   s   dS r$   r   r   r   r   r   ÚgetBroadcastAllowed\   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   s   s    
zMemoryReactor.__init__Ú é    c                 C   sZ   |dkr t ddƒ}|| jkrq q|| jk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   Ú	listenUDPx   s    


  ÿ

zMemoryReactor.listenUDPN)r.   r/   )r(   r)   r*   r+   r   r1   r   r   r   r   r,   g   s   
r,   N)r+   Z
__future__r   r   Zrandomr   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.internet.addressr   Ztwisted.internet.deferr   Ztwisted.internet.taskr	   Ztwisted.internet.interfacesr
   r   Úobjectr   r,   r   r   r   r   Ú<module>   s   L
!