-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator.py
69 lines (51 loc) · 2.51 KB
/
mediator.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
from socket import *
MEDIATOR_IP = '192.168.0.108' # Ip of device running mediator.py
MEDIATOR_SERVER_PORT = 10400
EDGE_IP = '192.168.61.3'
EDGE_SERVER_PORT = 9999
PROXY_IP = '192.168.0.106'
PROXY_SERVER_PORT = 11998
while(True):
# Open server socket for proxy
with socket(AF_INET, SOCK_STREAM) as server_for_proxy:
server_for_proxy.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_for_proxy.bind((MEDIATOR_IP, MEDIATOR_SERVER_PORT))
server_for_proxy.listen()
print('Waiting for proxy connection at port ', MEDIATOR_SERVER_PORT)
conn_proxy, addr_proxy = server_for_proxy.accept()
with conn_proxy:
print('Connected to proxy by ', addr_proxy)
data = conn_proxy.recv(200)
conn_proxy.close()
server_for_proxy.close()
if not data:
continue
else:
data = data.decode('utf-8')
print('Received data from proxy: ' + data)
# Open client socket for edge
with socket(AF_INET, SOCK_STREAM) as client_for_edge:
client_for_edge.connect((EDGE_IP, EDGE_SERVER_PORT))
client_for_edge.sendall(data.encode())
client_for_edge.close()
# Open server socket for edge
with socket(AF_INET, SOCK_STREAM) as server_for_edge:
server_for_edge.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_for_edge.bind((MEDIATOR_IP, MEDIATOR_SERVER_PORT))
server_for_edge.listen()
print('Waiting for edge connection at port ', MEDIATOR_SERVER_PORT)
conn_edge, addr_edge = server_for_edge.accept()
with conn_edge:
print('Connected to edge by ', addr_edge)
data = conn_edge.recv(200)
server_for_edge.close()
if not data:
continue
else:
data = data.decode('utf-8')
print('Received data from edge: ' + data)
# Open client socket for edge
with socket(AF_INET, SOCK_STREAM) as client_for_proxy:
client_for_proxy.connect((PROXY_IP, PROXY_SERVER_PORT))
client_for_proxy.sendall(data.encode())
client_for_proxy.close()