Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update ns3 wifi to match loic's simgrid wifi
[simgrid.git] / examples / s4u / dht-kademlia / node.cpp
index 76ac9716b43417d639fb7604b414350f1f94909d..d331cc361baf2af24d2236b8ea3e4cd40695ccab 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2020. 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. */
@@ -44,7 +44,7 @@ bool Node::join(unsigned int known_id)
       const Message* msg = static_cast<Message*>(received_msg);
       node_list    = msg->answer_;
       if (node_list) {
-        for (auto contact : node_list->nodes)
+        for (auto const& contact : node_list->getNodes())
           routingTableUpdate(contact.first);
       } else {
         handleFindNode(msg);
@@ -81,6 +81,7 @@ void Node::sendFindNode(unsigned int id, unsigned int destination)
   /* 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());
 
@@ -98,7 +99,7 @@ unsigned int Node::sendFindNodeToBest(const Answer* node_list)
   unsigned int i           = 0;
   unsigned int j           = 0;
   unsigned int destination = node_list->getDestinationId();
-  for (auto node_to_query : node_list->nodes) {
+  for (auto const& node_to_query : node_list->getNodes()) {
     /* We need to have at most "KADEMLIA_ALPHA" requests each time, according to the protocol */
     /* Gets the node we want to send the query to */
     if (node_to_query.first != id_) { /* No need to query ourselves */
@@ -121,19 +122,19 @@ void Node::routingTableUpdate(unsigned int 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);
+  auto id_pos = std::find(bucket->nodes_.begin(), bucket->nodes_.end(), id);
 
-  if (id_pos == bucket->nodes.end()) {
+  if (id_pos == bucket->nodes_.end()) {
     /* We check if the bucket is full or not. If it is, we evict an old element */
-    if (bucket->nodes.size() >= BUCKET_SIZE) {
-      bucket->nodes.pop_back();
+    if (bucket->nodes_.size() >= BUCKET_SIZE) {
+      bucket->nodes_.pop_back();
     }
-    bucket->nodes.push_front(id);
+    bucket->nodes_.push_front(id);
     XBT_VERB("I'm adding to my routing table %08x", id);
   } else {
     // We push the element to the front
-    bucket->nodes.erase(id_pos);
-    bucket->nodes.push_front(id);
+    bucket->nodes_.erase(id_pos);
+    bucket->nodes_.push_front(id);
     XBT_VERB("I'm updating %08x", id);
   }
 }
@@ -158,17 +159,16 @@ Answer* Node::findClosest(unsigned int destination_id)
   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) {
-      const Bucket* bucket_p = &table.buckets[bucket_id - i];
+      const Bucket* bucket_p = &table.getBucketAt(bucket_id - i);
       answer->addBucket(bucket_p);
     }
     /* We check the next buckets */
     if (bucket_id + i <= IDENTIFIER_SIZE) {
-      const Bucket* bucket_n = &table.buckets[bucket_id + i];
+      const Bucket* bucket_n = &table.getBucketAt(bucket_id + i);
       answer->addBucket(bucket_n);
     }
   }
   /* We trim the array to have only BUCKET_SIZE or less elements */
-  std::sort(answer->nodes.begin(), answer->nodes.end(), sortbydistance);
   answer->trim();
 
   return answer;
@@ -211,13 +211,13 @@ bool Node::findNode(unsigned int id_to_find, bool count_in_stats)
         if (msg->answer_ && msg->answer_->getDestinationId() == id_to_find) {
           routingTableUpdate(msg->sender_id_);
           // Handle the answer
-          for (auto contact : node_list->nodes)
+          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_->nodes.size());
+                    msg->issuer_host_name_.c_str(), msg->answer_->getSize());
         } else {
           if (msg->answer_) {
             routingTableUpdate(msg->sender_id_);
@@ -280,7 +280,12 @@ void Node::handleFindNode(const Message* msg)
   // Sending the answer
   msg->answer_to_->put_init(answer, 1)->detach(kademlia::destroy);
 }
+
+void Node::displaySuccessRate()
+{
+  XBT_INFO("%u/%u FIND_NODE have succeeded", find_node_success, find_node_success + find_node_failed);
 }
+} // namespace kademlia
 /**@brief Returns an identifier which is in a specific bucket of a routing table
  * @param id id of the routing table owner
  * @param prefix id of the bucket where we want that identifier to be