1 /* Copyright (c) 2012-2021. The SimGrid Team. All rights reserved. */
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. */
9 #include "simgrid/actor.h"
10 #include "simgrid/comm.h"
11 #include "simgrid/engine.h"
12 #include "simgrid/mailbox.h"
14 #include "xbt/asserts.h"
17 XBT_LOG_NEW_DEFAULT_CATEGORY(dht_kademlia, "Messages specific for this example");
19 /** @brief Node function
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
24 static void node(int argc, char* argv[])
26 unsigned int join_success = 1;
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);
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);
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);
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;
48 XBT_VERB("Main loop start");
50 sg_mailbox_t mailbox = get_node_mailbox(node->id);
52 while (simgrid_get_clock() < deadline) {
53 const kademlia_message_t msg = receive(node, mailbox);
55 // There has been a transfer, we need to handle it !
56 handle_find_node(node, msg);
57 answer_free(msg->answer);
60 /* We search for a pseudo random node */
61 if (simgrid_get_clock() >= next_lookup_time) {
63 next_lookup_time += RANDOM_LOOKUP_INTERVAL;
65 // Didn't get a task: sleep for a while...
66 sg_actor_sleep_for(1);
71 XBT_INFO("I couldn't join the network :(");
73 if (node->receive_comm)
74 sg_comm_unref(node->receive_comm);
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);
81 /** @brief Main function */
82 int main(int argc, char* argv[])
84 simgrid_init(&argc, argv);
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],
90 simgrid_load_platform(argv[1]);
91 simgrid_register_function("node", node);
92 simgrid_load_deployment(argv[2]);
96 XBT_INFO("Simulated time: %g", simgrid_get_clock());