public function testFindNodeResponse()
 {
     return;
     #########################################
     $zeros = str_repeat('00', N / 8);
     $FFs = str_repeat('FF', N / 8);
     $settings = new Kademlia\Settings();
     $settings->own_node_id = $FFs;
     $protocol = new Kademlia\Protocol($settings);
     # fill kbuckets with some nodes
     $node_list = new Kademlia\NodeList([]);
     for ($i = 0; $i < N / 8; $i++) {
         $node_id = $zeros;
         $hex = str_pad(dechex($i), 2, '0', STR_PAD_LEFT);
         $node_id[2 * $i] = $hex[0];
         $node_id[2 * $i + 1] = $hex[1];
         for ($j = $i + 1; $j < N / 8; $j++) {
             $hex = str_pad(dechex($j), 2, '0', STR_PAD_LEFT);
             $node_id[2 * $j] = $hex[0];
             $node_id[2 * $j + 1] = $hex[1];
             $data = ['id' => $node_id];
             $node = new Kademlia\Node($data);
             $node_list->addNode($node);
             $settings->kbuckets->nodeOnline($node);
         }
     }
     $sender = new Kademlia\Node(['id' => $zeros]);
     $response = $protocol->createFindNodeResponse($zeros, $sender);
     $expected_ids = ["0000000000000000000000000000000000001213", "0000000000000000000000000000000000111200", "0000000000000000000000000000000000111213", "0000000000000000000000000000000010110000", "0000000000000000000000000000000010111200", "0000000000000000000000000000000010111213", "0000000000000000000000000000000f10000000", "0000000000000000000000000000000f10110000", "0000000000000000000000000000000f10111200", "0000000000000000000000000000000f10111213", "00000000000000000000000000000e0f00000000", "00000000000000000000000000000e0f10000000", "00000000000000000000000000000e0f10110000", "00000000000000000000000000000e0f10111200", "00000000000000000000000000000e0f10111213", "000000000000000000000000000d0e0000000000", "000000000000000000000000000d0e0f00000000", "000000000000000000000000000d0e0f10000000", "000000000000000000000000000d0e0f10110000", "000000000000000000000000000d0e0f10111200"];
     $getId = function ($node) {
         return $node->idStr();
     };
     $node_ids = array_map($getId, $response->toArray());
     $this->assertEqual($node_ids, $expected_ids);
 }
 public function testClosestNodes()
 {
     $zeros = str_repeat('00', N / 8);
     $node_array = [];
     for ($i = 0; $i < N / 8; $i++) {
         $id = str_repeat('00', N / 8);
         $id[2 * $i + 1] = '1';
         array_push($node_array, KademliaTestFactory::constructNode(['id' => $id]));
     }
     $expected_nearest_node_id = str_repeat('00', N / 8 - 1) . '01';
     $expected_second_nearest_node_id = str_repeat('00', N / 8 - 2) . '0100';
     shuffle($node_array);
     $node_list = new Kademlia\NodeList($node_array);
     $two_closest_nodes = $node_list->closestNodes($zeros, 2)->toArray();
     $nearest_node = $two_closest_nodes[0];
     $second_nearest_node = $two_closest_nodes[1];
     $this->assertNotEqual($nearest_node->idStr(), $second_nearest_node->idStr());
     $this->assertEqual($nearest_node->idStr(), $expected_nearest_node_id);
     $this->assertEqual($second_nearest_node->idStr(), $expected_second_nearest_node_id);
 }
Exemple #3
0
 public function perform()
 {
     $data = ['type' => 'FIND_VALUE', 'key_id' => \Kademlia\Node::binId2hex($this->needle_id)];
     $results = parent::perform($data);
     $node_list = new \Kademlia\NodeList();
     $values = [];
     foreach ($results as $res) {
         $nodes = $this->parseNodeList($res['data']);
         $values = array_merge($values, $this->parseValues($res['data']));
         $node_list->addNodeList($nodes);
     }
     $emitted_result = ['node_list' => $node_list, 'values' => $values];
     if ($this->settings->verbosity >= 4) {
         print \Kademlia\Node::binId2hex($this->settings->own_node_id) . ": FindValue process found these nodes\n";
         var_dump($emitted_result['node_list']);
         print \Kademlia\Node::binId2hex($this->settings->own_node_id) . ": end of FindNode process found nodes\n";
     }
     $type = 'done';
     if (count($values) > 0) {
         $type = 'success';
     }
     $this->emit($type, $emitted_result);
 }