-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
executable file
·59 lines (49 loc) · 1.37 KB
/
sim.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
import os
import subprocess
import time
import random
node_list = [4, 8, 10, 20, 30]
def phase_one(num_nodes, exp_num):
commands = []
nodes = []
commands.append("detect\n")
while len(nodes) is not node_list[exp_num]:
var = random.randint(1, 255)
if var not in nodes:
random.seed(time.time())
time.sleep(0.5)
commands.append("join " + str(var) + "\n")
nodes.append(var)
return commands
def phase_two(num_nodes, exp_num):
commands = []
nodes = []
commands.append("detect\n")
for i in range(0, num_nodes[exp_num]):
var = random.randint(1, 255)
random.seed(time.time())
time.sleep(0.5)
commands.append("join " + str(var) + "\n")
nodes.append(var)
for i in range(0, 128):
node = random.choice(nodes)
random.seed(time.time())
time.sleep(0.5)
var = random.randint(0, 255)
random.seed(time.time())
time.sleep(0.5)
commands.append("find " + str(node) + " " + str(var) + "\n")
return commands
def main(cmd):
p = subprocess.Popen(["./chord"], stdin=subprocess.PIPE, stdout = subprocess.PIPE, shell = False, close_fds = True) #NOTE: no shell=True here
time.sleep(1)
for item in cmd:
p.stdin.write(item)
#print item
time.sleep(1)
p.stdin.write("print\nexit\n")
output = p.stdout.read()
print output
if __name__ == '__main__':
main(phase_one(node_list, 0))
#main(phase_two(node_list, 0))