X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/7f6d6ccebd0b469817710db21c45cf803223ec69..ee9ae717297c900fdfed52ed22739a9591b04cba:/examples/msg/kademlia/kademlia.c diff --git a/examples/msg/kademlia/kademlia.c b/examples/msg/kademlia/kademlia.c index 0173515a8d..f7e2195ef2 100644 --- a/examples/msg/kademlia/kademlia.c +++ b/examples/msg/kademlia/kademlia.c @@ -1,27 +1,25 @@ -/* Copyright (c) 2012. The SimGrid Team. +/* Copyright (c) 2012, 2014-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ + #include "kademlia.h" #include "node.h" #include "task.h" -#include "msg/msg.h" +#include "simgrid/msg.h" #include "xbt/log.h" #include "xbt/asserts.h" /** @addtogroup MSG_examples * kademlia/kademlia.c: Kademlia protocol * Implements the Kademlia protocol, using 32 bits identifiers. */ -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia, - "Messages specific for this msg example"); +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia, "Messages specific for this msg example"); extern long unsigned int smx_total_comms; -/** - * Main loop for the process - */ +/* Main loop for the process */ static void main_loop(node_t node, double deadline) { double next_lookup_time = MSG_get_clock() + random_lookup_interval; @@ -49,12 +47,10 @@ static void main_loop(node_t node, double deadline) node->receive_comm = NULL; if (status == MSG_OK) { - xbt_assert((node->task_received != NULL), - "We received an incorrect task"); + xbt_assert((node->task_received != NULL), "We received an incorrect task"); handle_task(node, node->task_received); } else { - xbt_assert((MSG_comm_get_task(node->receive_comm) == NULL), - "Comm failed but received a task."); + xbt_assert((MSG_comm_get_task(node->receive_comm) == NULL), "Comm failed but received a task."); XBT_DEBUG("Nevermind, the communication has failed."); } } @@ -65,20 +61,17 @@ static void main_loop(node_t node, double deadline) } //Cleanup the receiving communication. if (node->receive_comm != NULL) { - if (MSG_comm_test(node->receive_comm) - && MSG_comm_get_status(node->receive_comm) == MSG_OK) { + if (MSG_comm_test(node->receive_comm) && MSG_comm_get_status(node->receive_comm) == MSG_OK) { task_free(MSG_comm_get_task(node->receive_comm)); } MSG_comm_destroy(node->receive_comm); } } -/** - * \brief Node function - * Arguments : - * - my node ID - * - the ID of the person I know in the system (or not) - * - Time before I leave the system because I'm bored +/** @brief Node function + * @param my node ID + * @param the ID of the person I know in the system (or not) + * @param Time before I leave the system because I'm bored */ static int node(int argc, char *argv[]) { @@ -107,15 +100,14 @@ static int node(int argc, char *argv[]) XBT_INFO("I couldn't join the network :("); } XBT_DEBUG("I'm leaving the network"); - XBT_INFO("%d/%d FIND_NODE have succeeded", node->find_node_success, - node->find_node_success + node->find_node_failed); + XBT_INFO("%d/%d FIND_NODE have succeeded", node->find_node_success, node->find_node_success + node->find_node_failed); node_free(node); return 0; } /** - * Tries to join the network + * @brief Tries to join the network * @param node node data * @param id_known id of the node I know in the network. */ @@ -153,7 +145,6 @@ unsigned int join(node_t node, unsigned int id_known) node_list = data->answer; xbt_dynar_foreach(node_list->nodes, i, contact) { node_routing_table_update(node, contact->id); - //ping(node,contact->id); } task_free(node->task_received); } else { @@ -171,9 +162,7 @@ unsigned int join(node_t node, unsigned int id_known) } while (answer_got == 0 && trial < max_join_trials); /* Second step: Send a FIND_NODE to a a random node in buckets */ unsigned int bucket_id = routing_table_find_bucket(node->table, id_known)->id; - for (i = 0; - ((bucket_id - i) > 0 || (bucket_id + i) <= identifier_size) - && i < JOIN_BUCKETS_QUERIES; i++) { + for (i = 0; ((bucket_id - i) > 0 || (bucket_id + i) <= identifier_size) && i < JOIN_BUCKETS_QUERIES; i++) { if (bucket_id - i > 0) { unsigned int id_in_bucket = get_id_in_prefix(node->id, bucket_id - i); find_node(node, id_in_bucket, 0); @@ -186,15 +175,12 @@ unsigned int join(node_t node, unsigned int id_known) return answer_got; } -/** - * Send a request to find a node in the node routing table. - * @brief node our node data - * @brief id_to_find the id of the node we are trying to find +/** @brief Send a request to find a node in the node routing table. + * @param node our node data + * @param id_to_find the id of the node we are trying to find */ -unsigned int find_node(node_t node, unsigned int id_to_find, - unsigned int count_in_stats) +unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_in_stats) { - unsigned int i = 0; unsigned int queries, answers; unsigned int destination_found = 0; @@ -213,9 +199,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, msg_error_t status; - /* Ask the nodes on our list if they have information about - * the node we are trying to find */ - + /* Ask the nodes on our list if they have information about the node we are trying to find */ do { answers = 0; queries = send_find_node_to_best(node, node_list); @@ -226,8 +210,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, do { if (node->receive_comm == NULL) { node->task_received = NULL; - node->receive_comm = - MSG_task_irecv(&node->task_received, node->mailbox); + node->receive_comm = MSG_task_irecv(&node->task_received, node->mailbox); } if (node->receive_comm) { if (MSG_comm_test(node->receive_comm)) { @@ -241,8 +224,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, xbt_assert((data != NULL), "No data in the task"); //Check if what we have received is what we are looking for. - if (data->type == TASK_FIND_NODE_ANSWER - && data->answer->destination_id == id_to_find) { + if (data->type == TASK_FIND_NODE_ANSWER && data->answer->destination_id == id_to_find) { //Handle the answer node_routing_table_update(node, data->sender_id); node_contact_t contact; @@ -253,8 +235,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, nodes_added = answer_merge(node_list, data->answer); XBT_DEBUG("Received an answer from %s (%s) with %ld nodes on it", - data->answer_to, data->issuer_host_name, - xbt_dynar_length(data->answer->nodes)); + data->answer_to, data->issuer_host_name, xbt_dynar_length(data->answer->nodes)); task_free(node->task_received); } else { @@ -272,8 +253,8 @@ unsigned int find_node(node_t node, unsigned int id_to_find, } } while (MSG_get_clock() < timeout && answers < queries); destination_found = answer_destination_found(node_list); - } while (!destination_found && (nodes_added > 0 || answers == 0) - && MSG_get_clock() < global_timeout && steps < MAX_STEPS); + } while (!destination_found && (nodes_added > 0 || answers == 0) && MSG_get_clock() < global_timeout + && steps < MAX_STEPS); if (destination_found) { if (count_in_stats) node->find_node_success++; @@ -290,8 +271,7 @@ unsigned int find_node(node_t node, unsigned int id_to_find, return destination_found; } -/** - * Pings a node in the system to see if it is online. +/** @brief Pings a node in the system to see if it is online. * @param node Our node data * @param id_to_ping the id of a node we want to see if it is online. * @return if the ping succeded or not. @@ -304,9 +284,7 @@ unsigned int ping(node_t node, unsigned int id_to_ping) unsigned int destination_found = 0; double timeout = MSG_get_clock() + ping_timeout; - msg_task_t ping_task = - task_new_ping(node->id, node->mailbox, - MSG_host_get_name(MSG_host_self())); + msg_task_t ping_task = task_new_ping(node->id, node->mailbox, MSG_host_get_name(MSG_host_self())); msg_task_t task_received = NULL; XBT_VERB("PING %08x", id_to_ping); @@ -321,8 +299,7 @@ unsigned int ping(node_t node, unsigned int id_to_ping) do { task_received = NULL; msg_error_t status = - MSG_task_receive_with_timeout(&task_received, node->mailbox, - ping_timeout); + MSG_task_receive_with_timeout(&task_received, node->mailbox, ping_timeout); if (status == MSG_OK) { xbt_assert((task_received != NULL), "Invalid task received"); //Checking if it's what we are waiting for or not. @@ -351,8 +328,7 @@ unsigned int ping(node_t node, unsigned int id_to_ping) return 1; } -/** - * Does a pseudo-random lookup for someone in the system +/** @brief Does a pseudo-random lookup for someone in the system * @param node caller node data */ void random_lookup(node_t node) @@ -363,8 +339,7 @@ void random_lookup(node_t node) find_node(node, id_to_look, 1); } -/** - * @brief Send a "FIND_NODE" to a node +/** @brief Send a "FIND_NODE" to a node * @param node sender node data * @param id node we are querying * @param destination node we are trying to find. @@ -375,9 +350,7 @@ void send_find_node(node_t node, unsigned int id, unsigned int destination) /* Gets the mailbox to send to */ get_node_mailbox(id, mailbox); /* Build the task */ - msg_task_t task = - task_new_find_node(node->id, destination, node->mailbox, - MSG_host_get_name(MSG_host_self())); + msg_task_t task = task_new_find_node(node->id, destination, node->mailbox, MSG_host_get_name(MSG_host_self())); /* Send the task */ xbt_assert((task != NULL), "Trying to send a NULL task."); MSG_task_dsend(task, mailbox, task_free_v); @@ -392,7 +365,8 @@ unsigned int send_find_node_to_best(node_t node, answer_t node_list) unsigned int i = 0, j = 0; unsigned int destination = node_list->destination_id; node_contact_t node_to_query; - while (j < kademlia_alpha && i < node_list->size) { /* We need to have at most "kademlia_alpha" requets each time, according to the protocol */ + while (j < kademlia_alpha && i < node_list->size) { + /* We need to have at most "kademlia_alpha" requests each time, according to the protocol */ /* Gets the node we want to send the query to */ node_to_query = xbt_dynar_get_as(node_list->nodes, i, node_contact_t); if (node_to_query->id != node->id) { /* No need to query ourselves */ @@ -404,9 +378,7 @@ unsigned int send_find_node_to_best(node_t node, answer_t node_list) return i; } -/** - * \brief Handles an incomming received task - */ +/** @brief Handles an incoming received task */ void handle_task(node_t node, msg_task_t task) { task_data_t data = MSG_task_get_data(task); @@ -424,15 +396,12 @@ void handle_task(node_t node, msg_task_t task) handle_ping(node, data); break; default: - break; } task_free(task); } -/** - * \brief Handles the answer to an incomming "find_node" task - */ +/** @brief Handles the answer to an incoming "find_node" task */ void handle_find_node(node_t node, task_data_t data) { XBT_VERB("Received a FIND_NODE from %s (%s), he's trying to find %08x", @@ -440,48 +409,35 @@ void handle_find_node(node_t node, task_data_t data) //Building the answer to the request answer_t answer = node_find_closest(node, data->destination_id); //Building the task to send - msg_task_t task = - task_new_find_node_answer(node->id, data->destination_id, answer, - node->mailbox, - MSG_host_get_name(MSG_host_self())); + msg_task_t task = task_new_find_node_answer(node->id, data->destination_id, answer, node->mailbox, + MSG_host_get_name(MSG_host_self())); //Sending the task MSG_task_dsend(task, data->answer_to, task_free_v); } -/** - * \brief handles the answer to a ping - */ +/** @brief handles the answer to a ping */ void handle_ping(node_t node, task_data_t data) { - XBT_VERB("Received a PING request from %s (%s)", data->answer_to, - data->issuer_host_name); + XBT_VERB("Received a PING request from %s (%s)", data->answer_to, data->issuer_host_name); //Building the answer to the request - msg_task_t task = - task_new_ping_answer(node->id, data->answer_to, - MSG_host_get_name(MSG_host_self())); + msg_task_t task = task_new_ping_answer(node->id, data->answer_to, MSG_host_get_name(MSG_host_self())); MSG_task_dsend(task, data->answer_to, task_free_v); } -/** - * \brief Main function - */ +/** @brief Main function */ int main(int argc, char *argv[]) { - MSG_init(&argc, argv); /* Check the arguments */ - if (argc < 3) { - printf("Usage: %s platform_file deployment_file \n", argv[0]); - return -1; - } + xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n\tExample: %s msg_platform.xml msg_deployment.xml\n", + argv[0], argv[0]); const char *platform_file = argv[1]; const char *deployment_file = argv[2]; MSG_create_environment(platform_file); - MSG_function_register("node", node); MSG_launch_application(deployment_file); @@ -490,8 +446,5 @@ int main(int argc, char *argv[]) XBT_CRITICAL("Messages created: %ld", smx_total_comms); XBT_INFO("Simulated time: %g", MSG_get_clock()); - if (res == MSG_OK) - return 0; - else - return 1; + return res != MSG_OK; }