Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / examples / cpp / dht-chord / s4u-dht-chord-node.cpp
index 4a2e4f5..9dda4d5 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2023. 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. */
@@ -6,10 +6,16 @@
 #include "s4u-dht-chord.hpp"
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(s4u_chord);
+namespace sg4 = simgrid::s4u;
+
+void ChordMessage::destroy(void* message)
+{
+  delete static_cast<ChordMessage*>(message);
+}
 
 /* Returns whether an id belongs to the interval [start, end].
  *
- * The parameters are normalized to make sure they are between 0 and nb_keys - 1).
+ * The parameters are normalized to make sure they are between 0 and nb_keys_ - 1).
  * 1 belongs to [62, 3]
  * 1 does not belong to [3, 62]
  * 63 belongs to [62, 3]
@@ -22,27 +28,29 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(s4u_chord);
  * @param end upper bound
  * @return true if id in in [start, end]
  */
-static bool is_in_interval(int id, int start, int end)
+bool Node::is_in_interval(int id, int start, int end)
 {
-  int i = id % nb_keys;
-  int s = start % nb_keys;
-  int e = end % nb_keys;
+  int i = id % nb_keys_;
+  int s = start % nb_keys_;
+  int e = end % nb_keys_;
 
   // make sure end >= start and id >= start
   if (e < s) {
-    e += nb_keys;
+    e += nb_keys_;
   }
 
   if (i < s) {
-    i += nb_keys;
+    i += nb_keys_;
   }
 
   return i <= e;
 }
 
-void ChordMessage::destroy(void* message)
+void Node::set_parameters(int nb_bits, int nb_keys, int timeout)
 {
-  delete static_cast<ChordMessage*>(message);
+  nb_bits_ = nb_bits;
+  nb_keys_ = nb_keys;
+  timeout_ = timeout;
 }
 
 /* Initializes the current node as the first one of the system */
@@ -53,14 +61,14 @@ Node::Node(std::vector<std::string> args)
   // initialize my node
   id_                = std::stoi(args[1]);
   XBT_DEBUG("Initialize node with id: %d", id_);
-  random.set_seed(id_);
-  mailbox_           = simgrid::s4u::Mailbox::by_name(std::to_string(id_));
-  next_finger_to_fix = 0;
-  fingers_.resize(nb_bits, id_);
+  random_.set_seed(id_);
+  mailbox_           = sg4::Mailbox::by_name(std::to_string(id_));
+  next_finger_to_fix_ = 0;
+  fingers_.resize(nb_bits_, id_);
 
   if (args.size() == 3) { // first ring
     deadline_   = std::stod(args[2]);
-    start_time_ = simgrid::s4u::Engine::get_clock();
+    start_time_ = sg4::Engine::get_clock();
     XBT_DEBUG("Create a new Chord ring...");
   } else {
     known_id_   = std::stoi(args[2]);
@@ -88,7 +96,7 @@ void Node::join(int known_id)
   } else {
     setFinger(0, successor_id);
     printFingerTable();
-    joined = true;
+    joined_ = true;
   }
 }
 
@@ -97,7 +105,7 @@ void Node::leave()
 {
   XBT_INFO("Well Guys! I Think it's time for me to leave ;)");
   notifyAndQuit();
-  joined = false;
+  joined_ = false;
 }
 
 /* Notifies the successor and the predecessor of the current node before leaving */
@@ -110,7 +118,7 @@ void Node::notifyAndQuit()
 
   XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d", fingers_[0]);
   try {
-    simgrid::s4u::Mailbox::by_name(std::to_string(fingers_[0]))->put(pred_msg, 10, timeout);
+    sg4::Mailbox::by_name(std::to_string(fingers_[0]))->put(pred_msg, 10, timeout_);
   } catch (const simgrid::TimeoutException&) {
     XBT_DEBUG("Timeout expired when sending a 'PREDECESSOR_LEAVING' to my successor %d", fingers_[0]);
     delete pred_msg;
@@ -124,7 +132,7 @@ void Node::notifyAndQuit()
     XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d", pred_id_);
 
     try {
-      simgrid::s4u::Mailbox::by_name(std::to_string(pred_id_))->put(succ_msg, 10, timeout);
+      sg4::Mailbox::by_name(std::to_string(pred_id_))->put(succ_msg, 10, timeout_);
     } catch (const simgrid::TimeoutException&) {
       XBT_DEBUG("Timeout expired when sending a 'SUCCESSOR_LEAVING' to my predecessor %d", pred_id_);
       delete succ_msg;
@@ -136,7 +144,7 @@ void Node::notifyAndQuit()
 void Node::randomLookup()
 {
   int res          = id_;
-  int random_index = random.uniform_int(0, nb_bits - 1);
+  int random_index = random_.uniform_int(0, nb_bits_ - 1);
   int random_id    = fingers_[random_index];
   XBT_DEBUG("Making a lookup request for id %d", random_id);
   if (random_id != id_)
@@ -147,7 +155,7 @@ void Node::randomLookup()
 /* Sets a finger of the current node.
  *
  * @param node the current node
- * @param finger_index index of the finger to set (0 to nb_bits - 1)
+ * @param finger_index index of the finger to set (0 to nb_bits_ - 1)
  * @param id the id to set for this finger
  */
 void Node::setFinger(int finger_index, int id)
@@ -173,13 +181,13 @@ void Node::setPredecessor(int predecessor_id)
 void Node::fixFingers()
 {
   XBT_DEBUG("Fixing fingers");
-  int id = findSuccessor(id_ + (1U << next_finger_to_fix));
+  int id = findSuccessor(id_ + (1U << next_finger_to_fix_));
   if (id != -1) {
-    if (id != fingers_[next_finger_to_fix]) {
-      setFinger(next_finger_to_fix, id);
+    if (id != fingers_[next_finger_to_fix_]) {
+      setFinger(next_finger_to_fix_, id);
       printFingerTable();
     }
-    next_finger_to_fix = (next_finger_to_fix + 1) % nb_bits;
+    next_finger_to_fix_ = (next_finger_to_fix_ + 1) % nb_bits_;
   }
 }
 
@@ -189,8 +197,8 @@ void Node::printFingerTable()
   if (XBT_LOG_ISENABLED(s4u_chord, xbt_log_priority_verbose)) {
     XBT_VERB("My finger table:");
     XBT_VERB("Start | Succ");
-    for (int i = 0; i < nb_bits; i++) {
-      XBT_VERB(" %3u  | %3d", (id_ + (1U << i)) % nb_keys, fingers_[i]);
+    for (int i = 0; i < nb_bits_; i++) {
+      XBT_VERB(" %3u  | %3d", (id_ + (1U << i)) % nb_keys_, fingers_[i]);
     }
 
     XBT_VERB("Predecessor: %d", pred_id_);
@@ -204,8 +212,8 @@ void Node::checkPredecessor()
   if (pred_id_ == -1)
     return;
 
-  simgrid::s4u::Mailbox* mailbox        = simgrid::s4u::Mailbox::by_name(std::to_string(pred_id_));
-  simgrid::s4u::Mailbox* return_mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_is_alive");
+  sg4::Mailbox* mailbox        = sg4::Mailbox::by_name(std::to_string(pred_id_));
+  sg4::Mailbox* return_mailbox = sg4::Mailbox::by_name(std::to_string(id_) + "_is_alive");
 
   auto* message         = new ChordMessage(MessageType::PREDECESSOR_ALIVE);
   message->request_id   = pred_id_;
@@ -213,7 +221,7 @@ void Node::checkPredecessor()
 
   XBT_DEBUG("Sending a 'Predecessor Alive' request to my predecessor %d", pred_id_);
   try {
-    mailbox->put(message, 10, timeout);
+    mailbox->put(message, 10, timeout_);
   } catch (const simgrid::TimeoutException&) {
     XBT_DEBUG("Failed to send the 'Predecessor Alive' request to %d", pred_id_);
     delete message;
@@ -224,10 +232,10 @@ void Node::checkPredecessor()
   XBT_DEBUG("Sent 'Predecessor Alive' request to %d, waiting for the answer on my mailbox '%s'", pred_id_,
             message->answer_to->get_cname());
   ChordMessage* answer       = nullptr;
-  simgrid::s4u::CommPtr comm = return_mailbox->get_async<ChordMessage>(&answer);
+  sg4::CommPtr comm          = return_mailbox->get_async<ChordMessage>(&answer);
 
   try {
-    comm->wait_for(timeout);
+    comm->wait_for(timeout_);
     XBT_DEBUG("Received the answer to my 'Predecessor Alive': my predecessor %d is alive", pred_id_);
     delete answer;
   } catch (const simgrid::TimeoutException&) {
@@ -244,8 +252,8 @@ void Node::checkPredecessor()
 int Node::remoteGetPredecessor(int ask_to)
 {
   int predecessor_id                      = -1;
-  simgrid::s4u::Mailbox* mailbox          = simgrid::s4u::Mailbox::by_name(std::to_string(ask_to));
-  simgrid::s4u::Mailbox* return_mailbox   = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_pred");
+  sg4::Mailbox* mailbox                   = sg4::Mailbox::by_name(std::to_string(ask_to));
+  sg4::Mailbox* return_mailbox            = sg4::Mailbox::by_name(std::to_string(id_) + "_pred");
 
   auto* message         = new ChordMessage(MessageType::GET_PREDECESSOR);
   message->request_id   = id_;
@@ -254,7 +262,7 @@ int Node::remoteGetPredecessor(int ask_to)
   // send a "Get Predecessor" request to ask_to_id
   XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to);
   try {
-    mailbox->put(message, 10, timeout);
+    mailbox->put(message, 10, timeout_);
   } catch (const simgrid::TimeoutException&) {
     XBT_DEBUG("Failed to send the 'Get Predecessor' request to %d", ask_to);
     delete message;
@@ -265,10 +273,10 @@ int Node::remoteGetPredecessor(int ask_to)
   XBT_DEBUG("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to,
             message->answer_to->get_cname());
   ChordMessage* answer       = nullptr;
-  simgrid::s4u::CommPtr comm = return_mailbox->get_async<ChordMessage>(&answer);
+  sg4::CommPtr comm          = return_mailbox->get_async<ChordMessage>(&answer);
 
   try {
-    comm->wait_for(timeout);
+    comm->wait_for(timeout_);
     XBT_DEBUG("Received the answer to my 'Get Predecessor' request: the predecessor of node %d is %d", ask_to,
               answer->answer_id);
     predecessor_id = answer->answer_id;
@@ -288,7 +296,7 @@ int Node::remoteGetPredecessor(int ask_to)
  */
 int Node::closestPrecedingFinger(int id)
 {
-  for (int i = nb_bits - 1; i >= 0; i--) {
+  for (int i = nb_bits_ - 1; i >= 0; i--) {
     if (is_in_interval(fingers_[i], id_ + 1, id - 1)) {
       return fingers_[i];
     }
@@ -315,8 +323,8 @@ int Node::findSuccessor(int id)
 int Node::remoteFindSuccessor(int ask_to, int id)
 {
   int successor                           = -1;
-  simgrid::s4u::Mailbox* mailbox          = simgrid::s4u::Mailbox::by_name(std::to_string(ask_to));
-  simgrid::s4u::Mailbox* return_mailbox   = simgrid::s4u::Mailbox::by_name(std::to_string(id_) + "_succ");
+  sg4::Mailbox* mailbox                   = sg4::Mailbox::by_name(std::to_string(ask_to));
+  sg4::Mailbox* return_mailbox            = sg4::Mailbox::by_name(std::to_string(id_) + "_succ");
 
   auto* message         = new ChordMessage(MessageType::FIND_SUCCESSOR);
   message->request_id   = id_;
@@ -325,7 +333,7 @@ int Node::remoteFindSuccessor(int ask_to, int id)
   // send a "Find Successor" request to ask_to_id
   XBT_DEBUG("Sending a 'Find Successor' request to %d for id %d", ask_to, id);
   try {
-    mailbox->put(message, 10, timeout);
+    mailbox->put(message, 10, timeout_);
   } catch (const simgrid::TimeoutException&) {
     XBT_DEBUG("Failed to send the 'Find Successor' request to %d for id %d", ask_to, id_);
     delete message;
@@ -334,10 +342,10 @@ int Node::remoteFindSuccessor(int ask_to, int id)
   // receive the answer
   XBT_DEBUG("Sent a 'Find Successor' request to %d for key %d, waiting for the answer", ask_to, id);
   ChordMessage* answer       = nullptr;
-  simgrid::s4u::CommPtr comm = return_mailbox->get_async<ChordMessage>(&answer);
+  sg4::CommPtr comm          = return_mailbox->get_async<ChordMessage>(&answer);
 
   try {
-    comm->wait_for(timeout);
+    comm->wait_for(timeout_);
     XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d: the successor of key %d is %d",
               answer->request_id, id_, answer->answer_id);
     successor = answer->answer_id;
@@ -370,7 +378,7 @@ void Node::remoteNotify(int notify_id, int predecessor_candidate_id) const
 
   // send a "Notify" request to notify_id
   XBT_DEBUG("Sending a 'Notify' request to %d", notify_id);
-  simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(notify_id));
+  sg4::Mailbox* mailbox = sg4::Mailbox::by_name(std::to_string(notify_id));
   mailbox->put_init(message, 10)->detach(ChordMessage::destroy);
 }
 
@@ -417,7 +425,7 @@ void Node::handleMessage(ChordMessage* message)
         int closest = closestPrecedingFinger(message->request_id);
         XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
                   message->request_id, closest);
-        simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(closest));
+        sg4::Mailbox* mailbox = sg4::Mailbox::by_name(std::to_string(closest));
         mailbox->put_init(message, 10)->detach(ChordMessage::destroy);
       }
       break;
@@ -478,24 +486,24 @@ void Node::handleMessage(ChordMessage* message)
 
 void Node::operator()()
 {
-  simgrid::s4u::this_actor::sleep_for(start_time_);
+  sg4::this_actor::sleep_for(start_time_);
   if (known_id_ == -1) {
     setPredecessor(-1); // -1 means that I have no predecessor
     printFingerTable();
-    joined = true;
+    joined_ = true;
   } else {
     join(known_id_);
   }
 
-  if (not joined)
+  if (not joined_)
     return;
   ChordMessage* message              = nullptr;
-  double now                         = simgrid::s4u::Engine::get_clock();
+  double now                         = sg4::Engine::get_clock();
   double next_stabilize_date         = start_time_ + PERIODIC_STABILIZE_DELAY;
   double next_fix_fingers_date       = start_time_ + PERIODIC_FIX_FINGERS_DELAY;
   double next_check_predecessor_date = start_time_ + PERIODIC_CHECK_PREDECESSOR_DELAY;
   double next_lookup_date            = start_time_ + PERIODIC_LOOKUP_DELAY;
-  simgrid::s4u::CommPtr comm_receive = nullptr;
+  sg4::CommPtr comm_receive          = nullptr;
   while (now < std::min(start_time_ + deadline_, MAX_SIMULATION_TIME)) {
     if (comm_receive == nullptr)
       comm_receive = mailbox_->get_async<ChordMessage>(&message);
@@ -517,23 +525,23 @@ void Node::operator()()
       // no task was received: make some periodic calls
       if (now >= next_stabilize_date) {
         stabilize();
-        next_stabilize_date = simgrid::s4u::Engine::get_clock() + PERIODIC_STABILIZE_DELAY;
+        next_stabilize_date = sg4::Engine::get_clock() + PERIODIC_STABILIZE_DELAY;
       } else if (now >= next_fix_fingers_date) {
         fixFingers();
-        next_fix_fingers_date = simgrid::s4u::Engine::get_clock() + PERIODIC_FIX_FINGERS_DELAY;
+        next_fix_fingers_date = sg4::Engine::get_clock() + PERIODIC_FIX_FINGERS_DELAY;
       } else if (now >= next_check_predecessor_date) {
         checkPredecessor();
-        next_check_predecessor_date = simgrid::s4u::Engine::get_clock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
+        next_check_predecessor_date = sg4::Engine::get_clock() + PERIODIC_CHECK_PREDECESSOR_DELAY;
       } else if (now >= next_lookup_date) {
         randomLookup();
-        next_lookup_date = simgrid::s4u::Engine::get_clock() + PERIODIC_LOOKUP_DELAY;
+        next_lookup_date = sg4::Engine::get_clock() + PERIODIC_LOOKUP_DELAY;
       } else {
         // nothing to do: sleep for a while
-        simgrid::s4u::this_actor::sleep_for(SLEEP_DELAY);
+        sg4::this_actor::sleep_for(SLEEP_DELAY);
       }
     }
 
-    now = simgrid::s4u::Engine::get_clock();
+    now = sg4::Engine::get_clock();
   }
   if (comm_receive != nullptr) {
     try {