#include "s4u-dht-kademlia.hpp"
#include "simgrid/s4u.hpp"
+#include <memory>
#include <string>
namespace kademlia {
public:
unsigned int sender_id_ = 0; // Id of the guy who sent the task
unsigned int destination_id_ = 0; // Id we are trying to find, if needed.
- Answer* answer_ = nullptr; // Answer to the request made, if needed.
+ std::unique_ptr<Answer> answer_ = nullptr; // Answer to the request made, if needed.
simgrid::s4u::Mailbox* answer_to_ = nullptr; // mailbox to send the answer to (if not an answer).
std::string issuer_host_name_; // used for logging
- explicit Message(unsigned int sender_id, unsigned int destination_id, Answer* answer, simgrid::s4u::Mailbox* mailbox,
- const char* hostname)
+ explicit Message(unsigned int sender_id, unsigned int destination_id, std::unique_ptr<Answer> answer,
+ simgrid::s4u::Mailbox* mailbox, const char* hostname)
: sender_id_(sender_id)
, destination_id_(destination_id)
- , answer_(answer)
+ , answer_(std::move(answer))
, answer_to_(mailbox)
, issuer_host_name_(hostname)
{
static void destroy(void* message)
{
const auto* msg = static_cast<Message*>(message);
- delete msg->answer_;
delete msg;
}
*/
bool Node::join(unsigned int known_id)
{
- const Answer* node_list;
bool got_answer = false;
/* Add the guy we know to our routing table and ourselves. */
got_answer = true;
// retrieve the node list and ping them.
const auto* msg = static_cast<Message*>(received_msg);
- node_list = msg->answer_;
+ const Answer* node_list = msg->answer_.get();
if (node_list) {
for (auto const& contact : node_list->getNodes())
routingTableUpdate(contact.first);
} else {
handleFindNode(msg);
}
- delete msg->answer_;
delete msg;
receive_comm = nullptr;
} else
* @param node : our node
* @param destination_id : the id of the guy we are trying to find
*/
-Answer* Node::findClosest(unsigned int destination_id)
+std::unique_ptr<Answer> Node::findClosest(unsigned int destination_id)
{
- auto* answer = new Answer(destination_id);
+ auto answer = std::make_unique<Answer>(destination_id);
/* We find the corresponding bucket for the id */
const Bucket* bucket = table.findBucket(destination_id);
int bucket_id = bucket->getId();
unsigned int steps = 0;
/* First we build a list of who we already know */
- Answer* node_list = findClosest(id_to_find);
+ std::unique_ptr<Answer> node_list = findClosest(id_to_find);
xbt_assert((node_list != nullptr), "node_list incorrect");
XBT_DEBUG("Doing a FIND_NODE on %08x", id_to_find);
/* Ask the nodes on our list if they have information about the node we are trying to find */
do {
answers = 0;
- queries = sendFindNodeToBest(node_list);
+ queries = sendFindNodeToBest(node_list.get());
nodes_added = 0;
double timeout = simgrid::s4u::Engine::get_clock() + FIND_NODE_TIMEOUT;
steps++;
routingTableUpdate(contact.first);
answers++;
- nodes_added = node_list->merge(msg->answer_);
+ nodes_added = node_list->merge(msg->answer_.get());
XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", msg->answer_to_->get_cname(),
msg->issuer_host_name_.c_str(), msg->answer_->getSize());
} else {
timeout += simgrid::s4u::Engine::get_clock() - time_beginreceive;
time_beginreceive = simgrid::s4u::Engine::get_clock();
}
- delete msg->answer_;
delete msg;
receive_comm = nullptr;
} else {
XBT_VERB("%08x not found in %u steps", id_to_find, steps);
}
}
- delete node_list;
return destination_found;
}
#include "routing_table.hpp"
#include "s4u-dht-kademlia.hpp"
+#include <memory>
+
namespace kademlia {
class Node {
void sendFindNode(unsigned int id, unsigned int destination) const;
unsigned int sendFindNodeToBest(const Answer* node_list) const;
void routingTableUpdate(unsigned int id);
- Answer* findClosest(unsigned int destination_id);
+ std::unique_ptr<Answer> findClosest(unsigned int destination_id);
bool findNode(unsigned int id_to_find, bool count_in_stats);
void randomLookup();
void handleFindNode(const Message* msg);