ó
    Ö›‘hÉ$  ã                   ó  • S r SSKJr  SSKJr  SSKJrJr  SSKJr   " S S5      r	 " S S	\5      r
\" \
5       " S
 S5      5       r " S S\5      r " S S\5      r " S S\R                  5      r " S S\5      r " S S5      rg)a¼  
Hierarchical 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>}
é    )Útime)ÚOptional)Ú	InterfaceÚimplementer)Úpcpc                   ó\   • \ rS rSr% SrSr\\   \S'   Sr	\\   \S'   Sr
SS jrS rS	 rS
rg)ÚBucketé   a¿  
Implementation of a Token bucket.

A bucket can hold a certain number of tokens and it drains over time.

@cvar maxburst: The maximum number of tokens that the bucket can
    hold at any given time. If this is L{None}, the bucket has
    an infinite size.
@type maxburst: C{int}
@cvar rate: The rate at which the bucket drains, in number
    of tokens per second. If the rate is L{None}, the bucket
    drains instantaneously.
@type rate: C{int}
NÚmaxburstÚrater   c                 ó<   • SU l         Xl        [        5       U l        g)zø
Create a L{Bucket} that may have a parent L{Bucket}.

@param parentBucket: If a parent Bucket is specified,
    all L{add} and L{drip} operations on this L{Bucket}
    will be applied on the parent L{Bucket} as well.
@type parentBucket: L{Bucket}
r   N)ÚcontentÚparentBucketr   ÚlastDrip)Úselfr   s     ÚYC:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/protocols/htb.pyÚ__init__ÚBucket.__init__3   s   € ð ˆŒØ(ÔÜ›ˆ�ó    c                 ó  • U R                  5         U R                  c  UnO"[        XR                  U R                  -
  5      nU R                  b  U R                  R                  U5      nU =R                  U-  sl        U$ )a4  
Adds tokens to the L{Bucket} and its C{parentBucket}.

This will add as many of the C{amount} tokens as will fit into both
this L{Bucket} and its C{parentBucket}.

@param amount: The number of tokens to try to add.
@type amount: C{int}

@returns: The number of tokens that actually fit.
@returntype: C{int}
)Údripr   Úminr   r   Úadd)r   ÚamountÚ	allowables      r   r   Ú
Bucket.add@   si   € ð 	�	‰	ŒØ�=‰=Ñ Ø‰Iä˜F§M¡M°D·L±LÑ$@ÓAˆIà×ÑÑ(Ø×)Ñ)×-Ñ-¨iÓ8ˆIØ�Š˜	Ñ!�ØÐr   c                 ó,  • U R                   b  U R                   R                  5         U R                  c  SU l        OJ[	        5       nXR
                  -
  nX R                  -  n[        SU R                  U-
  5      U l        Xl        U R                  S:H  $ )zº
Let some of the bucket drain.

The L{Bucket} drains at the rate specified by the class
variable C{rate}.

@returns: C{True} if the bucket is empty after this drip.
@returntype: C{bool}
r   )r   r   r   r   r   r   Úmax)r   ÚnowÚ	deltaTimeÚdeltaTokenss       r   r   ÚBucket.dripX   s}   € ð ×ÑÑ(Ø×Ñ×"Ñ"Ô$à�9‰9ÑØˆD�Lä“&ˆCØŸm™mÑ+ˆIØ#§i¡iÑ/ˆKÜ˜q $§,¡,°Ñ"<Ó=ˆDŒLØŒMØ�|‰|˜qÑ Ð r   )r   r   r   ©N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   ÚintÚ__annotations__r   Ú	_refcountr   r   r   Ú__static_attributes__© r   r   r	   r	      s9   ‡ ñð #€Hˆh�s‰mÓ"Ø€Dˆ(�3‰-Óà€Iôòõ0!r   r	   c                   ó   • \ rS rSrS rSrg)ÚIBucketFilterép   c                  ó   • g)zV
Return a L{Bucket} corresponding to the provided parameters.

@returntype: L{Bucket}
Nr-   )Ú
somethingsÚsome_kws     r   ÚgetBucketForÚIBucketFilter.getBucketForq   s   � r   r-   N)r$   r%   r&   r'   r4   r,   r-   r   r   r/   r/   p   s   † õr   r/   c                   óN   • \ rS rSr% Sr\rSr\\	   \
S'   S
S jrS rS rS rS	rg)ÚHierarchicalBucketFilteréy   zß
Filter things into buckets that can be nested.

@cvar bucketFactory: Class of buckets to make.
@type bucketFactory: L{Bucket}
@cvar sweepInterval: Seconds between sweeping out the bucket cache.
@type sweepInterval: C{int}
NÚsweepIntervalc                 ó<   • 0 U l         Xl        [        5       U l        g r#   )ÚbucketsÚparentFilterr   Ú	lastSweep)r   r<   s     r   r   Ú!HierarchicalBucketFilter.__init__‡   s   € ØˆŒØ(ÔÜ›ˆ�r   c                 ó�  • U R                   b5  [        5       U R                  -
  U R                   :”  a  U R                  5         U R                  (       a!  U R                  R
                  " U /UQ70 UD6nOSnU R                  " U0 UD6nU R                  R                  U5      nUc  U R                  U5      nXPR                  U'   U$ )z»
Find or create a L{Bucket} corresponding to the provided parameters.

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

@returntype: L{Bucket}
N)
r9   r   r=   Úsweepr<   r4   ÚgetBucketKeyr;   ÚgetÚbucketFactory)r   ÚaÚkwr   ÚkeyÚbuckets         r   r4   Ú%HierarchicalBucketFilter.getBucketForŒ   s¯   € ð ×ÑÑ*Ü‹V�d—n‘nÑ$¨×(:Ñ(:Ó:à�J‰JŒLà××Ø×,Ñ,×9Ò9¸$ÐIÀÒIÀbÑI‰LàˆLà×Ò Ð) bÑ)ˆØ—‘×!Ñ! #Ó&ˆØ‰>Ø×'Ñ'¨Ó5ˆFØ &�L‰L˜ÑØˆr   c                 ó   • g)zý
Construct a key based on the input parameters to choose a L{Bucket}.

The default implementation returns the same key for all
arguments. Override this method to provide L{Bucket} selection.

@returns: Something to be used as a key in the bucket cache.
Nr-   )r   rD   rE   s      r   rA   Ú%HierarchicalBucketFilter.getBucketKey¦   s   € ð r   c                 óØ   • U R                   R                  5        H=  u  pUR                  5       nUR                  S:X  d  M'  U(       d  M0  U R                   U	 M?     [	        5       U l        g)z
Remove empty buckets.
r   N)r;   Úitemsr   r+   r   r=   )r   rF   rG   Úbucket_is_emptys       r   r@   ÚHierarchicalBucketFilter.sweep±   sQ   € ð  Ÿ<™<×-Ñ-Ö/‰KˆCØ$Ÿk™k›mˆOØ× Ñ  AÕ%¯?¨?Ø—L‘L Ò%ñ 0ô
 ›ˆ�r   )r;   r=   r<   r#   )r$   r%   r&   r'   r(   r	   rC   r9   r   r)   r*   r   r4   rA   r@   r,   r-   r   r   r7   r7   y   s0   ‡ ñð €MØ#'€M�8˜C‘=Ó'ô ò
ò4	õ	 r   r7   c                   ó"   • \ rS rSrSrSrS rSrg)ÚFilterByHosté½   z>
A Hierarchical Bucket filter with a L{Bucket} for each host.
i°  c                 ó(   • UR                  5       S   $ ©Né   )ÚgetPeer©r   Ú	transports     r   rA   ÚFilterByHost.getBucketKeyÄ   ó   € Ø× Ñ Ó" 1Ñ%Ð%r   r-   N©r$   r%   r&   r'   r(   r9   rA   r,   r-   r   r   rP   rP   ½   s   † ñð €Mõ&r   rP   c                   ó"   • \ rS rSrSrSrS rSrg)ÚFilterByServeréÈ   zA
A Hierarchical Bucket filter with a L{Bucket} for each service.
Nc                 ó(   • UR                  5       S   $ )Né   )ÚgetHostrV   s     r   rA   ÚFilterByServer.getBucketKeyÏ   rY   r   r-   rZ   r-   r   r   r\   r\   È   s   † ñð €Mõ&r   r\   c                   ó.   • \ rS rSrSrSrS rS rS rSr	g)	ÚShapedConsumeréÓ   zD
Wraps a C{Consumer} and shapes the rate at which it receives data.
Fc                 óŒ   • [         R                  R                  X5        X l        U R                  =R                  S-  sl        g rS   )r   ÚProducerConsumerProxyr   rG   r+   )r   ÚconsumerrG   s      r   r   ÚShapedConsumer.__init__Ü   s0   € Ü×!Ñ!×*Ñ*¨4Ô:ØŒØ�‰×Ò Ñ"Ör   c                 óŽ   • U R                   R                  [        U5      5      n[        R                  R                  XS U 5      $ r#   )rG   r   Úlenr   rf   Ú_writeSomeData)r   Údatar   s      r   rk   ÚShapedConsumer._writeSomeDataá   s7   € ð
 —‘—‘¤ T£Ó+ˆÜ×(Ñ(×7Ñ7¸À7ÀF¸mÓLÐLr   c                 ó€   • [         R                  R                  U 5        U R                  =R                  S-  sl        g rS   )r   rf   ÚstopProducingrG   r+   )r   s    r   ro   ÚShapedConsumer.stopProducingé   s+   € Ü×!Ñ!×/Ñ/°Ô5Ø�‰×Ò Ñ"Ör   )rG   N)
r$   r%   r&   r'   r(   ÚiAmStreamingr   rk   ro   r,   r-   r   r   rc   rc   Ó   s   † ñð €Lò#ò
Mõ#r   rc   c                   ó"   • \ rS rSrSrSrS rSrg)ÚShapedTransportéî   a:  
Wraps a C{Transport} and shapes the rate at which it receives data.

This is a L{ShapedConsumer} with a little bit of magic to provide for
the case where the consumer it wraps is also a C{Transport} and people
will be attempting to access attributes this does not proxy as a
C{Consumer} (e.g. C{loseConnection}).
Fc                 ó.   • [        U R                  U5      $ r#   )Úgetattrrg   )r   Únames     r   Ú__getattr__ÚShapedTransport.__getattr__ü   s   € ô �t—}‘} dÓ+Ð+r   r-   N)r$   r%   r&   r'   r(   rq   rx   r,   r-   r   r   rs   rs   î   s   † ñð €Lõ,r   rs   c                   ó$   • \ rS rSrSrS rS rSrg)ÚShapedProtocolFactoryi  ao  
Dispense C{Protocols} with traffic shaping on their transports.

Usage::

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

Where C{SomeServerFactory} is a L{twisted.internet.protocol.Factory}, and
C{bucketFilter} is an instance of L{HierarchicalBucketFilter}.
c                 ó   • Xl         X l        g)aT  
Tell me what to wrap and where to get buckets.

@param protoClass: The class of C{Protocol} this 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)ÚprotocolÚbucketFilter)r   Ú
protoClassr~   s      r   r   ÚShapedProtocolFactory.__init__  s   € ð #ŒØ(Õr   c                 ó`   ^ ^• T R                   " U0 UD6nUR                  mUU 4S jnXCl        U$ )z´
Make a C{Protocol} instance with a shaped transport.

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

@returns: A C{Protocol} instance with a L{ShapedTransport}.
c                 ó`   >• TR                   R                  U 5      n[        X5      nT" U5      $ r#   )r~   r4   rs   )rW   rG   ÚshapedTransportÚorigMakeConnectionr   s      €€r   ÚmakeConnectionÚ6ShapedProtocolFactory.__call__.<locals>.makeConnection,  s.   ø€ Ø×&Ñ&×3Ñ3°IÓ>ˆFÜ-¨iÓ@ˆOÙ% oÓ6Ð6r   )r}   r…   )r   rD   rE   Úprotor…   r„   s   `    @r   Ú__call__ÚShapedProtocolFactory.__call__!  s6   ù€ ð —’˜qÐ' BÑ'ˆØ"×1Ñ1Ðö	7ð
  .ÔØˆr   )r~   r}   N)r$   r%   r&   r'   r(   r   rˆ   r,   r-   r   r   r{   r{     s   † ñò)õ"r   r{   N)r(   r   Útypingr   Úzope.interfacer   r   Útwisted.protocolsr   r	   r/   r7   rP   r\   rf   rc   rs   r{   r-   r   r   Ú<module>r�      s•   ðñ
õ  Ý ç 1å !÷O!ñ O!ôd�Iô ñ ˆ]Ó÷@ ð @ ó ð@ ôF&Ð+ô &ô&Ð-ô &ô#�S×.Ñ.ô #ô6,�nô ,÷(0ò 0r   