1 /* Copyright (c) 2012-2019. 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. */
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(kademlia_node);
13 bool sortbydistance(const std::pair<unsigned int, unsigned int>& a, const std::pair<unsigned int, unsigned int>& b)
15 return (a.second < b.second);
18 /** @brief Prints a answer_t, for debugging purposes */
21 XBT_INFO("Searching %08x, size %u", destination_id_, size_);
23 for (auto contact : nodes)
24 XBT_INFO("Node %08x: %08x is at distance %u", i++, contact.first, contact.second);
27 /** @brief Merge two answers together, only keeping the best nodes
28 * @param source the source of the nodes to add
30 unsigned int Answer::merge(Answer* source)
35 unsigned int nb_added = 0;
36 for (auto contact : source->nodes) {
37 if (std::find(nodes.begin(), nodes.end(), contact) == nodes.end()) {
38 nodes.push_back(contact);
43 std::sort(nodes.begin(), nodes.end(), sortbydistance);
48 /** @brief Trims an Answer, in order for it to have a size of less or equal to "bucket_size" */
51 while (size_ > BUCKET_SIZE) {
55 xbt_assert(nodes.size() == size_, "Wrong size for the answer");
58 /** @brief Returns if the destination we are trying to find is found
59 * @return if the destination is found.
61 bool Answer::destinationFound()
66 return (*nodes.begin()).second == 0;
69 /** @brief Adds the content of a bucket unsigned into a answer object.
70 * @param bucket the bucket we have to had unsigned into
72 void Answer::addBucket(const Bucket* bucket)
74 xbt_assert((bucket != nullptr), "Provided a NULL bucket");
76 for (auto id : bucket->nodes) {
77 unsigned int distance = id ^ destination_id_;
78 nodes.push_back(std::pair<unsigned int, unsigned int>(id, distance));