Logo AND Algorithmique Numérique Distribuée

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