-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjitter.py
102 lines (89 loc) · 2.64 KB
/
jitter.py
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
import threading
import socket
import json
import os
from random import randint
import subprocess
min_delay = 1
max_delay = 1
class JitterThread(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
self.shutdown = False
def run(self):
print "Starting " + self.name
while not self.shutdown:
execute_dummynet()
print "Exiting " + self.name
#for time_delay in delay_profile:
# add_to_file ("%s_%s_%s" % (jitter_output_file, str(min_delay), str(max_delay)), "a", str(time_delay) + "\n")
#global delay_profile
#delay_profile = []
def execute_dummynet():
delay = randint(min_delay, max_delay)
dummynet_command = "sudo ipfw pipe 40270 config delay %dms" % delay
os.system(dummynet_command)
#global delay_profile
#delay_profile.append((datetime.datetime.now().time().isoformat(), delay))
def prep():
dummynet_command = "sudo ipfw add 100 pipe 1 ip from any to any in"
subprocess.call([dummynet_command], shell=True)
dummynet_command = "sudo ipfw add 100 allow ip from any to any out"
subprocess.call([dummynet_command], shell=True)
dummynet_command = "sudo ipfw pipe 1 config delay 1ms" # doesnt work without this
subprocess.call([dummynet_command], shell=True)
def handle_jitter_instruction():
TCP_IP = '0.0.0.0'
TCP_PORT = 5008
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
shutdown = False
global min_delay
global max_delay
#prep()
while not shutdown:
data = conn.recv(BUFFER_SIZE)
print "received data:", data
if not data:
print "there is no data received"
break
data = json.loads(data)
if data['command'] == 'START_JITTER':
min_delay = data['min_delay']
max_delay = data['max_delay']
jitter_thread = JitterThread('jitter_thread')
jitter_thread.shutdown = False
jitter_thread.start()
conn.send('STARTED_JITTER')
data = conn.recv(BUFFER_SIZE)
print "received data:", data
data = json.loads(data)
if data['command'] == 'STOP_JITTER':
print 'stopping jitter'
jitter_thread.shutdown = True
jitter_thread.join()
conn.send("STOPED_JITTER")
else:
print 'UNKNOWN_INSTRUCTION'
conn.send("UNKNOWN_INSTRUCTION")
break
else:
print 'UNKNOWN_INSTRUCTION'
conn.send("UNKNOWN_INSTRUCTION")
break
conn.close()
'''
subprocess.call(["sudo ipfw pipe 1 delete"], shell=True)
subprocess.call(["sudo ipfw -q flush"], shell=True)
'''
def main():
print "starting network emulator"
handle_jitter_instruction()
os.sys.exit
if __name__ == '__main__':
main()