Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/iosim.cpython-37.pyc

B

;��]�E�@sVdZddlmZmZmZddlZyddlmZWne	k
rDYnXddl
mZmZddl
mZmZddlmZmZddlmZdd	lmZdd
lmZddlmZddlmZGd
d�d�Zeej�Gdd�de��Zeej ej!�Gdd�d��Z"dd�Z#dd�Z$Gdd�d�Z%d%dd�Z&e#e$ddfdd�Z'dd �Z(Gd!d"�d"e�Z)d&d#d$�Z*dS)'z0
Utilities and helpers for simulating a network
�)�absolute_import�division�print_functionN)�Error)�implementer�directlyProvides)�TCP4ClientEndpoint�TCP4ServerEndpoint)�Factory�Protocol)�ConnectionRefusedError)�Failure)�error)�
interfaces)�MemoryReactorClockc@s$eZdZdd�Zdd�Zdd�ZdS)�TLSNegotiationcCs||_||_d|_||_dS)NF)�obj�connectState�sent�readyToSend)�selfrr�r�E/opt/alt/python37/lib64/python3.7/site-packages/twisted/test/iosim.py�__init__szTLSNegotiation.__init__cCsd|jfS)NzTLSNegotiation(%r))r)rrrr�__repr__&szTLSNegotiation.__repr__cCs"|j�|j�st�|_|��dS)N)rZiosimVerify�NativeOpenSSLError�disconnectReason�loseConnection)r�otherZtptrrr�pretendToVerify*szTLSNegotiation.pretendToVerifyN)�__name__�
__module__�__qualname__rrrrrrrrsrc@seZdZdZdS)�FakeAddressz]
    The default address type for the host and peer of L{FakeTransport}
    connections.
    N)r r!r"�__doc__rrrrr#4sr#c@s�eZdZdZee��fdd��ZdZdZ	dZ
e�d�Z
dZdZdZd0dd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Z d'd(�Z!d1d*d+�Z"d,d-�Z#d.d/�Z$dS)2�
FakeTransportz�
    A wrapper around a file-like object to make it behave as a Transport.

    This doesn't actually stream the file to the attached protocol,
    and is thus useful mainly as a utility for debugging protocols.
    cCst|�S)N)�next)�counterrrr�<lambda>G�zFakeTransport.<lambda>rzConnection doneNcCsH||_||_g|_|��|_|dkr*t�}||_|dkr>t�}||_dS)a�
        @param protocol: This transport will deliver bytes to this protocol.
        @type protocol: L{IProtocol} provider

        @param isServer: C{True} if this is the accepting side of the
            connection, C{False} if it is the connecting side.
        @type isServer: L{bool}

        @param hostAddress: The value to return from C{getHost}.  L{None}
            results in a new L{FakeAddress} being created to use as the value.
        @type hostAddress: L{IAddress} provider or L{None}

        @param peerAddress: The value to return from C{getPeer}.  L{None}
            results in a new L{FakeAddress} being created to use as the value.
        @type peerAddress: L{IAddress} provider or L{None}
        N)�protocol�isServer�stream�_nextserial�serialr#�hostAddress�peerAddress)rr*r+r/r0rrrrPs
zFakeTransport.__init__cCs d|jrdpd|j|jjjfS)NzFakeTransport<%s,%s,%s>�S�C)r+r.r*�	__class__r )rrrrrmszFakeTransport.__repr__cCs2|jr
dS|jdk	r"|j�|�n|j�|�dS)N)�
disconnecting�tls�tlsbuf�appendr,)r�datarrr�writess

zFakeTransport.writecCs|jr|js|j��dS)N)�producer�streamingProducer�resumeProducing)rrrr�_checkProducer~szFakeTransport._checkProducercCs||_||_|s|��dS)z.
        From abstract.FileDescriptor
        N)r:r;r<)rr:Z	streamingrrr�registerProducer�szFakeTransport.registerProducercCs
d|_dS)N)r:)rrrr�unregisterProducer�sz FakeTransport.unregisterProducercCs|��|��dS)N)r?r)rrrr�
stopConsuming�szFakeTransport.stopConsumingcCs|�d�|��dS)Nr))r9�join)rZiovecrrr�
writeSequence�szFakeTransport.writeSequencecCs
d|_dS)NT)r4)rrrrr�szFakeTransport.loseConnectioncCs
d|_dS)zp
        For the time being, this is the same as loseConnection; no buffered
        data will be lost.
        TN)r4)rrrr�abortConnection�szFakeTransport.abortConnectioncCs,|jdk	rt�}n|j}|j�t|��dS)N)r5rrr*ZconnectionLostr
)r�errrrr�reportDisconnect�s
zFakeTransport.reportDisconnectcCsdS)zM
        Identify this transport/event source to the logging system.
        Ziosimr)rrrr�	logPrefix�szFakeTransport.logPrefixcCs|jS)N)r0)rrrr�getPeer�szFakeTransport.getPeercCs|jS)N)r/)rrrr�getHost�szFakeTransport.getHostcCsdS)Nr)rrrrr<�szFakeTransport.resumeProducingcCsdS)Nr)rrrr�pauseProducing�szFakeTransport.pauseProducingcCs|��dS)N)r)rrrr�
stopProducing�szFakeTransport.stopProducingTcCs |j|A}t||�|_g|_dS)N)r+rr5r6)rZcontextFactoryZbeNormalrrrr�startTLS�s
zFakeTransport.startTLScCsH|j}|rg|_d�|�S|jdk	r@|jjr:d|j_|jSdSndSdS)z�
        Get the pending writes from this transport, clearing them from the
        pending buffer.

        @return: the bytes written with C{transport.write}
        @rtype: L{bytes}
        r)NT)r,rAr5rr)rr1rrr�getOutBuffer�s

zFakeTransport.getOutBuffercCstt|t�rd|jdk	st�|jjrZ|j�||�d|_|jd}|_|�|�t|t	j
�qpd|j_n|j�
|�dS)NT)�
isinstancerr5�AssertionErrorrrr6rBrrZ
ISSLTransportrr*ZdataReceived)r�buf�brrr�bufferReceived�s


zFakeTransport.bufferReceived)NN)T)%r r!r"r$�staticmethod�	itertools�countr-�closedr4�disconnectedrZConnectionDonerr:r;r5rrr9r=r>r?r@rBrrCrErFrGrHr<rIrJrKrLrQrrrrr%=s:




	r%cCst|dd�S)z�
    Create and return a new in-memory transport hooked up to the given protocol.

    @param clientProtocol: The client protocol to use.
    @type clientProtocol: L{IProtocol} provider

    @return: The transport.
    @rtype: L{FakeTransport}
    F)r+)r%)�clientProtocolrrr�makeFakeClients
rXcCst|dd�S)z�
    Create and return a new in-memory transport hooked up to the given protocol.

    @param serverProtocol: The server protocol to use.
    @type serverProtocol: L{IProtocol} provider

    @return: The transport.
    @rtype: L{FakeTransport}
    T)r+)r%)�serverProtocolrrr�makeFakeServers
rZc@s,eZdZdZdd�Zd
dd�Zddd�Zd	S)�IOPumpz�
    Utility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    cCs"||_||_||_||_||_dS)N)�client�server�clientIO�serverIO�debug)rr\r]r^r_r`rrrr&s
zIOPump.__init__FcCs8d}x.td�D]}|�|�r"d}qPqWds4td��|S)zk
        Pump until there is no more input or output.

        Returns whether any data was moved.
        Fi�TrzToo long)�range�pumprN)rr`�result�xrrr�flush.s
zIOPump.flushcCs,|js
|rtd�|j��}|j��}|j��|j��|jsD|rttd�|r`tdt|��|rttdt|��|r�|j�|�|r�|j�|�|s�|r�dS|jjr�|jj	s�|js�|r�td�d|j_	d|j_|j�
�dS|jj�r(|jj	�s(|j�s|�r
td�d|j_	d|j_|j�
�dSdS)	zX
        Move data back and forth.

        Returns whether any data was moved.
        z
-- GLUG --�.zC: zS: Tz* Cz* SF)r`�printr_rLr^r=�reprrQr4rVrE)rr`ZsDataZcDatarrrrb?sD








zIOPump.pumpN)F)F)r r!r"r$rrerbrrrrr[ s
r[FTcCs4|�|�|�|�t|||||�}|r0|��|S)aN
    Create a new L{IOPump} connecting two protocols.

    @param serverProtocol: The protocol to use on the accepting side of the
        connection.
    @type serverProtocol: L{IProtocol} provider

    @param serverTransport: The transport to associate with C{serverProtocol}.
    @type serverTransport: L{FakeTransport}

    @param clientProtocol: The protocol to use on the initiating side of the
        connection.
    @type clientProtocol: L{IProtocol} provider

    @param clientTransport: The transport to associate with C{clientProtocol}.
    @type clientTransport: L{FakeTransport}

    @param debug: A flag indicating whether to log information about what the
        L{IOPump} is doing.
    @type debug: L{bool}

    @param greet: Should the L{IOPump} be L{flushed <IOPump.flush>} once before
        returning to put the protocols into their post-handshake or
        post-server-greeting state?
    @type greet: L{bool}

    @return: An L{IOPump} which connects C{serverProtocol} and
        C{clientProtocol} and delivers bytes between them when it is pumped.
    @rtype: L{IOPump}
    )ZmakeConnectionr[re)rY�serverTransportrW�clientTransportr`�greetrbrrr�connectks 

rlc
	Cs4|�}|�}||�}||�}	||t||	||||�fS)a
    Connect a given server and client class to each other.

    @param ServerClass: a callable that produces the server-side protocol.
    @type ServerClass: 0-argument callable returning L{IProtocol} provider.

    @param ClientClass: like C{ServerClass} but for the other side of the
        connection.
    @type ClientClass: 0-argument callable returning L{IProtocol} provider.

    @param clientTransportFactory: a callable that produces the transport which
        will be attached to the protocol returned from C{ClientClass}.
    @type clientTransportFactory: callable taking (L{IProtocol}) and returning
        L{FakeTransport}

    @param serverTransportFactory: a callable that produces the transport which
        will be attached to the protocol returned from C{ServerClass}.
    @type serverTransportFactory: callable taking (L{IProtocol}) and returning
        L{FakeTransport}

    @param debug: Should this dump an escaped version of all traffic on this
        connection to stdout for inspection?
    @type debug: L{bool}

    @param greet: Should the L{IOPump} be L{flushed <IOPump.flush>} once before
        returning to put the protocols into their post-handshake or
        post-server-greeting state?
    @type greet: L{bool}

    @return: the client protocol, the server protocol, and an L{IOPump} which,
        when its C{pump} and C{flush} methods are called, will move data
        between the created client and server protocol instances.
    @rtype: 3-L{tuple} of L{IProtocol}, L{IProtocol}, L{IOPump}
    )rl)
ZServerClassZClientClassZclientTransportFactoryZserverTransportFactoryr`rk�c�sZcio�siorrr�connectedServerAndClient�s
&rpcCs2|\}}}}}|\}}}	}
||kr*||fSdSdS)a'
    Should the client and server described by the arguments be connected to
    each other, i.e. do their port numbers match?

    @param clientInfo: the args for connectTCP
    @type clientInfo: L{tuple}

    @param serverInfo: the args for listenTCP
    @type serverInfo: L{tuple}

    @return: If they do match, return factories for the client and server that
        should connect; otherwise return L{None}, indicating they shouldn't be
        connected.
    @rtype: L{None} or 2-L{tuple} of (L{ClientFactory},
        L{IProtocolFactory})
    Nr)�
clientInfo�
serverInfoZ
clientHostZ
clientPort�
clientFactoryZ
clientTimeoutZclientBindAddressZ
serverPort�
serverFactoryZ
serverBacklogZserverInterfacerrr�_factoriesShouldConnect�s
ruc@s4eZdZdZdd�Zd
dd�Zee��fdd�Zd	S)�ConnectionCompleterz�
    A L{ConnectionCompleter} can cause synthetic TCP connections established by
    L{MemoryReactor.connectTCP} and L{MemoryReactor.listenTCP} to succeed or
    fail.
    cCs
||_dS)z�
        Create a L{ConnectionCompleter} from a L{MemoryReactor}.

        @param memoryReactor: The reactor to attach to.
        @type memoryReactor: L{MemoryReactor}
        N)�_reactor)r�
memoryReactorrrrr�szConnectionCompleter.__init__Fc
Cs�|j}x�t|j�D]z\}}xp|jD]f}t||�}|r"|j�|�|j�|�|\}}|�d�}	|�d�}
t	|
�}t
|	�}t|
||	||�Sq"WqWdS)a�
        Complete a single TCP connection established on this
        L{ConnectionCompleter}'s L{MemoryReactor}.

        @param debug: A flag; whether to dump output from the established
            connection to stdout.
        @type debug: L{bool}

        @return: a pump for the connection, or L{None} if no connection could
            be established.
        @rtype: L{IOPump} or L{None}
        N)rw�	enumerate�
tcpClientsZ
tcpServersru�remove�
connectors�popZ
buildProtocolrZrXrl)
rr`rxZ	clientIdxrqrrZ	factoriesrsrtrWrYrirjrrr�succeedOnce�s



zConnectionCompleter.succeedOncecCs(|jj�d�d�|jj�d�|�dS)z�
        Fail a single TCP connection established on this
        L{ConnectionCompleter}'s L{MemoryReactor}.

        @param reason: the reason to provide that the connection failed.
        @type reason: L{Failure}
        r�N)rwrzr}ZclientConnectionFailedr|)r�reasonrrr�failOnceszConnectionCompleter.failOnceN)F)	r r!r"r$rr~r
rr�rrrrrv�s

rvcCs8t�}t|dd�}t|d�}|�t�t��|t|�fS)a�
    Create an endpoint that can be fired on demand.

    @param debug: A flag; whether to dump output from the established
        connection to stdout.
    @type debug: L{bool}

    @return: A client endpoint, and an object that will cause one of the
        L{Deferred}s returned by that client endpoint.
    @rtype: 2-L{tuple} of (L{IStreamClientEndpoint}, L{ConnectionCompleter})
    z0.0.0.0i�)rrr	�listenr
ZforProtocolrrv)r`ZreactorZclientEndpointZserverEndpointrrr�connectableEndpoints

r�)FT)F)+r$�
__future__rrrrSZOpenSSL.SSLrr�ImportErrorZzope.interfacerrZtwisted.internet.endpointsrr	Ztwisted.internet.protocolr
rZtwisted.internet.errorrZtwisted.python.failurer
Ztwisted.internetrrZtwisted.internet.testingrrZIAddress�objectr#Z
ITransportZ
ITLSTransportr%rXrZr[rlrprurvr�rrrr�<module>s<GL
,+<