1 /* Copyright (c) 2012, 2014-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "dht-kademlia.h"
11 #include "simgrid/msg.h"
12 /** @addtogroup MSG_examples
13 * <b>kademlia/kademlia.c: Kademlia protocol</b>
14 * Implements the Kademlia protocol, using 32 bits identifiers.
16 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia, "Messages specific for this msg example");
18 /* Main loop for the process */
19 static void main_loop(node_t node, double deadline)
21 double next_lookup_time = MSG_get_clock() + random_lookup_interval;
22 XBT_VERB("Main loop start");
23 while (MSG_get_clock() < deadline) {
25 if (node->receive_comm == NULL) {
26 node->task_received = NULL;
27 node->receive_comm = MSG_task_irecv(&node->task_received, node->mailbox);
29 if (node->receive_comm) {
30 if (!MSG_comm_test(node->receive_comm)) {
31 /* We search for a pseudo random node */
32 if (MSG_get_clock() >= next_lookup_time) {
34 next_lookup_time += random_lookup_interval;
36 //Didn't get a task: sleep for a while...
40 //There has been a transfer, we need to handle it !
41 msg_error_t status = MSG_comm_get_status(node->receive_comm);
42 MSG_comm_destroy(node->receive_comm);
43 node->receive_comm = NULL;
45 if (status == MSG_OK) {
46 xbt_assert((node->task_received != NULL), "We received an incorrect task");
47 handle_task(node, node->task_received);
49 xbt_assert((MSG_comm_get_task(node->receive_comm) == NULL), "Comm failed but received a task.");
50 XBT_DEBUG("Nevermind, the communication has failed.");
54 //Didn't get a comm: sleep.
58 //Cleanup the receiving communication.
59 if (node->receive_comm != NULL) {
60 if (MSG_comm_test(node->receive_comm) && MSG_comm_get_status(node->receive_comm) == MSG_OK) {
61 task_free(MSG_comm_get_task(node->receive_comm));
63 MSG_comm_destroy(node->receive_comm);
67 /** @brief Node function
69 * @param the ID of the person I know in the system (or not)
70 * @param Time before I leave the system because I'm bored
72 static int node(int argc, char *argv[])
74 unsigned int join_sucess = 1;
76 xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
77 /* Node initialization */
78 unsigned int id = strtoul(argv[1], NULL, 0);
79 node_t node = node_init(id);
82 XBT_INFO("Hi, I'm going to join the network with id %s", node->mailbox);
83 unsigned int id_known = strtoul(argv[2], NULL, 0);
84 join_sucess = join(node, id_known);
85 deadline = strtod(argv[3], NULL);
87 deadline = strtod(argv[2], NULL);
88 XBT_INFO("Hi, I'm going to create the network with id %s", node->mailbox);
89 node_routing_table_update(node, node->id);
92 XBT_VERB("Ok, I'm joining the network with id %s", node->mailbox);
93 //We start the main loop
94 main_loop(node, deadline);
96 XBT_INFO("I couldn't join the network :(");
98 XBT_DEBUG("I'm leaving the network");
99 XBT_INFO("%u/%u FIND_NODE have succeeded", node->find_node_success, node->find_node_success + node->find_node_failed);
106 * @brief Tries to join the network
107 * @param node node data
108 * @param id_known id of the node I know in the network.
110 unsigned int join(node_t node, unsigned int id_known)
114 unsigned int trial = 0;
116 unsigned int answer_got = 0;
118 /* Add the guy we know to our routing table and ourselves. */
119 node_routing_table_update(node, node->id);
120 node_routing_table_update(node, id_known);
122 /* First step: Send a "FIND_NODE" request to the node we know */
123 send_find_node(node, id_known, node->id);
125 if (node->receive_comm == NULL) {
126 node->task_received = NULL;
127 node->receive_comm = MSG_task_irecv(&node->task_received, node->mailbox);
129 if (node->receive_comm) {
130 if (MSG_comm_test(node->receive_comm)) {
131 status = MSG_comm_get_status(node->receive_comm);
132 MSG_comm_destroy(node->receive_comm);
133 node->receive_comm = NULL;
134 if (status == MSG_OK) {
135 XBT_DEBUG("Received an answer from the node I know.");
137 //retrieve the node list and ping them.
138 task_data_t data = MSG_task_get_data(node->task_received);
139 xbt_assert((data != NULL), "Null data received");
140 if (data->type == TASK_FIND_NODE_ANSWER) {
141 node_contact_t contact;
142 node_list = data->answer;
143 xbt_dynar_foreach(node_list->nodes, i, contact) {
144 node_routing_table_update(node, contact->id);
146 task_free(node->task_received);
148 handle_task(node, node->task_received);
154 MSG_process_sleep(1);
157 MSG_process_sleep(1);
159 } while (answer_got == 0 && trial < max_join_trials);
160 /* Second step: Send a FIND_NODE to a a random node in buckets */
161 unsigned int bucket_id = routing_table_find_bucket(node->table, id_known)->id;
162 for (i = 0; ((bucket_id > i) || (bucket_id + i) <= identifier_size) && i < JOIN_BUCKETS_QUERIES; i++) {
164 unsigned int id_in_bucket = get_id_in_prefix(node->id, bucket_id - i);
165 find_node(node, id_in_bucket, 0);
167 if (bucket_id + i <= identifier_size) {
168 unsigned int id_in_bucket = get_id_in_prefix(node->id, bucket_id + i);
169 find_node(node, id_in_bucket, 0);
175 /** @brief Send a request to find a node in the node routing table.
176 * @param node our node data
177 * @param id_to_find the id of the node we are trying to find
179 unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_in_stats)
182 unsigned int queries;
183 unsigned int answers;
184 unsigned int destination_found = 0;
185 unsigned int nodes_added = 0;
186 double global_timeout = MSG_get_clock() + find_node_global_timeout;
187 unsigned int steps = 0;
189 /* First we build a list of who we already know */
190 answer_t node_list = node_find_closest(node, id_to_find);
191 xbt_assert((node_list != NULL), "node_list incorrect");
193 XBT_DEBUG("Doing a FIND_NODE on %08x", id_to_find);
197 /* Ask the nodes on our list if they have information about the node we are trying to find */
200 queries = send_find_node_to_best(node, node_list);
202 double timeout = MSG_get_clock() + find_node_timeout;
204 double time_beginreceive = MSG_get_clock();
206 if (node->receive_comm == NULL) {
207 node->task_received = NULL;
208 node->receive_comm = MSG_task_irecv(&node->task_received, node->mailbox);
210 if (node->receive_comm) {
211 if (MSG_comm_test(node->receive_comm)) {
212 status = MSG_comm_get_status(node->receive_comm);
213 MSG_comm_destroy(node->receive_comm);
214 node->receive_comm = NULL;
215 if (status == MSG_OK) {
216 xbt_assert((node->task_received != NULL), "Invalid task received");
217 //Figure out if we received an answer or something else
218 task_data_t data = MSG_task_get_data(node->task_received);
219 xbt_assert((data != NULL), "No data in the task");
221 //Check if what we have received is what we are looking for.
222 if (data->type == TASK_FIND_NODE_ANSWER && data->answer->destination_id == id_to_find) {
224 node_routing_table_update(node, data->sender_id);
225 node_contact_t contact;
226 xbt_dynar_foreach(node_list->nodes, i, contact) {
227 node_routing_table_update(node, contact->id);
231 nodes_added = answer_merge(node_list, data->answer);
232 XBT_DEBUG("Received an answer from %s (%s) with %lu nodes on it", data->answer_to, data->issuer_host_name,
233 xbt_dynar_length(data->answer->nodes));
235 task_free(node->task_received);
237 handle_task(node, node->task_received);
238 //Update the timeout if we didn't have our answer
239 timeout += MSG_get_clock() - time_beginreceive;
240 time_beginreceive = MSG_get_clock();
244 MSG_process_sleep(1);
247 MSG_process_sleep(1);
249 } while (MSG_get_clock() < timeout && answers < queries);
250 destination_found = answer_destination_found(node_list);
251 } while (!destination_found && (nodes_added > 0 || answers == 0) && MSG_get_clock() < global_timeout
252 && steps < MAX_STEPS);
253 if (destination_found) {
255 node->find_node_success++;
257 XBT_VERB("FIND_NODE on %08x success in %u steps", id_to_find, steps);
258 node_routing_table_update(node, id_to_find);
260 if (count_in_stats) {
261 node->find_node_failed++;
262 XBT_VERB("%08x not found in %u steps", id_to_find, steps);
265 answer_free(node_list);
266 return destination_found;
269 /** @brief Pings a node in the system to see if it is online.
270 * @param node Our node data
271 * @param id_to_ping the id of a node we want to see if it is online.
272 * @return if the ping succeded or not.
274 unsigned int ping(node_t node, unsigned int id_to_ping)
276 char mailbox[MAILBOX_NAME_SIZE];
277 snprintf(mailbox, MAILBOX_NAME_SIZE, "%u", id_to_ping);
279 double timeout = MSG_get_clock() + ping_timeout;
281 msg_task_t ping_task = task_new_ping(node->id, node->mailbox, MSG_host_get_name(MSG_host_self()));
282 msg_task_t task_received = NULL;
284 XBT_VERB("PING %08x", id_to_ping);
286 //Check that we aren't trying to ping ourselves
287 if (id_to_ping == node->id) {
291 /* Sending the ping task */
292 MSG_task_dsend(ping_task, mailbox, task_free_v);
294 task_received = NULL;
296 MSG_task_receive_with_timeout(&task_received, node->mailbox, ping_timeout);
297 if (status == MSG_OK) {
298 xbt_assert((task_received != NULL), "Invalid task received");
299 //Checking if it's what we are waiting for or not.
300 task_data_t data = MSG_task_get_data(task_received);
301 xbt_assert((data != NULL), "didn't receive any data...");
302 if (data->type == TASK_PING_ANSWER && id_to_ping == data->sender_id) {
303 XBT_VERB("Ping to %s succeeded", mailbox);
304 node_routing_table_update(node, data->sender_id);
305 task_free(task_received);
306 return 1; // Destination found, ping succeeded!
308 //If it's not our answer, we answer the query anyway.
309 handle_task(node, task_received);
312 } while (MSG_get_clock() < timeout);
314 if (MSG_get_clock() >= timeout) {
315 XBT_DEBUG("Ping to %s has timeout.", mailbox);
318 XBT_DEBUG("It seems that %s is offline...", mailbox);
322 /** @brief Does a pseudo-random lookup for someone in the system
323 * @param node caller node data
325 void random_lookup(node_t node)
327 unsigned int id_to_look = RANDOM_LOOKUP_NODE; //Totally random.
328 /* TODO: Use some pseudorandom generator like RngStream. */
329 XBT_DEBUG("I'm doing a random lookup");
330 find_node(node, id_to_look, 1);
333 /** @brief Send a "FIND_NODE" to a node
334 * @param node sender node data
335 * @param id node we are querying
336 * @param destination node we are trying to find.
338 void send_find_node(node_t node, unsigned int id, unsigned int destination)
340 char mailbox[MAILBOX_NAME_SIZE];
341 /* Gets the mailbox to send to */
342 get_node_mailbox(id, mailbox);
344 msg_task_t task = task_new_find_node(node->id, destination, node->mailbox, MSG_host_get_name(MSG_host_self()));
346 xbt_assert((task != NULL), "Trying to send a NULL task.");
347 MSG_task_dsend(task, mailbox, task_free_v);
348 XBT_VERB("Asking %s for its closest nodes", mailbox);
352 * Sends to the best "kademlia_alpha" nodes in the "node_list" array a "FIND_NODE" request, to ask them for their best nodes
354 unsigned int send_find_node_to_best(node_t node, answer_t node_list)
358 unsigned int destination = node_list->destination_id;
359 while (j < kademlia_alpha && i < node_list->size) {
360 /* We need to have at most "kademlia_alpha" requests each time, according to the protocol */
361 /* Gets the node we want to send the query to */
362 node_contact_t node_to_query = xbt_dynar_get_as(node_list->nodes, i, node_contact_t);
363 if (node_to_query->id != node->id) { /* No need to query ourselves */
364 send_find_node(node, node_to_query->id, destination);
372 /** @brief Handles an incoming received task */
373 void handle_task(node_t node, msg_task_t task)
375 task_data_t data = MSG_task_get_data(task);
376 xbt_assert((data != NULL), "Received NULL data");
377 //Adding/updating the guy to our routing table
378 node_routing_table_update(node, data->sender_id);
379 switch (data->type) {
381 handle_find_node(node, data);
383 case TASK_FIND_NODE_ANSWER:
384 XBT_DEBUG("Received a wrong answer for a FIND_NODE");
387 handle_ping(node, data);
395 /** @brief Handles the answer to an incoming "find_node" task */
396 void handle_find_node(node_t node, task_data_t data)
398 XBT_VERB("Received a FIND_NODE from %s (%s), he's trying to find %08x",
399 data->answer_to, data->issuer_host_name, data->destination_id);
400 //Building the answer to the request
401 answer_t answer = node_find_closest(node, data->destination_id);
402 //Building the task to send
403 msg_task_t task = task_new_find_node_answer(node->id, data->destination_id, answer, node->mailbox,
404 MSG_host_get_name(MSG_host_self()));
406 MSG_task_dsend(task, data->answer_to, task_free_v);
409 /** @brief handles the answer to a ping */
410 void handle_ping(node_t node, task_data_t data)
412 XBT_VERB("Received a PING request from %s (%s)", data->answer_to, data->issuer_host_name);
413 //Building the answer to the request
414 msg_task_t task = task_new_ping_answer(node->id, data->answer_to, MSG_host_get_name(MSG_host_self()));
416 MSG_task_dsend(task, data->answer_to, task_free_v);
419 /** @brief Main function */
420 int main(int argc, char *argv[])
422 MSG_init(&argc, argv);
424 /* Check the arguments */
425 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n\tExample: %s msg_platform.xml msg_deployment.xml\n",
428 const char *platform_file = argv[1];
429 const char *deployment_file = argv[2];
431 MSG_create_environment(platform_file);
432 MSG_function_register("node", node);
433 MSG_launch_application(deployment_file);
435 msg_error_t res = MSG_main();
437 XBT_INFO("Simulated time: %g", MSG_get_clock());
439 return res != MSG_OK;