-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chord.php
69 lines (53 loc) · 1.42 KB
/
Chord.php
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
<?php
include('ChordNode.php');
include('ChordKey.php');
include('FingerTable.php');
include('Finger.php');
define('KEYSIZE', 4);
define('HASHKEYS', false);
define('NUM_OF_NODES', 16/*pow(2, KEYSIZE)*/);
class Chord {
public $nodeList = [];
function createNode($nodeId) {
$node = new ChordNode($nodeId);
$this->nodeList[] = $node;
usort($this->nodeList, function ($a, $b) {
if ($a->nodeKey->key == $b->nodeKey->key) {
return 0;
}
return ($a->nodeKey->key < $b->nodeKey->key) ? -1 : 1;
});
}
function getNode($i) {
return $this->nodeList[$i];
}
function size() {
return count($this->nodeList);
}
}
$chord = new Chord();
for($i = 0; $i < NUM_OF_NODES; $i++) {
$chord->createNode($i);
}
print(NUM_OF_NODES . " nodes are created.\n");
for($i = 1; $i < NUM_OF_NODES; $i++) {
$node = $chord->getNode($i);
$node->join($chord->getNode(0));
$preceding = $node->getSuccessor()->getPredecessor();
$node->stabilize();
if ($preceding == null) {
$node->getSuccessor()->stabilize();
} else {
$preceding->stabilize();
}
}
print("Chord ring is established.\n");
for($i = 0; $i < NUM_OF_NODES; $i++) {
$node = $chord->getNode($i);
$node->fixFingers();
}
print("Finger Tables are fixed.\n");
for($i = 0; $i < NUM_OF_NODES; $i++) {
$node = $chord->nodeList[$i];
$node->printFingerTable(true);;
}