-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetLatte.py
154 lines (125 loc) · 4.04 KB
/
NetLatte.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/python
# NetLatte
# Created by: Anis Kochlef
# Measures network delays and packet loss
# 7/20/2020: Duplicate packet detection added.
import random
import string
import json
import time
import datetime
import sys
import _thread
import socket
__VERSION__ = '1.1'
__ID__ = 'NetLatte'
__MESSAGE_SIZE__ = 1216
__BUFFER_SIZE__ = 1600
__SLEEP_TIME__ = 0.1
__MAX_BLOCK_SIZE_IN_DAYS__ = 14
__MAX_BLOCK_SIZE__ = round(__MAX_BLOCK_SIZE_IN_DAYS__*24*60*60/__SLEEP_TIME__)
__SERVER_LISTEN_IP__ = '0.0.0.0'
__LOG_FILE__ = 'netlatte.log'
__DELAY_MEASUREMENT_PERIODE__ = 300
def save_log(filename,line):
file_object = open(filename, 'a')
file_object.write(line)
file_object.write('\n')
file_object.close()
def rand_message(size):
msg = ''
for i in range(size):
msg += random.choice(string.ascii_letters)
return msg
def number_of_lost_packets(previous_block,previous_index,block,index):
loss = -999
if((previous_block==block)and(index>previous_index)):
loss = index-previous_index-1
else:
if(previous_block<block):
loss = __MAX_BLOCK_SIZE__*(block-previous_block) + index - previous_index - 1
else:
if((previous_block==__MAX_BLOCK_SIZE__-1) and (previous_index==__MAX_BLOCK_SIZE__-1)):
loss = __MAX_BLOCK_SIZE__*block+index
return loss
def server(listen_ip,listen_port):
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((listen_ip,listen_port))
while True:
data, addr = sock.recvfrom(1600)
sock.sendto(data,addr)
def sender(sock,server_ip,server_port,message,sleep_time,max_block_size):
b = 0
i = 0
while True:
ts = time.time()
dm = {'block': b,'index': i,'timestamp': ts,'payload': message}
jm = json.dumps(dm)
bm = bytes(jm,'utf-8')
sock.sendto(bm,(server_ip, server_port))
# simulate duplicate
# sock.sendto(bm,(server_ip, server_port))
time.sleep(sleep_time)
i += 1
if(i == max_block_size):
i = 0
b += 1
if(b == max_block_size):
b = 0
def reciever(sock,log_file):
previous_ts = time.time()
previous_block = 0
previous_index = -1
delay = 0
delay_samples = 0
delay_window_start = previous_ts
while True:
bm, addr = sock.recvfrom(__BUFFER_SIZE__)
msg = bm.decode('utf-8')
jm = json.loads(msg)
msg_ts = jm.get('timestamp')
msg_block = jm.get('block')
msg_index = jm.get('index')
ts = time.time()
# Delay measurement
delay += (ts-msg_ts)/2
delay_samples += 1
if((ts-delay_window_start)>__DELAY_MEASUREMENT_PERIODE__):
td = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
delay = round(1000000*delay/delay_samples)/1000
log = f'[{__ID__}] {td} Latency = {delay} ms, Samples = {delay_samples}'
print(log)
save_log(log_file,log)
delay = 0
delay_samples = 0
delay_window_start=ts
loss = number_of_lost_packets(previous_block,previous_index,msg_block,msg_index)
if(loss > 0):
td = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
duration = round((ts-previous_ts)*1000)/1000
log = f'[{__ID__}] {td} Network interruption detected (duration= {duration} s, loss={loss} packets)'
print(log)
save_log(log_file,log)
else:
if(loss<0):
td = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
duration = round((ts-previous_ts)*1000)/1000
log = f'[{__ID__}] {td} Duplicate packet detected (block= {msg_block}, index={msg_index})'
print(log)
save_log(log_file,log)
previous_ts = msg_ts
previous_block = msg_block
previous_index = msg_index
def client(server_ip,server_port,message_size,sleep_time,max_block_size,log_file):
rm = rand_message(message_size)
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
_thread.start_new_thread(sender,(sock,server_ip,server_port,rm,sleep_time,max_block_size))
_thread.start_new_thread(reciever(sock,log_file))
if __name__ == "__main__":
if (len(sys.argv)==3 and sys.argv[1]=='-s'):
port = int(sys.argv[2])
server(__SERVER_LISTEN_IP__,port)
if (len(sys.argv)==4 and sys.argv[1]=='-c'):
ip = sys.argv[2]
port = int(sys.argv[3])
client(ip,port,__MESSAGE_SIZE__,__SLEEP_TIME__,__MAX_BLOCK_SIZE__,__LOG_FILE__)