Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix quit_notify function
[simgrid.git] / examples / msg / chord / chord.c
index 4b6cb50..cad10c1 100644 (file)
@@ -20,7 +20,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
  */
 typedef struct finger {
   int id;
-  const char* mailbox;
+  char* mailbox;
 } s_finger_t, *finger_t;
 
 /**
@@ -28,10 +28,10 @@ typedef struct finger {
  */
 typedef struct node {
   int id;                                 // my id
-  const char* mailbox;
+  char* mailbox;
   s_finger_t fingers[NB_BITS];            // finger table (fingers[0] is my successor)
   int pred_id;                            // predecessor id
-  const char* pred_mailbox;
+  char* pred_mailbox;
   xbt_dynar_t comms;                      // current communications to finish
 } s_node_t, *node_t;
 
@@ -44,6 +44,8 @@ typedef struct task_data {
   int answer_id;
   const char* answer_to;
   const char* issuer_host_name; // used for logging
+  int successor_id;                            // used when quitting
+  int pred_id;                                 // used when quitting
 } s_task_data_t, *task_data_t;
 
 // utility functions
@@ -59,6 +61,7 @@ static int node(int argc, char *argv[]);
 static void initialize_first_node(node_t node);
 static void initialize_finger_table(node_t data, int known_id);
 static void join(node_t node, int known_id);
+static void leave(node_t node);
 
 // Chord core
 static int find_successor(node_t node, int id);
@@ -74,6 +77,7 @@ static void remote_update_finger_table(node_t node, int ask_to_id, int candidate
 static void notify(node_t node, int predecessor_candidate_id);
 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
 static void stabilize(node_t node);
+static void quit_notify(node_t node, int to);
 
 /**
  * \brief Turns an id into an equivalent id in [0, NB_KEYS[
@@ -86,7 +90,6 @@ static int normalize(int id) {
   while (id < 0) {
     id += NB_KEYS;
   }
-
   // make sure id < NB_KEYS
   id = id % NB_KEYS;
 
@@ -149,7 +152,7 @@ static void print_finger_table(node_t node) {
   INFO0("My finger table:");
   INFO0("Start | Succ ");
   for (i = 0; i < NB_BITS; i++) {
-    INFO2(" %4d | %4d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
+    INFO2(" %3d  | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
     pow = pow << 1;
   }
   INFO1("Predecessor: %d", node->pred_id);
@@ -164,9 +167,13 @@ static void print_finger_table(node_t node) {
  */
 int node(int argc, char *argv[])
 {
+  double init_time = MSG_get_clock();
   msg_comm_t comm = NULL;
+  int i;
+  char* mailbox;
+  double deadline;
 
-  xbt_assert0(argc == 2 || argc == 4, "Wrong number of arguments for this node");
+  xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
 
   // initialize my node
   s_node_t node = {0};
@@ -174,12 +181,15 @@ int node(int argc, char *argv[])
   node.mailbox = get_mailbox(node.id);
   node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
 
-  if (argc == 2) { // first ring
+
+  if (argc == 3) { // first ring
     initialize_first_node(&node);
+    deadline = atof(argv[2]);
   }
   else {
     int known_id = atoi(argv[2]);
     double sleep_time = atof(argv[3]);
+    deadline = atof(argv[4]);
 
     // sleep before starting
     INFO1("Let's sleep >>%f", sleep_time);
@@ -189,20 +199,22 @@ int node(int argc, char *argv[])
     join(&node, known_id);
   }
 
-  while (1) {
+  while ((MSG_get_clock( )- init_time) < deadline) {
 
     unsigned int cursor;
     comm = NULL;
     xbt_dynar_foreach(node.comms, cursor, comm) {
       if (MSG_comm_test(comm)) { // FIXME: try with MSG_comm_testany instead
         xbt_dynar_cursor_rm(node.comms, &cursor);
+       MSG_comm_destroy(comm);
       }
     }
 
     m_task_t task = NULL;
-    MSG_error_t res = MSG_task_receive(&task, node.mailbox);
+    MSG_error_t res = MSG_task_receive_with_timeout(&task, node.mailbox,45); // FIXME >> find the right timeout !!
 
-    xbt_assert0(res == MSG_OK, "MSG_task_receive failed");
+    if(res == MSG_OK)                           // else check deadline condition and keep waiting for a task
+    {
 
     // get data
     const char* task_name = MSG_task_get_name(task);
@@ -222,15 +234,13 @@ int node(int argc, char *argv[])
        // otherwise, forward the request to the closest preceding finger in my table
        int closest = closest_preceding_finger(&node, task_data->request_id);
        INFO2("Forwarding 'Find Successor' request for id %d to my closest preceding finger %d", task_data->request_id, closest);
-       comm = MSG_task_isend(task, get_mailbox(closest));
+       mailbox = get_mailbox(closest);
+       comm = MSG_task_isend(task, mailbox);
         xbt_dynar_push(node.comms, &comm);
+        xbt_free(mailbox);
       }
     }
-    /*
-    else if (!strcmp(task_name, "Find Successor Answer")) {
 
-    }
-    */
     else if (!strcmp(task_name, "Find Predecessor")) {
       INFO2("Receiving a 'Find Predecessor' Request from %s for id %d", task_data->issuer_host_name, task_data->request_id);
       // am I the predecessor?
@@ -245,38 +255,55 @@ int node(int argc, char *argv[])
        // otherwise, forward the request to the closest preceding finger in my table
        int closest = closest_preceding_finger(&node, task_data->request_id);
        INFO2("Forwarding 'Find Predecessor' request for id %d to my closest preceding finger %d", task_data->request_id, closest);
-       comm = MSG_task_isend(task, get_mailbox(closest));
+       mailbox = get_mailbox(closest);
+       comm = MSG_task_isend(task, mailbox);
         xbt_dynar_push(node.comms, &comm);
+        xbt_free(mailbox);
       }
     }
-    /*
-    else if (!strcmp(task_name, "Find Predecessor Answer")) {
 
-    }
-    */
     else if (!strcmp(task_name, "Update Finger")) {
       // someone is telling me that he may be my new finger
-      INFO1("Receving an 'Update Finger' from %s", task_data->issuer_host_name);
+      INFO1("Receiving an 'Update Finger' request from %s", task_data->issuer_host_name);
       update_finger_table(&node, task_data->request_id, task_data->request_finger);
     }
     else if (!strcmp(task_name, "Notify")) {
       // someone is telling me that he may be my new predecessor
-      INFO1("Receving an 'Notify' from %s", task_data->issuer_host_name);
+      INFO1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
       notify(&node, task_data->request_id);
     }
-    /*
-    else if (!strcmp(task_name, "Fix Fingers"))
-    {
-      int i;
-      for (i = KEY_BITS - 1 ; i >= 0; i--)
-      {
-       data->fingers[i] = find_finger_elem(data,(data->id)+pow(2,i-1));
-      }
+    else if (!strcmp(task_name, "Predecessor Leaving")) {
+          // my predecessor is about quitting
+          INFO1("Receiving a 'quite notify' from %s", task_data->issuer_host_name);
+          // modify my predecessor
+          node.pred_id = task_data->pred_id;
+          node.pred_mailbox = get_mailbox(node.id);
+          /*TODO :
+          >> notify my new predecessor
+          >> send a notify_predecessors !!
+          */
+
+        }
+    else if (!strcmp(task_name, "Successor Leaving")) {
+          // my successor is about quitting
+          INFO1("Receiving a 'quite notify' from %s", task_data->issuer_host_name);
+          // modify my successor FIXME : this should be implicit ?
+          node.fingers[0].id = task_data->successor_id;
+          node.fingers[0].mailbox = get_mailbox(node.fingers[0].id);
+          /* TODO
+          >> notify my new successor
+          >> update my table & predecessors table */
+        }
     }
-    */
   }
-
+  // leave the ring and quit simulation
+  leave(&node);
   xbt_dynar_free(&node.comms);
+  xbt_free(node.mailbox);
+  xbt_free(node.pred_mailbox);
+  for (i = 0; i < NB_BITS - 1; i++) {
+    xbt_free(node.fingers[i].mailbox);
+  }
 }
 
 /**
@@ -291,7 +318,7 @@ static void initialize_first_node(node_t node)
   int i;
   for (i = 0; i < NB_BITS; i++) {
     node->fingers[i].id = node->id;
-    node->fingers[i].mailbox = node->mailbox;
+    node->fingers[i].mailbox = xbt_strdup(node->mailbox);
   }
   node->pred_id = node->id;
   node->pred_mailbox = node->mailbox;
@@ -308,7 +335,50 @@ static void join(node_t node, int known_id)
   initialize_finger_table(node, known_id); // determine my fingers, asking to known_id
   remote_notify(node, node->fingers[0].id, node->id); // tell my successor that I'm his new predecessor
   notify_predecessors(node); // tell others that I may have became their finger
-  remote_move_keys(node, node->fingers[0].id); // take some key-value pairs from my sucessor
+  remote_move_keys(node, node->fingers[0].id); // take some key-value pairs from my successor
+}
+
+/**
+ * \brief Makes the current node quit the system
+ * \param node the current node
+ */
+static void leave(node_t node)
+{
+       INFO0("Well Guys! I Think it's time for me to quit ;)");
+       quit_notify(node,1);            // notify to my successor ( >>> 1 );
+       quit_notify(node,-1);           // notify my predecessor  ( >>> -1);
+    // TODO ...
+}
+/*
+ * \brief Notify msg to tell my successor||predecessor about my departure
+ * \param node the current node
+ */
+static void quit_notify(node_t node, int to)
+{
+       task_data_t req_data = xbt_new0(s_task_data_t, 1);
+       req_data->request_id = node->id;
+       req_data->successor_id = node->fingers[0].id;
+       req_data->pred_id = node->pred_id;
+       req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
+       const char *task_name = NULL;
+       const char* to_mailbox;
+       if( to == 1)    // notify my successor
+       {
+               to_mailbox = node->fingers[0].mailbox;
+           INFO1("Telling my Successor about my departure via mailbox %s", to_mailbox);
+               task_name = "Predecessor Leaving";
+
+       }
+       else if(to == -1)    // notify my predecessor
+       {
+               to_mailbox = node->pred_mailbox;
+           INFO1("Telling my Predecessor about my departure via mailbox %s",to_mailbox);
+               task_name = "Predecessor Leaving";
+       }
+    m_task_t task = MSG_task_create(task_name, 1000, 5000, req_data);
+    //char* mailbox = get_mailbox(to_mailbox);
+       msg_comm_t comm = MSG_task_isend(task, to_mailbox);
+       xbt_dynar_push(node->comms, &comm);
 }
 
 /*
@@ -386,6 +456,7 @@ static void update_finger_table(node_t node, int candidate_id, int finger_index)
   if (is_in_interval(candidate_id, node->id, node->fingers[finger_index].id - 1)) {
 //    INFO3("Candidate %d is between %d and %d!", candidate_id, node->id + pow, node->fingers[finger_index].id - 1);
     // candidate_id is my new finger
+    xbt_free(node->fingers[finger_index].mailbox);
     node->fingers[finger_index].id = candidate_id;
     node->fingers[finger_index].mailbox = get_mailbox(candidate_id);
     INFO2("My new finger #%d is %d", finger_index, candidate_id);
@@ -414,8 +485,10 @@ static void remote_update_finger_table(node_t node, int ask_to_id, int candidate
   // send a "Update Finger" request to ask_to_id
   INFO3("Sending an 'Update Finger' request to %d: his finger #%d may be %d now", ask_to_id, finger_index, candidate_id);
   m_task_t task = MSG_task_create("Update Finger", 1000, 5000, req_data);
-  msg_comm_t comm = MSG_task_isend(task, get_mailbox(ask_to_id));
+  char* mailbox = get_mailbox(ask_to_id);
+  msg_comm_t comm = MSG_task_isend(task, mailbox);
   xbt_dynar_push(node->comms, &comm);
+  xbt_free(mailbox);
 }
 
 /**
@@ -446,7 +519,7 @@ static int find_successor(node_t node, int id)
 static int remote_find_successor(node_t node, int ask_to, int id)
 {
   s_task_data_t req_data;
-  char *mailbox = bprintf("%s Find Successor", node->mailbox);
+  charmailbox = bprintf("%s Find Successor", node->mailbox);
   req_data.request_id = id;
   req_data.answer_to = mailbox;
   req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
@@ -468,6 +541,7 @@ static int remote_find_successor(node_t node, int ask_to, int id)
   return successor;
 }
 
+
 /**
  * \brief Makes the current node find the predecessor node of an id.
  * \param node the current node
@@ -498,7 +572,7 @@ static int find_predecessor(node_t node, int id)
 static int remote_find_predecessor(node_t node, int ask_to, int id)
 {
   s_task_data_t req_data;
-  char *mailbox = bprintf("%s Find Predecessor", node->mailbox);
+  charmailbox = bprintf("%s Find Predecessor", node->mailbox);
   req_data.request_id = id;
   req_data.answer_to = mailbox;
   req_data.issuer_host_name = MSG_host_get_name(MSG_host_self());
@@ -547,6 +621,7 @@ static void stabilize(node_t node) {
 
   int x = find_predecessor(node, node->fingers[0].id);
   if (is_in_interval(x, node->id + 1, node->fingers[0].id)) {
+    xbt_free(node->fingers[0].mailbox);
     node->fingers[0].id = x;
     node->fingers[0].mailbox = get_mailbox(x);
   }
@@ -589,8 +664,10 @@ static void remote_notify(node_t node, int notify_id, int predecessor_candidate_
   // send a "Notify" request to notify_id
   INFO1("Sending a 'Notify' request to %d", notify_id);
   m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
-  msg_comm_t comm = MSG_task_isend(task, get_mailbox(notify_id));
+  char* mailbox = get_mailbox(notify_id);
+  msg_comm_t comm = MSG_task_isend(task, mailbox);
   xbt_dynar_push(node->comms, &comm);
+  xbt_free(mailbox);
 }
 
 /**