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 "mc/modelchecker.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_chord,
14 "Messages specific for this msg example");
18 #define MAILBOX_NAME_SIZE 10
20 static int nb_bits = 24;
21 static int nb_keys = 0;
22 static int timeout = 50;
23 static int max_simulation_time = 1000;
24 static int periodic_stabilize_delay = 20;
25 static int periodic_fix_fingers_delay = 120;
26 static int periodic_check_predecessor_delay = 120;
27 static int periodic_lookup_delay = 10;
32 typedef struct finger {
34 char mailbox[MAILBOX_NAME_SIZE]; // string representation of the id
35 } s_finger_t, *finger_t;
42 char mailbox[MAILBOX_NAME_SIZE]; // my mailbox name (string representation of the id)
43 s_finger_t *fingers; // finger table, of size nb_bits (fingers[0] is my successor)
44 int pred_id; // predecessor id
45 char pred_mailbox[MAILBOX_NAME_SIZE]; // predecessor's mailbox name
46 int next_finger_to_fix; // index of the next finger to fix in fix_fingers()
47 msg_comm_t comm_receive; // current communication to receive
48 xbt_dynar_t comms; // current communications being sent
49 double last_change_date; // last time I changed a finger or my predecessor
53 * Types of tasks exchanged between nodes.
57 TASK_FIND_SUCCESSOR_ANSWER,
59 TASK_GET_PREDECESSOR_ANSWER,
61 TASK_SUCCESSOR_LEAVING,
62 TASK_PREDECESSOR_LEAVING
66 * Data attached with the tasks sent and received
68 typedef struct task_data {
69 e_task_type_t type; // type of task
70 int request_id; // id paramater (used by some types of tasks)
71 int request_finger; // finger parameter (used by some types of tasks)
72 int answer_id; // answer (used by some types of tasks)
73 char answer_to[MAILBOX_NAME_SIZE]; // mailbox to send an answer to (if any)
74 const char* issuer_host_name; // used for logging
75 } s_task_data_t, *task_data_t;
80 static void chord_initialize(void);
81 static int normalize(int id);
82 static int is_in_interval(int id, int start, int end);
83 static void get_mailbox(int host_id, char* mailbox);
84 static void task_data_destroy(task_data_t task_data);
85 static void print_finger_table(node_t node);
86 static void set_finger(node_t node, int finger_index, int id);
87 static void set_predecessor(node_t node, int predecessor_id);
90 static int node(int argc, char *argv[]);
91 static void handle_task(node_t node, m_task_t task);
94 static void create(node_t node);
95 static int join(node_t node, int known_id);
96 static void leave(node_t node);
97 static int find_successor(node_t node, int id);
98 static int remote_find_successor(node_t node, int ask_to_id, int id);
99 static int remote_get_predecessor(node_t node, int ask_to_id);
100 static int closest_preceding_node(node_t node, int id);
101 static void stabilize(node_t node);
102 static void notify(node_t node, int predecessor_candidate_id);
103 static void remote_notify(node_t node, int notify_to, int predecessor_candidate_id);
104 static void fix_fingers(node_t node);
105 static void check_predecessor(node_t node);
106 static void random_lookup(node_t);
107 static void quit_notify(node_t node, int to);
110 * \brief Global initialization of the Chord simulation.
112 static void chord_initialize(void)
114 // compute the powers of 2 once for all
115 powers2 = xbt_new(int, nb_bits);
118 for (i = 0; i < nb_bits; i++) {
123 DEBUG1("Sets nb_keys to %d", nb_keys);
127 * \brief Turns an id into an equivalent id in [0, nb_keys).
129 * \return the corresponding normalized id
131 static int normalize(int id)
137 // make sure id < nb_keys
144 * \brief Returns whether a id belongs to the interval [start, end].
146 * The parameters are noramlized to make sure they are between 0 and nb_keys - 1).
147 * 1 belongs to [62, 3]
148 * 1 does not belong to [3, 62]
149 * 63 belongs to [62, 3]
150 * 63 does not belong to [3, 62]
151 * 24 belongs to [21, 29]
152 * 24 does not belong to [29, 21]
154 * \param id id to check
155 * \param start lower bound
156 * \param end upper bound
157 * \return a non-zero value if id in in [start, end]
159 static int is_in_interval(int id, int start, int end)
162 start = normalize(start);
163 end = normalize(end);
165 // make sure end >= start and id >= start
178 * \brief Gets the mailbox name of a host given its chord id.
179 * \param node_id id of a node
180 * \param mailbox pointer to where the mailbox name should be written
181 * (there must be enough space)
183 static void get_mailbox(int node_id, char* mailbox)
185 snprintf(mailbox, MAILBOX_NAME_SIZE - 1, "%d", node_id);
189 * \brief Frees the memory used by some task data.
190 * \param task_data the task data to destroy
192 static void task_data_destroy(task_data_t task_data)
198 * \brief Displays the finger table of a node.
201 static void print_finger_table(node_t node)
203 if (XBT_LOG_ISENABLED(msg_chord, xbt_log_priority_verbose)) {
205 VERB0("My finger table:");
206 VERB0("Start | Succ ");
207 for (i = 0; i < nb_bits; i++) {
208 VERB2(" %3d | %3d ", (node->id + powers2[i]) % nb_keys, node->fingers[i].id);
210 VERB1("Predecessor: %d", node->pred_id);
215 * \brief Sets a finger of the current node.
216 * \param node the current node
217 * \param finger_index index of the finger to set (0 to nb_bits - 1)
218 * \param id the id to set for this finger
220 static void set_finger(node_t node, int finger_index, int id)
222 if (id != node->fingers[finger_index].id) {
223 node->fingers[finger_index].id = id;
224 get_mailbox(id, node->fingers[finger_index].mailbox);
225 node->last_change_date = MSG_get_clock();
226 DEBUG2("My new finger #%d is %d", finger_index, id);
231 * \brief Sets the predecessor of the current node.
232 * \param node the current node
233 * \param id the id to predecessor, or -1 to unset the predecessor
235 static void set_predecessor(node_t node, int predecessor_id)
237 if (predecessor_id != node->pred_id) {
238 node->pred_id = predecessor_id;
240 if (predecessor_id != -1) {
241 get_mailbox(predecessor_id, node->pred_mailbox);
243 node->last_change_date = MSG_get_clock();
245 DEBUG1("My new predecessor is %d", predecessor_id);
250 * \brief Node Function
253 * - the id of a guy I know in the system (except for the first node)
254 * - the time to sleep before I join (except for the first node)
256 int node(int argc, char *argv[])
258 double init_time = MSG_get_clock();
259 m_task_t task_received = NULL;
260 msg_comm_t comm_send = NULL;
263 int join_success = 0;
265 double next_stabilize_date = init_time + periodic_stabilize_delay;
266 double next_fix_fingers_date = init_time + periodic_fix_fingers_delay;
267 double next_check_predecessor_date = init_time + periodic_check_predecessor_delay;
268 double next_lookup_date = init_time + periodic_lookup_delay;
270 xbt_assert0(argc == 3 || argc == 5, "Wrong number of arguments for this node");
272 // initialize my node
274 node.id = atoi(argv[1]);
275 get_mailbox(node.id, node.mailbox);
276 node.next_finger_to_fix = 0;
277 node.comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
278 node.fingers = xbt_new0(s_finger_t, nb_bits);
279 node.last_change_date = init_time;
281 for (i = 0; i < nb_bits; i++) {
282 node.fingers[i].id = -1;
283 set_finger(&node, i, node.id);
286 if (argc == 3) { // first ring
287 deadline = atof(argv[2]);
292 int known_id = atoi(argv[2]);
293 //double sleep_time = atof(argv[3]);
294 deadline = atof(argv[4]);
297 // sleep before starting
298 DEBUG1("Let's sleep during %f", sleep_time);
299 MSG_process_sleep(sleep_time);
301 DEBUG0("Hey! Let's join the system.");
303 join_success = join(&node, known_id);
307 while (MSG_get_clock() < init_time + deadline
308 // && MSG_get_clock() < node.last_change_date + 1000
309 && MSG_get_clock() < max_simulation_time) {
311 if (node.comm_receive == NULL) {
312 task_received = NULL;
313 node.comm_receive = MSG_task_irecv(&task_received, node.mailbox);
314 // FIXME: do not make MSG_task_irecv() calls from several functions
317 if (!MSG_comm_test(node.comm_receive)) {
319 // no task was received: make some periodic calls
320 if (MSG_get_clock() >= next_stabilize_date) {
322 next_stabilize_date = MSG_get_clock() + periodic_stabilize_delay;
324 else if (MSG_get_clock() >= next_fix_fingers_date) {
326 next_fix_fingers_date = MSG_get_clock() + periodic_fix_fingers_delay;
328 else if (MSG_get_clock() >= next_check_predecessor_date) {
329 check_predecessor(&node);
330 next_check_predecessor_date = MSG_get_clock() + periodic_check_predecessor_delay;
332 else if (MSG_get_clock() >= next_lookup_date) {
333 random_lookup(&node);
334 next_lookup_date = MSG_get_clock() + periodic_lookup_delay;
337 // nothing to do: sleep for a while
338 MSG_process_sleep(5);
342 // a transfer has occured
344 MSG_error_t status = MSG_comm_get_status(node.comm_receive);
346 if (status != MSG_OK) {
347 DEBUG0("Failed to receive a task. Nevermind.");
348 node.comm_receive = NULL;
351 // the task was successfully received
352 MSG_comm_destroy(node.comm_receive);
353 node.comm_receive = NULL;
354 handle_task(&node, task_received);
358 // see if some communications are finished
359 while ((index = MSG_comm_testany(node.comms)) != -1) {
360 comm_send = xbt_dynar_get_as(node.comms, index, msg_comm_t);
361 MSG_error_t status = MSG_comm_get_status(comm_send);
362 xbt_dynar_remove_at(node.comms, index, &comm_send);
363 DEBUG3("Communication %p is finished with status %d, dynar size is now %lu",
364 comm_send, status, xbt_dynar_length(node.comms));
365 m_task_t task = MSG_comm_get_task(comm_send);
366 MSG_comm_destroy(comm_send);
367 if (status != MSG_OK) {
368 task_data_destroy(MSG_task_get_data(task));
369 MSG_task_destroy(task);
374 // clean unfinished comms sent
375 /* unsigned int cursor;
376 xbt_dynar_foreach(node.comms, cursor, comm_send) {
377 m_task_t task = MSG_comm_get_task(comm_send);
378 MSG_task_cancel(task);
379 task_data_destroy(MSG_task_get_data(task));
380 MSG_task_destroy(task);
381 MSG_comm_destroy(comm_send);
382 // FIXME: the task is actually not destroyed because MSG thinks that the other side (whose process is dead) is still using it
389 // stop the simulation
390 xbt_dynar_free(&node.comms);
391 xbt_free(node.fingers);
396 * \brief This function is called when the current node receives a task.
397 * \param node the current node
398 * \param task the task to handle (don't touch it then:
399 * it will be destroyed, reused or forwarded)
401 static void handle_task(node_t node, m_task_t task) {
403 DEBUG1("Handling task %p", task);
404 msg_comm_t comm = NULL;
405 char mailbox[MAILBOX_NAME_SIZE];
406 task_data_t task_data = (task_data_t) MSG_task_get_data(task);
407 e_task_type_t type = task_data->type;
411 case TASK_FIND_SUCCESSOR:
412 DEBUG2("Receiving a 'Find Successor' request from %s for id %d",
413 task_data->issuer_host_name, task_data->request_id);
414 // is my successor the successor?
415 if (is_in_interval(task_data->request_id, node->id + 1, node->fingers[0].id)) {
416 task_data->type = TASK_FIND_SUCCESSOR_ANSWER;
417 task_data->answer_id = node->fingers[0].id;
418 DEBUG4("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
419 task_data->issuer_host_name,
420 task_data->answer_to,
421 task_data->request_id, task_data->answer_id);
422 comm = MSG_task_isend(task, task_data->answer_to);
423 xbt_dynar_push(node->comms, &comm);
426 // otherwise, forward the request to the closest preceding finger in my table
427 int closest = closest_preceding_node(node, task_data->request_id);
428 DEBUG2("Forwarding the 'Find Successor' request for id %d to my closest preceding finger %d",
429 task_data->request_id, closest);
430 get_mailbox(closest, mailbox);
431 comm = MSG_task_isend(task, mailbox);
432 xbt_dynar_push(node->comms, &comm);
436 case TASK_GET_PREDECESSOR:
437 DEBUG1("Receiving a 'Get Predecessor' request from %s", task_data->issuer_host_name);
438 task_data->type = TASK_GET_PREDECESSOR_ANSWER;
439 task_data->answer_id = node->pred_id;
440 DEBUG3("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
441 task_data->issuer_host_name,
442 task_data->answer_to, task_data->answer_id);
443 comm = MSG_task_isend(task, task_data->answer_to);
444 xbt_dynar_push(node->comms, &comm);
448 // someone is telling me that he may be my new predecessor
449 DEBUG1("Receiving a 'Notify' request from %s", task_data->issuer_host_name);
450 notify(node, task_data->request_id);
451 task_data_destroy(task_data);
452 MSG_task_destroy(task);
455 case TASK_PREDECESSOR_LEAVING:
456 // my predecessor is about to quit
457 DEBUG1("Receiving a 'Predecessor Leaving' message from %s", task_data->issuer_host_name);
458 // modify my predecessor
459 set_predecessor(node, task_data->request_id);
460 task_data_destroy(task_data);
461 MSG_task_destroy(task);
463 >> notify my new predecessor
464 >> send a notify_predecessors !!
468 case TASK_SUCCESSOR_LEAVING:
469 // my successor is about to quit
470 DEBUG1("Receiving a 'Successor Leaving' message from %s", task_data->issuer_host_name);
471 // modify my successor FIXME : this should be implicit ?
472 set_finger(node, 0, task_data->request_id);
473 task_data_destroy(task_data);
474 MSG_task_destroy(task);
476 >> notify my new successor
477 >> update my table & predecessors table */
480 case TASK_FIND_SUCCESSOR_ANSWER:
481 case TASK_GET_PREDECESSOR_ANSWER:
482 DEBUG2("Ignoring unexpected task of type %d (%p)", type, task);
483 task_data_destroy(task_data);
484 MSG_task_destroy(task);
490 * \brief Initializes the current node as the first one of the system.
491 * \param node the current node
493 static void create(node_t node)
495 DEBUG0("Create a new Chord ring...");
496 set_predecessor(node, -1); // -1 means that I have no predecessor
497 print_finger_table(node);
501 * \brief Makes the current node join the ring, knowing the id of a node
502 * already in the ring
503 * \param node the current node
504 * \param known_id id of a node already in the ring
505 * \return 1 if the join operation succeeded, 0 otherwise
507 static int join(node_t node, int known_id)
509 INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
510 set_predecessor(node, -1); // no predecessor (yet)
512 int successor_id = remote_find_successor(node, known_id, node->id);
513 if (successor_id == -1) {
514 INFO0("Cannot join the ring.");
517 set_finger(node, 0, successor_id);
518 print_finger_table(node);
521 return successor_id != -1;
525 * \brief Makes the current node quit the system
526 * \param node the current node
528 static void leave(node_t node)
530 DEBUG0("Well Guys! I Think it's time for me to quit ;)");
531 quit_notify(node, 1); // notify to my successor ( >>> 1 );
532 quit_notify(node, -1); // notify my predecessor ( >>> -1);
537 * \brief Notifies the successor or the predecessor of the current node
539 * \param node the current node
540 * \param to 1 to notify the successor, -1 to notify the predecessor
541 * FIXME: notify both nodes with only one call
543 static void quit_notify(node_t node, int to)
546 task_data_t req_data = xbt_new0(s_task_data_t, 1);
547 req_data->request_id = node->id;
548 req_data->successor_id = node->fingers[0].id;
549 req_data->pred_id = node->pred_id;
550 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
551 req_data->answer_to = NULL;
552 const char* task_name = NULL;
553 const char* to_mailbox = NULL;
554 if (to == 1) { // notify my successor
555 to_mailbox = node->fingers[0].mailbox;
556 INFO2("Telling my Successor %d about my departure via mailbox %s",
557 node->fingers[0].id, to_mailbox);
558 req_data->type = TASK_PREDECESSOR_LEAVING;
560 else if (to == -1) { // notify my predecessor
562 if (node->pred_id == -1) {
566 to_mailbox = node->pred_mailbox;
567 INFO2("Telling my Predecessor %d about my departure via mailbox %s",
568 node->pred_id, to_mailbox);
569 req_data->type = TASK_SUCCESSOR_LEAVING;
571 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
572 //char* mailbox = get_mailbox(to_mailbox);
573 msg_comm_t comm = MSG_task_isend(task, to_mailbox);
574 xbt_dynar_push(node->comms, &comm);
579 * \brief Makes the current node find the successor node of an id.
580 * \param node the current node
581 * \param id the id to find
582 * \return the id of the successor node, or -1 if the request failed
584 static int find_successor(node_t node, int id)
586 // is my successor the successor?
587 if (is_in_interval(id, node->id + 1, node->fingers[0].id)) {
588 return node->fingers[0].id;
591 // otherwise, ask the closest preceding finger in my table
592 int closest = closest_preceding_node(node, id);
593 return remote_find_successor(node, closest, id);
597 * \brief Asks another node the successor node of an id.
598 * \param node the current node
599 * \param ask_to the node to ask to
600 * \param id the id to find
601 * \return the id of the successor node, or -1 if the request failed
603 static int remote_find_successor(node_t node, int ask_to, int id)
607 char mailbox[MAILBOX_NAME_SIZE];
608 get_mailbox(ask_to, mailbox);
609 task_data_t req_data = xbt_new0(s_task_data_t, 1);
610 req_data->type = TASK_FIND_SUCCESSOR;
611 req_data->request_id = id;
612 get_mailbox(node->id, req_data->answer_to);
613 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
615 // send a "Find Successor" request to ask_to_id
616 m_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
617 DEBUG3("Sending a 'Find Successor' request (task %p) to %d for id %d", task_sent, ask_to, id);
618 MSG_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
621 DEBUG3("Failed to send the 'Find Successor' request (task %p) to %d for id %d",
622 task_sent, ask_to, id);
623 MSG_task_destroy(task_sent);
624 task_data_destroy(req_data);
628 // receive the answer
629 DEBUG3("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 m_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 DEBUG2("Failed to receive the answer to my 'Find Successor' request (task %p): %d",
644 MSG_comm_destroy(node->comm_receive);
645 node->comm_receive = NULL;
648 m_task_t task_received = MSG_comm_get_task(node->comm_receive);
649 DEBUG1("Received a task (%p)", task_received);
650 task_data_t ans_data = MSG_task_get_data(task_received);
652 MC_assert(task_received == task_sent);
654 if (task_received != task_sent) {
655 // this is not the expected answer
656 MSG_comm_destroy(node->comm_receive);
657 node->comm_receive = NULL;
658 handle_task(node, task_received);
661 // this is our answer
662 DEBUG4("Received the answer to my 'Find Successor' request for id %d (task %p): the successor of key %d is %d",
663 ans_data->request_id, task_received, id, ans_data->answer_id);
664 successor = ans_data->answer_id;
666 MSG_comm_destroy(node->comm_receive);
667 node->comm_receive = NULL;
668 MSG_task_destroy(task_received);
669 task_data_destroy(req_data);
679 * \brief Asks another node its predecessor.
680 * \param node the current node
681 * \param ask_to the node to ask to
682 * \return the id of its predecessor node, or -1 if the request failed
683 * (or if the node does not know its predecessor)
685 static int remote_get_predecessor(node_t node, int ask_to)
687 int predecessor_id = -1;
689 char mailbox[MAILBOX_NAME_SIZE];
690 get_mailbox(ask_to, mailbox);
691 task_data_t req_data = xbt_new0(s_task_data_t, 1);
692 req_data->type = TASK_GET_PREDECESSOR;
693 get_mailbox(node->id, req_data->answer_to);
694 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
696 // send a "Get Predecessor" request to ask_to_id
697 DEBUG1("Sending a 'Get Predecessor' request to %d", ask_to);
698 m_task_t task_sent = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
699 MSG_error_t res = MSG_task_send_with_timeout(task_sent, mailbox, timeout);
702 DEBUG2("Failed to send the 'Get Predecessor' request (task %p) to %d",
704 MSG_task_destroy(task_sent);
705 task_data_destroy(req_data);
709 // receive the answer
710 DEBUG3("Sent 'Get Predecessor' request (task %p) to %d, waiting for the answer on my mailbox '%s'",
711 task_sent, ask_to, req_data->answer_to);
714 if (node->comm_receive == NULL) { // FIXME simplify this
715 m_task_t task_received = NULL;
716 node->comm_receive = MSG_task_irecv(&task_received, node->mailbox);
719 res = MSG_comm_wait(node->comm_receive, timeout);
722 DEBUG2("Failed to receive the answer to my 'Get Predecessor' request (task %p): %d",
725 MSG_comm_destroy(node->comm_receive);
726 node->comm_receive = NULL;
729 m_task_t task_received = MSG_comm_get_task(node->comm_receive);
730 task_data_t ans_data = MSG_task_get_data(task_received);
732 MC_assert(task_received == task_sent);
734 if (task_received != task_sent) {
735 MSG_comm_destroy(node->comm_receive);
736 node->comm_receive = NULL;
737 handle_task(node, task_received);
740 DEBUG3("Received the answer to my 'Get Predecessor' request (task %p): the predecessor of node %d is %d",
741 task_received, ask_to, ans_data->answer_id);
742 predecessor_id = ans_data->answer_id;
744 MSG_comm_destroy(node->comm_receive);
745 node->comm_receive = NULL;
746 MSG_task_destroy(task_received);
747 task_data_destroy(req_data);
753 return predecessor_id;
757 * \brief Returns the closest preceding finger of an id
758 * with respect to the finger table of the current node.
759 * \param node the current node
760 * \param id the id to find
761 * \return the closest preceding finger of that id
763 int closest_preceding_node(node_t node, int id)
766 for (i = nb_bits - 1; i >= 0; i--) {
767 if (is_in_interval(node->fingers[i].id, node->id + 1, id - 1)) {
768 return node->fingers[i].id;
775 * \brief This function is called periodically. It checks the immediate
776 * successor of the current node.
777 * \param node the current node
779 static void stabilize(node_t node)
781 DEBUG0("Stabilizing node");
783 // get the predecessor of my immediate successor
785 int successor_id = node->fingers[0].id;
786 if (successor_id != node->id) {
787 candidate_id = remote_get_predecessor(node, successor_id);
790 candidate_id = node->pred_id;
793 // this node is a candidate to become my new successor
794 if (candidate_id != -1
795 && is_in_interval(candidate_id, node->id + 1, successor_id - 1)) {
796 set_finger(node, 0, candidate_id);
798 if (successor_id != node->id) {
799 remote_notify(node, successor_id, node->id);
804 * \brief Notifies the current node that its predecessor may have changed.
805 * \param node the current node
806 * \param candidate_id the possible new predecessor
808 static void notify(node_t node, int predecessor_candidate_id) {
810 if (node->pred_id == -1
811 || is_in_interval(predecessor_candidate_id, node->pred_id + 1, node->id - 1)) {
813 set_predecessor(node, predecessor_candidate_id);
814 print_finger_table(node);
817 DEBUG1("I don't have to change my predecessor to %d", predecessor_candidate_id);
822 * \brief Notifies a remote node that its predecessor may have changed.
823 * \param node the current node
824 * \param notify_id id of the node to notify
825 * \param candidate_id the possible new predecessor
827 static void remote_notify(node_t node, int notify_id, int predecessor_candidate_id) {
829 task_data_t req_data = xbt_new0(s_task_data_t, 1);
830 req_data->type = TASK_NOTIFY;
831 req_data->request_id = predecessor_candidate_id;
832 req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
834 // send a "Notify" request to notify_id
835 m_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, req_data);
836 DEBUG2("Sending a 'Notify' request (task %p) to %d", task, notify_id);
837 char mailbox[MAILBOX_NAME_SIZE];
838 get_mailbox(notify_id, mailbox);
839 msg_comm_t comm = MSG_task_isend(task, mailbox);
840 xbt_dynar_push(node->comms, &comm);
844 * \brief This function is called periodically.
845 * It refreshes the finger table of the current node.
846 * \param node the current node
848 static void fix_fingers(node_t node) {
850 DEBUG0("Fixing fingers");
851 int i = node->next_finger_to_fix;
852 int id = find_successor(node, node->id + powers2[i]);
855 if (id != node->fingers[i].id) {
856 set_finger(node, i, id);
857 print_finger_table(node);
859 node->next_finger_to_fix = (i + 1) % nb_bits;
864 * \brief This function is called periodically.
865 * It checks whether the predecessor has failed
866 * \param node the current node
868 static void check_predecessor(node_t node)
870 DEBUG0("Checking whether my predecessor is alive");
875 * \brief Performs a find successor request to a random id.
876 * \param node the current node
878 static void random_lookup(node_t node)
880 int id = 1337; // TODO pick a pseudorandom id
881 DEBUG1("Making a lookup request for id %d", id);
882 find_successor(node, id);
886 * \brief Main function.
888 int main(int argc, char *argv[])
891 printf("Usage: %s [-nb_bits=n] [-timeout=t] platform_file deployment_file\n", argv[0]);
892 printf("example: %s ../msg_platform.xml chord.xml\n", argv[0]);
896 MSG_global_init(&argc, argv);
898 char **options = &argv[1];
899 while (!strncmp(options[0], "-", 1)) {
901 int length = strlen("-nb_bits=");
902 if (!strncmp(options[0], "-nb_bits=", length) && strlen(options[0]) > length) {
903 nb_bits = atoi(options[0] + length);
904 DEBUG1("Set nb_bits to %d", nb_bits);
908 length = strlen("-timeout=");
909 if (!strncmp(options[0], "-timeout=", length) && strlen(options[0]) > length) {
910 timeout = atoi(options[0] + length);
911 DEBUG1("Set timeout to %d", timeout);
914 xbt_assert1(0, "Invalid chord option '%s'", options[0]);
920 const char* platform_file = options[0];
921 const char* application_file = options[1];
925 MSG_set_channel_number(0);
926 MSG_create_environment(platform_file);
928 MSG_function_register("node", node);
929 MSG_launch_application(application_file);
931 MSG_error_t res = MSG_main();
932 INFO1("Simulation time: %g", MSG_get_clock());