Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Disable unused copy constructors (and please cppcheck).
[simgrid.git] / examples / s4u / dht-kademlia / s4u-dht-kademlia.cpp
1 /* Copyright (c) 2012, 2014-2017. 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 int node(int argc, char* argv[])
21 {
22   bool join_success = true;
23   double deadline;
24   xbt_assert(argc == 3 || argc == 4, "Wrong number of arguments");
25   /* Node initialization */
26   unsigned int node_id = strtoul(argv[1], nullptr, 0);
27   kademlia::Node* node = new kademlia::Node(node_id);
28
29   if (argc == 4) {
30     XBT_INFO("Hi, I'm going to join the network with id %u", node->getId());
31     unsigned int known_id = strtoul(argv[2], NULL, 0);
32     join_success          = node->join(known_id);
33     deadline              = std::stod(argv[3]);
34   } else {
35     deadline = std::stod(argv[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::getClock() + random_lookup_interval;
44
45     XBT_VERB("Main loop start");
46
47     simgrid::s4u::MailboxPtr mailbox = simgrid::s4u::Mailbox::byName(std::to_string(node->getId()));
48
49     while (simgrid::s4u::Engine::getClock() < deadline) {
50       if (node->receive_comm == nullptr)
51         node->receive_comm = mailbox->get_async(&node->received_msg);
52
53       if (node->receive_comm->test()) {
54         // There has been a message, we need to handle it !
55         kademlia::Message* msg = static_cast<kademlia::Message*>(node->received_msg);
56         if (msg) {
57           node->handleFindNode(msg);
58           delete msg->answer_;
59           delete msg;
60           node->receive_comm = nullptr;
61         } else
62           simgrid::s4u::this_actor::sleep_for(1);
63       } else {
64         /* We search for a pseudo random node */
65         if (simgrid::s4u::Engine::getClock() >= next_lookup_time) {
66           node->randomLookup();
67           next_lookup_time += random_lookup_interval;
68         } else {
69           // Didn't get a message: sleep for a while...
70           simgrid::s4u::this_actor::sleep_for(1);
71         }
72       }
73     }
74   } else {
75     XBT_INFO("I couldn't join the network :(");
76   }
77   XBT_DEBUG("I'm leaving the network");
78   XBT_INFO("%u/%u FIND_NODE have succeeded", node->find_node_success, node->find_node_success + node->find_node_failed);
79   delete node;
80
81   return 0;
82 }
83
84 /** @brief Main function */
85 int main(int argc, char* argv[])
86 {
87   simgrid::s4u::Engine e(&argc, argv);
88
89   /* Check the arguments */
90   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n\tExample: %s cluster.xml dht-kademlia_d.xml\n",
91              argv[0], argv[0]);
92
93   e.loadPlatform(argv[1]);
94   e.registerFunction("node", node);
95   e.loadDeployment(argv[2]);
96
97   e.run();
98
99   XBT_INFO("Simulated time: %g", simgrid::s4u::Engine::getClock());
100
101   return 0;
102 }