summaryrefslogtreecommitdiff
path: root/tasks/vlc_stream_rtp/task.py
blob: 427fc0e8268d289ff249b6b27894017ab896e99f (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# kpov_util should be imported by add_assignment.py

# Postavi nek film na Internet tako, da ga bodo lahko ostali videli.
# TODO: finish this

instructions = {
    'si': '''\
<p>
Postavi navidezni računalnik <em>SimpleArbiter</em> in <em>StudentVLC</em>. Poskrbi, da bosta na istem omrežju, od koder bosta imela dostop tudi do interneta.

<p>
Na <em>StudentVLC</em> posodobi datoteko <code>/etc/apt/sources.list</code>, preveri posodobitve in naloži VLC.

<p>
Posnemi ali kako drugače ustvari film ter poskrbi, da bo film dostopen na lokalnem omrežju prek RTP z imenom toka <code>{{TOK}}</code> na naslovu <code>{{NASLOV}}</code>, vrata <code>{{PORT}}</code>. Računaj, da bodo film lahko videli tvoji sošolci. Kršenje avtorskih pravic je pri reševanju te naloge strogo prepovedano.
''',
     'en': '''\
<p>
Set up a virtual computer called <em>SimpleArbiter</em> using the simpleArbiter disk and
a virtual computer called <em>StudentVLC</em> using the student-VLC disk. Make sure they
are on the same network and that they have access to the Internet.

<p>
On StundentVLC, update /etc/apt/sources.list, check the for updates and install VLC.

<p>
Record or otherwise create a movie and make sure the movie is avaliable on your local network via RTP with the name of the stream <code>{{TOK}}</code> at the address <code>{{NASLOV}}</code> on port <code>{{PORT}}</code>. Take into account that the movie may be seen by your classmates. Copyright infrigement while solving this task is strictly prohibited.
''',
}

computers = {
    'SimpleArbiter': {
        'disks': [
            {   'name': 'simpleArbiter',
            },
        ],
        'network_interfaces': [{'network': 'test-net'}],
        'flavor': 'm1.tiny',
        'config_drive': False
    },
    'StudentVLC': {
        'disks': [
            {   'name': 'student-VLC',
            },
        ],
        'network_interfaces': [{'network': 'test-net'}],
        'flavor': 'm1.tiny',
        'config_drive': False
    }

}

networks = { 'test-net': {'public': True} }

params_meta = {
    'NASLOV': {'descriptions': {'si': 'RTP multicast IP naslov'}, 'w': False, 'public':True, 'type': 'stream_name', 'generated': True},
    'PORT': {'descriptions': {'si': 'RTP VRATA'}, 'w': False, 'public':True, 'type': 'port', 'generated': True},
    'TOK': {'descriptions': {'si': 'Naslov (ime) toka'}, 'w': False, 'public':True, 'type': 'IP', 'generated': True},
}

def task(NASLOV, TOK):
    import pexpect
    results = dict()
    results['ps'] = pexpect.run('ps xa')
    results['tcpdump_hex'] = pexpect.run('sudo /usr/sbin/tcpdump -x -c 2 dst host 239.255.255.255 and port 9875'.format(NASLOV))
    results['tcpdump'] = pexpect.run('sudo /usr/sbin/tcpdump -c 8 dst host {}'.format(NASLOV))
    return results
    
def gen_params(user_id, params_meta):
    params = dict()
    r = random.Random(user_id)
    net = kpov_util.IPv4_net_gen(r, min_hosts = 16,
        local=True, multicast=True)
    params['NASLOV'] = kpov_util.IPv4_addr_gen(r, net, 1)[0]
    params['PORT'] = str(r.randint(5000, 6000))
    params['TOK'] = kpov_util.hostname_gen(r)
    return params

def task_check(results, params):
    import re
    score = 0
    hints = []
    p1_s = ""
    sname = ""
    try:
        packs = results['tcpdump_hex'].split('> 239.255.255.255.9875: UDP, length')
        p1 = packs[1]
        p1_l = p1.split('\n')
        p1_d = p1_l[1:-1]
        p1_s = ""
        for i in p1_d:
            p1_s = p1_s + "".join([j.strip() for j in i.split(' ')[1:]])
        sname = "".join([hex(ord(i))[2:] for i in params['TOK']])
    except:
        hints.append("problem parsing RTP stream capture result")
    if p1_s.find(sname) > 2:
        score += 5
    else:
        hints.append("stream name not found in stream announcement")
    s = "IP [^ ]* > {}.{}: UDP, length [0-9]+".format(
        re.escape(params['NASLOV']),
        params['PORT'])
    if re.search(s, results['tcpdump']):
        score += 5
    else:
        hints.append("RTP stream not detected in " + results['tcpdump'])
    return score, hints

def prepare_disks(templates, task_params, global_params):
    write_default_config(templates['simpleArbiter'], global_params)