forked from manuels/kademlia.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
72 lines (50 loc) · 1.69 KB
/
bootstrap.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
70
71
72
<?php
namespace Kademlia;
class Bootstrap extends Task {
public $find_node_task = NULL;
function __construct(&$settings, $bootstrap_nodes) {
parent::__construct($settings);
$this->settings = &$settings;
$this->bootstrap_nodes = $bootstrap_nodes;
if(strlen($settings->own_node_id) !== N/8)
$settings->own_node_id = Node::randomNodeId();
}
public function enqueueSelf() {
$this->find_node_task = new FindNode($this->settings, $this->settings->own_node_id, $this->bootstrap_nodes);
$this->find_node_task->enqueue()->done([$this, 'perform']);
return $this;
}
public function perform($results) {
$node_list = new NodeList([]);
foreach($results as $nodes) {
$node_list->addNodeArray($nodes);
}
if($node_list->containsNodeId($this->settings->own_node_id)) {
$this->settings->own_node_id = Node::randomNodeId();
$this->find_node_task = new Bootstrap($this->settings, $this->bootstrap_nodes);
$this->find_node_task->enqueue()->done([$this, 'emitSuccess']);
}
else {
$this->emitSuccess($this->settings->own_node_id);
}
}
public function emitSuccess($own_node_id) {
$this->settings->own_node_id = $own_node_id;
$this->emit('success', $own_node_id);
$kbuckets_size = $this->settings->kbuckets->toNodeList()->size();
$this->fillBuckets();
}
public function fillBuckets() {
for($i = 0; $i < N/8; $i+=4) {
$j = 0;
# for($j = 0; $j < 8; $j++) {
$node_id = $this->settings->own_node_id;
$byte = $node_id[$i] ^ chr(1 << $j);
$node_id[$i] = $byte;
$task = new FindNode($this->settings, $node_id);
$task->enqueue();
# }
}
}
}
?>