Logo AND Algorithmique Numérique Distribuée

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