From: Frederic Suter Date: Wed, 10 Feb 2016 13:56:14 +0000 (+0100) Subject: Example cleaning X-Git-Tag: v3_13~890 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/ee9ae717297c900fdfed52ed22739a9591b04cba Example cleaning - replace deployment by something closer to what the provided generator produces - Stop using the ugly platform.xml file - revalidate tesh - reindent to 120 character width - use proper dynar function when possible - more uniform comments --- diff --git a/examples/msg/kademlia/answer.c b/examples/msg/kademlia/answer.c index ee1082bdd8..5aa893ff62 100644 --- a/examples/msg/kademlia/answer.c +++ b/examples/msg/kademlia/answer.c @@ -8,9 +8,7 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(msg_kademlia_node); -/** - * Initialize a node answer object. - */ +/** Initialize a node answer object. */ answer_t answer_init(unsigned int destination_id) { answer_t answer = xbt_new(s_answer_t, 1); @@ -21,9 +19,7 @@ answer_t answer_init(unsigned int destination_id) return answer; } -/** - * Destroys a node answer object. - */ +/** Destroys a node answer object. */ void answer_free(answer_t answer) { unsigned int i; @@ -34,22 +30,18 @@ void answer_free(answer_t answer) xbt_free(answer); } -/** - * @brief Prints a answer_t, for debugging purposes - */ +/** @brief Prints a answer_t, for debugging purposes */ void answer_print(answer_t answer) { unsigned int cpt; node_contact_t contact; XBT_INFO("Searching %08x, size %d", answer->destination_id, answer->size); xbt_dynar_foreach(answer->nodes, cpt, contact) { - XBT_INFO("Node %08x: %08x is at distance %d", cpt, contact->id, - contact->distance); + XBT_INFO("Node %08x: %08x is at distance %d", cpt, contact->id, contact->distance); } } -/** - * @brief Merge two answer_t together, only keeping the best nodes +/** @brief Merge two answer_t together, only keeping the best nodes * @param destination the destination in which the nodes will be put * @param source the source of the nodes to add */ @@ -72,9 +64,7 @@ unsigned int answer_merge(answer_t destination, answer_t source) return nb_added; } -/** - * Helper to sort answer_t objects - */ +/** Helper to sort answer_t objects */ static int _answer_sort_function(const void *e1, const void *e2) { node_contact_t c1 = *(void **) e1; @@ -88,8 +78,7 @@ static int _answer_sort_function(const void *e1, const void *e2) return 1; } -/** - * Sorts a answer_t, by node distance. +/** @brief Sorts a answer_t, by node distance. * @param answer the answer to sort * @param destination_id the id of the guy we are trying to find */ @@ -98,9 +87,7 @@ void answer_sort(answer_t answer) xbt_dynar_sort(answer->nodes, &_answer_sort_function); } -/** - * Trims a answer_t, in order for it to have a size of less or equal - * to "bucket_size" +/** @brief Trims a answer_t, in order for it to have a size of less or equal to "bucket_size" * @param answer the answer_t to trim */ void answer_trim(answer_t answer) @@ -111,12 +98,10 @@ void answer_trim(answer_t answer) answer->size--; node_contact_free(value); } - xbt_assert(xbt_dynar_length(answer->nodes) == answer->size, - "Wrong size for the answer"); + xbt_assert(xbt_dynar_length(answer->nodes) == answer->size, "Wrong size for the answer"); } -/** - * Adds the content of a bucket unsigned into a answer object. +/** @brief Adds the content of a bucket unsigned into a answer object. * @param bucket the bucket we have to had unsigned into * @param answer the answer object we're going to put the data in * @param destination_id the id of the guy we are trying to find. @@ -136,16 +121,14 @@ void answer_add_bucket(bucket_t bucket, answer_t answer) } } -/** - * Returns if the id supplied is in the answer. +/** @brief Returns if the id supplied is in the answer. * @param id : id we're looking for */ unsigned int answer_contains(answer_t answer, unsigned int id) { - unsigned int i = 0, size = xbt_dynar_length(answer->nodes); + unsigned int i = 0; node_contact_t contact; - for (i = 0; i < size; i++) { - contact = xbt_dynar_get_as(answer->nodes, i, node_contact_t); + xbt_dynar_foreach(answer->nodes, i, contact){ if (id == contact->id) { return 1; } @@ -153,8 +136,7 @@ unsigned int answer_contains(answer_t answer, unsigned int id) return 0; } -/** - * Returns if the destination we are trying to find is found +/** @brief Returns if the destination we are trying to find is found * @param answer the answer * @return if the destination is found. */ @@ -163,7 +145,6 @@ unsigned int answer_destination_found(answer_t answer) if (xbt_dynar_length(answer->nodes) < 1) { return 0; } - node_contact_t contact_tail = - xbt_dynar_get_as(answer->nodes, 0, node_contact_t); + node_contact_t contact_tail = xbt_dynar_get_as(answer->nodes, 0, node_contact_t); return contact_tail->distance == 0; } diff --git a/examples/msg/kademlia/answer.h b/examples/msg/kademlia/answer.h index 24902dd953..03fe76511d 100644 --- a/examples/msg/kademlia/answer.h +++ b/examples/msg/kademlia/answer.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012, 2014. 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 @@ -8,10 +8,8 @@ #define _KADEMLIA_EXAMPLES_ANSWER_H_ #include #include "routing_table.h" -/* - * Node query anwser. contains the elements closest - * to the id given. - */ + +/* Node query anwser. contains the elements closest to the id given. */ typedef struct s_node_answer { unsigned int destination_id; xbt_dynar_t nodes; //Dynar of node_contact_t diff --git a/examples/msg/kademlia/generate.py b/examples/msg/kademlia/generate.py index 1b9526cefb..c1c7a721a9 100755 --- a/examples/msg/kademlia/generate.py +++ b/examples/msg/kademlia/generate.py @@ -20,9 +20,9 @@ max_id = 2 ** nb_bits - 1 all_ids = [0] sys.stdout.write("\n" -"\n" -"\n" -" \n" % end_date) +"\n" +"\n \n" +" \n \n \n" % end_date) for i in range(1, nb_nodes): ok = False @@ -31,7 +31,8 @@ for i in range(1, nb_nodes): ok = not my_id in all_ids known_id = all_ids[random.randint(0, len(all_ids) - 1)] start_date = i * 10 - line = " \n" % (i, my_id, known_id,end_date) + line = " \n "\ + "\n \n \n \n" % (i, my_id, known_id,end_date) sys.stdout.write(line) all_ids.append(my_id) diff --git a/examples/msg/kademlia/kademlia.c b/examples/msg/kademlia/kademlia.c index 5ba12857b2..f7e2195ef2 100644 --- a/examples/msg/kademlia/kademlia.c +++ b/examples/msg/kademlia/kademlia.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2012, 2014-2015. 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 @@ -15,14 +15,11 @@ * 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; @@ -50,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."); } } @@ -66,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[]) { @@ -108,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. */ @@ -154,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 { @@ -172,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); @@ -187,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; @@ -214,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); @@ -227,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)) { @@ -242,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; @@ -254,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 { @@ -273,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++; @@ -291,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. @@ -305,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); @@ -322,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. @@ -352,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) @@ -364,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. @@ -376,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); @@ -393,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 */ @@ -405,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); @@ -425,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", @@ -441,47 +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 */ - xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n" - "\tExample: %s msg_platform.xml msg_deployment.xml\n", - argv[0], argv[0]); + 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); diff --git a/examples/msg/kademlia/kademlia.h b/examples/msg/kademlia/kademlia.h index 1fce788afb..b0990acc05 100644 --- a/examples/msg/kademlia/kademlia.h +++ b/examples/msg/kademlia/kademlia.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012, 2014. 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 @@ -11,8 +11,7 @@ //core kademlia functions 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 find_node(node_t node, unsigned int id_to_find, unsigned int count_in_stats); unsigned int ping(node_t node, unsigned int id_to_ping); void random_lookup(node_t node); @@ -23,5 +22,4 @@ void handle_task(node_t node, msg_task_t task); void handle_find_node(node_t node, task_data_t data); void handle_ping(node_t node, task_data_t data); - #endif /* _MSG_EXAMPLES_KADEMLIA_H */ diff --git a/examples/msg/kademlia/kademlia.tesh b/examples/msg/kademlia/kademlia.tesh index 2dfb2f0f02..ef3212a577 100644 --- a/examples/msg/kademlia/kademlia.tesh +++ b/examples/msg/kademlia/kademlia.tesh @@ -3,32 +3,32 @@ p Testing the Kademlia implementation with MSG ! output sort 19 -$ $SG_TEST_EXENV ${bindir:=.}/kademlia ${srcdir:=.}/../../platforms/platform.xml ${srcdir:=.}/kademlia.xml "--log=root.fmt:[%10.6r]%e(%02i:%P@%h)%e%m%n" -> [ 0.000000] ( 1:node@Jacquelin) Hi, I'm going to create the network with id 00000000 -> [ 0.000000] ( 2:node@Boivin) Hi, I'm going to join the network with id 00000001 -> [ 0.000000] ( 3:node@Jean_Yves) Hi, I'm going to join the network with id 00000003 -> [ 0.000000] ( 4:node@TeX) Hi, I'm going to join the network with id 00000007 -> [ 0.000000] ( 5:node@Geoff) Hi, I'm going to join the network with id 0000000f -> [ 0.000000] ( 6:node@Disney) Hi, I'm going to join the network with id 0000001f -> [ 0.000000] ( 7:node@iRMX) Hi, I'm going to join the network with id 0000003f -> [ 0.000000] ( 8:node@McGee) Hi, I'm going to join the network with id 0000007f -> [ 0.000000] ( 9:node@Gatien) Hi, I'm going to join the network with id 000000ff -> [ 0.000000] (10:node@Laroche) Hi, I'm going to join the network with id 000001ff -> [ 0.000000] (11:node@Tanguay) Hi, I'm going to join the network with id 000003ff -> [ 0.000000] (12:node@Morin) Hi, I'm going to join the network with id 000007ff -> [ 0.000000] (13:node@Ethernet) Hi, I'm going to join the network with id 00000fff -> [900.000000] ( 0:maestro@) Messages created: 1277 -> [900.000000] ( 0:maestro@) Simulated time: 900 -> [900.000000] ( 1:node@Jacquelin) 8/8 FIND_NODE have succeeded -> [900.000000] ( 2:node@Boivin) 7/7 FIND_NODE have succeeded -> [900.000000] ( 3:node@Jean_Yves) 6/6 FIND_NODE have succeeded -> [900.000000] ( 4:node@TeX) 6/6 FIND_NODE have succeeded -> [900.000000] ( 5:node@Geoff) 6/6 FIND_NODE have succeeded -> [900.000000] ( 6:node@Disney) 6/6 FIND_NODE have succeeded -> [900.000000] ( 7:node@iRMX) 6/6 FIND_NODE have succeeded -> [900.000000] ( 8:node@McGee) 7/7 FIND_NODE have succeeded -> [900.000000] ( 9:node@Gatien) 6/6 FIND_NODE have succeeded -> [900.000000] (10:node@Laroche) 6/6 FIND_NODE have succeeded -> [900.000000] (11:node@Tanguay) 6/6 FIND_NODE have succeeded -> [900.000000] (12:node@Morin) 7/7 FIND_NODE have succeeded -> [900.000000] (13:node@Ethernet) 7/7 FIND_NODE have succeeded +$ $SG_TEST_EXENV ${bindir:=.}/kademlia ${srcdir:=.}/../../platforms/cluster.xml ${srcdir:=.}/kademlia.xml "--log=root.fmt:[%10.6r]%e(%02i:%P@%h)%e%m%n" +> [ 0.000000] ( 1:node@node-0.acme.org) Hi, I'm going to create the network with id 00000000 +> [ 0.000000] ( 2:node@node-1.acme.org) Hi, I'm going to join the network with id 00000001 +> [ 0.000000] ( 3:node@node-2.acme.org) Hi, I'm going to join the network with id 00000003 +> [ 0.000000] ( 4:node@node-3.acme.org) Hi, I'm going to join the network with id 00000007 +> [ 0.000000] ( 5:node@node-4.acme.org) Hi, I'm going to join the network with id 0000000f +> [ 0.000000] ( 6:node@node-5.acme.org) Hi, I'm going to join the network with id 0000001f +> [ 0.000000] ( 7:node@node-6.acme.org) Hi, I'm going to join the network with id 0000003f +> [ 0.000000] ( 8:node@node-7.acme.org) Hi, I'm going to join the network with id 0000007f +> [ 0.000000] ( 9:node@node-8.acme.org) Hi, I'm going to join the network with id 000000ff +> [ 0.000000] (10:node@node-9.acme.org) Hi, I'm going to join the network with id 000001ff +> [ 0.000000] (11:node@node-10.acme.org) Hi, I'm going to join the network with id 000003ff +> [ 0.000000] (12:node@node-11.acme.org) Hi, I'm going to join the network with id 000007ff +> [ 0.000000] (13:node@node-12.acme.org) Hi, I'm going to join the network with id 00000fff +> [780.000000] ( 7:node@node-6.acme.org) 5/5 FIND_NODE have succeeded +> [780.000000] ( 9:node@node-8.acme.org) 5/5 FIND_NODE have succeeded +> [780.000000] ( 3:node@node-2.acme.org) 5/5 FIND_NODE have succeeded +> [780.000000] ( 2:node@node-1.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] (11:node@node-10.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] ( 1:node@node-0.acme.org) 7/7 FIND_NODE have succeeded +> [780.000000] ( 5:node@node-4.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] (13:node@node-12.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] ( 8:node@node-7.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] ( 6:node@node-5.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] (10:node@node-9.acme.org) 5/5 FIND_NODE have succeeded +> [780.000000] (12:node@node-11.acme.org) 6/6 FIND_NODE have succeeded +> [780.000000] ( 4:node@node-3.acme.org) 5/5 FIND_NODE have succeeded +> [780.000000] ( 0:maestro@) Messages created: 1179 +> [780.000000] ( 0:maestro@) Simulated time: 780 diff --git a/examples/msg/kademlia/kademlia.xml b/examples/msg/kademlia/kademlia.xml index 28a0b77f96..06278349fc 100644 --- a/examples/msg/kademlia/kademlia.xml +++ b/examples/msg/kademlia/kademlia.xml @@ -2,71 +2,71 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/examples/msg/kademlia/node.c b/examples/msg/kademlia/node.c index a01c9c494a..bc0c9e5155 100644 --- a/examples/msg/kademlia/node.c +++ b/examples/msg/kademlia/node.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2010, 2012-2015. The SimGrid Team. +/* Copyright (c) 2010, 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -10,12 +10,11 @@ #include "simgrid/msg.h" #include "xbt/log.h" #include "xbt/asserts.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_node, - "Messages specific for this msg example"); -/** - * \brief Initialization of a node - * \param node_id the id of the node - * \return the node created +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_node, "Messages specific for this msg example"); + +/** @brief Initialization of a node + * @param node_id the id of the node + * @return the node created */ node_t node_init(unsigned int node_id) { @@ -33,17 +32,14 @@ node_t node_init(unsigned int node_id) return node; } -/* - * \brief Node destructor - */ +/* @brief Node destructor */ void node_free(node_t node) { routing_table_free(node->table); xbt_free(node); } -/** - * @brief Updates/Puts the node id unsigned into our routing table +/** @brief Updates/Puts the node id unsigned into our routing table * @param node Our node data * @param id The id of the node we need to add unsigned into our routing table */ @@ -57,27 +53,25 @@ void node_routing_table_update(node_t node, unsigned int id) unsigned int id_pos = bucket_find_id(bucket, id); if (id_pos == -1) { - /* We check if the bucket is full or not. If it is, we evict - * old offline elements */ + /* We check if the bucket is full or not. If it is, we evict old offline elements */ if (xbt_dynar_length(bucket->nodes) < bucket_size) { //TODO: this is not really very efficient. Maybe we should use something else than dynars ? xbt_dynar_unshift(bucket->nodes, &id); XBT_VERB("I'm adding to my routing table %08x", id); } else { - /* TODO: we need to evict the old elements: that's why this function is in "node" instead of "routing table". This is not implemented yet. */ + /* TODO: we need to evict the old elements: that's why this function is in "node" instead of "routing table". + * This is not implemented yet. */ } } else { //We push to the front of the dynar the element. - unsigned int element = - xbt_dynar_get_as(bucket->nodes, id_pos, unsigned int); + unsigned int element = xbt_dynar_get_as(bucket->nodes, id_pos, unsigned int); xbt_dynar_remove_at(bucket->nodes, id_pos, NULL); xbt_dynar_unshift(bucket->nodes, &element); XBT_VERB("I'm updating %08x", element); } } -/** - * Finds the closest nodes to the node given. +/** @brief Finds the closest nodes to the node given. * @param node : our node * @param destination_id : the id of the guy we are trying to find */ @@ -88,19 +82,14 @@ answer_t node_find_closest(node_t node, unsigned int destination_id) /* We find the corresponding bucket for the id */ bucket_t bucket = routing_table_find_bucket(node->table, destination_id); int bucket_id = bucket->id; - xbt_assert((bucket_id <= identifier_size), - "Bucket found has a wrong identifier"); + xbt_assert((bucket_id <= identifier_size), "Bucket found has a wrong identifier"); /* So, we copy the contents of the bucket unsigned into our result dynar */ answer_add_bucket(bucket, answer); - /* However, if we don't have enough elements in our bucket, we NEED to - include at least - * "bucket_size" elements (if, of course, we know at least "bucket_size" elements. So we're going to look unsigned into the other buckets. + /* However, if we don't have enough elements in our bucket, we NEED to include at least "bucket_size" elements + * (if, of course, we know at least "bucket_size" elements. So we're going to look unsigned into the other buckets. */ - for (i = 1; - answer->size < bucket_size && ((bucket_id - i > 0) - || (bucket_id + i < identifier_size)); - i++) { + for (i = 1; answer->size < bucket_size && ((bucket_id - i > 0) || (bucket_id + i < identifier_size)); i++) { /* We check the previous buckets */ if (bucket_id - i >= 0) { bucket_t bucket_p = &node->table->buckets[bucket_id - i]; @@ -120,10 +109,9 @@ answer_t node_find_closest(node_t node, unsigned int destination_id) return answer; } -/** - * Returns an identifier which is in a specific bucket of a routing table - * @brief id id of the routing table owner - * @brief prefix id of the bucket where we want that identifier to be +/**@brief Returns an identifier which is in a specific bucket of a routing table + * @param id id of the routing table owner + * @param prefix id of the bucket where we want that identifier to be */ unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix) { @@ -134,8 +122,7 @@ unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix) return n ^ id; } -/** - * \brief Returns the prefix of an identifier. +/** @brief Returns the prefix of an identifier. * The prefix is the id of the bucket in which the remote identifier xor our identifier should be stored. * @param id : bigunsigned int id to test * @param nb_bits : key size @@ -151,17 +138,14 @@ unsigned int get_node_prefix(unsigned int id, unsigned int nb_bits) return 0; } -/** - * \brief Gets the mailbox name of a host given its identifier +/** @brief Gets the mailbox name of a host given its identifier */ void get_node_mailbox(unsigned int id, char *mailbox) { sprintf(mailbox, "%0*x", MAILBOX_NAME_SIZE, id); } -/** - * Constructor, build a new contact information. - */ +/** Constructor, build a new contact information. */ node_contact_t node_contact_new(unsigned int id, unsigned int distance) { node_contact_t contact = xbt_new(s_node_contact_t, 1); @@ -172,9 +156,7 @@ node_contact_t node_contact_new(unsigned int id, unsigned int distance) return contact; } -/** - * Builds a contact information from a contact information - */ +/** Builds a contact information from a contact information */ node_contact_t node_contact_copy(node_contact_t node_contact) { node_contact_t contact = xbt_new(s_node_contact_t, 1); @@ -185,10 +167,7 @@ node_contact_t node_contact_copy(node_contact_t node_contact) return contact; } -/** - * Destructor - * @param contact the node_contact to kill. - */ +/** Destructor */ void node_contact_free(node_contact_t contact) { xbt_free(contact); diff --git a/examples/msg/kademlia/node.h b/examples/msg/kademlia/node.h index 06b9a47689..9a5eab29df 100644 --- a/examples/msg/kademlia/node.h +++ b/examples/msg/kademlia/node.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012, 2014-2015. 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 @@ -10,20 +10,16 @@ #include "simgrid/msg.h" #include "common.h" - #include "answer.h" #include "routing_table.h" -/** - * Information about a foreign node - */ + +/* Information about a foreign node */ typedef struct s_node_contact { unsigned int id; //The node identifier unsigned int distance; //The distance from the node } s_node_contact_t, *node_contact_t; -/* - * Node data - */ +/* Node data */ typedef struct s_node { unsigned int id; //node id - 160 bits routing_table_t table; //node routing table @@ -39,11 +35,9 @@ typedef struct s_node { // node functions node_t node_init(unsigned int id); void node_free(node_t node); - void node_routing_table_update(node_t node, unsigned int id); answer_t node_find_closest(node_t node, unsigned int destination_id); - // identifier functions unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix); unsigned int get_node_prefix(unsigned int id, unsigned int nb_bits); diff --git a/examples/msg/kademlia/routing_table.c b/examples/msg/kademlia/routing_table.c index 350186850b..dae7c95857 100644 --- a/examples/msg/kademlia/routing_table.c +++ b/examples/msg/kademlia/routing_table.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2012-2015. The SimGrid Team. +/* Copyright (c) 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -9,11 +9,9 @@ #include "simgrid/msg.h" #include "xbt/log.h" #include "xbt/asserts.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_routing_table, - "Messages specific for this msg example"); -/** - * \brief Initialization of a node routing table. - */ +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_routing_table, "Messages specific for this msg example"); + +/** @brief Initialization of a node routing table. */ routing_table_t routing_table_init(unsigned int node_id) { unsigned int i; @@ -27,9 +25,7 @@ routing_table_t routing_table_init(unsigned int node_id) return table; } -/** - * \brief Frees the routing table - */ +/** @brief Frees the routing table */ void routing_table_free(routing_table_t table) { unsigned int i; @@ -41,18 +37,14 @@ void routing_table_free(routing_table_t table) xbt_free(table); } -/** - * Returns if the routing table contains the id. - */ +/** Returns if the routing table contains the id. */ unsigned int routing_table_contains(routing_table_t table, unsigned int node_id) { bucket_t bucket = routing_table_find_bucket(table, node_id); return bucket_contains(bucket, node_id); } -/** - * @brief prints the routing table, to debug stuff. - */ +/**@brief prints the routing table, to debug stuff. */ void routing_table_print(routing_table_t table) { unsigned int i, j, value; @@ -68,39 +60,28 @@ void routing_table_print(routing_table_t table) } } -/** - * Finds an identifier in a bucket and returns its position - * or returns -1 if it doesn't exists +/** @brief Finds an identifier in a bucket and returns its position or returns -1 if it doesn't exists * @param bucket the bucket in which we try to find our identifier * @param id the id */ unsigned int bucket_find_id(bucket_t bucket, unsigned int id) { - unsigned int i, length = xbt_dynar_length(bucket->nodes); - for (i = 0; i < length; i++) { //TODO: Use foreach maybe ? - if (id == xbt_dynar_get_as(bucket->nodes, i, unsigned int)) { + unsigned int i, current_id; + xbt_dynar_foreach(bucket->nodes, i, current_id){ + if (id == current_id){ return i; } } return -1; } -/** - * Returns if the bucket contains an identifier. - */ +/** Returns if the bucket contains an identifier. */ unsigned int bucket_contains(bucket_t bucket, unsigned int id) { - unsigned int length = xbt_dynar_length(bucket->nodes), i = 0; - for (i = 0; i < length; i++) { - if (id == xbt_dynar_get_as(bucket->nodes, i, unsigned int)) { - return 1; - } - } - return 0; + return xbt_dynar_member(bucket->nodes, &id); } -/** - * Fins the corresponding bucket in a routing table for a given identifier +/** @brief Finds the corresponding bucket in a routing table for a given identifier * @param table the routing table * @param id the identifier * @return the bucket in which the the identifier would be. @@ -109,9 +90,7 @@ bucket_t routing_table_find_bucket(routing_table_t table, unsigned int id) { unsigned int xor_number = table->id ^ id; unsigned int prefix = get_node_prefix(xor_number, identifier_size); - xbt_assert(prefix >= 0 - && prefix <= identifier_size, - "Tried to return a bucket that doesn't exist."); + xbt_assert(prefix >= 0 && prefix <= identifier_size, "Tried to return a bucket that doesn't exist."); bucket_t bucket = &table->buckets[prefix]; return bucket; } diff --git a/examples/msg/kademlia/routing_table.h b/examples/msg/kademlia/routing_table.h index 6baa1cf99b..63ae7b545e 100644 --- a/examples/msg/kademlia/routing_table.h +++ b/examples/msg/kademlia/routing_table.h @@ -8,31 +8,28 @@ #define _MSG_KADEMLIA_EXAMPLES_ROUTING_TABLE #include "common.h" #include -/* - * Routing table bucket - */ + +/* Routing table bucket */ typedef struct s_bucket { xbt_dynar_t nodes; //Nodes in the bucket. unsigned int id; //bucket id } s_bucket_t, *bucket_t; -/* - * Node routing table - */ +/* Node routing table */ typedef struct s_routing_table { unsigned int id; //node id of the client's routing table s_bucket_t *buckets; //Node bucket list - 160 sized. } s_routing_table_t, *routing_table_t; + // bucket functions unsigned int bucket_find_id(bucket_t bucket, unsigned int id); unsigned int bucket_contains(bucket_t bucket, unsigned int id); + // routing table functions routing_table_t routing_table_init(unsigned int node_id); void routing_table_free(routing_table_t table); -unsigned int routing_table_contains(routing_table_t table, - unsigned int node_id); +unsigned int routing_table_contains(routing_table_t table, unsigned int node_id); void routing_table_print(routing_table_t table); bucket_t routing_table_find_bucket(routing_table_t table, unsigned int id); - #endif /* _MSG_KADEMLIA_EXAMPLES_ROUTING_TABLE */ diff --git a/examples/msg/kademlia/task.c b/examples/msg/kademlia/task.c index 5268482b28..790a2e65db 100644 --- a/examples/msg/kademlia/task.c +++ b/examples/msg/kademlia/task.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2012, 2014. 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 @@ -6,19 +6,15 @@ #include "task.h" #include "answer.h" -XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_task, - "Messages specific for this msg example"); -/** - * Creates a new "find node" task +XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_task, "Messages specific for this msg example"); + +/** @brief Creates a new "find node" task * @param sender_id the id of the node who sends the task * @param destination_id the id the sender is trying to find * @param hostname the hostname of the node, for logging purposes */ -msg_task_t task_new_find_node(unsigned int sender_id, - unsigned int destination_id, char *mailbox, - const char *hostname) +msg_task_t task_new_find_node(unsigned int sender_id, unsigned int destination_id, char *mailbox, const char *hostname) { - task_data_t data = xbt_new(s_task_data_t, 1); data->type = TASK_FIND_NODE; @@ -28,26 +24,21 @@ msg_task_t task_new_find_node(unsigned int sender_id, data->answer_to = mailbox; data->issuer_host_name = hostname; - msg_task_t task = MSG_task_create(NULL, COMP_SIZE, COMM_SIZE, data); return task; } -/** - * Creates a new "answer to find node" task +/** @brief Creates a new "answer to find node" task * @param sender_id the node who sent the task * @param destination_id the node that should be found * @param answer the answer to send * @param mailbox The mailbox of the sender * @param hostname sender hostname */ -msg_task_t task_new_find_node_answer(unsigned int sender_id, - unsigned int destination_id, - answer_t answer, char *mailbox, - const char *hostname) +msg_task_t task_new_find_node_answer(unsigned int sender_id, unsigned int destination_id, answer_t answer, + char *mailbox, const char *hostname) { - task_data_t data = xbt_new(s_task_data_t, 1); data->type = TASK_FIND_NODE_ANSWER; @@ -62,16 +53,13 @@ msg_task_t task_new_find_node_answer(unsigned int sender_id, return task; } -/** - * Creates a new "ping" task +/** @brief Creates a new "ping" task * @param sender_id : sender node identifier * @param mailbox : mailbox where we should respond * @param hostname : hostname of the sender, for debugging purposes */ -msg_task_t task_new_ping(unsigned int sender_id, char *mailbox, - const char *hostname) +msg_task_t task_new_ping(unsigned int sender_id, char *mailbox, const char *hostname) { - task_data_t data = xbt_new(s_task_data_t, 1); data->type = TASK_PING; @@ -86,16 +74,13 @@ msg_task_t task_new_ping(unsigned int sender_id, char *mailbox, return task; } -/** - * Creates a new "ping answer" task +/** @brief Creates a new "ping answer" task * @param sender_id : sender node identifier * @param mailbox : mailbox of the sender * @param hostname : hostname of the sender, for debugging purposes */ -msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox, - const char *hostname) +msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox, const char *hostname) { - task_data_t data = xbt_new(s_task_data_t, 1); data->type = TASK_PING_ANSWER; @@ -110,8 +95,7 @@ msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox, return task; } -/** - * Destroys a task and its data +/** @brief Destroys a task and its data * @param task the task that'll be destroyed */ void task_free(msg_task_t task) @@ -126,11 +110,9 @@ void task_free(msg_task_t task) xbt_free(data); MSG_task_destroy(task); - } -/** - * Destroys a task and its data (taking a void* pointer +/** @brief Destroys a task and its data (taking a void* pointer * @param task The task that'll be destroyed */ void task_free_v(void *task) diff --git a/examples/msg/kademlia/task.h b/examples/msg/kademlia/task.h index 251a1e1311..a7cd143d91 100644 --- a/examples/msg/kademlia/task.h +++ b/examples/msg/kademlia/task.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012, 2014-2015. 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 @@ -9,9 +9,8 @@ #include "common.h" #include "node.h" #include "simgrid/msg.h" -/** - * Types of tasks exchanged - */ + +/* Types of tasks exchanged */ typedef enum { TASK_FIND_NODE, TASK_FIND_NODE_ANSWER, @@ -21,9 +20,8 @@ typedef enum { TASK_PING_ANSWER, TASK_LEAVING } e_task_type_t; -/** - * Data attached with the tasks - */ + +/* Data attached with the tasks */ typedef struct s_task_data { e_task_type_t type; unsigned int sender_id; //Id of the guy who sent the task @@ -34,19 +32,11 @@ typedef struct s_task_data { } s_task_data_t, *task_data_t; //Task handling functions -msg_task_t task_new_find_node(unsigned int sender_id, - unsigned int destination_id, char *mailbox, - const char *hostname); - -msg_task_t task_new_find_node_answer(unsigned int sender_id, - unsigned int destination_id, - answer_t answer, char *mailbox, - const char *hostname); - -msg_task_t task_new_ping(unsigned int sender_id, char *mailbox, - const char *hostname); -msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox, - const char *hostname); +msg_task_t task_new_find_node(unsigned int sender_id, unsigned int destination_id, char *mailbox, const char *hostname); +msg_task_t task_new_find_node_answer(unsigned int sender_id, unsigned int destination_id, answer_t answer, + char *mailbox, const char *hostname); +msg_task_t task_new_ping(unsigned int sender_id, char *mailbox, const char *hostname); +msg_task_t task_new_ping_answer(unsigned int sender_id, char *mailbox, const char *hostname); void task_free(msg_task_t task); void task_free_v(void *task); #endif /* _MSG_KADEMLIA_EXAMPLES_TASK */