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/htb.pyc


[XMc@sdZddlmZddd!ZddlmZddlmZmZddlm	Z	d	dd
YZ
defdYZd
ddYZdefdYZ
defdYZde	jfdYZdefdYZdddYZdS(sHierarchical Token Bucket traffic shaping.

Patterned after U{Martin Devera's Hierarchical Token Bucket traffic
shaper for the Linux kernel<http://luxik.cdi.cz/~devik/qos/htb/>}.

@seealso: U{HTB Linux queuing discipline manual - user guide
  <http://luxik.cdi.cz/~devik/qos/htb/manual/userg.htm>}
@seealso: U{Token Bucket Filter in Linux Advanced Routing & Traffic Control
    HOWTO<http://lartc.org/howto/lartc.qdisc.classless.html#AEN682>}
@author: Kevin Turner
i(t
nested_scopess$Revision: 1.5 $ii(ttime(t
implementst	Interface(tpcptBucketcBs>eZdZdZdZdZddZdZdZ	RS(snToken bucket, or something like it.

    I can hold up to a certain number of tokens, and I drain over time.

    @cvar maxburst: Size of the bucket, in bytes.  If None, the bucket is
        never full.
    @type maxburst: int
    @cvar rate: Rate the bucket drains, in bytes per second.  If None,
        the bucket drains instantaneously.
    @type rate: int
    icCs"d|_||_t|_dS(Ni(tcontenttparentBucketRtlastDrip(tselfR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyt__init__3s		cCsr|j|jdkr"|}nt||j|j}|jdk	r_|jj|}n|j|7_|S(sAdd tokens to me.

        @param amount: A quanity of tokens to add.
        @type amount: int

        @returns: The number of tokens that fit.
        @returntype: int
        N(tdriptmaxbursttNonetminRRtadd(R	tamountt	allowable((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR8s	
	cCs|jdk	r|jjn|jdkr;d|_tSt}||j}tt	d|j||j|_||_t
SdS(sLet some of the bucket drain.

        How much of the bucket drains depends on how long it has been
        since I was last called.

        @returns: True if I am now empty.
        @returntype: bool
        iN(RR
RtrateRtTrueRRtlongtmaxtFalse(R	tnowtdeltaT((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyRLs			
&	N(
t__name__t
__module__t__doc__R
RRt	_refcountR
RR(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR!s	t
IBucketFiltercBseZdZRS(cOsdS(sNI'll give you a bucket for something.

        @returntype: L{Bucket}
        N((t
somethingstsome_kw((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pytgetBucketFords(RRR (((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyRcstHierarchicalBucketFiltercBsKeZdZeeeZdZddZ	dZ
dZdZRS(sI filter things into buckets, and I am nestable.

    @cvar bucketFactory: Class of buckets to make.
    @type bucketFactory: L{Bucket} class
    @cvar sweepInterval: Seconds between sweeping out the bucket cache.
    @type sweepInterval: int
    cCs"i|_||_t|_dS(N(tbucketstparentFilterRt	lastSweep(R	R#((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR
xs		cOs|jdk	r5t|j|jkr5|jn|jrY|jj|||}nd}|j||}|jj	|}|dkr|j
|}||j|<n|S(sYou want a bucket for that?  I'll give you a bucket.

        Any parameters are passed on to L{getBucketKey}, from them it
        decides which bucket you get.

        @returntype: L{Bucket}
        N(t
sweepIntervalR
RR$tsweepR#R tgetBucketKeyR"tgett
bucketFactory(R	tatkwRtkeytbucket((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR }s
	cOsdS(sI determine who gets which bucket.

        Unless I'm overridden, everything gets the same bucket.

        @returns: something to be used as a key in the bucket cache.
        N(R
(R	R*R+((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR'scCsXxE|jjD]4\}}|jdkr|jr|j|=qqWt|_dS(s)I throw away references to empty buckets.iN(R"titemsRRRR$(R	R,R-((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR&sN(
RRRRRRR)R
R%R
R R'R&(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR!js
			tFilterByHostcBseZdZdZdZRS(s1A bucket filter with a bucket for each host.
    i<icCs|jdS(Ni(tgetPeer(R	t	transport((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR'si(RRRR%R'(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR/stFilterByServercBseZdZdZdZRS(s4A bucket filter with a bucket for each service.
    cCs|jdS(Ni(tgetHost(R	R1((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR'sN(RRRR
R%R'(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR2stShapedConsumercBs/eZdZeZdZdZdZRS(sDI wrap a Consumer and shape the rate at which it receives data.
    cCs2tjj||||_|jjd7_dS(Ni(RtProducerConsumerProxyR
R-R(R	tconsumerR-((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR
s	cCs/|jjt|}tjj||| S(N(R-RtlenRR5t_writeSomeData(R	tdataR((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR8scCs&tjj||jjd8_dS(Ni(RR5t
stopProducingR-R(R	((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR:s(RRRRtiAmStreamingR
R8R:(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR4s
		tShapedTransportcBseZdZeZdZRS(s7I wrap a Transport and shape the rate at which it receives data.

    I am a L{ShapedConsumer} with a little bit of magic to provide for
    the case where the consumer I wrap is also a Transport and people
    will be attempting to access attributes I do not proxy as a
    Consumer (e.g. loseConnection).
    cCst|j|S(N(tgetattrR6(R	tname((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyt__getattr__s(RRRRR;R?(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR<stShapedProtocolFactorycBs eZdZdZdZRS(sI dispense Protocols with traffic shaping on their transports.

    Usage::

        myserver = SomeFactory()
        myserver.protocol = ShapedProtocolFactory(myserver.protocol,
                                                  bucketFilter)

    Where SomeServerFactory is a L{twisted.internet.protocol.Factory}, and
    bucketFilter is an instance of L{HierarchicalBucketFilter}.
    cCs||_||_dS(sTell me what to wrap and where to get buckets.

        @param protoClass: The class of Protocol I will generate
          wrapped instances of.
        @type protoClass: L{Protocol<twisted.internet.interfaces.IProtocol>}
          class
        @param bucketFilter: The filter which will determine how
          traffic is shaped.
        @type bucketFilter: L{HierarchicalBucketFilter}.
        N(tprotocoltbucketFilter(R	t
protoClassRB((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR
s
	cs:j||}|jfd}||_|S(sMake a Protocol instance with a shaped transport.

        Any parameters will be passed on to the protocol's initializer.

        @returns: a Protocol instance with a L{ShapedTransport}.
        cs+jj|}t||}|S(N(RBR R<(R1R-tshapedTransport(R	torigMakeConnection(s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pytmakeConnections(RARF(R	R*R+tprotoRF((R	REs9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyt__call__s
		(RRRR
RH(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyR@s	N((((Rt
__future__Rt__version__Rtzope.interfaceRRttwisted.protocolsRRRR!R/R2R5R4R<R@(((s9/usr/lib/python2.7/dist-packages/twisted/protocols/htb.pyt<module>s
B=