-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
122 lines (95 loc) · 3.3 KB
/
main.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
from advancedDHT import *
import code # code.interact(local=dict(globals(), **locals()))
def main():
# start a network with 4 random nodes
# start first 2 nodes setting manually succ and pred.
print("Creating first two nodes")
n1 = NodeDHT("node1")
n2 = NodeDHT("node2")
n1.setSuccessor(n2)
n1.setPredecessor(n2)
n2.setSuccessor(n1)
n2.setPredecessor(n1)
print("Creating further two nodes")
# add 2 more nodes by join procedure
n3 = NodeDHT("node3")
n4 = NodeDHT("node4")
print("n3 joining from n2")
n2.join(n3)
print("n4 joining from n1")
n1.join(n4)
nodes = [n1, n2, n3, n4]
# Inject some contents
niceQuote = """nel mezzo del cammin di nostra vita mi
ritrovai per una selva oscura che la diritta via era smarrita"""
contents = niceQuote.split(" ")
contents = [w.strip() for w in contents if w]
for word in contents:
key = compute_key(word)
node = random.choice(nodes)
node.store(key, word)
print("Print DHT after 4 nodes inserted plus Divine Comedy content")
print(printDHT(n1))
print("TEST LOOKUP QUERIES")
# Test lookups queries
# Correct queries
for _ in range(4):
c = random.choice(contents)
node = random.choice(nodes)
print("Searching for \'{}\'".format(c))
node.lookup(compute_key(c))
print("\n")
# Wrong query
c = "NOT INSERTED UNLESS HASH CONFLICT XD"
print("Searching for \'{}\'".format(c))
n1.lookup(compute_key(c))
print("TEST CONTENT PASSING DURING JOIN")
print("Add one node and check restructuring of DHT")
print("DHT BEFORE JOIN -->")
print(printDHT(n1))
n7 = NodeDHT("node7")
n1.join(n7)
nodes.append(n7)
print("DHT AFTER JOIN -->")
print(printDHT(n1))
# Example of leaving, check DHT after leaving
print("TEST CONTENT PASSING DURING LEAVE")
print("Make one node leave and check restructuring of DHT")
print("DHT BEFORE LEAVE -->")
print(printDHT(n1))
n4.leave()
nodes.remove(n4)
del n4
print("DHT AFTER LEAVE -->")
print(printDHT(n1))
print("NOW ADDING more nodes and 1000 more items")
# Now add a lot more nodes, then add more contents
for i in range(10, 100):
newnode = NodeDHT(f"node{str(i)}")
n1.join(newnode)
nodes.append(newnode)
# Now add many nodes and more contents, read by a file
moreWords = wordsOfFile("lipsum.txt")
for word in moreWords:
key = compute_key(word)
node = random.choice(nodes)
node.store(key, word)
print("Saving large DHT in html")
with open("table.html", 'w') as tablehtml:
html = printDHT(n1, 'html')
tablehtml.write(html)
print("COMPUTING FINGER TABLES FOR ALL NODES")
for n in nodes:
n.update()
print("SAVING DHT GRAPH WITH FINGER LINKS OF node1")
drawCircularWithFinger(n1)
print("COMPARE #STEPS FINGERLOOKUP VS STANDARD LOOKUP")
tabulable = [["content", "fingerSteps", "standardSteps"]]
for word in contents:
key = compute_key(word)
found, fsteps = n1.fingerLookup(key)
found, stdsteps = n1.lookup(key)
tabulable.append([word, fsteps, stdsteps])
print(tabulate(tabulable, headers="firstrow", tablefmt='pretty'))
if __name__ == "__main__":
main()