/** @brief Adds the content of a bucket unsigned into a answer object.
* @param bucket the bucket we have to had unsigned into
*/
-void Answer::addBucket(Bucket* bucket)
+void Answer::addBucket(const Bucket* bucket)
{
xbt_assert((bucket != nullptr), "Provided a NULL bucket");
unsigned int merge(Answer* a);
void trim();
bool destinationFound();
- void addBucket(kademlia::Bucket* bucket);
+ void addBucket(const kademlia::Bucket* bucket);
};
}
#include "s4u-dht-kademlia.hpp"
#include "simgrid/s4u.hpp"
+#include <string>
+
namespace kademlia {
class Message {
unsigned int destination_id_ = 0; // Id we are trying to find, if needed.
Answer* answer_ = nullptr; // Answer to the request made, if needed.
simgrid::s4u::MailboxPtr answer_to_ = nullptr; // mailbox to send the answer to (if not an answer).
- char* issuer_host_name_ = nullptr; // used for logging
+ std::string issuer_host_name_; // used for logging
explicit Message(unsigned int sender_id, unsigned int destination_id, Answer* answer,
simgrid::s4u::MailboxPtr mailbox, const char* hostname)
, destination_id_(destination_id)
, answer_(answer)
, answer_to_(mailbox)
- , issuer_host_name_(xbt_strdup(hostname))
+ , issuer_host_name_(hostname)
{
}
explicit Message(unsigned int sender_id, unsigned int destination_id, simgrid::s4u::MailboxPtr mailbox,
}
Message(const Message&) = delete;
Message& operator=(const Message&) = delete;
- ~Message()
- {
- if (issuer_host_name_)
- xbt_free(issuer_host_name_);
- }
};
}
#endif
} while (not got_answer);
/* Second step: Send a FIND_NODE to a a random node in buckets */
- unsigned int bucket_id = table->findBucket(known_id)->getId();
+ unsigned int bucket_id = table.findBucket(known_id)->getId();
xbt_assert(bucket_id <= IDENTIFIER_SIZE);
for (i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) {
if (bucket_id > i) {
void Node::routingTableUpdate(unsigned int id)
{
// retrieval of the bucket in which the should be
- Bucket* bucket = table->findBucket(id);
+ Bucket* bucket = table.findBucket(id);
// check if the id is already in the bucket.
auto id_pos = std::find(bucket->nodes.begin(), bucket->nodes.end(), id);
{
Answer* answer = new Answer(destination_id);
/* We find the corresponding bucket for the id */
- Bucket* bucket = table->findBucket(destination_id);
+ const Bucket* bucket = table.findBucket(destination_id);
int bucket_id = bucket->getId();
xbt_assert((bucket_id <= IDENTIFIER_SIZE), "Bucket found has a wrong identifier");
/* So, we copy the contents of the bucket unsigned into our answer */
for (int i = 1; answer->getSize() < BUCKET_SIZE && ((bucket_id - i > 0) || (bucket_id + i < IDENTIFIER_SIZE)); i++) {
/* We check the previous buckets */
if (bucket_id - i >= 0) {
- Bucket* bucket_p = table->buckets[bucket_id - i];
+ const Bucket* bucket_p = &table.buckets[bucket_id - i];
answer->addBucket(bucket_p);
}
/* We check the next buckets */
if (bucket_id + i <= IDENTIFIER_SIZE) {
- Bucket* bucket_n = table->buckets[bucket_id + i];
+ const Bucket* bucket_n = &table.buckets[bucket_id + i];
answer->addBucket(bucket_n);
}
}
nodes_added = node_list->merge(msg->answer_);
XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", msg->answer_to_->get_cname(),
- msg->issuer_host_name_, msg->answer_->nodes.size());
+ msg->issuer_host_name_.c_str(), msg->answer_->nodes.size());
} else {
if (msg->answer_) {
routingTableUpdate(msg->sender_id_);
{
routingTableUpdate(msg->sender_id_);
XBT_VERB("Received a FIND_NODE from %s (%s), he's trying to find %08x", msg->answer_to_->get_cname(),
- msg->issuer_host_name_, msg->destination_id_);
+ msg->issuer_host_name_.c_str(), msg->destination_id_);
// Building the answer to the request
Message* answer =
new Message(id_, msg->destination_id_, findClosest(msg->destination_id_),
class Node {
unsigned int id_; // node id - 160 bits
- RoutingTable* table = nullptr; // node routing table
+ RoutingTable table; // node routing table
public:
simgrid::s4u::CommPtr receive_comm;
void* received_msg = nullptr;
unsigned int find_node_success = 0; // Number of find_node which have succeeded.
unsigned int find_node_failed = 0; // Number of find_node which have failed.
- explicit Node(unsigned int node_id) : id_(node_id), table(new RoutingTable(node_id)), receive_comm(nullptr) {}
+ explicit Node(unsigned int node_id) : id_(node_id), table(node_id), receive_comm(nullptr) {}
Node(const Node&) = delete;
Node& operator=(const Node&) = delete;
- ~Node() { delete table; }
unsigned int getId() { return id_; }
bool join(unsigned int known_id);
/** @brief Initialization of a node routing table. */
RoutingTable::RoutingTable(unsigned int node_id) : id_(node_id)
{
- buckets = new Bucket*[IDENTIFIER_SIZE + 1];
+ buckets.reserve(IDENTIFIER_SIZE + 1);
for (unsigned int i = 0; i < IDENTIFIER_SIZE + 1; i++)
- buckets[i] = new Bucket(i);
+ buckets.emplace_back(i);
}
-RoutingTable::~RoutingTable()
-{
- // Free the buckets.
- for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) {
- delete buckets[i];
- }
- delete[] buckets;
-}
-
-void RoutingTable::print()
+void RoutingTable::print() const
{
XBT_INFO("Routing table of %08x:", id_);
for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) {
- if (not buckets[i]->nodes.empty()) {
+ if (not buckets[i].nodes.empty()) {
XBT_INFO("Bucket number %u: ", i);
int j = 0;
- for (auto value : buckets[i]->nodes) {
+ for (auto value : buckets[i].nodes) {
XBT_INFO("Element %d: %08x", j, value);
j++;
}
unsigned int xor_number = id_ ^ id;
unsigned int prefix = get_node_prefix(xor_number, IDENTIFIER_SIZE);
xbt_assert(prefix <= IDENTIFIER_SIZE, "Tried to return a bucket that doesn't exist.");
- return buckets[prefix];
+ return &buckets[prefix];
}
/** Returns if the routing table contains the id. */
bool RoutingTable::contains(unsigned int node_id)
{
- Bucket* bucket = findBucket(node_id);
+ const Bucket* bucket = findBucket(node_id);
return std::find(bucket->nodes.begin(), bucket->nodes.end(), node_id) != bucket->nodes.end();
}
}
#define _KADEMLIA_ROUTING_TABLE_HPP
#include "s4u-dht-kademlia.hpp"
#include <deque>
+#include <vector>
namespace kademlia {
unsigned int id_; // bucket id
public:
std::deque<unsigned int> nodes; // Nodes in the bucket.
- unsigned int getId() { return id_; }
+ unsigned int getId() const { return id_; }
explicit Bucket(unsigned int id) : id_(id) {}
- ~Bucket() = default;
};
/* Node routing table */
class RoutingTable {
unsigned int id_; // node id of the client's routing table
public:
- Bucket** buckets; // Node bucket list - 160 sized.
+ std::vector<Bucket> buckets; // Node bucket list - 160 sized.
explicit RoutingTable(unsigned int node_id);
RoutingTable(const RoutingTable&) = delete;
RoutingTable& operator=(const RoutingTable&) = delete;
- ~RoutingTable();
- void print();
+ void print() const;
Bucket* findBucket(unsigned int id);
bool contains(unsigned int node_id);
};