Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/protocols/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyc


0Nc@sdZddlZddlZddlmZmZddlmZmZm	Z	ddl
mZddlm
Z
ddlmZdZd	efd
YZde	fdYZd
efdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefdYZdefd YZd!d#d"YZdS($s]
Resource limiting policies.

@seealso: See also L{twisted.protocols.htb} for rate limiting.
iN(tdirectlyProvidest
providedBy(t
ServerFactorytProtocolt
ClientFactory(terror(tILoggingContext(tlogcCs>tj|r|j}n|jj}d||jjfS(sY
    Compute a log prefix for a wrapper and the object it wraps.

    @rtype: C{str}
    s%s (%s)(RRt	logPrefixt	__class__t__name__(twrappertwrappedR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt_wrappedLogPrefixstProtocolWrappercBseZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZdZdZRS(s
    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}.
    icCs||_||_dS(N(twrappedProtocoltfactory(tselfRR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt__init__3s	cCst||jS(so
        Use a customized log prefix mentioning both the wrapped protocol and
        the current one.
        (R
R(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR8scCsGt|t|tj|||jj||jj|dS(s
        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(RRRtmakeConnectionRtregisterProtocolR(Rt	transport((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR@scCs|jj|dS(N(Rtwrite(Rtdata((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRNscCs|jj|dS(N(Rt
writeSequence(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRRscCsd|_|jjdS(Ni(t
disconnectingRtloseConnection(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRVs	cCs
|jjS(N(RtgetPeer(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRcCs
|jjS(N(RtgetHost(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR_scCs|jj||dS(N(RtregisterProducer(Rtproducert	streaming((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRcscCs|jjdS(N(RtunregisterProducer(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR gscCs|jjdS(N(Rt
stopConsuming(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR!kscCst|j|S(N(tgetattrR(Rtname((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt__getattr__oscCs|jj|dS(N(RtdataReceived(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR%uscCs$|jj||jj|dS(N(RtunregisterProtocolRtconnectionLost(Rtreason((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR'ys(R
t
__module__t__doc__RRRRRRRRRRR R!R$R%R'(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR%s 
													tWrappingFactorycBsneZdZeZdZdZdZdZdZ	dZ
dZdZd	Z
d
ZRS(sE
    Wraps a factory and its protocols, and keeps track of them.
    cCs||_i|_dS(N(twrappedFactoryt	protocols(RR,((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs	cCst||jS(sY
        Generate a log prefix mentioning both the wrapped factory and this one.
        (R
R,(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRscCs|jjtj|dS(N(R,tdoStartR(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR.s
cCs|jjtj|dS(N(R,tdoStopR(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR/s
cCs|jj|dS(N(R,tstartedConnecting(Rt	connector((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR0scCs|jj||dS(N(R,tclientConnectionFailed(RR1R(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR2scCs|jj||dS(N(R,tclientConnectionLost(RR1R(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR3scCs|j||jj|S(N(tprotocolR,t
buildProtocol(Rtaddr((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR5scCsd|j|<dS(s8
        Called by protocol to register itself.
        iN(R-(Rtp((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRscCs|j|=dS(s8
        Called by protocols when they go away.
        N(R-(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR&s(R
R)R*RR4RRR.R/R0R2R3R5RR&(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR+s									tThrottlingProtocolcBs_eZdZdZdZdZdZdZdZdZ	dZ
d	ZRS(
sProtocol for ThrottlingFactory.cCs*|jjt|tj||dS(N(RtregisterWrittentlenRR(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRscCs9|jjttjtt|tj||dS(N(	RR9treducetoperatortaddtmapR:RR(Rtseq((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs%cCs*|jjt|tj||dS(N(RtregisterReadR:RR%(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR%scCs ||_tj|||dS(N(RRR(RRR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs	cCs|`tj|dS(N(RRR (R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR scCs|jjdS(N(RtpauseProducing(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt
throttleReadsscCs|jjdS(N(RtresumeProducing(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytunthrottleReadsscCs#t|dr|jjndS(NR(thasattrRRA(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytthrottleWritesscCs#t|dr|jjndS(NR(RERRC(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytunthrottleWritess(R
R)R*RRR%RR RBRDRFRG(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR8s								tThrottlingFactorycBseZdZeZejd
d
dZdZ	dZ
dZdZdZ
dZdZd	Zd
ZdZdZRS(s
    Throttles bandwidth and number of connections.

    Write bandwidth will only be throttled if there is a producer
    registered.
    cCsntj||d|_||_||_||_d|_d|_d|_	d|_
d|_d|_dS(Ni(
R+RtconnectionCounttmaxConnectionCountt	readLimitt
writeLimittreadThisSecondtwrittenThisSecondtNonetunthrottleReadsIDtcheckReadBandwidthIDtunthrottleWritesIDtcheckWriteBandwidthID(RR,RJRKRL((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs									cCs ddlm}|j||S(sG
        Wrapper around L{reactor.callLater} for test purpose.
        i(treactor(ttwisted.internetRTt	callLater(RtperiodtfuncRT((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRVscCs|j|7_dS(sH
        Called by protocol to tell us more bytes were written.
        N(RN(Rtlength((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR9scCs|j|7_dS(sE
        Called by protocol to tell us more bytes were read.
        N(RM(RRY((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR@scCsv|j|jkrQ|jt|j|jd}|j||j|_nd|_|jd|j|_dS(s:
        Checks if we've passed bandwidth limits.
        g?iiN(	RMRKRBtfloatRVRDRPtcheckReadBandwidthRQ(RtthrottleTime((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR[s
		cCsv|j|jkrQ|jt|j|jd}|j||j|_nd|_|jd|j|_dS(Ng?ii(	RNRLRFRZRVRGRRtcheckWriteBandwidthRS(RR\((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR] s
		cCs9tjd|x!|jjD]}|jq!WdS(s2
        Throttle reads on all protocols.
        sThrottling reads on %sN(RtmsgR-tkeysRB(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRB+scCsBd|_tjd|x!|jjD]}|jq*WdS(s9
        Stop throttling reads on all protocols.
        sStopped throttling reads on %sN(RORPRR^R-R_RD(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRD4s	cCs9tjd|x!|jjD]}|jq!WdS(s3
        Throttle writes on all protocols.
        sThrottling writes on %sN(RR^R-R_RF(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRF>scCsBd|_tjd|x!|jjD]}|jq*WdS(s:
        Stop throttling writes on all protocols.
        sStopped throttling writes on %sN(RORRRR^R-R_RG(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRGGs	cCs|jdkrJ|jdk	r+|jn|jdk	rJ|jqJn|j|jkr{|jd7_tj||St	j
ddSdS(NiisMax connection count reached!(RIRKROR[RLR]RJR+R5RR^(RR6((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR5Qs

cCstj|||jd8_|jdkr|jdk	rM|jjn|jdk	rl|jjn|jdk	r|jjn|jdk	r|jjqndS(Nii(	R+R&RIRPROtcancelRQRRRS(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR&`sN(R
R)R*R8R4tsystmaxintRORRVR9R@R[R]RBRDRFRGR5R&(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRHs				
				
			
	tSpewingProtocolcBseZdZdZRS(cCs%tjd|tj||dS(NsReceived: %r(RR^RR%(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR%pscCs%tjd|tj||dS(NsSending: %r(RR^RR(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRts(R
R)R%R(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRcos	tSpewingFactorycBseZeZRS((R
R)RcR4(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRdzstLimitConnectionsByPeercBs)eZdZdZdZdZRS(icCs
i|_dS(N(tpeerConnections(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytstartFactoryscCsS|d}|jj|d}||jkr2dS|d|j|<tj||S(Nii(RftgettmaxConnectionsPerPeerROR+R5(RR6tpeerHostRI((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR5s
cCsG|jd}|j|cd8<|j|dkrC|j|=ndS(Nii(RRf(RR7Rj((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR&s(R
R)RiRgR5R&(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRes		tLimitTotalConnectionsFactorycBs;eZdZdZdZdZdZdZdZ	RS(s
    Factory that limits the number of simultaneous connections.

    @type connectionCount: C{int}
    @ivar connectionCount: number of current connections.
    @type connectionLimit: C{int} or C{None}
    @cvar connectionLimit: maximum number of connections.
    @type overflowProtocol: L{Protocol} or C{None}
    @cvar overflowProtocol: Protocol to use for new connections when
        connectionLimit is exceeded.  If C{None} (the default value), excess
        connections will be closed immediately.
    icCsz|jdks!|j|jkr0|j}n|jdkrCdS|j}||_t||}|jd7_|S(Ni(tconnectionLimitRORIR4toverflowProtocolRR(RR6RR4((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR5s	cCsdS(N((RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRscCs|jd8_dS(Ni(RI(RR7((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR&sN(
R
R)R*RIRORlRmR5RR&(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRks		tTimeoutProtocolcBsbeZdZdZd
dZdZdZdZdZ	dZ
dZd	ZRS(sN
    Protocol that automatically disconnects when the connection is idle.
    cCs-tj|||d|_|j|dS(s
        Constructor.

        @param factory: An L{IFactory}.
        @param wrappedProtocol: A L{Protocol} to wrapp.
        @param timeoutPeriod: Number of seconds to wait for activity before
            timing out.
        N(RRROttimeoutCallt
setTimeout(RRRt
timeoutPeriod((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs		cCsD|j|dk	r"||_n|jj|j|j|_dS(s
        Set a timeout.

        This will cancel any existing timeouts.

        @param timeoutPeriod: If not C{None}, change the timeout period.
            Otherwise, use the existing value.
        N(t
cancelTimeoutRORqRRVttimeoutFuncRo(RRq((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRps	
cCsA|jr=y|jjWntjk
r0nXd|_ndS(sg
        Cancel the timeout.

        If the timeout was already cancelled, this does nothing.
        N(RoR`Rt
AlreadyCalledRO(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRrs	cCs#|jr|jj|jndS(sQ
        Reset the timeout, usually because some activity just happened.
        N(RotresetRq(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytresetTimeouts	cCs|jtj||dS(N(RvRR(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs
cCs|jtj||dS(N(RvRR(RR?((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs
cCs|jtj||dS(N(RvRR%(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR%s
cCs|jtj||dS(N(RrRR'(RR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR's
cCs|jdS(s
        This method is called when the timeout is triggered.

        By default it calls L{loseConnection}.  Override this if you want
        something else to happen.
        N(R(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRssN(
R
R)R*RRORpRrRvRRR%R'Rs(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRns							tTimeoutFactorycBs2eZdZeZddZdZdZRS(s%
    Factory for TimeoutWrapper.
    ii<cCs||_tj||dS(N(RqR+R(RR,Rq((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs	cCs%|j||jj|d|jS(NRq(R4R,R5Rq(RR6((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR5"scCs ddlm}|j||S(sG
        Wrapper around L{reactor.callLater} for test purpose.
        i(RT(RURTRV(RRWRXRT((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRV'si(R
R)R*RnR4RR5RV(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRws
	tTrafficLoggingProtocolcBs_eZd
ddZdZdZdZdZdZdZ	dZ
d	ZRS(icCs2tj|||||_||_||_dS(s
        @param factory: factory which created this protocol.
        @type factory: C{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(RRtlogfiletlengthLimitt_number(RRRRyRztnumber((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR2s		cCs%|jj|d|jjdS(Ns
(RyRtflush(Rtline((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt_logFscCs:|jr6t||jkr6||jd d}n|S(Nis<... elided>(RzR:(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt
_mungeDataKscCs|jdtj|S(Nt*(RRtconnectionMade(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRRs
cCs3|jd|j|j|ftj||S(NsC %d: %r(RR{RRR%(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR%Ws#cCs*|jd|j|ftj||S(NsC %d: %r(RR{RR'(RR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR'\scCs3|jd|j|j|ftj||S(NsS %d: %r(RR{RRR(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRbs#cCsF|jd|jg|D]}|j|^qftj||S(Ns	SV %d: %r(RR{RRR(Rtiovectd((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRgs6cCs$|jd|jftj|S(NsS %d: *(RR{RR(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRlsN(R
R)RORRRRR%R'RRR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRx0s							tTrafficLoggingFactorycBs;eZeZdZddZdZdZdZ	RS(icCs&||_||_tj||dS(N(t
logfilePrefixRzR+R(RR,RRz((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRws		cCs
t|dS(Ntw(tfile(RR#((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytopen}scCs]|jd7_|j|jdt|j}|j||jj|||j|jS(Nit-(t_counterRRtstrR4R,R5Rz(RR6Ry((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyR5s#cCs
d|_dS(sN
        Reset the value of the counter used to identify connections.
        iN(R(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pytresetCountersN(
R
R)RxR4RRORRR5R(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRrs		tTimeoutMixincBsGeZdZdZdZdZdZdZdZ	dZ
RS(sL
    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.
    cCs ddlm}|j||S(sG
        Wrapper around L{reactor.callLater} for test purpose.
        i(RT(RURTRV(RRWRXRT((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRVscCs8|jdk	r4|jdk	r4|jj|jndS(s
        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(t_TimeoutMixin__timeoutCallROttimeOutRu(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRvscCs|j}||_|jdk	rY|dkrF|jjd|_q|jj|n'|dk	r|j||j|_n|S(s
        Change the timeout period

        @type period: C{int} or C{NoneType}
        @param period: The period, in seconds, to change the timeout to, or
        C{None} to disable the timeout.
        N(RRROR`RuRVt_TimeoutMixin__timedOut(RRWtprev((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRps		
cCsd|_|jdS(N(RORttimeoutConnection(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt
__timedOuts	cCs|jjdS(s
        Called when the connection times out.

        Override to define behavior other than dropping the connection.
        N(RR(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRsN(R
R)R*RORRRVRvRpRR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyRs					((R*RaR<tzope.interfaceRRttwisted.internet.protocolRRRRURttwisted.internet.interfacesRttwisted.pythonRR
RR+R8RHRcRdReRkRnRwRxRR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/policies.pyt<module>	s(	Z<)*WB