forked from manuels/kademlia.php
/
node_list.php
161 lines (118 loc) · 3.83 KB
/
node_list.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
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
<?php
namespace Kademlia;
class NodeList implements \JsonSerializable {
function __construct($node_array = []) {
$this->node_array = $node_array;
}
public function containsNodeId($needle_id) {
$needle_id = Node::hexId2bin($needle_id);
foreach($this->node_array as $other_node) {
if($needle_id === $other_node->idBin())
return true;
}
return false;
}
public function addNodeList($node_list) {
assert(get_class($node_list) === 'Kademlia\NodeList');
return $this->addNodeArray($node_list->toArray());
}
public function addNodeArray($node_array) {
assert(gettype($node_array) === 'array');
foreach($node_array as $node) {
$this->addNode($node);
}
}
public function addNode($new_node) {
if(!(get_class($new_node) === 'Kademlia\Node' || get_class($new_node) === 'MockNode')) {
assert(false);
}
$found = false;
foreach($this->node_array as $node)
if($node->idBin() === $new_node->idBin()) {
$found = true;
break;
}
if(!$found)
array_push($this->node_array, $new_node);
}
public function toArray() {
return $this->node_array;
}
public function without($other_node_list) {
$this_node_array = $this->toArray();
$other_node_array = $other_node_list->toArray();
$without = [];
foreach($this_node_array as $own_node) {
$found = false;
foreach($other_node_array as $other_node) {
if($own_node->idBin() === $other_node->idBin()) {
$found = true;
break;
}
}
if(!$found)
array_push($without, $own_node);
}
return new NodeList($without);
}
public function groupByProtocols($settings) {
$groups = [];
foreach($this->node_array as $node) {
$prot_id = $node->favoriteProtocolId($settings);
if(!isset($groups[$prot_id]))
$groups[$prot_id] = [];
array_push($groups[$prot_id], $node);
}
foreach($groups as $protocol_id => $node_array) {
$groups[$protocol_id] = new NodeList($node_array);
}
return $groups;
}
public function sendFindNodeRequest(&$settings, $node_id) {
return $this->sendFindRequest(Find\NODE, $settings, $node_id);
}
public function sendFindValueRequest(&$settings, $node_id) {
return $this->sendFindRequest(Find\VALUE, $settings, $node_id);
}
public function sendFindRequest($type, &$settings, $node_id) {
$protocol_groups = $this->groupByProtocols($settings);
unset($protocol_groups['']);
$task_group = new TaskGroup($settings);
foreach($protocol_groups as $prot_id => $node_list) {
$protocol = $settings->instantiateProtocolById($prot_id);
$task = $protocol->sendFindRequest($type, $node_id, $node_list);
$task_group->add($task);
}
return $task_group;
}
public function sendStoreRequest($settings, $key_id, $value, $expire) {
$protocol_groups = $this->groupByProtocols($settings);
unset($protocol_groups['']);
$task_group = new TaskGroup($settings);
foreach($protocol_groups as $prot_id => $node_list) {
$protocol = &$settings->instantiateProtocolById($prot_id);
$task = $protocol->sendStoreRequest($key_id, $value, $expire, $node_list);
$task_group->add($task);
}
return $task_group;
}
public function size() {
return count($this->node_array);
}
public function closestNodes($needle_id, $count) {
$cmp = function ($node_a, $node_b) use ($needle_id) {
$dist_a = $node_a->distanceTo($needle_id);
$dist_b = $node_b->distanceTo($needle_id);
if($dist_a === $dist_b)
return 0;
return ($dist_a < $dist_b ? -1 : +1);
};
usort($this->node_array, $cmp);
$nodes = array_slice($this->node_array, 0, $count);
return new NodeList($nodes);
}
public function jsonSerialize() {
return $this->node_array;
}
}
?>