Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Chord: add a deployment file with 90 nodes and fix some bugs
[simgrid.git] / examples / msg / chord / chord.c
1
2 /* Copyright (c) 2010. The SimGrid Team.
3  * All rights reserved.                                                     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include "msg/msg.h"
10 #include "xbt/log.h"
11 #include "xbt/asserts.h"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
13                              "Messages specific for this msg example");
14
15 #define NB_BITS 16
16 #define NB_KEYS 65536
17 #define COMM_SIZE 10
18 #define COMP_SIZE 0
19 #define TIMEOUT 100
20
21 /**
22  * Finger element.
23  */
24 typedef struct finger {
25   int id;
26   char* mailbox;
27 } s_finger_t, *finger_t;
28
29 /**
30  * Node data.
31  */
32 typedef struct node {
33   int id;                                 // my id
34   char* mailbox;                          // my usual mailbox name
35   s_finger_t fingers[NB_BITS];            // finger table (fingers[0] is my successor)
36   int pred_id;                            // predecessor id
37   char* pred_mailbox;                     // predecessor's mailbox name
38   int next_finger_to_fix;                 // index of the next finger to fix in fix_fingers()
39   msg_comm_t comm_receive;                // current communication to receive
40   xbt_dynar_t comms;                      // current communications being sent
41 } s_node_t, *node_t;
42
43 /**
44  * Types of tasks exchanged between nodes.
45  */
46 typedef enum {
47   TASK_FIND_SUCCESSOR,
48   TASK_FIND_SUCCESSOR_ANSWER,
49   TASK_GET_PREDECESSOR,
50   TASK_GET_PREDECESSOR_ANSWER,
51   TASK_NOTIFY,
52   TASK_SUCCESSOR_LEAVING,
53   TASK_PREDECESSOR_LEAVING
54 } e_task_type_t;
55
56 /**
57  * Data attached with the tasks sent and received
58  */
59 typedef struct task_data {
60   e_task_type_t type;                     // type of task
61   int request_id;                         // id paramater (used by some types of tasks)
62   int request_finger;                     // finger parameter (used by some types of tasks)
63   int answer_id;                          // answer (used by some types of tasks)
64   char* answer_to;                        // mailbox to send an answer to (or NULL)
65   const char* issuer_host_name;           // used for logging
66 } s_task_data_t, *task_data_t;
67
68 static int powers2[NB_BITS];
69
70 // utility functions
71 static void chord_initialize(void);
72 static int normalize(int id);
73 static int is_in_interval(int id, int start, int end);
74 static char* get_mailbox(int host_id);
75 static void task_data_destroy(task_data_t task_data);
76 static void print_finger_table(node_t node);
77 static void set_finger(node_t node, int finger_index, int id);
78 static void set_predecessor(node_t node, int predecessor_id);
79
80 // process functions
81 static int node(int argc, char *argv[]);
82 static void handle_task(node_t node, m_task_t task);
83
84 // Chord core
85 static void create(node_t node);
86 static int join(node_t node, int known_id);
87 static void leave(node_t node);
88 static int find_successor(node_t node, int id);
89 static int remote_find_successor(node_t node, int ask_to_id, int id);
90 static int remote_get_predecessor(node_t node, int ask_to_id);
91 static int closest_preceding_node(node_t node, int id);
92 static void stabilize(node_t node);
93 static void notify(node_t node, int predecessor_candidate_id);
94 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
95 static void fix_fingers(node_t node);
96 static void check_predecessor(node_t node);
97 static void quit_notify(node_t node, int to);
98
99 /**
100  * \brief Global initialization of the Chord simulation.
101  */
102 static void chord_initialize(void)
103 {
104   // compute the powers of 2 once for all
105   int pow = 1;
106   int i;
107   for (i = 0; i < NB_BITS; i++) {
108     powers2[i] = pow;
109     pow = pow << 1;
110   }
111 }
112
113 /**
114  * \brief Turns an id into an equivalent id in [0, NB_KEYS).
115  * \param id an id
116  * \return the corresponding normalized id
117  */
118 static int normalize(int id)
119 {
120   // make sure id >= 0
121   while (id < 0) {
122     id += NB_KEYS;
123   }
124   // make sure id < NB_KEYS
125   id = id % NB_KEYS;
126
127   return id;
128 }
129
130 /**
131  * \brief Returns whether a id belongs to the interval [start, end].
132  *
133  * The parameters are noramlized to make sure they are between 0 and CHORD_NB_KEYS - 1).
134  * 1 belongs to [62, 3]
135  * 1 does not belong to [3, 62]
136  * 63 belongs to [62, 3]
137  * 63 does not belong to [3, 62]
138  * 24 belongs to [21, 29]
139  * 24 does not belong to [29, 21]
140  *
141  * \param id id to check
142  * \param start lower bound
143  * \param end upper bound
144  * \return a non-zero value if id in in [start, end]
145  */
146 static int is_in_interval(int id, int start, int end)
147 {
148   id = normalize(id);
149   start = normalize(start);
150   end = normalize(end);
151
152   // make sure end >= start and id >= start
153   if (end < start) {
154     end += NB_KEYS;
155   }
156
157   if (id < start) {
158     id += NB_KEYS;
159   }
160
161   return id <= end;
162 }
163
164 /**
165  * \brief Gets the mailbox name of a host given its chord id.
166  * \param node_id id of a node
167  * \return the name of its mailbox
168  */
169 static char* get_mailbox(int node_id)
170 {
171   return bprintf("mailbox%d", node_id);
172 }
173
174 /**
175  * \brief Frees the memory used by some task data.
176  * \param task_data the task data to destroy
177  */
178 static void task_data_destroy(task_data_t task_data)
179 {
180   xbt_free(task_data->answer_to);
181   xbt_free(task_data);
182 }
183
184 /**
185  * \brief Displays the finger table of a node.
186  * \param node a node
187  */
188 static void print_finger_table(node_t node)
189 {
190   int i;
191   int pow = 1;
192   INFO0("My finger table:");
193   INFO0("Start | Succ ");
194   for (i = 0; i < NB_BITS; i++) {
195     INFO2(" %3d  | %3d ", (node->id + pow) % NB_KEYS, node->fingers[i].id);
196     pow = pow << 1;
197   }
198   INFO1("Predecessor: %d", node->pred_id);
199 }
200
201 /**
202  * \brief Sets a finger of the current node.
203  * \param node the current node
204  * \param finger_index index of the finger to set (0 to NB_BITS - 1)
205  * \param id the id to set for this finger
206  */
207 static void set_finger(node_t node, int finger_index, int id)
208 {
209   node->fingers[finger_index].id = id;
210   xbt_free(node->fingers[finger_index].mailbox);
211   node->fingers[finger_index].mailbox = get_mailbox(id);
212   DEBUG2("My new finger #%d is %d", finger_index, id);
213 }
214
215 /**
216  * \brief Sets the predecessor of the current node.
217  * \param node the current node
218  * \param id the id to predecessor, or -1 to unset the predecessor
219  */
220 static void set_predecessor(node_t node, int predecessor_id)
221 {
222   node->pred_id = predecessor_id;
223   xbt_free(node->pred_mailbox);
224
225   if (predecessor_id != -1) {
226     node->pred_mailbox = get_mailbox(predecessor_id);
227   }
228
229   DEBUG1("My new predecessor is %d", predecessor_id);
230 }
231
232 /**
233  * \brief Node Function
234  * Arguments:
235  * - my id
236  * - the id of a guy I know in the system (except for the first node)
237  * - the time to sleep before I join (except for the first node)
238  */
239 int node(int argc, char *argv[])
240 {
241   double init_time = MSG_get_clock();
242   m_task_t task = NULL;
243   m_task_t task_received = NULL;
244   msg_comm_t comm_send = NULL;
245   int i;
246   int index;
247   int join_success = 0;
248   double deadline;
249   double next_stabilize_date = init_time + 10;
250   double next_fix_fingers_date = init_time + 10;
251   double next_check_predecessor_date = init_time + 10;
252
253   xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
254
255   // initialize my node
256   s_node_t node = {0};
257   node.id = atoi(argv[1]);
258   node.mailbox = get_mailbox(node.id);
259   node.next_finger_to_fix = 0;
260   node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
261
262   for (i = 0; i < NB_BITS; i++) {
263     set_finger(&node, i, node.id);
264   }
265
266   if (argc == 3) { // first ring
267     deadline = atof(argv[2]);
268     create(&node);
269     join_success = 1;
270   }
271   else {
272     int known_id = atoi(argv[2]);
273     double sleep_time = atof(argv[3]);
274     deadline = atof(argv[4]);
275
276     // sleep before starting
277     DEBUG1("Let's sleep during %f", sleep_time);
278     MSG_process_sleep(sleep_time);
279     DEBUG0("Hey! Let's join the system.");
280
281     join_success = join(&node, known_id);
282   }
283
284   if (join_success) {
285     while (MSG_get_clock() < init_time + deadline) {
286
287       if (node.comm_receive == NULL) {
288         task_received = NULL;
289         node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
290         // FIXME: do not make MSG_task_irecv() calls from several functions
291       }
292
293       if (!MSG_comm_test(node.comm_receive)) {
294
295         // no task was received: make some periodic calls
296         if (MSG_get_clock() >= next_stabilize_date) {
297           stabilize(&node);
298           next_stabilize_date = MSG_get_clock() + 10;
299         }
300         else if (MSG_get_clock() >= next_fix_fingers_date) {
301           fix_fingers(&node);
302           next_fix_fingers_date = MSG_get_clock() + 10;
303         }
304         else if (MSG_get_clock() >= next_check_predecessor_date) {
305           check_predecessor(&node);
306           next_check_predecessor_date = MSG_get_clock() + 10;
307         }
308         else {
309           // nothing to do: sleep for a while
310           MSG_process_sleep(5);
311         }
312       }
313       else {
314         // a transfer has occured
315
316         MSG_error_t status = MSG_comm_get_status(node.comm_receive);
317
318         if (status != MSG_OK) {
319           DEBUG0("Failed to receive a task. Nevermind.");
320           node.comm_receive = NULL;
321         }
322         else {
323           // the task was successfully received
324           MSG_comm_destroy(node.comm_receive);
325           node.comm_receive = NULL;
326           handle_task(&node, task_received);
327         }
328       }
329
330       // see if some communications are finished
331       while ((index = MSG_comm_testany(node.comms)) != -1) {
332         comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
333         MSG_error_t status = MSG_comm_get_status(comm_send);
334         xbt_dynar_remove_at(node.comms, index, &comm_send);
335         DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
336             comm_send, status, xbt_dynar_length(node.comms));
337         MSG_comm_destroy(comm_send);
338       }
339     }
340
341     // clean unfinished comms sent
342     unsigned int cursor;
343     xbt_dynar_foreach(node.comms, cursor, comm_send) {
344       task = MSG_comm_get_task(comm_send);
345       MSG_task_cancel(task);
346       task_data_destroy(MSG_task_get_data(task));
347       MSG_task_destroy(task);
348       MSG_comm_destroy(comm_send);
349       // FIXME: the task is actually not destroyed because MSG thinks that the other side (whose process is dead) is still using it
350     }
351
352     // leave the ring
353     leave(&node);
354   }
355
356   // stop the simulation
357   xbt_dynar_free(&node.comms);
358   xbt_free(node.mailbox);
359   xbt_free(node.pred_mailbox);
360   for (i = 0; i < NB_BITS - 1; i++) {
361     xbt_free(node.fingers[i].mailbox);
362   }
363   return 0;
364 }
365
366 /**
367  * \brief This function is called when the current node receives a task.
368  * \param node the current node
369  * \param task the task to handle (don't touch it then:
370  * it will be destroyed, reused or forwarded)
371  */
372 static void handle_task(node_t node, m_task_t task) {
373
374   DEBUG1("Handling task %p", task);
375   msg_comm_t comm = NULL;
376   char* mailbox = NULL;
377   task_data_t task_data = (task_data_t) MSG_task_get_data(task);
378   e_task_type_t type = task_data->type;
379
380   switch (type) {
381
382     case TASK_FIND_SUCCESSOR:
383       DEBUG2("Receiving a 'Find Successor' request from %s for id %d",
384           task_data->issuer_host_name, task_data->request_id);
385       // is my successor the successor?
386       if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
387         task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
388         task_data->answer_id = node->fingers[0].id;
389         DEBUG3("Sending back a 'Find Successor Answer' to %s: the successor of %d is %d",
390             task_data->issuer_host_name,
391             task_data->request_id, task_data->answer_id);
392         comm = MSG_task_isend(task, task_data->answer_to);
393         xbt_dynar_push(node->comms, &comm);
394       }
395       else {
396         // otherwise, forward the request to the closest preceding finger in my table
397         int closest = closest_preceding_node(node, task_data->request_id);
398         DEBUG2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
399             task_data->request_id, closest);
400         mailbox = get_mailbox(closest);
401         comm = MSG_task_isend(task, mailbox);
402         xbt_dynar_push(node->comms, &comm);
403         xbt_free(mailbox);
404       }
405       break;
406
407     case TASK_GET_PREDECESSOR:
408       DEBUG1("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
409       task_data->type = TASK_GET_PREDECESSOR_ANSWER;
410       task_data->answer_id = node->pred_id;
411       DEBUG3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
412           task_data->issuer_host_name,
413           task_data->answer_to, task_data->answer_id);
414       comm = MSG_task_isend(task, task_data->answer_to);
415       xbt_dynar_push(node->comms, &comm);
416       break;
417
418     case TASK_NOTIFY:
419       // someone is telling me that he may be my new predecessor
420       DEBUG1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
421       notify(node, task_data->request_id);
422       task_data_destroy(task_data);
423       MSG_task_destroy(task);
424       break;
425
426     case TASK_PREDECESSOR_LEAVING:
427       // my predecessor is about to quit
428       DEBUG1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
429       // modify my predecessor
430       set_predecessor(node, task_data->request_id);
431       task_data_destroy(task_data);
432       MSG_task_destroy(task);
433       /*TODO :
434       >> notify my new predecessor
435       >> send a notify_predecessors !!
436        */
437       break;
438
439     case TASK_SUCCESSOR_LEAVING:
440       // my successor is about to quit
441       DEBUG1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
442       // modify my successor FIXME : this should be implicit ?
443       set_finger(node, 0, task_data->request_id);
444       task_data_destroy(task_data);
445       MSG_task_destroy(task);
446       /* TODO
447       >> notify my new successor
448       >> update my table & predecessors table */
449       break;
450
451     case TASK_FIND_SUCCESSOR_ANSWER:
452     case TASK_GET_PREDECESSOR_ANSWER:
453       DEBUG2("Ignoring unexpected task of type %d (%p)", type, task);
454       break;
455   }
456 }
457
458 /**
459  * \brief Initializes the current node as the first one of the system.
460  * \param node the current node
461  */
462 static void create(node_t node)
463 {
464   DEBUG0("Create a new Chord ring...");
465   set_predecessor(node, -1); // -1 means that I have no predecessor
466   print_finger_table(node);
467 }
468
469 /**
470  * \brief Makes the current node join the ring, knowing the id of a node
471  * already in the ring
472  * \param node the current node
473  * \param known_id id of a node already in the ring
474  * \return 1 if the join operation succeeded, 0 otherwise
475  */
476 static int join(node_t node, int known_id)
477 {
478   INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
479   set_predecessor(node, -1); // no predecessor (yet)
480
481   int successor_id = remote_find_successor(node, known_id, node->id);
482   if (successor_id == -1) {
483     DEBUG0("Cannot join the ring.");
484   }
485   else {
486     set_finger(node, 0, successor_id);
487     print_finger_table(node);
488   }
489
490   return successor_id != -1;
491 }
492
493 /**
494  * \brief Makes the current node quit the system
495  * \param node the current node
496  */
497 static void leave(node_t node)
498 {
499   DEBUG0("Well Guys! I Think it's time for me to quit ;)");
500   quit_notify(node, 1);  // notify to my successor ( >>> 1 );
501   quit_notify(node, -1); // notify my predecessor  ( >>> -1);
502   // TODO ...
503 }
504
505 /*
506  * \brief Notifies the successor or the predecessor of the current node
507  * of the departure
508  * \param node the current node
509  * \param to 1 to notify the successor, -1 to notify the predecessor
510  * FIXME: notify both nodes with only one call
511  */
512 static void quit_notify(node_t node, int to)
513 {
514   /* TODO
515   task_data_t req_data = xbt_new0(s_task_data_t, 1);
516   req_data->request_id = node->id;
517   req_data->successor_id = node->fingers[0].id;
518   req_data->pred_id = node->pred_id;
519   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
520   req_data->answer_to = NULL;
521   const char* task_name = NULL;
522   const char* to_mailbox = NULL;
523   if (to == 1) {    // notify my successor
524     to_mailbox = node->fingers[0].mailbox;
525     INFO2("Telling my Successor %d about my departure via mailbox %s",
526           node->fingers[0].id, to_mailbox);
527     req_data->type = TASK_PREDECESSOR_LEAVING;
528   }
529   else if (to == -1) {    // notify my predecessor
530
531     if (node->pred_id == -1) {
532       return;
533     }
534
535     to_mailbox = node->pred_mailbox;
536     INFO2("Telling my Predecessor %d about my departure via mailbox %s",
537           node->pred_id, to_mailbox);
538     req_data->type = TASK_SUCCESSOR_LEAVING;
539   }
540   m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
541   //char* mailbox = get_mailbox(to_mailbox);
542   msg_comm_t comm = MSG_task_isend(task, to_mailbox);
543   xbt_dynar_push(node->comms, &comm);
544   */
545 }
546
547 /**
548  * \brief Makes the current node find the successor node of an id.
549  * \param node the current node
550  * \param id the id to find
551  * \return the id of the successor node, or -1 if the request failed
552  */
553 static int find_successor(node_t node, int id)
554 {
555   // is my successor the successor?
556   if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
557     return node->fingers[0].id;
558   }
559
560   // otherwise, ask the closest preceding finger in my table
561   int closest = closest_preceding_node(node, id);
562   return remote_find_successor(node, closest, id);
563 }
564
565 /**
566  * \brief Asks another node the successor node of an id.
567  * \param node the current node
568  * \param ask_to the node to ask to
569  * \param id the id to find
570  * \return the id of the successor node, or -1 if the request failed
571  */
572 static int remote_find_successor(node_t node, int ask_to, int id)
573 {
574   int successor = -1;
575   int stop = 0;
576   char* mailbox = get_mailbox(ask_to);
577   task_data_t req_data = xbt_new0(s_task_data_t, 1);
578   req_data->type = TASK_FIND_SUCCESSOR;
579   req_data->request_id = id;
580   req_data->answer_to = xbt_strdup(node->mailbox);
581   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
582
583   // send a "Find Successor" request to ask_to_id
584   m_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
585   DEBUG3("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
586   MSG_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, 50);
587
588   if (res != MSG_OK) {
589     DEBUG3("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
590         task_sent, ask_to, id);
591     MSG_task_destroy(task_sent);
592     task_data_destroy(req_data);
593   }
594   else {
595
596     // receive the answer
597     DEBUG3("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
598         task_sent, ask_to, id);
599
600     do {
601       if (node->comm_receive == NULL) {
602         m_task_t task_received = NULL;
603         node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
604       }
605
606       res = MSG_comm_wait(node->comm_receive, 50);
607
608       if (res != MSG_OK) {
609         DEBUG2("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
610             task_sent, res);
611         stop = 1;
612         //MSG_comm_destroy(node->comm_receive);
613       }
614       else {
615         m_task_t task_received = MSG_comm_get_task(node->comm_receive);
616         DEBUG1("Received a task (%p)", task_received);
617         task_data_t ans_data = MSG_task_get_data(task_received);
618
619         if (task_received != task_sent) {
620           // this is not the expected answer
621           handle_task(node, task_received);
622         }
623         else {
624           // this is our answer
625           DEBUG4("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
626               ans_data->request_id, task_received, id, ans_data->answer_id);
627           successor = ans_data->answer_id;
628           stop = 1;
629           MSG_task_destroy(task_received);
630           task_data_destroy(req_data);
631         }
632       }
633       node->comm_receive = NULL;
634     } while (!stop);
635   }
636
637   xbt_free(mailbox);
638   return successor;
639 }
640
641 /**
642  * \brief Asks another node its predecessor.
643  * \param node the current node
644  * \param ask_to the node to ask to
645  * \return the id of its predecessor node, or -1 if the request failed
646  * (or if the node does not know its predecessor)
647  */
648 static int remote_get_predecessor(node_t node, int ask_to)
649 {
650   int predecessor_id = -1;
651   int stop = 0;
652   char* mailbox = get_mailbox(ask_to);
653   task_data_t req_data = xbt_new0(s_task_data_t, 1);
654   req_data->type = TASK_GET_PREDECESSOR;
655   req_data->answer_to = xbt_strdup(node->mailbox);
656   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
657
658   // send a "Get Predecessor" request to ask_to_id
659   DEBUG1("Sending a 'Get Predecessor' request to %d", ask_to);
660   m_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
661   MSG_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, 50);
662
663   if (res != MSG_OK) {
664     DEBUG2("Failed to send the 'Get Predecessor' request (task %p) to %d",
665         task_sent, ask_to);
666     MSG_task_destroy(task_sent);
667     task_data_destroy(req_data);
668   }
669   else {
670
671     // receive the answer
672     DEBUG3("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
673         task_sent, ask_to, req_data->answer_to);
674
675     do {
676       if (node->comm_receive == NULL) { // FIXME simplify this
677         m_task_t task_received = NULL;
678         node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
679       }
680
681       res = MSG_comm_wait(node->comm_receive, 50);
682
683       if (res != MSG_OK) {
684         DEBUG2("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
685             task_sent, res);
686         stop = 1;
687         //MSG_comm_destroy(node->comm_receive);
688       }
689       else {
690         m_task_t task_received = MSG_comm_get_task(node->comm_receive);
691         task_data_t ans_data = MSG_task_get_data(task_received);
692
693         if (task_received != task_sent) {
694           handle_task(node, task_received);
695         }
696         else {
697           DEBUG3("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
698               task_received, ask_to, ans_data->answer_id);
699           predecessor_id = ans_data->answer_id;
700           stop = 1;
701           MSG_task_destroy(task_received);
702           task_data_destroy(req_data);
703         }
704       }
705       node->comm_receive = NULL;
706     } while (!stop);
707   }
708
709   xbt_free(mailbox);
710   return predecessor_id;
711 }
712
713 /**
714  * \brief Returns the closest preceding finger of an id
715  * with respect to the finger table of the current node.
716  * \param node the current node
717  * \param id the id to find
718  * \return the closest preceding finger of that id
719  */
720 int closest_preceding_node(node_t node, int id)
721 {
722   int i;
723   for (i = NB_BITS - 1; i >= 0; i--) {
724     if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
725       return node->fingers[i].id;
726     }
727   }
728   return node->id;
729 }
730
731 /**
732  * \brief This function is called periodically. It checks the immediate
733  * successor of the current node.
734  * \param node the current node
735  */
736 static void stabilize(node_t node)
737 {
738   DEBUG0("Stabilizing node");
739
740   // get the predecessor of my immediate successor
741   int candidate_id;
742   int successor_id = node->fingers[0].id;
743   if (successor_id != node->id) {
744     candidate_id = remote_get_predecessor(node, successor_id);
745   }
746   else {
747     candidate_id = node->pred_id;
748   }
749
750   // this node is a candidate to become my new successor
751   if (candidate_id != -1
752       && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
753     set_finger(node, 0, candidate_id);
754   }
755   if (successor_id != node->id) {
756     remote_notify(node, successor_id, node->id);
757   }
758 }
759
760 /**
761  * \brief Notifies the current node that its predecessor may have changed.
762  * \param node the current node
763  * \param candidate_id the possible new predecessor
764  */
765 static void notify(node_t node, int predecessor_candidate_id) {
766
767   if (node->pred_id == -1
768     || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
769
770     set_predecessor(node, predecessor_candidate_id);
771     print_finger_table(node);
772   }
773   else {
774     DEBUG1("I don't have to change my predecessor to %d", predecessor_candidate_id);
775   }
776 }
777
778 /**
779  * \brief Notifies a remote node that its predecessor may have changed.
780  * \param node the current node
781  * \param notify_id id of the node to notify
782  * \param candidate_id the possible new predecessor
783  */
784 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
785
786   task_data_t req_data = xbt_new0(s_task_data_t, 1);
787   req_data->type = TASK_NOTIFY;
788   req_data->request_id = predecessor_candidate_id;
789   req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
790   req_data->answer_to = NULL;
791
792   // send a "Notify" request to notify_id
793   m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
794   DEBUG2("Sending a 'Notify' request (task %p) to %d", task, notify_id);
795   char* mailbox = get_mailbox(notify_id);
796   msg_comm_t comm = MSG_task_isend(task, mailbox);
797   xbt_dynar_push(node->comms, &comm);
798   xbt_free(mailbox);
799 }
800
801 /**
802  * \brief This function is called periodically.
803  * It refreshes the finger table of the current node.
804  * \param node the current node
805  */
806 static void fix_fingers(node_t node) {
807
808   DEBUG0("Fixing fingers");
809   int i = node->next_finger_to_fix;
810   int id = find_successor(node, node->id + powers2[i]);
811   if (id != -1) {
812
813     if (id != node->fingers[i].id) {
814       set_finger(node, i, id);
815       print_finger_table(node);
816     }
817     node->next_finger_to_fix = (i + 1) % NB_BITS;
818   }
819 }
820
821 /**
822  * \brief This function is called periodically.
823  * It checks whether the predecessor has failed
824  * \param node the current node
825  */
826 static void check_predecessor(node_t node)
827 {
828   DEBUG0("Checking whether my predecessor is alive");
829   // TODO
830 }
831
832 /**
833  * \brief Main function.
834  */
835 int main(int argc, char *argv[])
836 {
837   if (argc < 3) {
838     printf("Usage: %s platform_file deployment_file\n", argv[0]);
839     printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
840     exit(1);
841   }
842
843   const char* platform_file = argv[1];
844   const char* application_file = argv[2];
845
846   chord_initialize();
847
848   MSG_global_init(&argc, argv);
849   MSG_set_channel_number(0);
850   MSG_create_environment(platform_file);
851
852   MSG_function_register("node", node);
853   MSG_launch_application(application_file);
854
855   MSG_error_t res = MSG_main();
856   INFO1("Simulation time: %g", MSG_get_clock());
857
858   MSG_clean();
859
860   if (res == MSG_OK)
861     return 0;
862   else
863     return 1;
864 }