2 /* Copyright (c) 2010. The SimGrid Team.
3 * All rights reserved. */
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. */
11 #include "xbt/asserts.h"
12 #include "simgrid/modelchecker.h"
14 /** @addtogroup MSG_examples
16 * - <b>chord/chord.c: Classical Chord P2P protocol</b>
17 * This example implements the well known Chord P2P protocol. Its
18 * main advantage is that it constitute a fully working non-trivial
19 * example. In addition, its implementation is rather efficient, as
20 * demonstrated in http://hal.inria.fr/inria-00602216/
24 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
25 "Messages specific for this msg example");
29 #define MAILBOX_NAME_SIZE 10
31 static int nb_bits = 24;
32 static int nb_keys = 0;
33 static int timeout = 50;
34 static int max_simulation_time = 1000;
35 static int periodic_stabilize_delay = 20;
36 static int periodic_fix_fingers_delay = 120;
37 static int periodic_check_predecessor_delay = 120;
38 static int periodic_lookup_delay = 10;
40 extern long int smx_total_comms;
45 typedef struct s_finger {
47 char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
48 } s_finger_t, *finger_t;
53 typedef struct s_node {
55 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
56 s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
57 int pred_id; // predecessor id
58 char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
59 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
60 msg_comm_t comm_receive; // current communication to receive
61 double last_change_date; // last time I changed a finger or my predecessor
65 * Types of tasks exchanged between nodes.
69 TASK_FIND_SUCCESSOR_ANSWER,
71 TASK_GET_PREDECESSOR_ANSWER,
73 TASK_SUCCESSOR_LEAVING,
74 TASK_PREDECESSOR_LEAVING
78 * Data attached with the tasks sent and received
80 typedef struct s_task_data {
81 e_task_type_t type; // type of task
82 int request_id; // id paramater (used by some types of tasks)
83 int request_finger; // finger parameter (used by some types of tasks)
84 int answer_id; // answer (used by some types of tasks)
85 char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
86 const char* issuer_host_name; // used for logging
87 } s_task_data_t, *task_data_t;
92 static void chord_initialize(void);
93 static void chord_exit(void);
94 static int normalize(int id);
95 static int is_in_interval(int id, int start, int end);
96 static void get_mailbox(int host_id, char* mailbox);
97 static void task_free(void* task);
98 static void print_finger_table(node_t node);
99 static void set_finger(node_t node, int finger_index, int id);
100 static void set_predecessor(node_t node, int predecessor_id);
103 static int node(int argc, char *argv[]);
104 static void handle_task(node_t node, msg_task_t task);
107 static void create(node_t node);
108 static int join(node_t node, int known_id);
109 static void leave(node_t node);
110 static int find_successor(node_t node, int id);
111 static int remote_find_successor(node_t node, int ask_to_id, int id);
112 static int remote_get_predecessor(node_t node, int ask_to_id);
113 static int closest_preceding_node(node_t node, int id);
114 static void stabilize(node_t node);
115 static void notify(node_t node, int predecessor_candidate_id);
116 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
117 static void fix_fingers(node_t node);
118 static void check_predecessor(node_t node);
119 static void random_lookup(node_t);
120 static void quit_notify(node_t node);
123 * \brief Global initialization of the Chord simulation.
125 static void chord_initialize(void)
127 // compute the powers of 2 once for all
128 powers2 = xbt_new(int, nb_bits);
131 for (i = 0; i < nb_bits; i++) {
136 XBT_DEBUG("Sets nb_keys to %d", nb_keys);
139 static void chord_exit(void)
145 * \brief Turns an id into an equivalent id in [0, nb_keys).
147 * \return the corresponding normalized id
149 static int normalize(int id)
151 // like id % nb_keys, but works with negatives numbers (and faster)
152 return id & (nb_keys - 1);
156 * \brief Returns whether an id belongs to the interval [start, end].
158 * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
159 * 1 belongs to [62, 3]
160 * 1 does not belong to [3, 62]
161 * 63 belongs to [62, 3]
162 * 63 does not belong to [3, 62]
163 * 24 belongs to [21, 29]
164 * 24 does not belong to [29, 21]
166 * \param id id to check
167 * \param start lower bound
168 * \param end upper bound
169 * \return a non-zero value if id in in [start, end]
171 static int is_in_interval(int id, int start, int end)
174 start = normalize(start);
175 end = normalize(end);
177 // make sure end >= start and id >= start
190 * \brief Gets the mailbox name of a host given its chord id.
191 * \param node_id id of a node
192 * \param mailbox pointer to where the mailbox name should be written
193 * (there must be enough space)
195 static void get_mailbox(int node_id, char* mailbox)
197 snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
201 * \brief Frees the memory used by a task.
202 * \param task the MSG task to destroy
204 static void task_free(void* task)
206 // TODO add a parameter data_free_function to MSG_task_create?
207 xbt_free(MSG_task_get_data(task));
208 MSG_task_destroy(task);
212 * \brief Displays the finger table of a node.
215 static void print_finger_table(node_t node)
217 if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
219 XBT_VERB("My finger table:");
220 XBT_VERB("Start | Succ ");
221 for (i = 0; i < nb_bits; i++) {
222 XBT_VERB(" %3d | %3d ", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
224 XBT_VERB("Predecessor: %d", node->pred_id);
229 * \brief Sets a finger of the current node.
230 * \param node the current node
231 * \param finger_index index of the finger to set (0 to nb_bits - 1)
232 * \param id the id to set for this finger
234 static void set_finger(node_t node, int finger_index, int id)
236 if (id != node->fingers[finger_index].id) {
237 node->fingers[finger_index].id = id;
238 get_mailbox(id, node->fingers[finger_index].mailbox);
239 node->last_change_date = MSG_get_clock();
240 XBT_DEBUG("My new finger #%d is %d", finger_index, id);
245 * \brief Sets the predecessor of the current node.
246 * \param node the current node
247 * \param id the id to predecessor, or -1 to unset the predecessor
249 static void set_predecessor(node_t node, int predecessor_id)
251 if (predecessor_id != node->pred_id) {
252 node->pred_id = predecessor_id;
254 if (predecessor_id != -1) {
255 get_mailbox(predecessor_id, node->pred_mailbox);
257 node->last_change_date = MSG_get_clock();
259 XBT_DEBUG("My new predecessor is %d", predecessor_id);
264 * \brief Node Function
267 * - the id of a guy I know in the system (except for the first node)
268 * - the time to sleep before I join (except for the first node)
270 int node(int argc, char *argv[])
272 /* Reduce the run size for the MC */
274 periodic_stabilize_delay = 8;
275 periodic_fix_fingers_delay = 8;
276 periodic_check_predecessor_delay = 8;
279 double init_time = MSG_get_clock();
280 msg_task_t task_received = NULL;
282 int join_success = 0;
284 double next_stabilize_date = init_time + periodic_stabilize_delay;
285 double next_fix_fingers_date = init_time + periodic_fix_fingers_delay;
286 double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
287 double next_lookup_date = init_time + periodic_lookup_delay;
289 xbt_assert(argc == 3 || argc == 5, "Wrong number of arguments for this node");
291 // initialize my node
293 node.id = atoi(argv[1]);
294 get_mailbox(node.id, node.mailbox);
295 node.next_finger_to_fix = 0;
296 node.fingers = xbt_new0(s_finger_t, nb_bits);
297 node.last_change_date = init_time;
299 for (i = 0; i < nb_bits; i++) {
300 node.fingers[i].id = -1;
301 set_finger(&node, i, node.id);
304 if (argc == 3) { // first ring
305 deadline = atof(argv[2]);
310 int known_id = atoi(argv[2]);
311 //double sleep_time = atof(argv[3]);
312 deadline = atof(argv[4]);
315 // sleep before starting
316 XBT_DEBUG("Let's sleep during %f", sleep_time);
317 MSG_process_sleep(sleep_time);
319 XBT_DEBUG("Hey! Let's join the system.");
321 join_success = join(&node, known_id);
325 while (MSG_get_clock() < init_time + deadline
326 // && MSG_get_clock() < node.last_change_date + 1000
327 && MSG_get_clock() < max_simulation_time) {
329 if (node.comm_receive == NULL) {
330 task_received = NULL;
331 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
332 // FIXME: do not make MSG_task_irecv() calls from several functions
335 if (!MSG_comm_test(node.comm_receive)) {
337 // no task was received: make some periodic calls
342 }else if(MC_random()){
344 }else if(MC_random()){
345 check_predecessor(&node);
346 }else if(MC_random()){
347 random_lookup(&node);
349 MSG_process_sleep(5);
352 if (MSG_get_clock() >= next_stabilize_date) {
354 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
356 else if (MSG_get_clock() >= next_fix_fingers_date) {
358 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
360 else if (MSG_get_clock() >= next_check_predecessor_date) {
361 check_predecessor(&node);
362 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
364 else if (MSG_get_clock() >= next_lookup_date) {
365 random_lookup(&node);
366 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
369 // nothing to do: sleep for a while
370 MSG_process_sleep(5);
375 // a transfer has occurred
377 msg_error_t status = MSG_comm_get_status(node.comm_receive);
379 if (status != MSG_OK) {
380 XBT_DEBUG("Failed to receive a task. Nevermind.");
381 MSG_comm_destroy(node.comm_receive);
382 node.comm_receive = NULL;
385 // the task was successfully received
386 MSG_comm_destroy(node.comm_receive);
387 node.comm_receive = NULL;
388 handle_task(&node, task_received);
393 if (node.comm_receive) {
394 MSG_comm_destroy(node.comm_receive);
395 node.comm_receive = NULL;
402 // stop the simulation
403 xbt_free(node.fingers);
408 * \brief This function is called when the current node receives a task.
409 * \param node the current node
410 * \param task the task to handle (don't touch it then:
411 * it will be destroyed, reused or forwarded)
413 static void handle_task(node_t node, msg_task_t task) {
415 XBT_DEBUG("Handling task %p", task);
416 char mailbox[MAILBOX_NAME_SIZE];
417 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
418 e_task_type_t type = task_data->type;
422 case TASK_FIND_SUCCESSOR:
423 XBT_DEBUG("Receiving a 'Find Successor' request from %s for id %d",
424 task_data->issuer_host_name, task_data->request_id);
425 // is my successor the successor?
426 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
427 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
428 task_data->answer_id = node->fingers[0].id;
429 XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
430 task_data->issuer_host_name,
431 task_data->answer_to,
432 task_data->request_id, task_data->answer_id);
433 MSG_task_dsend(task, task_data->answer_to, task_free);
436 // otherwise, forward the request to the closest preceding finger in my table
437 int closest = closest_preceding_node(node, task_data->request_id);
438 XBT_DEBUG("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
439 task_data->request_id, closest);
440 get_mailbox(closest, mailbox);
441 MSG_task_dsend(task, mailbox, task_free);
445 case TASK_GET_PREDECESSOR:
446 XBT_DEBUG("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
447 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
448 task_data->answer_id = node->pred_id;
449 XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
450 task_data->issuer_host_name,
451 task_data->answer_to, task_data->answer_id);
452 MSG_task_dsend(task, task_data->answer_to, task_free);
456 // someone is telling me that he may be my new predecessor
457 XBT_DEBUG("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
458 notify(node, task_data->request_id);
462 case TASK_PREDECESSOR_LEAVING:
463 // my predecessor is about to quit
464 XBT_DEBUG("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
465 // modify my predecessor
466 set_predecessor(node, task_data->request_id);
469 >> notify my new predecessor
470 >> send a notify_predecessors !!
474 case TASK_SUCCESSOR_LEAVING:
475 // my successor is about to quit
476 XBT_DEBUG("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
477 // modify my successor FIXME : this should be implicit ?
478 set_finger(node, 0, task_data->request_id);
481 >> notify my new successor
482 >> update my table & predecessors table */
485 case TASK_FIND_SUCCESSOR_ANSWER:
486 case TASK_GET_PREDECESSOR_ANSWER:
487 XBT_DEBUG("Ignoring unexpected task of type %d (%p)", (int)type, task);
494 * \brief Initializes the current node as the first one of the system.
495 * \param node the current node
497 static void create(node_t node)
499 XBT_DEBUG("Create a new Chord ring...");
500 set_predecessor(node, -1); // -1 means that I have no predecessor
501 print_finger_table(node);
505 * \brief Makes the current node join the ring, knowing the id of a node
506 * already in the ring
507 * \param node the current node
508 * \param known_id id of a node already in the ring
509 * \return 1 if the join operation succeeded, 0 otherwise
511 static int join(node_t node, int known_id)
513 XBT_INFO("Joining the ring with id %d, knowing node %d", node->id, known_id);
514 set_predecessor(node, -1); // no predecessor (yet)
518 for (i = 0; i < nb_bits; i++) {
519 set_finger(node, i, known_id);
523 int successor_id = remote_find_successor(node, known_id, node->id);
524 if (successor_id == -1) {
525 XBT_INFO("Cannot join the ring.");
528 set_finger(node, 0, successor_id);
529 print_finger_table(node);
532 return successor_id != -1;
536 * \brief Makes the current node quit the system
537 * \param node the current node
539 static void leave(node_t node)
541 XBT_DEBUG("Well Guys! I Think it's time for me to quit ;)");
546 * \brief Notifies the successor and the predecessor of the current node
548 * \param node the current node
550 static void quit_notify(node_t node)
552 char mailbox[MAILBOX_NAME_SIZE];
553 //send the PREDECESSOR_LEAVING to our successor
554 task_data_t req_data = xbt_new0(s_task_data_t,1);
555 req_data->type = TASK_PREDECESSOR_LEAVING;
556 req_data->request_id = node->pred_id;
557 get_mailbox(node->id, req_data->answer_to);
558 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
560 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
561 XBT_DEBUG("Sending a 'PREDECESSOR_LEAVING' to my successor %d",node->fingers[0].id);
562 MSG_task_send_with_timeout(task_sent, node->fingers[0].mailbox, timeout);
564 //send the SUCCESSOR_LEAVING to our predecessor
565 get_mailbox(node->pred_id, mailbox);
566 task_data_t req_data_s = xbt_new0(s_task_data_t,1);
567 req_data_s->type = TASK_SUCCESSOR_LEAVING;
568 req_data_s->request_id = node->fingers[0].id;
569 req_data_s->request_id = node->pred_id;
570 get_mailbox(node->id, req_data_s->answer_to);
571 req_data_s->issuer_host_name = MSG_host_get_name(MSG_host_self());
573 msg_task_t task_sent_s = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data_s);
574 XBT_DEBUG("Sending a 'SUCCESSOR_LEAVING' to my predecessor %d",node->pred_id);
575 MSG_task_send_with_timeout(task_sent_s, mailbox, timeout);
580 * \brief Makes the current node find the successor node of an id.
581 * \param node the current node
582 * \param id the id to find
583 * \return the id of the successor node, or -1 if the request failed
585 static int find_successor(node_t node, int id)
587 // is my successor the successor?
588 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
589 return node->fingers[0].id;
592 // otherwise, ask the closest preceding finger in my table
593 int closest = closest_preceding_node(node, id);
594 return remote_find_successor(node, closest, id);
598 * \brief Asks another node the successor node of an id.
599 * \param node the current node
600 * \param ask_to the node to ask to
601 * \param id the id to find
602 * \return the id of the successor node, or -1 if the request failed
604 static int remote_find_successor(node_t node, int ask_to, int id)
608 char mailbox[MAILBOX_NAME_SIZE];
609 get_mailbox(ask_to, mailbox);
610 task_data_t req_data = xbt_new0(s_task_data_t, 1);
611 req_data->type = TASK_FIND_SUCCESSOR;
612 req_data->request_id = id;
613 get_mailbox(node->id, req_data->answer_to);
614 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
616 // send a "Find Successor" request to ask_to_id
617 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
618 XBT_DEBUG("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
619 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
622 XBT_DEBUG("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
623 task_sent, ask_to, id);
624 task_free(task_sent);
628 // receive the answer
629 XBT_DEBUG("Sent a 'Find Successor' request (task %p) to %d for key %d, waiting for the answer",
630 task_sent, ask_to, id);
633 if (node->comm_receive == NULL) {
634 msg_task_t task_received = NULL;
635 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
638 res = MSG_comm_wait(node->comm_receive, timeout);
641 XBT_DEBUG("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
642 task_sent, (int)res);
644 MSG_comm_destroy(node->comm_receive);
645 node->comm_receive = NULL;
648 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
649 XBT_DEBUG("Received a task (%p)", task_received);
650 task_data_t ans_data = MSG_task_get_data(task_received);
652 // Once upon a time, our code assumed that here, task_received != task_sent all the time
654 // This assumption is wrong (as messages from differing round can interleave), leading to a bug in our code.
655 // We failed to find this bug directly, as it only occured on large platforms, leading to hardly usable traces.
656 // Instead, we used the model-checker to track down the issue by adding the following test here in the code:
657 // if (MC_is_active()) {
658 // MC_assert(task_received == task_sent);
660 // That explained the bug in a snap, with a very cool example and everything.
662 // This MC_assert is now desactivated as the case is now properly handled in our code and we don't want the
663 // MC to fail any further under that condition, but this comment is here to as a memorial for this first
664 // brillant victory of the model-checking in the SimGrid community :)
666 if (task_received != task_sent) {
667 // this is not the expected answer
668 MSG_comm_destroy(node->comm_receive);
669 node->comm_receive = NULL;
670 handle_task(node, task_received);
673 // this is our answer
674 XBT_DEBUG("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
675 ans_data->request_id, task_received, id, ans_data->answer_id);
676 successor = ans_data->answer_id;
678 MSG_comm_destroy(node->comm_receive);
679 node->comm_receive = NULL;
680 task_free(task_received);
690 * \brief Asks another node its predecessor.
691 * \param node the current node
692 * \param ask_to the node to ask to
693 * \return the id of its predecessor node, or -1 if the request failed
694 * (or if the node does not know its predecessor)
696 static int remote_get_predecessor(node_t node, int ask_to)
698 int predecessor_id = -1;
700 char mailbox[MAILBOX_NAME_SIZE];
701 get_mailbox(ask_to, mailbox);
702 task_data_t req_data = xbt_new0(s_task_data_t, 1);
703 req_data->type = TASK_GET_PREDECESSOR;
704 get_mailbox(node->id, req_data->answer_to);
705 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
707 // send a "Get Predecessor" request to ask_to_id
708 XBT_DEBUG("Sending a 'Get Predecessor' request to %d", ask_to);
709 msg_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
710 msg_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
713 XBT_DEBUG("Failed to send the 'Get Predecessor' request (task %p) to %d",
715 task_free(task_sent);
719 // receive the answer
720 XBT_DEBUG("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
721 task_sent, ask_to, req_data->answer_to);
724 if (node->comm_receive == NULL) { // FIXME simplify this
725 msg_task_t task_received = NULL;
726 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
729 res = MSG_comm_wait(node->comm_receive, timeout);
732 XBT_DEBUG("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
733 task_sent, (int)res);
735 MSG_comm_destroy(node->comm_receive);
736 node->comm_receive = NULL;
739 msg_task_t task_received = MSG_comm_get_task(node->comm_receive);
740 task_data_t ans_data = MSG_task_get_data(task_received);
742 if (MC_is_active()) {
743 MC_assert(task_received == task_sent);
746 if (task_received != task_sent) {
747 MSG_comm_destroy(node->comm_receive);
748 node->comm_receive = NULL;
749 handle_task(node, task_received);
752 XBT_DEBUG("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
753 task_received, ask_to, ans_data->answer_id);
754 predecessor_id = ans_data->answer_id;
756 MSG_comm_destroy(node->comm_receive);
757 node->comm_receive = NULL;
758 task_free(task_received);
764 return predecessor_id;
768 * \brief Returns the closest preceding finger of an id
769 * with respect to the finger table of the current node.
770 * \param node the current node
771 * \param id the id to find
772 * \return the closest preceding finger of that id
774 int closest_preceding_node(node_t node, int id)
777 for (i = nb_bits - 1; i >= 0; i--) {
778 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
779 return node->fingers[i].id;
786 * \brief This function is called periodically. It checks the immediate
787 * successor of the current node.
788 * \param node the current node
790 static void stabilize(node_t node)
792 XBT_DEBUG("Stabilizing node");
794 // get the predecessor of my immediate successor
796 int successor_id = node->fingers[0].id;
797 if (successor_id != node->id) {
798 candidate_id = remote_get_predecessor(node, successor_id);
801 candidate_id = node->pred_id;
804 // this node is a candidate to become my new successor
805 if (candidate_id != -1
806 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
807 set_finger(node, 0, candidate_id);
809 if (successor_id != node->id) {
810 remote_notify(node, successor_id, node->id);
815 * \brief Notifies the current node that its predecessor may have changed.
816 * \param node the current node
817 * \param candidate_id the possible new predecessor
819 static void notify(node_t node, int predecessor_candidate_id) {
821 if (node->pred_id == -1
822 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
824 set_predecessor(node, predecessor_candidate_id);
825 print_finger_table(node);
828 XBT_DEBUG("I don't have to change my predecessor to %d", predecessor_candidate_id);
833 * \brief Notifies a remote node that its predecessor may have changed.
834 * \param node the current node
835 * \param notify_id id of the node to notify
836 * \param candidate_id the possible new predecessor
838 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
840 task_data_t req_data = xbt_new0(s_task_data_t, 1);
841 req_data->type = TASK_NOTIFY;
842 req_data->request_id = predecessor_candidate_id;
843 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
845 // send a "Notify" request to notify_id
846 msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
847 XBT_DEBUG("Sending a 'Notify' request (task %p) to %d", task, notify_id);
848 char mailbox[MAILBOX_NAME_SIZE];
849 get_mailbox(notify_id, mailbox);
850 MSG_task_dsend(task, mailbox, task_free);
854 * \brief This function is called periodically.
855 * It refreshes the finger table of the current node.
856 * \param node the current node
858 static void fix_fingers(node_t node) {
860 XBT_DEBUG("Fixing fingers");
861 int i = node->next_finger_to_fix;
862 int id = find_successor(node, node->id + powers2[i]);
865 if (id != node->fingers[i].id) {
866 set_finger(node, i, id);
867 print_finger_table(node);
869 node->next_finger_to_fix = (i + 1) % nb_bits;
874 * \brief This function is called periodically.
875 * It checks whether the predecessor has failed
876 * \param node the current node
878 static void check_predecessor(node_t node)
880 XBT_DEBUG("Checking whether my predecessor is alive");
885 * \brief Performs a find successor request to a random id.
886 * \param node the current node
888 static void random_lookup(node_t node)
890 int id = 1337; // TODO pick a pseudorandom id
891 XBT_DEBUG("Making a lookup request for id %d", id);
892 find_successor(node, id);
896 * \brief Main function.
898 int main(int argc, char *argv[])
900 MSG_init(&argc, argv);
902 printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
903 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
907 char **options = &argv[1];
908 while (!strncmp(options[0], "-", 1)) {
910 int length = strlen("-nb_bits=");
911 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
912 nb_bits = atoi(options[0] + length);
913 XBT_DEBUG("Set nb_bits to %d", nb_bits);
917 length = strlen("-timeout=");
918 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
919 timeout = atoi(options[0] + length);
920 XBT_DEBUG("Set timeout to %d", timeout);
923 xbt_die("Invalid chord option '%s'", options[0]);
929 const char* platform_file = options[0];
930 const char* application_file = options[1];
934 MSG_create_environment(platform_file);
936 MSG_function_register("node", node);
937 MSG_launch_application(application_file);
939 msg_error_t res = MSG_main();
940 XBT_CRITICAL("Messages created: %ld", smx_total_comms);
941 XBT_INFO("Simulated time: %g", MSG_get_clock());