1 /* Copyright (c) 2012-2023. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "routing_table.hpp"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(kademlia_routing_table, "Messages specific for this example");
14 /** @brief Initialization of a node routing table. */
15 RoutingTable::RoutingTable(unsigned int node_id) : id_(node_id)
17 buckets_.reserve(IDENTIFIER_SIZE + 1);
18 for (unsigned int i = 0; i < IDENTIFIER_SIZE + 1; i++)
19 buckets_.emplace_back(i);
22 void RoutingTable::print() const
24 XBT_INFO("Routing table of %08x:", id_);
26 for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) {
27 if (not buckets_[i].nodes_.empty()) {
28 XBT_INFO("Bucket number %u: ", i);
30 for (auto value : buckets_[i].nodes_) {
31 XBT_INFO("Element %d: %08x", j, value);
38 /** @brief Finds the corresponding bucket in a routing table for a given identifier
39 * @param id the identifier
40 * @return the bucket in which the the identifier would be.
42 Bucket* RoutingTable::findBucket(unsigned int id)
44 unsigned int xor_number = id_ ^ id;
45 unsigned int prefix = get_node_prefix(xor_number, IDENTIFIER_SIZE);
46 xbt_assert(prefix <= IDENTIFIER_SIZE, "Tried to return a bucket that doesn't exist.");
47 return &buckets_[prefix];
50 /** Returns if the routing table contains the id. */
51 bool RoutingTable::contains(unsigned int node_id)
53 const Bucket* bucket = findBucket(node_id);
54 return std::find(bucket->nodes_.begin(), bucket->nodes_.end(), node_id) != bucket->nodes_.end();
56 } // namespace kademlia