-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_network.py
51 lines (44 loc) · 1.26 KB
/
start_network.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
import os
import sys
import json
import signal
pids = []
def init(hosts):
first_node = (hosts['host_0'][0], hosts['host_0'][1])
inst = ['python3', 'first_node.py', first_node[0], first_node[1]]
pid = os.fork()
if pid:
pids.append(pid)
print(str(first_node) + " ---> " + str(pid))
else:
os.execvp(inst[0], inst)
for i in range(1, len(hosts)):
idx = 'host_' + str(i)
inst = ['python3', 'tangled_node.py', hosts[idx][0], hosts[idx][1], first_node[0], first_node[1]]
pid = os.fork()
if pid:
print(str((hosts[idx][0], hosts[idx][1])) + ' ---> ' + str(pid))
pids.append(pid)
else:
os.execvp(inst[0], inst)
def run():
with open("./hosts.json", "r") as f:
hosts = json.load(f)
print("<hosts> ----------------> <pid>")
init(hosts)
def stop():
for pid in pids:
os.kill(pid, signal.SIGSTOP)
if __name__ == '__main__':
run()
print('>> running network successfuly!')
while True:
try:
if str(input()) == 'stop':
stop()
print('>> stop network')
break
except KeyboardInterrupt:
stop()
print('>> stop network')
break