Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove global variable.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 6 Feb 2023 12:58:59 +0000 (13:58 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Mon, 6 Feb 2023 12:58:59 +0000 (13:58 +0100)
examples/cpp/dht-chord/s4u-dht-chord-node.cpp
examples/cpp/dht-chord/s4u-dht-chord.cpp
examples/cpp/dht-chord/s4u-dht-chord.hpp

index e591638..03dfffa 100644 (file)
@@ -23,7 +23,7 @@ namespace sg4 = simgrid::s4u;
  * @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;
@@ -41,6 +41,13 @@ static bool is_in_interval(int id, int start, int end)
   return i <= e;
 }
 
+void Node::set_parameters(int nb_bits, int nb_keys, int timeout)
+{
+  Node::nb_bits = nb_bits;
+  Node::nb_keys = nb_keys;
+  Node::timeout = timeout;
+}
+
 void ChordMessage::destroy(void* message)
 {
   delete static_cast<ChordMessage*>(message);
index 0e15152..08934aa 100644 (file)
@@ -7,10 +7,6 @@
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_chord, "Messages specific for this s4u example");
 
-int nb_bits  = 24;
-int nb_keys  = 0;
-int timeout  = 50;
-
 int main(int argc, char* argv[])
 {
   simgrid::s4u::Engine e(&argc, argv);
@@ -20,6 +16,8 @@ int main(int argc, char* argv[])
              argv[0], argv[0]);
   std::string platform_file(argv[argc - 2]);
   std::string deployment_file(argv[argc - 1]);
+  int nb_bits = 24;
+  int timeout = 50;
   for (const auto& option : std::vector<std::string>(argv + 1, argv + argc - 2)) {
     if (option.rfind("-nb_bits=", 0) == 0) {
       nb_bits = std::stoi(option.substr(option.find('=') + 1));
@@ -31,12 +29,13 @@ int main(int argc, char* argv[])
       xbt_die("Invalid chord option '%s'", option.c_str());
     }
   }
+  int nb_keys = 1U << nb_bits;
+  XBT_DEBUG("Sets nb_keys to %d", nb_keys);
 
   e.load_platform(platform_file);
 
   /* Global initialization of the Chord simulation. */
-  nb_keys = 1U << nb_bits;
-  XBT_DEBUG("Sets nb_keys to %d", nb_keys);
+  Node::set_parameters(nb_bits, nb_keys, timeout);
 
   e.register_actor<Node>("node");
   e.load_deployment(deployment_file);
index 6566ff7..b12c6cd 100644 (file)
@@ -18,10 +18,6 @@ constexpr double PERIODIC_CHECK_PREDECESSOR_DELAY = 120;
 constexpr double PERIODIC_LOOKUP_DELAY            = 10;
 constexpr double SLEEP_DELAY                      = 4.9999;
 
-extern int nb_bits;
-extern int nb_keys;
-extern int timeout;
-
 /* Types of tasks exchanged between nodes. */
 enum class MessageType {
   FIND_SUCCESSOR,
@@ -50,6 +46,10 @@ public:
 };
 
 class Node {
+  inline static int nb_bits;
+  inline static int nb_keys;
+  inline static int timeout;
+
   int known_id_      = -1;
   double start_time_ = -1;
   double deadline_   = -1;
@@ -61,7 +61,11 @@ class Node {
   std::vector<int> fingers_;         // finger table,(fingers[0] is my successor)
   int next_finger_to_fix;            // index of the next finger to fix in fix_fingers()
 
+  static bool is_in_interval(int id, int start, int end);
+
 public:
+  static void set_parameters(int nb_bits, int nb_keys, int timeout);
+
   explicit Node(std::vector<std::string> args);
   Node(const Node&) = delete;
   Node& operator=(const Node&) = delete;