Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Factorize handling of asynchronous receives.
[simgrid.git] / examples / cpp / dht-kademlia / node.hpp
1 /* Copyright (c) 2012-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef KADEMLIA_NODE_HPP
8 #define KADEMLIA_NODE_HPP
9 #include "answer.hpp"
10 #include "message.hpp"
11 #include "routing_table.hpp"
12 #include "s4u-dht-kademlia.hpp"
13
14 #include <memory>
15
16 namespace kademlia {
17
18 class Node {
19   unsigned int id_;              // node id - 160 bits
20   RoutingTable table;            // node routing table
21   unsigned int find_node_success = 0; // Number of find_node which have succeeded.
22   unsigned int find_node_failed  = 0; // Number of find_node which have failed.
23   simgrid::s4u::CommPtr receive_comm = nullptr;
24   Message* received_msg              = nullptr;
25
26 public:
27   explicit Node(unsigned int node_id) : id_(node_id), table(node_id) {}
28   Node(const Node&) = delete;
29   Node& operator=(const Node&) = delete;
30   unsigned int getId() const { return id_; }
31
32   Message* receive(simgrid::s4u::Mailbox* mailbox);
33   bool join(unsigned int known_id);
34   void sendFindNode(unsigned int id, unsigned int destination) const;
35   unsigned int sendFindNodeToBest(const Answer* node_list) const;
36   void routingTableUpdate(unsigned int id);
37   std::unique_ptr<Answer> findClosest(unsigned int destination_id);
38   bool findNode(unsigned int id_to_find, bool count_in_stats);
39   void randomLookup();
40   void handleFindNode(const Message* msg);
41   void displaySuccessRate() const;
42 };
43 } // namespace kademlia
44 // identifier functions
45 unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix);
46 unsigned int get_node_prefix(unsigned int id, unsigned int nb_bits);
47
48 #endif /* KADEMLIA_NODE_HPP */