
[XMc           @   s   d  Z  d d l Z y d d l m Z Wn e k
 r9 n Xd d l m Z m Z d d l	 m
 Z
 d d l m Z d d l m Z d d d	     YZ d
 d d     YZ d   Z d   Z d d d     YZ e e e d  Z d S(   s0   
Utilities and helpers for simulating a network
iN(   t   Error(   t
   implementst   directlyProvides(   t   Failure(   t   error(   t
   interfacest   TLSNegotiationc           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s(   | |  _  | |  _ t |  _ | |  _ d  S(   N(   t   objt   connectStatet   Falset   sentt   readyToSend(   t   selfR   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   __init__   s    			c         C   s   d |  j  f S(   Ns   TLSNegotiation(%r)(   R   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   __repr__   s    c         C   s2   |  j  j | j   s. t   | _ | j   n  d  S(   N(   R   t   iosimVerifyt   NativeOpenSSLErrort   disconnectReasont   loseConnection(   R   t   othert   tpt(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   pretendToVerify    s    (   t   __name__t
   __module__R   R   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR      s   		t   FakeTransportc           B   s  e  Z d  Z e e j e j  e j   j	 Z
 d Z d Z d Z e j d  Z d Z d Z d Z d   Z d   Z d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z  d   Z! d   Z" d   Z# e$ d  Z% d   Z& d   Z' RS(   s   
    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.
    i    s   Connection donec         C   s   g  |  _  |  j   |  _ d  S(   N(   t   streamt   _nextserialt   serial(   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR   <   s    	c         C   s,   d |  j  r d p d |  j |  j j j f S(   Ns   FakeTransport<%s,%s,%s>t   St   C(   t   isServerR   t   protocolt	   __class__R   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR   @   s    c         C   s6   |  j  d  k	 r" |  j j |  n |  j j |  d  S(   N(   t   tlst   Nonet   tlsbuft   appendR   (   R   t   data(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   writeE   s    c         C   s   |  j  r |  j  j   n  d  S(   N(   t   producert   resumeProducing(   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   _checkProducerK   s    	c         C   s)   | |  _  | |  _ | s% | j   n  d S(   s%   From abstract.FileDescriptor
        N(   R'   t   streamingProducerR(   (   R   R'   t	   streaming(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   registerProducerQ   s    		c         C   s   d  |  _ d  S(   N(   R"   R'   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   unregisterProducerY   s    c         C   s   |  j    |  j   d  S(   N(   R-   R   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   stopConsuming\   s    
c         C   s   |  j  d j |   d  S(   Nt    (   R&   t   join(   R   t   iovec(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   writeSequence`   s    c         C   s   t  |  _ d  S(   N(   t   Truet   disconnecting(   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR   c   s    c         C   s>   |  j  d  k	 r t   } n	 |  j } |  j j t |   d  S(   N(   R!   R"   R   R   R   t   connectionLostR   (   R   t   err(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   reportDisconnectf   s    	c         C   s   d S(   Nt   file(   s   files   file(    (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   getPeern   s    c         C   s   d S(   NR8   (    (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   getHostr   s    c         C   s   d  S(   N(    (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR(   v   s    c         C   s   d  S(   N(    (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   pauseProducingz   s    c         C   s   |  j    d  S(   N(   R   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   stopProducing~   s    c         C   s,   |  j  | A} t | |  |  _ g  |  _ d  S(   N(   R   R   R!   R#   (   R   t   contextFactoryt   beNormalR   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   startTLS   s    c         C   sb   |  j  } | r% g  |  _  d j |  S|  j d  k	 rZ |  j j rS t |  j _ |  j Sd  Sn d  Sd  S(   NR/   (   R   R0   R!   R"   R   R3   R
   (   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   getOutBuffer   s    		c         C   s   t  | t  r |  j d  k	 s$ t  |  j j r |  j j | |   d  |  _ |  j d  } |  _ |  j |  t	 |  t
 j  q t |  j _ n |  j j |  d  S(   N(   t
   isinstanceR   R!   R"   t   AssertionErrorR
   R   R#   R2   R   R   t   ISSLTransportR3   R   R   t   dataReceived(   R   t   buft   b(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   bufferReceived   s    	N((   R   R   t   __doc__R   R   t
   ITransportt   ITLSTransportt	   itertoolst   countt   nextR   t   closedR4   t   disconnectedR   t   ConnectionDoneR   R"   R'   R*   R!   R   R   R&   R)   R,   R-   R.   R2   R   R7   R9   R:   R(   R;   R<   R3   R?   R@   RG   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR   )   s:   	
																c         C   s   t    } t | _ |  | _ | S(   N(   R   R	   R   R   (   t   ct   ft(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   makeFakeClient   s    			c         C   s   t    } t | _ |  | _ | S(   N(   R   R3   R   R   (   t   sRR   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   makeFakeServer   s    			t   IOPumpc           B   s/   e  Z d  Z d   Z e d  Z e d  Z RS(   s   Utility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   clientt   servert   clientIOt   serverIOt   debug(   R   RW   RX   RY   RZ   R[   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR      s
    				c         C   sI   t  } x< t d  D] } |  j |  r1 t } q Pq Wd sE t d  | S(   sb   Pump until there is no more input or output.

        Returns whether any data was moved.
        i  i    s   Too long(   R	   t   ranget   pumpR3   RB   (   R   R[   t   resultt   x(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   flush   s    	c      	   C   s  |  j  s | r d GHn  |  j j   } |  j j   } |  j j   |  j j   |  j  s^ | r d GH| r{ d t |  GHn  | r d t |  GHq n  | r |  j j |  n  | r |  j j |  n  | s | r t S|  j j r1|  j j	 r1|  j  s | rd GHn  t |  j _	 t |  j _ |  j j
   t S|  j j r|  j j	 r|  j  sY| rad GHn  t |  j _	 t |  j _ |  j j
   t St S(   sO   Move data back and forth.

        Returns whether any data was moved.
        s
   -- GLUG --t   .s   C: s   S: s   * Cs   * S(   R[   RZ   R@   RY   R)   t   reprRG   R3   R4   RO   R7   R	   (   R   R[   t   sDatat   cData(    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyR]      sD    (   R   R   RH   R   R	   R`   R]   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyRV      s   	c   
      C   ss   |   } |    } | |  } | |  } | j  |  | j  |  t | | | | |  }	 |	 j   | | |	 f S(   s.   Returns a 3-tuple: (client, server, pump)
    (   t   makeConnectionRV   R`   (
   t   ServerClasst   ClientClasst   clientTransportFactoryt   serverTransportFactoryR[   RQ   RT   t   ciot   sioR]   (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   connectedServerAndClient   s    		
(    (    (    (   RH   RK   t   OpenSSL.SSLR    R   t   ImportErrort   zope.interfaceR   R   t   twisted.python.failureR   t   twisted.internetR   R   R   R   RS   RU   RV   R	   Rl   (    (    (    s6   /usr/lib/python2.7/dist-packages/twisted/test/iosim.pyt   <module>   s"   		G