Logo AND Algorithmique Numérique Distribuée

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