Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::unique_ptr for Message::answer_.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 23 Nov 2020 09:53:21 +0000 (10:53 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 23 Nov 2020 10:07:52 +0000 (11:07 +0100)
examples/s4u/dht-kademlia/message.hpp
examples/s4u/dht-kademlia/node.cpp
examples/s4u/dht-kademlia/node.hpp
examples/s4u/dht-kademlia/s4u-dht-kademlia.cpp

index 15f5e5f..a51b902 100644 (file)
@@ -9,6 +9,7 @@
 #include "s4u-dht-kademlia.hpp"
 #include "simgrid/s4u.hpp"
 
+#include <memory>
 #include <string>
 
 namespace kademlia {
@@ -17,15 +18,15 @@ class Message {
 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)
   {
index 86130d5..d72a5c3 100644 (file)
@@ -12,7 +12,6 @@ namespace kademlia {
 static void destroy(void* message)
 {
   const auto* msg = static_cast<Message*>(message);
-  delete msg->answer_;
   delete msg;
 }
 
@@ -22,7 +21,6 @@ static void destroy(void* message)
   */
 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. */
@@ -41,14 +39,13 @@ bool Node::join(unsigned int known_id)
       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
@@ -142,9 +139,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)
 {
-  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();
@@ -186,14 +183,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++;
@@ -214,7 +211,7 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats)
             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 {
@@ -228,7 +225,6 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats)
           timeout += simgrid::s4u::Engine::get_clock() - time_beginreceive;
           time_beginreceive = simgrid::s4u::Engine::get_clock();
         }
-        delete msg->answer_;
         delete msg;
         receive_comm = nullptr;
       } else {
@@ -251,7 +247,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;
 }
 
index f65106c..c508c06 100644 (file)
@@ -11,6 +11,8 @@
 #include "routing_table.hpp"
 #include "s4u-dht-kademlia.hpp"
 
+#include <memory>
+
 namespace kademlia {
 
 class Node {
@@ -30,7 +32,7 @@ public:
   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);
index dd3acdd..e8fcbcd 100644 (file)
@@ -55,7 +55,6 @@ static void node(std::vector<std::string> args)
         const auto* msg = static_cast<kademlia::Message*>(node.received_msg);
         if (msg) {
           node.handleFindNode(msg);
-          delete msg->answer_;
           delete msg;
           node.receive_comm = nullptr;
         } else