U
    
W[OS  ã                   @   s>  d Z ddlmZmZ ddlZddlmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ d	d
„ ZG dd„ de
ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ de	ƒZG dd„ deƒZG dd„ deƒZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$ƒZdS )%z]
Resource limiting policies.

@seealso: See also L{twisted.protocols.htb} for rate limiting.
é    )ÚdivisionÚabsolute_importN)ÚdirectlyProvidesÚ
providedBy)ÚServerFactoryÚProtocolÚClientFactory)Úerror)ÚILoggingContext)Úlogc                 C   s,   t  |¡r| ¡ }n|jj}d|| jjf S )zY
    Compute a log prefix for a wrapper and the object it wraps.

    @rtype: C{str}
    z%s (%s))r
   r   Ú	logPrefixÚ	__class__Ú__name__)ÚwrapperÚwrappedr   © r   ú</usr/lib/python3/dist-packages/twisted/protocols/policies.pyÚ_wrappedLogPrefix   s    

r   c                   @   s„   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚProtocolWrappera…  
    Wraps protocol instances and acts as their transport as well.

    @ivar wrappedProtocol: An L{IProtocol<twisted.internet.interfaces.IProtocol>}
        provider to which L{IProtocol<twisted.internet.interfaces.IProtocol>}
        method calls onto this L{ProtocolWrapper} will be proxied.

    @ivar factory: The L{WrappingFactory} which created this
        L{ProtocolWrapper}.
    r   c                 C   s   || _ || _d S ©N)ÚwrappedProtocolÚfactory)Úselfr   r   r   r   r   Ú__init__5   s    zProtocolWrapper.__init__c                 C   s   t | | jƒS )zo
        Use a customized log prefix mentioning both the wrapped protocol and
        the current one.
        )r   r   ©r   r   r   r   r   :   s    zProtocolWrapper.logPrefixc                 C   s6   t | t|ƒƒ t | |¡ | j | ¡ | j | ¡ dS )zä
        When a connection is made, register this wrapper with its factory,
        save the real transport, and connect the wrapped protocol to this
        L{ProtocolWrapper} to intercept any transport calls it makes.
        N)r   r   r   ÚmakeConnectionr   ÚregisterProtocolr   )r   Ú	transportr   r   r   r   B   s    zProtocolWrapper.makeConnectionc                 C   s   | j  |¡ d S r   )r   Úwrite©r   Údatar   r   r   r   P   s    zProtocolWrapper.writec                 C   s   | j  |¡ d S r   )r   ÚwriteSequencer   r   r   r   r!   T   s    zProtocolWrapper.writeSequencec                 C   s   d| _ | j ¡  d S ©Né   )Údisconnectingr   ÚloseConnectionr   r   r   r   r%   X   s    zProtocolWrapper.loseConnectionc                 C   s
   | j  ¡ S r   )r   ÚgetPeerr   r   r   r   r&   ]   s    zProtocolWrapper.getPeerc                 C   s
   | j  ¡ S r   )r   ÚgetHostr   r   r   r   r'   a   s    zProtocolWrapper.getHostc                 C   s   | j  ||¡ d S r   )r   ÚregisterProducer©r   ÚproducerZ	streamingr   r   r   r(   e   s    z ProtocolWrapper.registerProducerc                 C   s   | j  ¡  d S r   )r   ÚunregisterProducerr   r   r   r   r+   i   s    z"ProtocolWrapper.unregisterProducerc                 C   s   | j  ¡  d S r   )r   ÚstopConsumingr   r   r   r   r,   m   s    zProtocolWrapper.stopConsumingc                 C   s   t | j|ƒS r   )Úgetattrr   ©r   Únamer   r   r   Ú__getattr__q   s    zProtocolWrapper.__getattr__c                 C   s   | j  |¡ d S r   )r   ÚdataReceivedr   r   r   r   r1   w   s    zProtocolWrapper.dataReceivedc                 C   s"   | j  | ¡ | j |¡ d | _d S r   )r   ÚunregisterProtocolr   ÚconnectionLost©r   Úreasonr   r   r   r3   {   s    zProtocolWrapper.connectionLostN)r   Ú
__module__Ú__qualname__Ú__doc__r$   r   r   r   r   r!   r%   r&   r'   r(   r+   r,   r0   r1   r3   r   r   r   r   r   '   s    r   c                   @   sd   e Zd ZdZeZdd„ Zdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚWrappingFactoryzE
    Wraps a factory and its protocols, and keeps track of them.
    c                 C   s   || _ i | _d S r   )ÚwrappedFactoryÚ	protocols)r   r:   r   r   r   r   Š   s    zWrappingFactory.__init__c                 C   s   t | | jƒS )zY
        Generate a log prefix mentioning both the wrapped factory and this one.
        )r   r:   r   r   r   r   r      s    zWrappingFactory.logPrefixc                 C   s   | j  ¡  t | ¡ d S r   )r:   ÚdoStartr   r   r   r   r   r<   –   s    
zWrappingFactory.doStartc                 C   s   | j  ¡  t | ¡ d S r   )r:   ÚdoStopr   r   r   r   r   r=   ›   s    
zWrappingFactory.doStopc                 C   s   | j  |¡ d S r   )r:   ÚstartedConnecting)r   Ú	connectorr   r   r   r>       s    z!WrappingFactory.startedConnectingc                 C   s   | j  ||¡ d S r   )r:   ÚclientConnectionFailed©r   r?   r5   r   r   r   r@   ¤   s    z&WrappingFactory.clientConnectionFailedc                 C   s   | j  ||¡ d S r   )r:   ÚclientConnectionLostrA   r   r   r   rB   ¨   s    z$WrappingFactory.clientConnectionLostc                 C   s   |   | | j |¡¡S r   )Úprotocolr:   ÚbuildProtocol©r   Úaddrr   r   r   rD   ¬   s    zWrappingFactory.buildProtocolc                 C   s   d| j |< dS )z8
        Called by protocol to register itself.
        r#   N©r;   ©r   Úpr   r   r   r   °   s    z WrappingFactory.registerProtocolc                 C   s   | j |= dS )z8
        Called by protocols when they go away.
        NrG   rH   r   r   r   r2   ·   s    z"WrappingFactory.unregisterProtocolN)r   r6   r7   r8   r   rC   r   r   r<   r=   r>   r@   rB   rD   r   r2   r   r   r   r   r9   ƒ   s   r9   c                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚThrottlingProtocolz,
    Protocol for L{ThrottlingFactory}.
    c                 C   s    | j  t|ƒ¡ t | |¡ d S r   )r   ÚregisterWrittenÚlenr   r   r   r   r   r   r   Æ   s    zThrottlingProtocol.writec                 C   s&   | j  ttt|ƒƒ¡ t | |¡ d S r   )r   rK   ÚsumÚmaprL   r   r!   ©r   Úseqr   r   r   r!   Ë   s    z ThrottlingProtocol.writeSequencec                 C   s    | j  t|ƒ¡ t | |¡ d S r   )r   ÚregisterReadrL   r   r1   r   r   r   r   r1   Ð   s    zThrottlingProtocol.dataReceivedc                 C   s   || _ t | ||¡ d S r   )r*   r   r(   r)   r   r   r   r(   Õ   s    z#ThrottlingProtocol.registerProducerc                 C   s   | ` t | ¡ d S r   )r*   r   r+   r   r   r   r   r+   Ú   s    z%ThrottlingProtocol.unregisterProducerc                 C   s   | j  ¡  d S r   )r   ÚpauseProducingr   r   r   r   ÚthrottleReadsß   s    z ThrottlingProtocol.throttleReadsc                 C   s   | j  ¡  d S r   )r   ÚresumeProducingr   r   r   r   ÚunthrottleReadsã   s    z"ThrottlingProtocol.unthrottleReadsc                 C   s   t | dƒr| j ¡  d S ©Nr*   )Úhasattrr*   rR   r   r   r   r   ÚthrottleWritesç   s    
z!ThrottlingProtocol.throttleWritesc                 C   s   t | dƒr| j ¡  d S rV   )rW   r*   rT   r   r   r   r   ÚunthrottleWritesì   s    
z#ThrottlingProtocol.unthrottleWritesN)r   r6   r7   r8   r   r!   r1   r(   r+   rS   rU   rX   rY   r   r   r   r   rJ   ¿   s   rJ   c                   @   s~   e Zd ZdZeZejddfdd„Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚThrottlingFactoryz‹
    Throttles bandwidth and number of connections.

    Write bandwidth will only be throttled if there is a producer
    registered.
    Nc                 C   sL   t  | |¡ d| _|| _|| _|| _d| _d| _d | _d | _	d | _
d | _d S )Nr   )r9   r   ÚconnectionCountÚmaxConnectionCountÚ	readLimitÚ
writeLimitÚreadThisSecondÚwrittenThisSecondÚunthrottleReadsIDÚcheckReadBandwidthIDÚunthrottleWritesIDÚcheckWriteBandwidthID)r   r:   r\   r]   r^   r   r   r   r   ü   s    zThrottlingFactory.__init__c                 C   s   ddl m} | ||¡S ©z‹
        Wrapper around
        L{reactor.callLater<twisted.internet.interfaces.IReactorTime.callLater>}
        for test purpose.
        r   )Úreactor©Útwisted.internetrf   Ú	callLater©r   ÚperiodÚfuncrf   r   r   r   ri     s    zThrottlingFactory.callLaterc                 C   s   |  j |7  _ dS )zH
        Called by protocol to tell us more bytes were written.
        N)r`   ©r   Zlengthr   r   r   rK     s    z!ThrottlingFactory.registerWrittenc                 C   s   |  j |7  _ dS )zE
        Called by protocol to tell us more bytes were read.
        N)r_   rm   r   r   r   rQ     s    zThrottlingFactory.registerReadc                 C   sR   | j | jkr8|  ¡  t| j ƒ| j d }|  || j¡| _d| _ |  d| j¡| _dS )z:
        Checks if we've passed bandwidth limits.
        ç      ð?r   r#   N)	r_   r]   rS   Úfloatri   rU   ra   ÚcheckReadBandwidthrb   ©r   ZthrottleTimer   r   r   rp   #  s    ÿz$ThrottlingFactory.checkReadBandwidthc                 C   sR   | j | jkr8|  ¡  t| j ƒ| j d }|  || j¡| _d| _ |  d| j¡| _d S )Nrn   r   r#   )	r`   r^   rX   ro   ri   rY   rc   ÚcheckWriteBandwidthrd   rq   r   r   r   rr   0  s    ÿz%ThrottlingFactory.checkWriteBandwidthc                 C   s*   t  d|  ¡ | j ¡ D ]}| ¡  qdS )z2
        Throttle reads on all protocols.
        zThrottling reads on %sN)r   Úmsgr;   ÚkeysrS   rH   r   r   r   rS   ;  s    zThrottlingFactory.throttleReadsc                 C   s0   d| _ t d|  ¡ | j ¡ D ]}| ¡  qdS )z9
        Stop throttling reads on all protocols.
        NzStopped throttling reads on %s)ra   r   rs   r;   rt   rU   rH   r   r   r   rU   D  s    z!ThrottlingFactory.unthrottleReadsc                 C   s*   t  d|  ¡ | j ¡ D ]}| ¡  qdS )z3
        Throttle writes on all protocols.
        zThrottling writes on %sN)r   rs   r;   rt   rX   rH   r   r   r   rX   N  s    z ThrottlingFactory.throttleWritesc                 C   s0   d| _ t d|  ¡ | j ¡ D ]}| ¡  qdS )z:
        Stop throttling writes on all protocols.
        NzStopped throttling writes on %s)rc   r   rs   r;   rt   rY   rH   r   r   r   rY   W  s    z"ThrottlingFactory.unthrottleWritesc                 C   sf   | j dkr.| jd k	r|  ¡  | jd k	r.|  ¡  | j | jk rT|  j d7  _ t | |¡S t 	d¡ d S d S )Nr   r#   zMax connection count reached!)
r[   r]   rp   r^   rr   r\   r9   rD   r   rs   rE   r   r   r   rD   a  s    



zThrottlingFactory.buildProtocolc                 C   sx   t  | |¡ |  jd8  _| jdkrt| jd k	r8| j ¡  | jd k	rL| j ¡  | jd k	r`| j ¡  | jd k	rt| j ¡  d S ©Nr#   r   )r9   r2   r[   ra   Úcancelrb   rc   rd   rH   r   r   r   r2   p  s    







z$ThrottlingFactory.unregisterProtocol)r   r6   r7   r8   rJ   rC   ÚsysÚmaxsizer   ri   rK   rQ   rp   rr   rS   rU   rX   rY   rD   r2   r   r   r   r   rZ   ò   s"    ÿ

	
	
rZ   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSpewingProtocolc                 C   s   t  d| ¡ t | |¡ d S )NzReceived: %r)r   rs   r   r1   r   r   r   r   r1   €  s    zSpewingProtocol.dataReceivedc                 C   s   t  d| ¡ t | |¡ d S )NzSending: %r)r   rs   r   r   r   r   r   r   r   „  s    zSpewingProtocol.writeN)r   r6   r7   r1   r   r   r   r   r   ry     s   ry   c                   @   s   e Zd ZeZdS )ÚSpewingFactoryN)r   r6   r7   ry   rC   r   r   r   r   rz   Š  s   rz   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚLimitConnectionsByPeeré   c                 C   s
   i | _ d S r   )ÚpeerConnectionsr   r   r   r   ÚstartFactory“  s    z#LimitConnectionsByPeer.startFactoryc                 C   s>   |d }| j  |d¡}|| jkr$d S |d | j |< t | |¡S )Nr   r#   )r}   ÚgetÚmaxConnectionsPerPeerr9   rD   )r   rF   ÚpeerHostr[   r   r   r   rD   –  s    
z$LimitConnectionsByPeer.buildProtocolc                 C   s8   |  ¡ d }| j|  d8  < | j| dkr4| j|= d S ru   )r&   r}   )r   rI   r   r   r   r   r2   ž  s    z)LimitConnectionsByPeer.unregisterProtocolN)r   r6   r7   r€   r~   rD   r2   r   r   r   r   r{     s   r{   c                   @   s4   e Zd ZdZdZdZdZdd„ Zdd„ Zdd	„ Z	dS )
ÚLimitTotalConnectionsFactoryaþ  
    Factory that limits the number of simultaneous connections.

    @type connectionCount: C{int}
    @ivar connectionCount: number of current connections.
    @type connectionLimit: C{int} or L{None}
    @cvar connectionLimit: maximum number of connections.
    @type overflowProtocol: L{Protocol} or L{None}
    @cvar overflowProtocol: Protocol to use for new connections when
        connectionLimit is exceeded.  If L{None} (the default value), excess
        connections will be closed immediately.
    r   Nc                 C   sX   | j d ks| j| j k r |  ¡ }n| jd kr.d S |  ¡ }| |_t| |ƒ}|  jd7  _|S r"   )ÚconnectionLimitr[   rC   ÚoverflowProtocolr   r   )r   rF   r   rC   r   r   r   rD   ¶  s    

ÿ


z*LimitTotalConnectionsFactory.buildProtocolc                 C   s   d S r   r   rH   r   r   r   r   Ç  s    z-LimitTotalConnectionsFactory.registerProtocolc                 C   s   |  j d8  _ d S r"   )r[   rH   r   r   r   r2   Ê  s    z/LimitTotalConnectionsFactory.unregisterProtocol)
r   r6   r7   r8   r[   rƒ   r„   rD   r   r2   r   r   r   r   r‚   ¥  s   r‚   c                   @   sZ   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚTimeoutProtocolzN
    Protocol that automatically disconnects when the connection is idle.
    c                 C   s(   t  | ||¡ d| _d| _|  |¡ dS )zé
        Constructor.

        @param factory: An L{TimeoutFactory}.
        @param wrappedProtocol: A L{Protocol} to wrapp.
        @param timeoutPeriod: Number of seconds to wait for activity before
            timing out.
        N)r   r   ÚtimeoutCallÚtimeoutPeriodÚ
setTimeout)r   r   r   r‡   r   r   r   r   Ô  s    	zTimeoutProtocol.__init__Nc                 C   s.   |   ¡  || _|dk	r*| j | j| j¡| _dS )zÊ
        Set a timeout.

        This will cancel any existing timeouts.

        @param timeoutPeriod: If not L{None}, change the timeout period.
            Otherwise, use the existing value.
        N)ÚcancelTimeoutr‡   r   ri   ÚtimeoutFuncr†   )r   r‡   r   r   r   rˆ   ã  s    	zTimeoutProtocol.setTimeoutc              	   C   sB   d| _ | jr>z| j ¡  W n tjtjfk
r6   Y nX d| _dS )zg
        Cancel the timeout.

        If the timeout was already cancelled, this does nothing.
        N)r‡   r†   rv   r	   ÚAlreadyCalledÚAlreadyCancelledr   r   r   r   r‰   ò  s    zTimeoutProtocol.cancelTimeoutc                 C   s   | j r| j  | j¡ dS )zQ
        Reset the timeout, usually because some activity just happened.
        N)r†   Úresetr‡   r   r   r   r   ÚresetTimeout  s    zTimeoutProtocol.resetTimeoutc                 C   s   |   ¡  t | |¡ d S r   )rŽ   r   r   r   r   r   r   r   	  s    zTimeoutProtocol.writec                 C   s   |   ¡  t | |¡ d S r   )rŽ   r   r!   rO   r   r   r   r!     s    zTimeoutProtocol.writeSequencec                 C   s   |   ¡  t | |¡ d S r   )rŽ   r   r1   r   r   r   r   r1     s    zTimeoutProtocol.dataReceivedc                 C   s   |   ¡  t | |¡ d S r   )r‰   r   r3   r4   r   r   r   r3     s    zTimeoutProtocol.connectionLostc                 C   s   |   ¡  dS )z³
        This method is called when the timeout is triggered.

        By default it calls I{loseConnection}.  Override this if you want
        something else to happen.
        N)r%   r   r   r   r   rŠ     s    zTimeoutProtocol.timeoutFunc)N)r   r6   r7   r8   r   rˆ   r‰   rŽ   r   r!   r1   r3   rŠ   r   r   r   r   r…   Ï  s   
r…   c                   @   s.   e Zd ZdZeZd
dd„Zdd„ Zdd„ Zd	S )ÚTimeoutFactoryz%
    Factory for TimeoutWrapper.
    é  c                 C   s   || _ t | |¡ d S r   )r‡   r9   r   )r   r:   r‡   r   r   r   r   /  s    zTimeoutFactory.__init__c                 C   s   | j | | j |¡| jdS )N)r‡   )rC   r:   rD   r‡   rE   r   r   r   rD   4  s    ÿzTimeoutFactory.buildProtocolc                 C   s   ddl m} | ||¡S re   rg   rj   r   r   r   ri   9  s    zTimeoutFactory.callLaterN)r   )	r   r6   r7   r8   r…   rC   r   rD   ri   r   r   r   r   r   (  s
   
r   c                   @   sV   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚTrafficLoggingProtocolNr   c                 C   s$   t  | ||¡ || _|| _|| _dS )a  
        @param factory: factory which created this protocol.
        @type factory: L{protocol.Factory}.
        @param wrappedProtocol: the underlying protocol.
        @type wrappedProtocol: C{protocol.Protocol}.
        @param logfile: file opened for writing used to write log messages.
        @type logfile: C{file}
        @param lengthLimit: maximum size of the datareceived logged.
        @type lengthLimit: C{int}
        @param number: identifier of the connection.
        @type number: C{int}.
        N)r   r   ÚlogfileÚlengthLimitÚ_number)r   r   r   r’   r“   Znumberr   r   r   r   F  s    zTrafficLoggingProtocol.__init__c                 C   s   | j  |d ¡ | j  ¡  d S )NÚ
)r’   r   Úflush)r   Úliner   r   r   Ú_logZ  s    zTrafficLoggingProtocol._logc                 C   s.   | j r*t|ƒ| j kr*|d | j d … d }|S )Né   z<... elided>)r“   rL   r   r   r   r   Ú
_mungeData_  s    z!TrafficLoggingProtocol._mungeDatac                 C   s   |   d¡ t | ¡S )NÚ*)r˜   r   ÚconnectionMader   r   r   r   rœ   f  s    
z%TrafficLoggingProtocol.connectionMadec                 C   s&   |   d| j|  |¡f ¡ t | |¡S ©NzC %d: %r)r˜   r”   rš   r   r1   r   r   r   r   r1   k  s    z#TrafficLoggingProtocol.dataReceivedc                 C   s    |   d| j|f ¡ t | |¡S r   )r˜   r”   r   r3   r4   r   r   r   r3   p  s    z%TrafficLoggingProtocol.connectionLostc                 C   s&   |   d| j|  |¡f ¡ t | |¡S )NzS %d: %r)r˜   r”   rš   r   r   r   r   r   r   r   v  s    zTrafficLoggingProtocol.writec                    s.   ˆ   dˆ j‡ fdd„|D ƒf ¡ t ˆ |¡S )Nz	SV %d: %rc                    s   g | ]}ˆ   |¡‘qS r   )rš   )Ú.0Údr   r   r   Ú
<listcomp>|  s     z8TrafficLoggingProtocol.writeSequence.<locals>.<listcomp>)r˜   r”   r   r!   )r   Ziovecr   r   r   r!   {  s    "z$TrafficLoggingProtocol.writeSequencec                 C   s   |   d| jf ¡ t | ¡S )NzS %d: *)r˜   r”   r   r%   r   r   r   r   r%   €  s    z%TrafficLoggingProtocol.loseConnection)Nr   )r   r6   r7   r   r˜   rš   rœ   r1   r3   r   r!   r%   r   r   r   r   r‘   D  s     ÿ
r‘   c                   @   s6   e Zd ZeZdZddd„Zdd„ Zdd„ Zd	d
„ Z	dS )ÚTrafficLoggingFactoryr   Nc                 C   s   || _ || _t | |¡ d S r   )ÚlogfilePrefixr“   r9   r   )r   r:   r¢   r“   r   r   r   r   ‹  s    zTrafficLoggingFactory.__init__c                 C   s
   t |dƒS )NÚw)Úopenr.   r   r   r   r¤   ‘  s    zTrafficLoggingFactory.openc                 C   sF   |  j d7  _ |  | jd t| j ƒ ¡}|  | | j |¡|| j| j ¡S )Nr#   ú-)Ú_counterr¤   r¢   ÚstrrC   r:   rD   r“   )r   rF   r’   r   r   r   rD   •  s      ÿz#TrafficLoggingFactory.buildProtocolc                 C   s
   d| _ dS )zN
        Reset the value of the counter used to identify connections.
        r   N)r¦   r   r   r   r   ÚresetCounterœ  s    z"TrafficLoggingFactory.resetCounter)N)
r   r6   r7   r‘   rC   r¦   r   r¤   rD   r¨   r   r   r   r   r¡   †  s   
r¡   c                   @   s@   e Zd ZdZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )ÚTimeoutMixinaL  
    Mixin for protocols which wish to timeout connections.

    Protocols that mix this in have a single timeout, set using L{setTimeout}.
    When the timeout is hit, L{timeoutConnection} is called, which, by
    default, closes the connection.

    @cvar timeOut: The number of seconds after which to timeout the connection.
    Nc                 C   s   ddl m} | ||¡S re   rg   rj   r   r   r   ri   ²  s    zTimeoutMixin.callLaterc                 C   s&   | j dk	r"| jdk	r"| j  | j¡ dS )a³  
        Reset the timeout count down.

        If the connection has already timed out, then do nothing.  If the
        timeout has been cancelled (probably using C{setTimeout(None)}), also
        do nothing.

        It's often a good idea to call this when the protocol has received
        some meaningful input from the other end of the connection.  "I've got
        some data, they're still there, reset the timeout".
        N)Ú_TimeoutMixin__timeoutCallÚtimeOutr   r   r   r   r   rŽ   ¼  s    zTimeoutMixin.resetTimeoutc              	   C   s|   | j }|| _ | jdk	r`|dkrRz| j ¡  W n tjtjfk
rH   Y nX d| _qx| j |¡ n|dk	rx|  || j¡| _|S )zÈ
        Change the timeout period

        @type period: C{int} or L{None}
        @param period: The period, in seconds, to change the timeout to, or
        L{None} to disable the timeout.
        N)	r«   rª   rv   r	   rŒ   r‹   r   ri   Ú_TimeoutMixin__timedOut)r   rk   Úprevr   r   r   rˆ   Ë  s    
zTimeoutMixin.setTimeoutc                 C   s   d | _ |  ¡  d S r   )rª   ÚtimeoutConnectionr   r   r   r   Z
__timedOutå  s    zTimeoutMixin.__timedOutc                 C   s   | j  ¡  dS )z€
        Called when the connection times out.

        Override to define behavior other than dropping the connection.
        N)r   r%   r   r   r   r   r®   é  s    zTimeoutMixin.timeoutConnection)r   r6   r7   r8   r«   rª   ri   rŽ   rˆ   r¬   r®   r   r   r   r   r©   ¤  s   	
r©   ) r8   Z
__future__r   r   rw   Zzope.interfacer   r   Ztwisted.internet.protocolr   r   r   rh   r	   Ztwisted.internet.interfacesr
   Ztwisted.pythonr   r   r   r9   rJ   rZ   ry   rz   r{   r‚   r…   r   r‘   r¡   r©   r   r   r   r   Ú<module>   s,   \<3 *YB