Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce the scope of some variables to please codacy
[simgrid.git] / examples / msg / dht-kademlia / dht-kademlia.c
index ad73ec1..439ad0f 100644 (file)
@@ -159,8 +159,8 @@ unsigned int join(node_t node, unsigned int id_known)
   } while (answer_got == 0 && trial < max_join_trials);
   /* Second step: Send a FIND_NODE to a a random node in buckets */
   unsigned int bucket_id = routing_table_find_bucket(node->table, id_known)->id;
-  for (i = 0; ((bucket_id - i) > 0 || (bucket_id + i) <= identifier_size) && i < JOIN_BUCKETS_QUERIES; i++) {
-    if (bucket_id - i > 0) {
+  for (i = 0; ((bucket_id  > i) || (bucket_id + i) <= identifier_size) && i < JOIN_BUCKETS_QUERIES; i++) {
+    if (bucket_id  > i) {
       unsigned int id_in_bucket = get_id_in_prefix(node->id, bucket_id - i);
       find_node(node, id_in_bucket, 0);
     }
@@ -280,7 +280,6 @@ unsigned int ping(node_t node, unsigned int id_to_ping)
   char mailbox[MAILBOX_NAME_SIZE];
   snprintf(mailbox,MAILBOX_NAME_SIZE, "%d", id_to_ping);
 
-  unsigned int destination_found = 0;
   double timeout = MSG_get_clock() + ping_timeout;
 
   msg_task_t ping_task = task_new_ping(node->id, node->mailbox, MSG_host_get_name(MSG_host_self()));
@@ -307,24 +306,21 @@ unsigned int ping(node_t node, unsigned int id_to_ping)
       if (data->type == TASK_PING_ANSWER && id_to_ping == data->sender_id) {
         XBT_VERB("Ping to %s succeeded", mailbox);
         node_routing_table_update(node, data->sender_id);
-        destination_found = 1;
         task_free(task_received);
+        return 1; // Destination found, ping succeeded!
       } else {
         //If it's not our answer, we answer the query anyway.
         handle_task(node, task_received);
       }
     }
-  } while (destination_found == 0 && MSG_get_clock() < timeout);
+  } while (MSG_get_clock() < timeout);
 
   if (MSG_get_clock() >= timeout) {
     XBT_DEBUG("Ping to %s has timeout.", mailbox);
     return 0;
   }
-  if (destination_found == -1) {
-    XBT_DEBUG("It seems that %s is offline...", mailbox);
-    return 0;
-  }
-  return 1;
+  XBT_DEBUG("It seems that %s is offline...", mailbox);
+  return -1;
 }
 
 /** @brief Does a pseudo-random lookup for someone in the system
@@ -364,11 +360,10 @@ unsigned int send_find_node_to_best(node_t node, answer_t node_list)
   unsigned int i = 0;
   unsigned int j = 0;
   unsigned int destination = node_list->destination_id;
-  node_contact_t node_to_query;
   while (j < kademlia_alpha && i < node_list->size) {
     /* 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 */
-    node_to_query = xbt_dynar_get_as(node_list->nodes, i, node_contact_t);
+    node_contact_t node_to_query = xbt_dynar_get_as(node_list->nodes, i, node_contact_t);
     if (node_to_query->id != node->id) {        /* No need to query ourselves */
       send_find_node(node, node_to_query->id, destination);
       j++;