<s> import logging <EOL> import platformsettings <EOL> import re <EOL> BANDWIDTH_PATTERN = r'<STR_LIT>' <EOL> class TrafficShaperException ( Exception ) : <EOL> pass <EOL> class BandwidthValueError ( TrafficShaperException ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . value , BANDWIDTH_PATTERN ) <EOL> class TrafficShaper ( object ) : <EOL> """<STR_LIT>""" <EOL> _UPLOAD_PIPE = '<STR_LIT>' <EOL> _UPLOAD_QUEUE = '<STR_LIT>' <EOL> _UPLOAD_RULE = '<STR_LIT>' <EOL> _DOWNLOAD_PIPE = '<STR_LIT>' <EOL> _DOWNLOAD_QUEUE = '<STR_LIT>' <EOL> _DOWNLOAD_RULE = '<STR_LIT>' <EOL> _QUEUE_SLOTS = <NUM_LIT:100> <EOL> _BANDWIDTH_RE = re . compile ( BANDWIDTH_PATTERN ) <EOL> def __init__ ( self , <EOL> dont_use = None , <EOL> host = '<STR_LIT:127.0.0.1>' , <EOL> ports = None , <EOL> up_bandwidth = '<STR_LIT:0>' , <EOL> down_bandwidth = '<STR_LIT:0>' , <EOL> delay_ms = '<STR_LIT:0>' , <EOL> packet_loss_rate = '<STR_LIT:0>' , <EOL> init_cwnd = '<STR_LIT:0>' , <EOL> use_loopback = True ) : <EOL> """<STR_LIT>""" <EOL> assert dont_use is None <EOL> self . host = host <EOL> self . ports = ports <EOL> self . up_bandwidth = up_bandwidth <EOL> self . down_bandwidth = down_bandwidth <EOL> self . delay_ms = delay_ms <EOL> self . packet_loss_rate = packet_loss_rate <EOL> self . init_cwnd = init_cwnd <EOL> self . use_loopback = use_loopback <EOL> if not self . _BANDWIDTH_RE . match ( self . up_bandwidth ) : <EOL> raise BandwidthValueError ( self . up_bandwidth ) <EOL> if not self . _BANDWIDTH_RE . match ( self . down_bandwidth ) : <EOL> raise BandwidthValueError ( self . down_bandwidth ) <EOL> self . is_shaping = False <EOL> def __enter__ ( self ) : <EOL> if self . use_loopback : <EOL> platformsettings . setup_temporary_loopback_config ( ) <EOL> if self . init_cwnd != '<STR_LIT:0>' : <EOL> platformsettings . set_temporary_tcp_init_cwnd ( self . init_cwnd ) <EOL> try : <EOL> ipfw_list = platformsettings . ipfw ( '<STR_LIT:list>' ) <EOL> if not ipfw_list . startswith ( '<STR_LIT>' ) : <EOL> logging . warn ( '<STR_LIT>' , ipfw_list ) <EOL> self . _delete_rules ( ipfw_list ) <EOL> except Exception : <EOL> pass <EOL> if ( self . up_bandwidth == '<STR_LIT:0>' and self . down_bandwidth == '<STR_LIT:0>' and <EOL> self . delay_ms == '<STR_LIT:0>' and self . packet_loss_rate == '<STR_LIT:0>' ) : <EOL> logging . info ( '<STR_LIT>' ) <EOL> return <EOL> if not self . ports : <EOL> raise TrafficShaperException ( '<STR_LIT>' ) <EOL> ports = '<STR_LIT:U+002C>' . join ( str ( p ) for p in self . ports ) <EOL> half_delay_ms = int ( self . delay_ms ) / <NUM_LIT:2> <EOL> try : <EOL> platformsettings . ipfw ( <EOL> '<STR_LIT>' , self . _UPLOAD_PIPE , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . up_bandwidth , <EOL> '<STR_LIT>' , half_delay_ms , <EOL> ) <EOL> platformsettings . ipfw ( <EOL> '<STR_LIT>' , self . _UPLOAD_QUEUE , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . _UPLOAD_PIPE , <EOL> '<STR_LIT>' , self . packet_loss_rate , <EOL> '<STR_LIT>' , self . _QUEUE_SLOTS , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> platformsettings . ipfw ( <EOL> '<STR_LIT>' , self . _UPLOAD_RULE , <EOL> '<STR_LIT>' , self . _UPLOAD_QUEUE , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:to>' , self . host , <EOL> self . use_loopback and '<STR_LIT>' or '<STR_LIT>' , <EOL> '<STR_LIT>' , ports , <EOL> ) <EOL> self . is_shaping = True <EOL> platformsettings . ipfw ( <EOL> '<STR_LIT>' , self . _DOWNLOAD_PIPE , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . down_bandwidth , <EOL> '<STR_LIT>' , half_delay_ms , <EOL> ) <EOL> platformsettings . ipfw ( <EOL> '<STR_LIT>' , self . _DOWNLOAD_QUEUE , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . _DOWNLOAD_PIPE , <EOL> '<STR_LIT>' , self . packet_loss_rate , <EOL> '<STR_LIT>' , self . _QUEUE_SLOTS , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> platformsettings . ipfw ( <EOL> '<STR_LIT>' , self . _DOWNLOAD_RULE , <EOL> '<STR_LIT>' , self . _DOWNLOAD_QUEUE , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , self . host , <EOL> '<STR_LIT:to>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ports , <EOL> ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> except Exception : <EOL> logging . error ( '<STR_LIT>' ) <EOL> raise <EOL> def __exit__ ( self , unused_exc_type , unused_exc_val , unused_exc_tb ) : <EOL> if self . is_shaping : <EOL> try : <EOL> self . _delete_rules ( ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> except Exception : <EOL> logging . error ( '<STR_LIT>' ) <EOL> raise <EOL> def _delete_rules ( self , ipfw_list = None ) : <EOL> if ipfw_list is None : <EOL> ipfw_list = platformsettings . ipfw ( '<STR_LIT:list>' ) <EOL> existing_rules = set ( <EOL> r . split ( ) [ <NUM_LIT:0> ] . lstrip ( '<STR_LIT:0>' ) for r in ipfw_list . splitlines ( ) ) <EOL> delete_rules = [ r for r in ( self . _DOWNLOAD_RULE , self . _UPLOAD_RULE ) <EOL> if r in existing_rules ] <EOL> if delete_rules : <EOL> platformsettings . ipfw ( '<STR_LIT>' , * delete_rules ) </s>
