Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Constify pointer and reference local variables in examples/.
[simgrid.git] / examples / deprecated / msg / dht-kademlia / dht-kademlia.c
index 09afc14..f33ba10 100644 (file)
@@ -20,7 +20,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia, "Messages specific for this msg examp
 /* Main loop for the process */
 static void main_loop(node_t node, double deadline)
 {
-  double next_lookup_time = MSG_get_clock() + random_lookup_interval;
+  double next_lookup_time = MSG_get_clock() + RANDOM_LOOKUP_INTERVAL;
   XBT_VERB("Main loop start");
   while (MSG_get_clock() < deadline) {
 
@@ -33,7 +33,7 @@ static void main_loop(node_t node, double deadline)
         /* We search for a pseudo random node */
         if (MSG_get_clock() >= next_lookup_time) {
           random_lookup(node);
-          next_lookup_time += random_lookup_interval;
+          next_lookup_time += RANDOM_LOOKUP_INTERVAL;
         } else {
           //Didn't get a task: sleep for a while...
           MSG_process_sleep(1);
@@ -111,7 +111,7 @@ static int node(int argc, char *argv[])
   */
 unsigned int join(node_t node, unsigned int id_known)
 {
-  answer_t node_list;
+  const s_answer_t* node_list;
   msg_error_t status;
   unsigned int trial = 0;
   unsigned int i;
@@ -137,7 +137,7 @@ unsigned int join(node_t node, unsigned int id_known)
           XBT_DEBUG("Received an answer from the node I know.");
           answer_got = 1;
           //retrieve the node list and ping them.
-          task_data_t data = MSG_task_get_data(node->task_received);
+          const s_task_data_t* data = MSG_task_get_data(node->task_received);
           xbt_assert((data != NULL), "Null data received");
           if (data->type == TASK_FIND_NODE_ANSWER) {
             node_contact_t contact;
@@ -158,15 +158,15 @@ unsigned int join(node_t node, unsigned int id_known)
     } else {
       MSG_process_sleep(1);
     }
-  } while (answer_got == 0 && trial < max_join_trials);
+  } 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) || (bucket_id + i) <= identifier_size) && i < JOIN_BUCKETS_QUERIES; i++) {
+  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);
     }
-    if (bucket_id + i <= identifier_size) {
+    if (bucket_id + i <= IDENTIFIER_SIZE) {
       unsigned int id_in_bucket = get_id_in_prefix(node->id, bucket_id + i);
       find_node(node, id_in_bucket, 0);
     }
@@ -185,7 +185,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_
   unsigned int answers;
   unsigned int destination_found = 0;
   unsigned int nodes_added = 0;
-  double global_timeout = MSG_get_clock() + find_node_global_timeout;
+  double global_timeout          = MSG_get_clock() + FIND_NODE_GLOBAL_TIMEOUT;
   unsigned int steps = 0;
 
   /* First we build a list of who we already know */
@@ -201,7 +201,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_
     answers = 0;
     queries = send_find_node_to_best(node, node_list);
     nodes_added = 0;
-    double timeout = MSG_get_clock() + find_node_timeout;
+    double timeout = MSG_get_clock() + FIND_NODE_TIMEOUT;
     steps++;
     double time_beginreceive = MSG_get_clock();
     do {
@@ -217,7 +217,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_
           if (status == MSG_OK) {
             xbt_assert((node->task_received != NULL), "Invalid task received");
             //Figure out if we received an answer or something else
-            task_data_t data = MSG_task_get_data(node->task_received);
+            const s_task_data_t* data = MSG_task_get_data(node->task_received);
             xbt_assert((data != NULL), "No data in the task");
 
             //Check if what we have received is what we are looking for.
@@ -268,66 +268,13 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_
   return destination_found;
 }
 
-/** @brief Pings a node in the system to see if it is online.
-  * @param node Our node data
-  * @param id_to_ping the id of a node we want to see if it is online.
-  * @return if the ping succeded or not.
-  */
-unsigned int ping(node_t node, unsigned int id_to_ping)
-{
-  char mailbox[MAILBOX_NAME_SIZE];
-  snprintf(mailbox, MAILBOX_NAME_SIZE, "%u", id_to_ping);
-
-  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()));
-  msg_task_t task_received = NULL;
-
-  XBT_VERB("PING %08x", id_to_ping);
-
-  //Check that we aren't trying to ping ourselves
-  if (id_to_ping == node->id) {
-    return 1;
-  }
-
-  /* Sending the ping task */
-  MSG_task_dsend(ping_task, mailbox, task_free_v);
-  do {
-    task_received = NULL;
-    msg_error_t status =
-        MSG_task_receive_with_timeout(&task_received, node->mailbox, ping_timeout);
-    if (status == MSG_OK) {
-      xbt_assert((task_received != NULL), "Invalid task received");
-      //Checking if it's what we are waiting for or not.
-      task_data_t data = MSG_task_get_data(task_received);
-      xbt_assert((data != NULL), "didn't receive any data...");
-      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);
-        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 (MSG_get_clock() < timeout);
-
-  if (MSG_get_clock() >= timeout) {
-    XBT_DEBUG("Ping to %s has timeout.", mailbox);
-    return 0;
-  }
-  XBT_DEBUG("It seems that %s is offline...", mailbox);
-  return -1;
-}
-
 /** @brief Does a pseudo-random lookup for someone in the system
   * @param node caller node data
   */
 void random_lookup(node_t node)
 {
   unsigned int id_to_look = RANDOM_LOOKUP_NODE; //Totally random.
-  /* TODO: Use some pseudorandom generator like RngStream. */
+  /* TODO: Use some pseudorandom generator. */
   XBT_DEBUG("I'm doing a random lookup");
   find_node(node, id_to_look, 1);
 }
@@ -351,17 +298,18 @@ void send_find_node(node_t node, unsigned int id, unsigned int destination)
 }
 
 /**
-  * Sends to the best "kademlia_alpha" nodes in the "node_list" array a "FIND_NODE" request, to ask them for their best nodes
+  * Sends to the best "KADEMLIA_ALPHA" nodes in the "node_list" array a "FIND_NODE" request, to ask them for their best
+ * nodes
   */
 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;
-  while (j < kademlia_alpha && i < node_list->size) {
-    /* We need to have at most "kademlia_alpha" requests each time, according to the protocol */
+  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_contact_t node_to_query = xbt_dynar_get_as(node_list->nodes, i, node_contact_t);
+    const s_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++;
@@ -385,9 +333,6 @@ void handle_task(node_t node, msg_task_t task)
   case TASK_FIND_NODE_ANSWER:
     XBT_DEBUG("Received a wrong answer for a FIND_NODE");
     break;
-  case TASK_PING:
-    handle_ping(node, data);
-    break;
   default:
     break;
   }
@@ -408,16 +353,6 @@ void handle_find_node(node_t node, task_data_t data)
   MSG_task_dsend(task, data->answer_to, task_free_v);
 }
 
-/** @brief handles the answer to a ping */
-void handle_ping(node_t node, task_data_t data)
-{
-  XBT_VERB("Received a PING request from %s (%s)", data->answer_to, data->issuer_host_name);
-  //Building the answer to the request
-  msg_task_t task = task_new_ping_answer(node->id, data->answer_to, MSG_host_get_name(MSG_host_self()));
-
-  MSG_task_dsend(task, data->answer_to, task_free_v);
-}
-
 /** @brief Main function */
 int main(int argc, char *argv[])
 {