~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python

__metaclass__ = type

from optparse import OptionParser
import sys
import subprocess

from script_commands import (
    Command,
    UserError,
    )


def tc(command):
     subprocess.call('sudo tc ' + command, shell=True)


class StartCommand(Command):

    @classmethod
    def get_parser(cls):
        parser = OptionParser()
        parser.add_option(
            '-d', '--delay', dest='delay', type='int',
            help='Length of delay in miliseconds (each way).')
        return parser

    @staticmethod
    def run(delay=500, port=443):
        tc('qdisc add dev lo root handle 1: prio')
        tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
        tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
           ' dport %d 0xffff flowid 1:3' % port)
        tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
           ' sport %d 0xffff flowid 1:3' % port)


Command.commands['start'] = StartCommand


class StopCommand(Command):

    @staticmethod
    def get_parser():
        parser = OptionParser()
        return parser

    @staticmethod
    def run():
        tc('qdisc del dev lo root')


Command.commands['stop'] = StopCommand



if __name__ == "__main__":
    try:
        Command.run_subcommand(sys.argv[1:])
    except UserError as e:
        sys.stderr.write(str(e)+'\n')