-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest2.go
442 lines (319 loc) · 9.43 KB
/
test2.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
func main() {
// Set Environment variable
for i:=0; i<100; i++ {
prevport = port
port = startport + i
ports[i] = port
storeMap = make(map[string]string)
nodeIDMap = make(map[string]string)
// create new command and store in array
// start kademlia instance by starting command
cmd[i] := exec.Command("./mainRef","localhost:"+port,"localhost:"+prevport)
setPipes(i)
// run commands in the kademlia instance:
// Start the process
if err := cmd[i].Start(); err != nil {
log.Fatalf("failed to start instance %d due to :%s", i, err)
}
// whoami - store the result in whoami
command = "whoami"
res := runcommand(command, i)
whoamiArray[i] := res
fmt.Println("nodeId of instance %d is %s", i, res)
// store nodeID and value - store in the store map. for nodeID equal to whoami-10 to whoami+10
for j:= i-10;j <= i;j++{
if j<0{
j = 0
}
for k:=-5;k< 5; k++{
key = generateKey(j, k )
command = "store "+ whoamiArray[j]+" "+key+" "+ports[j]
res = runcommand(command, i)
if res != "\n"{
fmt.Println("Error in store rpc")
}
else{
storeMap[key] = ports[j]
nodeIDMap[key] = whoamiArray[j]
}
}
}
//local find value
for j= i-1;j<=i;j++{
if j<0{
j = 0
}
for k:=-5;k< 5; k++{
key = generateKey(j, k)
command = "local_find_value "+key
res = runcommand(command, i)
if j==i{
if res != ports[i]{
fmt.Println("Error local find value")
break
}
continue
}
if j < i{
if res != "ERR"{
fmt.Println("Error in local find value")
break
}
continue
}
else{
break
}
}
}
// find node use whoami for the previous instance of kademlia and verify with the value in nodeIDs map. for any 10 nodeid in store
for j= 1;j<=5;j++{
k = randInt(-5, 5)
nodenumber = randInt(0, i)
key = generateKey(nodenumber, k)
node = randInt(0,i)
command = "find_node "+whoami[node]+" "+key
res = runcommand(command, i)
verifyFindNodeResult(res, node)
}
}
// findvalue for any random 10 value in store and verify with the nodeid map and the whoami map
for j= 1;j<=5;j++{
k = randInt(-5, 5)
nodenumber = randInt(0, i)
key = generateKey(nodenumber, k)
node = randInt(0,i)
command = "find_value "+whoami[node]+" "+key
res = runcommand(command, i)
if rs = veriftyFindValueResult(res, node); rs != true{
verifyFindNodeResult(res, node)
}
}
}
// ping checking
for j= 1;j<=5;j++{
node = randInt(0,i)
command = "ping "+whoami[node]
res = runcommand(command, i)
if res != nil{
fmt.Println("Error in ping with nodeid")
break
}
node = randInt(0,i)
p = startport + node
command = "ping localhost:"+string(p)
res = runcommand(command, i)
if res != nil{
fmt.Println("Error in ping with ip and port")
break
}
}
}
}
for f:=0;f<3;f++{
for i:=0; i<100; i++{
//for random 10 nodeid in store map
// iterative findnode - result nodeid must be present in whoami map and one of the result must be equal to the real nodeid storing the value which can be verified using nodeids map
for j= 1;j<=5;j++{
node = randInt(0,i)
command = "iterativeFindNode "+whoami[node]
res = runcommand(command, i)
if rs := verifyItFindNodeResult(res, node); rs != true{
fmt.Println("Error in iterative find node :%s", res)
}
}
// iterative findvalue - for random 10 values in the store and nodeid maps. result can be verifeid using the store map
// iterativeFindValue key
//printf("%v %v\n", ID, value), where ID refers to the node that finally
returned the value. If you do not find a value, print "ERR".
for j= 1;j<=5;j++{
k = randInt(-5, 5)
nodenumber = randInt(0, i)
key = generateKey(nodenumber, k)
command = "iterativeFindValue "+key
res = runcommand(command, i)
if rs = veriftyFindValueResult(res, node); rs != true{
fmt.Println("Error in iterative find value : %s", res)
break
}
}
// iterative store - store 10 random ipport on nodeids, and then check using findvalue using the xor distance method
//iterativeStore key value
//Perform the iterativeStore operation and then print the ID of the node that
//received the final STORE operation.
j = randInt(-10, 10)
key = generateKey(j, i )
command = "iterativeStore " "+key+" "+ports[i]
res = runcommand(command, i)
if res != "\n"{
fmt.Println("Error in iterativeStore : %s", res)
break
}
else{
verifyIterativeStore(res, i)
}
}
}
//Sample Kademlia - mainSample
i = 100
port = startport+100
cmd[100] = exec.Command("./mainSample","localhost:"+port,"localhost:"+prevport)
setPipes(i)
// run commands in the kademlia instance:
// Start the process
if err := cmd[i].Start(); err != nil {
log.Fatalf("failed to start instance %d due to :%s", i, err)
}
// whoami - store the result in whoami
command = "whoami"
res := runcommand(command, i)
whoamiArray[i] := res
fmt.Println("nodeId of instance %d is %s", i, res)
// store nodeID and value - store in the store map. for nodeID equal to whoami-10 to whoami+10
for j:= i-10;j <= i;j++{
if j<0{
j = 0
}
for k:=-5;k< 5; k++{
key = generateKey(j, k )
command = "store "+ whoamiArray[j]+" "+key+" "+ports[j]
res = runcommand(command, i)
if res != "\n"{
fmt.Println("Error in store rpc")
}
else{
storeMap[key] = ports[j]
nodeIDMap[key] = whoamiArray[j]
}
}
}
//local find value
for j= i-1;j<=i;j++{
if j<0{
j = 0
}
for k:=-5;k< 5; k++{
key = generateKey(j, k)
command = "local_find_value "+key
res = runcommand(command, i)
if j==i{
if res != ports[i]{
fmt.Println("Error local find value")
break
}
continue
}
if j < i{
if res != "ERR"{
fmt.Println("Error in local find value")
break
}
continue
}
else{
break
}
}
}
// find node use whoami for the previous instance of kademlia and verify with the value in nodeIDs map. for any 10 nodeid in store
for j= 1;j<=5;j++{
k = randInt(-5, 5)
nodenumber = randInt(0, i)
key = generateKey(nodenumber, k)
node = randInt(0,i)
command = "find_node "+whoami[node]+" "+key
res = runcommand(command, i)
verifyFindNodeResult(res, node)
}
}
// findvalue for any random 10 value in store and verify with the nodeid map and the whoami map
for j= 1;j<=5;j++{
k = randInt(-5, 5)
nodenumber = randInt(0, i)
key = generateKey(nodenumber, k)
node = randInt(0,i)
command = "find_value "+whoami[node]+" "+key
res = runcommand(command, i)
if rs = veriftyFindValueResult(res, node); rs != true{
verifyFindNodeResult(res, node)
}
}
}
// ping checking
for j= 1;j<=5;j++{
node = randInt(0,i)
command = "ping "+whoami[node]
res = runcommand(command, i)
if res != nil{
fmt.Println("Error in ping with nodeid")
break
}
node = randInt(0,i)
p = startport + node
command = "ping localhost:"+string(p)
res = runcommand(command, i)
if res != nil{
fmt.Println("Error in ping with ip and port")
break
}
}
for f:=0;f<3;f++{
//for random 10 nodeid in store map
// iterative findnode - result nodeid must be present in whoami map and one of the result must be equal to the real nodeid storing the value which can be verified using nodeids map
for j= 1;j<=5;j++{
node = randInt(0,i)
command = "iterativeFindNode "+whoami[node]
res = runcommand(command, i)
if rs := verifyItFindNodeResult(res, node); rs != true{
fmt.Println("Error in iterative find node :%s", res)
}
}
// iterative findvalue - for random 10 values in the store and nodeid maps. result can be verifeid using the store map
// iterativeFindValue key
//printf("%v %v\n", ID, value), where ID refers to the node that finally
returned the value. If you do not find a value, print "ERR".
for j= 1;j<=5;j++{
k = randInt(-5, 5)
nodenumber = randInt(0, i)
key = generateKey(nodenumber, k)
command = "iterativeFindValue "+key
res = runcommand(command, i)
if rs = veriftyFindValueResult(res, node); rs != true{
fmt.Println("Error in iterative find value : %s", res)
break
}
}
// iterative store - store 10 random ipport on nodeids, and then check using findvalue using the xor distance method
//iterativeStore key value
//Perform the iterativeStore operation and then print the ID of the node that
//received the final STORE operation.
j = randInt(-10, 10)
key = generateKey(j, i )
command = "iterativeStore " "+key+" "+ports[i]
res = runcommand(command, i)
if res != "\n"{
fmt.Println("Error in iterativeStore : %s", res)
break
}
else{
verifyIterativeStore(res, i)
}
}
// Stopping all processes
for i=0;i<101;i++{
// Wait for the process to finish
if err = cmd[i].Wait(); err != nil {
log.Fatalf("command failed....: %s", err)
}
// Kill the process if it doesn't exit in time
defer time.AfterFunc(CommandTimeout, func() {
log.Printf("command timed out")
cmd[i].Process.Kill()
}).Stop()
}
/*
get_contact ID
If your buckets contain a node with the given ID,
printf("%v %v\n", theNode.addr, theNode.port)
If your buckers do not contain any such node, print "ERR".
*/