Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Suppressed a bit too much of codes
[simgrid.git] / examples / c / dht-pastry / dht-pastry.c
index e6d6919683a52ae21be8f4dee2feaf603cdf08c0..c50c6a4fbbccf198d11d61cede1b373738b8267b 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2013-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. */
@@ -9,11 +9,11 @@
 #include "simgrid/mailbox.h"
 
 #include "xbt/dynar.h"
+#include "xbt/ex.h"
 #include "xbt/log.h"
 #include "xbt/str.h"
 #include "xbt/sysdep.h"
 
-#include <math.h>
 #include <stdio.h>
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(pastry, "Messages specific for this example");
@@ -83,7 +83,7 @@ unsigned int domain_mask = 0;
 static int domain(unsigned int a, unsigned int level)
 {
   if (domain_mask == 0)
-    domain_mask = pow(2, DOMAIN_SIZE) - 1;
+    domain_mask = (1U << DOMAIN_SIZE) - 1;
   unsigned int shift = (LEVELS_COUNT - level - 1) * DOMAIN_SIZE;
   return (a >> shift) & domain_mask;
 }
@@ -183,7 +183,7 @@ static state_t node_get_state(const_node_t node)
 
 static void print_node_id(const_node_t node)
 {
-  XBT_INFO(" Id: %i '%08x' ", node->id, (unsigned)node->id);
+  XBT_INFO(" Id: %i '%08x'", node->id, (unsigned)node->id);
 }
 
 /* Print the node namespace set */
@@ -265,7 +265,7 @@ static void handle_message(node_t node, pastry_message_t message)
       }
       node->namespace_set[NAMESPACE_SIZE / 2 + j] = message->sender_id;
       node->ready += message->steps + 1;
-      /* no break */
+      /* fallthrough */
     case JOIN_REPLY:
       XBT_DEBUG("Joining Reply");
 
@@ -425,6 +425,7 @@ static int join(const_node_t node)
   if (err == SG_ERROR_TIMEOUT) {
     XBT_DEBUG("Timeout expired when joining ring with node %d", node->known_id);
     message_free(request);
+    return 0;
   }
 
   return 1;
@@ -446,7 +447,7 @@ static void node(int argc, char* argv[])
   double deadline;
   xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
   s_node_t node         = {0};
-  node.id               = xbt_str_parse_int(argv[1], "Invalid ID: %s");
+  node.id               = (int)xbt_str_parse_int(argv[1], "Invalid ID");
   node.known_id         = -1;
   node.ready            = -1;
   node.pending_messages = xbt_dynar_new(sizeof(pastry_message_t), NULL);
@@ -468,14 +469,14 @@ static void node(int argc, char* argv[])
 
   if (argc == 3) { // first ring
     XBT_DEBUG("Hey! Let's create the system.");
-    deadline   = xbt_str_parse_double(argv[2], "Invalid deadline: %s");
+    deadline   = xbt_str_parse_double(argv[2], "Invalid deadline");
     node.ready = 0;
     XBT_DEBUG("Create a new Pastry ring...");
     join_success = 1;
   } else {
-    node.known_id     = xbt_str_parse_int(argv[2], "Invalid known ID: %s");
-    double sleep_time = xbt_str_parse_double(argv[3], "Invalid sleep time: %s");
-    deadline          = xbt_str_parse_double(argv[4], "Invalid deadline: %s");
+    node.known_id     = (int)xbt_str_parse_int(argv[2], "Invalid known ID");
+    double sleep_time = xbt_str_parse_double(argv[3], "Invalid sleep time");
+    deadline          = xbt_str_parse_double(argv[4], "Invalid deadline");
 
     // sleep before starting
     XBT_DEBUG("Let's sleep during %f", sleep_time);
@@ -514,23 +515,21 @@ int main(int argc, char* argv[])
   simgrid_init(&argc, argv);
   xbt_assert(argc > 2,
              "Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n"
-             "\tExample: %s ../msg_platform.xml pastry10.xml\n",
+             "\tExample: %s ../platform.xml pastry10.xml\n",
              argv[0], argv[0]);
 
   char** options = &argv[1];
   while (!strncmp(options[0], "-", 1)) {
-    int length = strlen("-nb_bits=");
+    size_t length = strlen("-nb_bits=");
     if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
-      nb_bits = xbt_str_parse_int(options[0] + length, "Invalid nb_bits parameter: %s");
+      nb_bits = (int)xbt_str_parse_int(options[0] + length, "Invalid nb_bits parameter");
       XBT_DEBUG("Set nb_bits to %d", nb_bits);
     } else {
       length = strlen("-timeout=");
-      if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
-        timeout = xbt_str_parse_int(options[0] + length, "Invalid timeout parameter: %s");
-        XBT_DEBUG("Set timeout to %d", timeout);
-      } else {
-        xbt_die("Invalid pastry option '%s'", options[0]);
-      }
+      xbt_assert(strncmp(options[0], "-timeout=", length) == 0 && strlen(options[0]) > length,
+                 "Invalid pastry option '%s'", options[0]);
+      timeout = (int)xbt_str_parse_int(options[0] + length, "Invalid timeout parameter");
+      XBT_DEBUG("Set timeout to %d", timeout);
     }
     options++;
   }