X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d2cd6190c7a6ba8f102134cf6ab3043f7df8f77e..611d822b02f836d7abe031cced6adc4281ef4356:/examples/msg/dht-kademlia/dht-kademlia.c diff --git a/examples/msg/dht-kademlia/dht-kademlia.c b/examples/msg/dht-kademlia/dht-kademlia.c index 186c620109..f9323730d0 100644 --- a/examples/msg/dht-kademlia/dht-kademlia.c +++ b/examples/msg/dht-kademlia/dht-kademlia.c @@ -1,5 +1,4 @@ -/* Copyright (c) 2012, 2014-2016. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2012-2018. 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. */ @@ -9,6 +8,9 @@ #include "task.h" #include "simgrid/msg.h" + +#include /* snprintf */ + /** @addtogroup MSG_examples * kademlia/kademlia.c: Kademlia protocol * Implements the Kademlia protocol, using 32 bits identifiers. @@ -96,7 +98,7 @@ 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("%u/%u FIND_NODE have succeeded", node->find_node_success, node->find_node_success + node->find_node_failed); node_free(node); return 0; @@ -112,7 +114,8 @@ unsigned int join(node_t node, unsigned int id_known) answer_t node_list; msg_error_t status; unsigned int trial = 0; - unsigned int i, answer_got = 0; + unsigned int i; + unsigned int answer_got = 0; /* Add the guy we know to our routing table and ourselves. */ node_routing_table_update(node, node->id); @@ -158,8 +161,8 @@ 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++) { - if (bucket_id - i > 0) { + for (i = 0; ((bucket_id > i) || (bucket_id + i) <= identifier_size) && i < JOIN_BUCKETS_QUERIES; i++) { + if (bucket_id > i) { unsigned int id_in_bucket = get_id_in_prefix(node->id, bucket_id - i); find_node(node, id_in_bucket, 0); } @@ -178,15 +181,13 @@ unsigned int join(node_t node, unsigned int id_known) 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 queries; + unsigned int answers; unsigned int destination_found = 0; unsigned int nodes_added = 0; - double time_beginreceive; - double timeout, global_timeout = MSG_get_clock() + find_node_global_timeout; + double global_timeout = MSG_get_clock() + find_node_global_timeout; unsigned int steps = 0; - xbt_assert((id_to_find >= 0), "Id supplied incorrect"); - /* First we build a list of who we already know */ answer_t node_list = node_find_closest(node, id_to_find); xbt_assert((node_list != NULL), "node_list incorrect"); @@ -200,9 +201,9 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_ answers = 0; queries = send_find_node_to_best(node, node_list); nodes_added = 0; - timeout = MSG_get_clock() + find_node_timeout; + double timeout = MSG_get_clock() + find_node_timeout; steps++; - time_beginreceive = MSG_get_clock(); + double time_beginreceive = MSG_get_clock(); do { if (node->receive_comm == NULL) { node->task_received = NULL; @@ -230,8 +231,8 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_ answers++; 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)); + XBT_DEBUG("Received an answer from %s (%s) with %lu nodes on it", data->answer_to, data->issuer_host_name, + xbt_dynar_length(data->answer->nodes)); task_free(node->task_received); } else { @@ -255,12 +256,12 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_ if (count_in_stats) node->find_node_success++; if (queries > 4) - XBT_VERB("FIND_NODE on %08x success in %d steps", id_to_find, steps); + XBT_VERB("FIND_NODE on %08x success in %u steps", id_to_find, steps); node_routing_table_update(node, id_to_find); } else { if (count_in_stats) { node->find_node_failed++; - XBT_VERB("%08x not found in %d steps", id_to_find, steps); + XBT_VERB("%08x not found in %u steps", id_to_find, steps); } } answer_free(node_list); @@ -274,10 +275,9 @@ unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_ */ unsigned int ping(node_t node, unsigned int id_to_ping) { - char mailbox[MAILBOX_NAME_SIZE + 1]; - sprintf(mailbox, "%0*x", MAILBOX_NAME_SIZE, id_to_ping); + char mailbox[MAILBOX_NAME_SIZE]; + snprintf(mailbox, MAILBOX_NAME_SIZE, "%u", 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())); @@ -304,24 +304,21 @@ unsigned int ping(node_t node, unsigned int id_to_ping) if (data->type == TASK_PING_ANSWER && id_to_ping == data->sender_id) { XBT_VERB("Ping to %s succeeded", mailbox); node_routing_table_update(node, data->sender_id); - destination_found = 1; task_free(task_received); + return 1; // Destination found, ping succeeded! } else { //If it's not our answer, we answer the query anyway. handle_task(node, task_received); } } - } while (destination_found == 0 && MSG_get_clock() < timeout); + } while (MSG_get_clock() < timeout); if (MSG_get_clock() >= timeout) { XBT_DEBUG("Ping to %s has timeout.", mailbox); return 0; } - if (destination_found == -1) { - XBT_DEBUG("It seems that %s is offline...", mailbox); - return 0; - } - return 1; + XBT_DEBUG("It seems that %s is offline...", mailbox); + return -1; } /** @brief Does a pseudo-random lookup for someone in the system @@ -342,7 +339,7 @@ void random_lookup(node_t node) */ void send_find_node(node_t node, unsigned int id, unsigned int destination) { - char mailbox[MAILBOX_NAME_SIZE + 1]; + char mailbox[MAILBOX_NAME_SIZE]; /* Gets the mailbox to send to */ get_node_mailbox(id, mailbox); /* Build the task */ @@ -358,13 +355,13 @@ void send_find_node(node_t node, unsigned int id, unsigned int destination) */ unsigned int send_find_node_to_best(node_t node, answer_t node_list) { - unsigned int i = 0, j = 0; + unsigned int i = 0; + unsigned int 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" 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); + node_contact_t 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 */ send_find_node(node, node_to_query->id, destination); j++;