Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[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       if (node->receive_comm == NULL)
54         node->receive_comm = sg_mailbox_get_async(mailbox, &node->received_msg);
55
56       if (sg_comm_test(node->receive_comm)) {
57         // There has been a transfer, we need to handle it !
58         const kademlia_message_t msg = (kademlia_message_t)(node->received_msg);
59         if (msg) {
60           handle_find_node(node, msg);
61           answer_free(msg->answer);
62           free(msg);
63           node->receive_comm = NULL;
64         } else {
65           sg_actor_sleep_for(1);
66         }
67       } else {
68         /* We search for a pseudo random node */
69         if (simgrid_get_clock() >= next_lookup_time) {
70           random_lookup(node);
71           next_lookup_time += RANDOM_LOOKUP_INTERVAL;
72         } else {
73           // Didn't get a task: sleep for a while...
74           sg_actor_sleep_for(1);
75         }
76       }
77     }
78   } else {
79     XBT_INFO("I couldn't join the network :(");
80   }
81   if (node->receive_comm)
82     sg_comm_unref(node->receive_comm);
83
84   XBT_DEBUG("I'm leaving the network");
85   XBT_INFO("%u/%u FIND_NODE have succeeded", node->find_node_success, node->find_node_success + node->find_node_failed);
86   node_free(node);
87 }
88
89 /** @brief Main function */
90 int main(int argc, char* argv[])
91 {
92   simgrid_init(&argc, argv);
93
94   /* Check the arguments */
95   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n\tExample: %s msg_platform.xml msg_deployment.xml\n",
96              argv[0], argv[0]);
97
98   simgrid_load_platform(argv[1]);
99   simgrid_register_function("node", node);
100   simgrid_load_deployment(argv[2]);
101
102   simgrid_run();
103
104   XBT_INFO("Simulated time: %g", simgrid_get_clock());
105
106   return 0;
107 }