Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / dht-kademlia / routing_table.hpp
1 /* Copyright (c) 2012-2022. 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_ROUTING_TABLE_HPP
8 #define _KADEMLIA_ROUTING_TABLE_HPP
9 #include "s4u-dht-kademlia.hpp"
10 #include <deque>
11 #include <vector>
12
13 namespace kademlia {
14
15 /* Routing table bucket */
16 class Bucket {
17 public:
18   const unsigned int id_;          // bucket id
19   std::deque<unsigned int> nodes_; // Nodes in the bucket.
20   unsigned int getId() const { return id_; }
21   explicit Bucket(unsigned int id) : id_(id) {}
22   // Use rule-of-three, and implicitely disable the move constructor which cannot be 'noexcept' (as required by C++ Core
23   // Guidelines), due to the std::deque member.
24   Bucket(const Bucket&) = default;
25   ~Bucket()             = default;
26 };
27
28 /* Node routing table */
29 class RoutingTable {
30   unsigned int id_; // node id of the client's routing table
31   std::vector<Bucket> buckets_; // Node bucket list
32 public:
33   explicit RoutingTable(unsigned int node_id);
34   RoutingTable(const RoutingTable&) = delete;
35   RoutingTable& operator=(const RoutingTable&) = delete;
36   void print() const;
37   Bucket* findBucket(unsigned int id);
38   const Bucket& getBucketAt(unsigned int pos) const { return buckets_[pos]; }
39   bool contains(unsigned int node_id);
40 };
41 } // namespace kademlia
42
43 #endif