-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelayPeerEcho.py
129 lines (109 loc) · 4.64 KB
/
RelayPeerEcho.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
# RelayPeerEcho.py
# Billy Kihei (c) 2021
# Intelligent Mobile Device Lab @ Kennesaw State University
# Part of the 5Gtoolkit for testing commercial 5G networks.
# This app measures the Layer 7 delay to send the character '0' to a peer via a RendezvousRelayServer.
# The purpose of this app is to measure the Layer 7 round trip time from this phone to the other phone via RendezvousRelayServer.
# A->RendezvousRelayServer->B->RendezvousRelayServer->A
# The intended use is to run this app in Termux.
# Provide the number of times you would like to run this application.
# Statistics will be returned to you.
# 1. Open Termux.
# 2. Download the 5gtoolkit git repo.
# 3. Edit the config.json file so that RendezvousRelayServer server ip and port are correct.
# 4. python RelayPeerEcho.py <a|b> <number of measurements>
# 5. Example: python RelayPeerEcho.py a 10, means: login as user 'a', get 10 echoes from 'b'
# 6. Example: python RelayPeerEcho.py b 10, means: login as user 'b', get 10 echoes from 'a'
import socket
import sys
import signal
import time
import json
f = open('config.json',)
conf = json.load(f)
f.close()
username = sys.argv[1] # can only be a or b
NumTimesToRun = int(sys.argv[2])
pktnumber = 0
server_addr = (conf["rendezvous_relay_server"]["ip"], conf["rendezvous_relay_server"]["port"])
udpClientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def signal_handler(sig, frame):
if username=='a':
# sending relay server to logout all users
udpClientSock.sendto(str.encode("done:a"), server_addr)
udpClientSock.close()
print('\n')
print("%d bytes echoed!\n" % (pktnumber))
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
udpClientSock.sendto(str.encode("checkstatus:" + username), server_addr)
print("Logging In To Rendezvous Relay Server as username: " + username + "...")
respFromServer=''
while ("OK" not in respFromServer):
udpClientSock.sendto(str.encode("login:" + username), server_addr)
respFromServer = udpClientSock.recvfrom(1024)
respFromServer = respFromServer[0].decode()
respFromServer=''
print('Logged in as: '+username+ ", awaiting peer...")
while ("PEER" not in respFromServer):
respFromServer = udpClientSock.recvfrom(1024)
respFromServer = respFromServer[0].decode()
udpClientSock.sendto(str.encode("OK"), server_addr)
print("Peer found. Echo system ready.")
# Device 1 should be logged into relay server as: a
if username == 'a':
print("Ensure b displays \"Listening for packets...\" then when ready...")
x=input("The press any key to begin delay test through Rendezvous Relay Server...")
while True:
print('Sending Packets')
pktnumber = 0
delays = []
while (pktnumber < NumTimesToRun):
udpClientSock.sendto(str.encode("0"), server_addr)
t = time.time()
data = udpClientSock.recvfrom(1024)
if data[0].decode()=="keep-alive":
continue
elapsed = time.time() - t
if elapsed==0.0:
continue
delays.append(elapsed)
pktnumber += 1
if len(delays) == 0:
print("Divide by zero error. Maybe decrease the packet size? Try again.")
else:
mu = sum(delays) / len(delays)
variance = sum([((x - mu) ** 2) for x in delays]) / len(delays)
stddev = variance ** 0.5
multiplied_delays = [element * 1000 for element in delays]
themin = min(multiplied_delays)
themax = max(multiplied_delays)
print("Peer Relay completed %s measurements" % (pktnumber))
print("Average: " + str(mu*1000) + "ms")
print("Std.Dev: " + str(stddev*1000) + "ms")
print("Min: " + str(themin) + "ms")
print("Max: " + str(themax) + "ms")
print('\n')
x=input("Run again? (y/n)")
if x == "n":
print("Sending B: closed, message.")
udpClientSock.sendto(str.encode("peer_close"), server_addr)
udpClientSock.close()
break
elif x=="y":
continue
# Device 2 should be logged into relay server as: b
elif username == 'b':
print("WARNING: Ensure b shows: \"Listening for packets...\", before running a")
x=input("Press any key to receiving packets...")
print('Listening for packets...')
while True:
data, client_addr = udpClientSock.recvfrom(1024)
if data.decode() == "peer_close":
udpClientSock.close()
break
elif data.decode() == "keep-alive":
continue
else:
udpClientSock.sendto(data, client_addr)
pktnumber += 1