Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / cpp / dht-kademlia / s4u-dht-kademlia.cpp
1 /* Copyright (c) 2012-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "s4u-dht-kademlia.hpp"
8
9 #include "message.hpp"
10 #include "node.hpp"
11 #include "simgrid/s4u.hpp"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(kademlia, "Messages specific for this example");
14
15 /** @brief Node function
16   * @param my node ID
17   * @param the ID of the person I know in the system (or not)
18   * @param Time before I leave the system because I'm bored
19   */
20 static void node(std::vector<std::string> args)
21 {
22   bool join_success = true;
23   double deadline;
24   xbt_assert(args.size() == 3 || args.size() == 4, "Wrong number of arguments");
25   /* Node initialization */
26   auto node_id = static_cast<unsigned int>(std::stoul(args[1], nullptr, 0));
27   kademlia::Node node(node_id);
28
29   if (args.size() == 4) {
30     XBT_INFO("Hi, I'm going to join the network with id %u", node.getId());
31     auto known_id = static_cast<unsigned int>(std::stoul(args[2], nullptr, 0));
32     join_success  = node.join(known_id);
33     deadline      = std::stod(args[3]);
34   } else {
35     deadline = std::stod(args[2]);
36     XBT_INFO("Hi, I'm going to create the network with id %u", node.getId());
37     node.routingTableUpdate(node.getId());
38   }
39
40   if (join_success) {
41     XBT_VERB("Ok, I'm joining the network with id %u", node.getId());
42     // We start the main loop
43     double next_lookup_time = simgrid::s4u::Engine::get_clock() + RANDOM_LOOKUP_INTERVAL;
44
45     XBT_VERB("Main loop start");
46
47     simgrid::s4u::Mailbox* mailbox = simgrid::s4u::Mailbox::by_name(std::to_string(node.getId()));
48
49     while (simgrid::s4u::Engine::get_clock() < deadline) {
50       if (kademlia::Message* msg = node.receive(mailbox)) {
51         // There has been a message, we need to handle it !
52         node.handleFindNode(msg);
53         delete msg;
54       } else {
55         /* We search for a pseudo random node */
56         if (simgrid::s4u::Engine::get_clock() >= next_lookup_time) {
57           node.randomLookup();
58           next_lookup_time += RANDOM_LOOKUP_INTERVAL;
59         } else {
60           // Didn't get a message: sleep for a while...
61           simgrid::s4u::this_actor::sleep_for(1);
62         }
63       }
64     }
65   } else {
66     XBT_INFO("I couldn't join the network :(");
67   }
68   XBT_DEBUG("I'm leaving the network");
69   node.displaySuccessRate();
70 }
71
72 /** @brief Main function */
73 int main(int argc, char* argv[])
74 {
75   simgrid::s4u::Engine e(&argc, argv);
76
77   /* Check the arguments */
78   xbt_assert(argc > 2,
79              "Usage: %s platform_file deployment_file\n\tExample: %s cluster_backbone.xml dht-kademlia_d.xml\n",
80              argv[0], argv[0]);
81
82   e.load_platform(argv[1]);
83   e.register_function("node", node);
84   e.load_deployment(argv[2]);
85
86   e.run();
87
88   XBT_INFO("Simulated time: %g", simgrid::s4u::Engine::get_clock());
89
90   return 0;
91 }