Logo AND Algorithmique Numérique Distribuée

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