+ INFO2("Joining the ring with id %d, knowing node %d", node->id, known_id);
+ set_predecessor(node, -1); // no predecessor (yet)
+
+ int successor_id;
+ do {
+ successor_id = remote_find_successor(node, known_id, node->id);
+ INFO0("My 'Find Successor' request has failed, let's try again");
+ } while (successor_id == -1);
+
+ set_finger(node, 0, successor_id);
+ print_finger_table(node);
+}
+
+/**
+ * \brief Notifies the current node that a node has just joined the network.
+ * \param node the current node
+ * \param new_node_id id of the new node in the network
+ */
+/*
+static void notify_node_joined(node_t node, int new_node_id)
+{
+ INFO1("A new node %d has joined the network, let's see if it is a neighboor", new_node_id);
+ if (is_in_interval(new_node_id, node->id + 1, node->fingers[0].id - 1)) {
+ // new_node_id is my new successor
+ set_finger(node, 0, new_node_id);
+ bootstrap(node, new_node_id);
+ }
+
+ if (is_in_interval(new_node_id, node->pred_id + 1, node->id - 1)) {
+ // new_node_id is my new predecessor
+ set_predecessor(node, new_node_id);
+ bootstrap(node, new_node_id);
+ }
+}
+*/
+
+/**
+ * \brief Notifies a remote node that the current node has just joined
+ * the network.
+ * \param node the current node
+ * \param notify_id id of the remote node to notify
+ */
+/*
+static void remote_notify_node_joined(node_t node, int notify_id)
+{
+ task_data_t req_data = xbt_new0(s_task_data_t, 1);
+ req_data->request_id = node->id;
+ req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
+
+ // send a "Notify" request to notify_id
+ INFO2("Sending a 'Notify' request to %d because I have joined the network with id %d", notify_id, node->id);
+ m_task_t task = MSG_task_create("Notify", 1000, 5000, req_data);
+ char* mailbox = get_mailbox(notify_id);
+ msg_comm_t comm = MSG_task_isend(task, mailbox);
+ xbt_dynar_push(node->comms, &comm);
+ xbt_free(mailbox);
+}
+*/
+
+/**
+* \brief Let the current node send queries to fill in its own finger table.
+* \param node the current node
+* \param ask_to_id id of a node to send queries to
+*/
+/*
+static void bootstrap(node_t node, int ask_to_id)
+{
+ INFO0("Filling my finger table");
+ int i, pred_id, succ_id;
+ int pow = 1;
+
+ for (i = 0; i < NB_BITS; i++)
+ {
+ pred_id = remote_find_successor(node, ask_to_id, pow);
+ do {
+ succ_id = pred_id;
+ pred_id = remote_find_predecessor(node, pred_id, pred_id);
+ } while (pred_id >= pow);
+
+ pow = pow << 1;
+ set_finger(node, i, succ_id);
+ }
+}
+*/
+
+/**
+ * \brief Makes the current node quit the system
+ * \param node the current node
+ */
+static void leave(node_t node)
+{
+ INFO0("Well Guys! I Think it's time for me to quit ;)");
+ quit_notify(node, 1); // notify to my successor ( >>> 1 );
+ quit_notify(node, -1); // notify my predecessor ( >>> -1);
+ // TODO ...
+}
+
+/*
+ * \brief Notifies the successor or the predecessor of the current node
+ * of the departure
+ * \param node the current node
+ * \param to 1 to notify the successor, -1 to notify the predecessor
+ * FIXME: notify both nodes with only one call
+ */
+static void quit_notify(node_t node, int to)
+{
+ task_data_t req_data = xbt_new0(s_task_data_t, 1);
+ req_data->request_id = node->id;
+ req_data->successor_id = node->fingers[0].id;
+ req_data->pred_id = node->pred_id;
+ req_data->issuer_host_name = MSG_host_get_name(MSG_host_self());
+ const char *task_name = NULL;
+ const char* to_mailbox = NULL;
+ if (to == 1) // notify my successor
+ {
+ to_mailbox = node->fingers[0].mailbox;
+ INFO2("Telling my Successor %d about my departure via mailbox %s",
+ node->fingers[0].id, to_mailbox);
+ task_name = "Predecessor Leaving";
+
+ }
+ else if (to == -1) // notify my predecessor
+ {
+ if (node->pred_id == -1) {
+ return;
+ }
+
+ to_mailbox = node->pred_mailbox;
+ INFO2("Telling my Predecessor %d about my departure via mailbox %s",
+ node->pred_id, to_mailbox);
+ task_name = "Predecessor Leaving";
+
+ }
+ m_task_t task = MSG_task_create(task_name, 1000, 5000, req_data);
+ //char* mailbox = get_mailbox(to_mailbox);
+ msg_comm_t comm = MSG_task_isend(task, to_mailbox);
+ xbt_dynar_push(node->comms, &comm);