Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert kademlia example to s4u
[simgrid.git] / examples / s4u / dht-kademlia / node.hpp
1 /* Copyright (c) 2012, 2014-2017. 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 namespace kademlia {
15
16 class Node {
17   unsigned int id_;              // node id - 160 bits
18   RoutingTable* table = nullptr; // node routing table
19 public:
20   simgrid::s4u::CommPtr receive_comm;
21   void* received_msg             = nullptr;
22   unsigned int find_node_success = 0; // Number of find_node which have succeeded.
23   unsigned int find_node_failed  = 0; // Number of find_node which have failed.
24   explicit Node(unsigned int node_id) : id_(node_id), table(new RoutingTable(node_id)), receive_comm(nullptr) {}
25   ~Node() { delete table; }
26   unsigned int getId() { return id_; }
27
28   bool join(unsigned int known_id);
29   void sendFindNode(unsigned int id, unsigned int destination);
30   unsigned int sendFindNodeToBest(Answer* node_list);
31   void routingTableUpdate(unsigned int id);
32   Answer* findClosest(unsigned int destination_id);
33   bool findNode(unsigned int id_to_find, bool count_in_stats);
34   void randomLookup();
35   void handleFindNode(Message* msg);
36 };
37 }
38 // identifier functions
39 unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix);
40 unsigned int get_node_prefix(unsigned int id, unsigned int nb_bits);
41
42 #endif /* _MSG_EXAMPLES_ROUTING_H */