ó
[³XMc           @   sS   d  Z  d d l m Z d d l m Z d d	 d „  ƒ  YZ d e f d „  ƒ  YZ d S(
   s   
Producer-Consumer Proxy.
iÿÿÿÿ(   t
   implements(   t
   interfacest   BasicProducerConsumerProxyc           B   sœ   e  Z d  Z e e j e j ƒ d
 Z d
 Z	 d
 Z
 e Z e Z e Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z RS(   sa  
    I can act as a man in the middle between any Producer and Consumer.

    @ivar producer: the Producer I subscribe to.
    @type producer: L{IProducer<interfaces.IProducer>}
    @ivar consumer: the Consumer I publish to.
    @type consumer: L{IConsumer<interfaces.IConsumer>}
    @ivar paused: As a Producer, am I paused?
    @type paused: bool
    c         C   s8   g  |  _  | d  k	 r4 | |  _ | j |  |  j ƒ n  d  S(   N(   t   _buffert   Nonet   consumert   registerProducert   iAmStreaming(   t   selfR   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyt   __init__#   s    		c         C   s&   t  |  _ |  j r" |  j j ƒ  n  d  S(   N(   t   Truet   pausedt   producert   pauseProducing(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   +   s    		c         C   ss   t  |  _ |  j r; |  j j d j |  j ƒ ƒ g  |  j (n |  j sP t |  _ n  |  j	 d  k	 ro |  j	 j ƒ  n  d  S(   Nt    (   t   FalseR   R   R   t   writet   joinR   R
   t   outstandingPullR   R   t   resumeProducing(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   0   s    			c         C   s;   |  j  d  k	 r |  j  j ƒ  n  |  j d  k	 r7 |  ` n  d  S(   N(   R   R   t   stopProducingR   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   =   s    c         C   s_   |  j  s |  j r0 |  j r0 |  j j | ƒ n+ |  j d  k	 r[ |  j j | ƒ t |  _ n  d  S(   N(	   R   R   R   R   t   appendR   R   R   R   (   R   t   data(    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   E   s
    c         C   s-   |  j  d  k	 r |  j  j ƒ  n  |  j ƒ  d  S(   N(   R   R   t   finisht   unregisterProducer(   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   N   s    c         C   s   | |  _  | |  _ d  S(   N(   R   t   producerIsStreaming(   R   R   t	   streaming(    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   S   s    	c         C   s;   |  j  d  k	 r |  `  |  ` n  |  j r7 |  j j ƒ  n  d  S(   N(   R   R   R   R   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   W   s
    		c         C   s   d |  j  t |  ƒ |  j f S(   Ns   <%s@%x around %s>(   t	   __class__t   idR   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyt   __repr__^   s    N(   t   __name__t
   __module__t   __doc__R    R   t	   IProducert	   IConsumerR   R   R   R   R
   R   R   R   R   t   stoppedR	   R   R   R   R   R   R   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR      s$   
									t   ProducerConsumerProxyc           B   s^   e  Z d  Z d d d Z e Z e Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(	   s˜   ProducerConsumerProxy with a finite buffer.

    When my buffer fills up, I have my parent Producer pause until my buffer
    has room in it again.
    i   c         C   s   t  |  _ d  S(   N(   R
   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   n   s    c         C   s[  t  |  _ |  j r d j |  j ƒ } |  j | ƒ } | t | ƒ k  rr | | } |  j sb t d ‚ | g |  j (q… g  |  j (n d } |  j r½ | r½ |  j r½ |  j	 d  k	 r½ |  j	 j ƒ  n  |  j sÓ | |  _ n  |  j d  k	 rWt g  |  j D] } t | ƒ ^ qï ƒ } |  j r;| |  j k  r;t  |  _ |  j j ƒ  qW|  j rW|  j j ƒ  qWn  d  S(   NR   s.   Streaming producer did not write all its data.i    (   R   R   R   R   t   _writeSomeDatat   lenR   t   AssertionErrort   unregisteredR   R   R   R   R   t   sumt   producerPausedt
   bufferSizeR   (   R   R   t	   bytesSentt   unsentt   st   bytesBuffered(    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   s   s.    		
	(		c         C   s  |  j  s |  j r0 |  j r0 |  j j | ƒ ny |  j d  k	 r© |  j sR t d ‚ |  j | ƒ } t	 |  _ | t
 | ƒ k s© |  j s t d ‚ |  j j | | ƒ q© n  |  j d  k	 r|  j rt g  |  j D] } t
 | ƒ ^ qÎ ƒ } | |  j k r|  j j ƒ  t |  _ qn  d  S(   Ns9   Writing fresh data to consumer before my buffer is empty!s.   Streaming producer did not write all its data.(   R   R   R   R   R   R   R   R'   R%   R   R&   R   R   R)   R+   R   R
   R*   (   R   R   R,   R.   R/   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   ›   s     	(c         C   s3   t  |  _ t j |  | | ƒ | s/ | j ƒ  n  d  S(   N(   R   R(   R   R   R   (   R   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   ¶   s    	c         C   sN   |  j  d  k	 r |  `  |  ` n  t |  _ |  j rJ |  j rJ |  j j ƒ  n  d  S(   N(   R   R   R   R
   R(   R   R   R   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR   ¼   s    		c         C   s-   |  j  d k r d S|  j  j | ƒ t | ƒ S(   s`   Write as much of this data as possible.

        @returns: The number of bytes written.
        i    N(   R   R   R   R&   (   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR%   Ä   s    i   (   R   R   R    R+   R   R*   R(   R   R   R   R   R   R%   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyR$   b   s   		(			N(    (   R    t   zope.interfaceR    t   twisted.internetR   R   R$   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/protocols/pcp.pyt   <module>   s   T