Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / s4u / dht-kademlia / node.cpp
index d331cc3..8ffc0c1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -11,8 +11,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(kademlia_node, "Messages specific for this example"
 namespace kademlia {
 static void destroy(void* message)
 {
-  const Message* msg = static_cast<Message*>(message);
-  delete msg->answer_;
+  const auto* msg = static_cast<Message*>(message);
   delete msg;
 }
 
@@ -22,8 +21,6 @@ static void destroy(void* message)
   */
 bool Node::join(unsigned int known_id)
 {
-  const Answer* node_list;
-  unsigned int i;
   bool got_answer = false;
 
   /* Add the guy we know to our routing table and ourselves. */
@@ -36,30 +33,28 @@ bool Node::join(unsigned int known_id)
   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_));
   do {
     if (receive_comm == nullptr)
-      receive_comm = mailbox->get_async(&received_msg);
+      receive_comm = mailbox->get_async<Message>(&received_msg);
     if (receive_comm->test()) {
       XBT_DEBUG("Received an answer from the node I know.");
       got_answer = true;
       // retrieve the node list and ping them.
-      const Message* msg = static_cast<Message*>(received_msg);
-      node_list    = msg->answer_;
+      const Answer* node_list = received_msg->answer_.get();
       if (node_list) {
         for (auto const& contact : node_list->getNodes())
           routingTableUpdate(contact.first);
       } else {
-        handleFindNode(msg);
+        handleFindNode(received_msg);
       }
-      delete msg->answer_;
-      delete msg;
+      delete received_msg;
       receive_comm = nullptr;
     } else
       simgrid::s4u::this_actor::sleep_for(1);
   } while (not got_answer);
 
-  /* Second step: Send a FIND_NODE to a random node in buckets */
+  /* Second step: Send a FIND_NODE to a random node in buckets */
   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++) {
+  for (unsigned int i = 0; ((bucket_id > i) || (bucket_id + i) <= IDENTIFIER_SIZE) && i < JOIN_BUCKETS_QUERIES; i++) {
     if (bucket_id > i) {
       unsigned int id_in_bucket = get_id_in_prefix(id_, bucket_id - i);
       findNode(id_in_bucket, false);
@@ -76,14 +71,14 @@ bool Node::join(unsigned int known_id)
   * @param id node we are querying
   * @param destination node we are trying to find.
   */
-void Node::sendFindNode(unsigned int id, unsigned int destination)
+void Node::sendFindNode(unsigned int id, unsigned int destination) const
 {
   /* Gets the mailbox to send to */
   simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id));
   /* Build the task */
 
-  Message* msg = new Message(id_, destination, simgrid::s4u::Mailbox::by_name(std::to_string(id_)),
-                             simgrid::s4u::Host::current()->get_cname());
+  auto* msg = new Message(id_, destination, simgrid::s4u::Mailbox::by_name(std::to_string(id_)),
+                          simgrid::s4u::Host::current()->get_cname());
 
   /* Send the task */
   mailbox->put_init(msg, 1)->detach(kademlia::destroy);
@@ -94,7 +89,7 @@ void Node::sendFindNode(unsigned int id, unsigned int destination)
   * Sends to the best "KADEMLIA_ALPHA" nodes in the "node_list" array a "FIND_NODE" request, to ask them for their best
   * nodes
   */
-unsigned int Node::sendFindNodeToBest(const Answer* node_list)
+unsigned int Node::sendFindNodeToBest(const Answer* node_list) const
 {
   unsigned int i           = 0;
   unsigned int j           = 0;
@@ -143,9 +138,9 @@ void Node::routingTableUpdate(unsigned int id)
   * @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)
 {
-  Answer* 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();
@@ -187,14 +182,14 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats)
   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++;
@@ -203,34 +198,32 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats)
     simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_));
     do {
       if (receive_comm == nullptr)
-        receive_comm = mailbox->get_async(&received_msg);
+        receive_comm = mailbox->get_async<Message>(&received_msg);
 
       if (receive_comm->test()) {
-        const Message* msg = static_cast<Message*>(received_msg);
         // Check if what we have received is what we are looking for.
-        if (msg->answer_ && msg->answer_->getDestinationId() == id_to_find) {
-          routingTableUpdate(msg->sender_id_);
+        if (received_msg->answer_ && received_msg->answer_->getDestinationId() == id_to_find) {
+          routingTableUpdate(received_msg->sender_id_);
           // Handle the answer
           for (auto const& contact : node_list->getNodes())
             routingTableUpdate(contact.first);
           answers++;
 
-          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_.c_str(), msg->answer_->getSize());
+          nodes_added = node_list->merge(received_msg->answer_.get());
+          XBT_DEBUG("Received an answer from %s (%s) with %zu nodes on it", received_msg->answer_to_->get_cname(),
+                    received_msg->issuer_host_name_.c_str(), received_msg->answer_->getSize());
         } else {
-          if (msg->answer_) {
-            routingTableUpdate(msg->sender_id_);
+          if (received_msg->answer_) {
+            routingTableUpdate(received_msg->sender_id_);
             XBT_DEBUG("Received a wrong answer for a FIND_NODE");
           } else {
-            handleFindNode(msg);
+            handleFindNode(received_msg);
           }
           // Update the timeout if we didn't have our answer
           timeout += simgrid::s4u::Engine::get_clock() - time_beginreceive;
           time_beginreceive = simgrid::s4u::Engine::get_clock();
         }
-        delete msg->answer_;
-        delete msg;
+        delete received_msg;
         receive_comm = nullptr;
       } else {
         simgrid::s4u::this_actor::sleep_for(1);
@@ -252,7 +245,6 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats)
       XBT_VERB("%08x not found in %u steps", id_to_find, steps);
     }
   }
-  delete node_list;
   return destination_found;
 }
 
@@ -274,14 +266,14 @@ void Node::handleFindNode(const Message* msg)
   XBT_VERB("Received a FIND_NODE from %s (%s), he's trying to find %08x", msg->answer_to_->get_cname(),
            msg->issuer_host_name_.c_str(), msg->destination_id_);
   // Building the answer to the request
-  Message* answer =
+  auto* answer =
       new Message(id_, msg->destination_id_, findClosest(msg->destination_id_),
                   simgrid::s4u::Mailbox::by_name(std::to_string(id_)), simgrid::s4u::Host::current()->get_cname());
   // Sending the answer
   msg->answer_to_->put_init(answer, 1)->detach(kademlia::destroy);
 }
 
-void Node::displaySuccessRate()
+void Node::displaySuccessRate() const
 {
   XBT_INFO("%u/%u FIND_NODE have succeeded", find_node_success, find_node_success + find_node_failed);
 }
@@ -295,7 +287,7 @@ unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix)
   if (prefix == 0) {
     return 0;
   } else {
-    return (1U << ((unsigned int)(prefix - 1))) ^ id;
+    return (1U << (prefix - 1)) ^ id;
   }
 }