Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
65736a283de564b8bf87ff649e892609b4e796c5
[simgrid.git] / examples / c / dht-kademlia / dht-kademlia.c
1 /* Copyright (c) 2012-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "message.h"
7 #include "node.h"
8
9 #include "simgrid/actor.h"
10 #include "simgrid/comm.h"
11 #include "simgrid/engine.h"
12 #include "simgrid/mailbox.h"
13
14 #include "xbt/asserts.h"
15 #include "xbt/log.h"
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(dht_kademlia, "Messages specific for this example");
18
19 /** @brief Node function
20  * @param my node ID
21  * @param the ID of the person I know in the system (or not)
22  * @param Time before I leave the system because I'm bored
23  */
24 static void node(int argc, char* argv[])
25 {
26   unsigned int join_success = 1;
27   double deadline;
28   xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
29   /* Node initialization */
30   unsigned int id = (unsigned int)strtoul(argv[1], NULL, 0);
31   node_t node     = node_init(id);
32
33   if (argc == 4) {
34     XBT_INFO("Hi, I'm going to join the network with id %s", sg_mailbox_get_name(node->mailbox));
35     unsigned int id_known = (unsigned int)strtoul(argv[2], NULL, 0);
36     join_success          = join(node, id_known);
37     deadline              = strtod(argv[3], NULL);
38   } else {
39     deadline = strtod(argv[2], NULL);
40     XBT_INFO("Hi, I'm going to create the network with id %s", sg_mailbox_get_name(node->mailbox));
41     routing_table_update(node, node->id);
42   }
43   if (join_success) {
44     XBT_VERB("Ok, I'm joining the network with id %s", sg_mailbox_get_name(node->mailbox));
45     // We start the main loop
46     double next_lookup_time = simgrid_get_clock() + RANDOM_LOOKUP_INTERVAL;
47
48     XBT_VERB("Main loop start");
49
50     sg_mailbox_t mailbox = get_node_mailbox(node->id);
51
52     while (simgrid_get_clock() < deadline) {
53       const kademlia_message_t msg = receive(node, mailbox);
54       if (msg) {
55         // There has been a transfer, we need to handle it !
56         handle_find_node(node, msg);
57         answer_free(msg->answer);
58         free(msg);
59       } else {
60         /* We search for a pseudo random node */
61         if (simgrid_get_clock() >= next_lookup_time) {
62           random_lookup(node);
63           next_lookup_time += RANDOM_LOOKUP_INTERVAL;
64         } else {
65           // Didn't get a task: sleep for a while...
66           sg_actor_sleep_for(1);
67         }
68       }
69     }
70   } else {
71     XBT_INFO("I couldn't join the network :(");
72   }
73   if (node->receive_comm)
74     sg_comm_unref(node->receive_comm);
75
76   XBT_DEBUG("I'm leaving the network");
77   XBT_INFO("%u/%u FIND_NODE have succeeded", node->find_node_success, node->find_node_success + node->find_node_failed);
78   node_free(node);
79 }
80
81 /** @brief Main function */
82 int main(int argc, char* argv[])
83 {
84   simgrid_init(&argc, argv);
85
86   /* Check the arguments */
87   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n\tExample: %s platform.xml deployment.xml\n", argv[0],
88              argv[0]);
89
90   simgrid_load_platform(argv[1]);
91   simgrid_register_function("node", node);
92   simgrid_load_deployment(argv[2]);
93
94   simgrid_run();
95
96   XBT_INFO("Simulated time: %g", simgrid_get_clock());
97
98   return 0;
99 }