-
Notifications
You must be signed in to change notification settings - Fork 1
/
transmit.py
59 lines (47 loc) · 1.68 KB
/
transmit.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
#!/usr/bin/env python2
from random import randint
import pickle
dummy = ' '
reorder = 0
counter = 1
error = 1
p = 5
def transmit(csocket, message, serverName, serverPort):
global counter, error, reorder, dummy, p
message = pickle.dumps(message)
if (randint(1, p) % p) != 0:
csocket.sendto(message, (serverName, serverPort))
print 'UDT: Properly sent packet with number ' + str(counter)
else:
# Make some error with probability p
if error == 1:
print 'UDT: Dropped a packet with number ' + str(counter)
error = 2
elif error == 2:
print 'UDT: Duplicated a packet with number ' + str(counter)
csocket.sendto(message, (serverName, serverPort))
csocket.sendto(message, (serverName, serverPort))
error = 3
elif error == 3:
if reorder == 1:
print 'UDT: Re-ordering a packet with number ' + str(counter)
csocket.sendto(message, (serverName, serverPort))
csocket.sendto(dummy, (serverName, serverPort))
reorder = 0
counter = counter + 1
error = 4
else:
dummy = message
reorder = 1
counter = counter - 1
elif error == 4:
print 'UDT: Creating corrupt packet with number ' + str(counter)
mylist = list(message)
# get last char of the string
x = ord(mylist[-1])
x = x ^ 1
mylist[-1] = chr(x)
dummy = ''.join(mylist)
csocket.sendto(dummy, (serverName, serverPort))
error = 1
counter = counter + 1