1 /* Copyright (c) 2012-2020. 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 if (node->receive_comm == NULL)
54 node->receive_comm = sg_mailbox_get_async(mailbox, &node->received_msg);
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);
60 handle_find_node(node, msg);
61 answer_free(msg->answer);
63 node->receive_comm = NULL;
65 sg_actor_sleep_for(1);
68 /* We search for a pseudo random node */
69 if (simgrid_get_clock() >= next_lookup_time) {
71 next_lookup_time += RANDOM_LOOKUP_INTERVAL;
73 // Didn't get a task: sleep for a while...
74 sg_actor_sleep_for(1);
79 XBT_INFO("I couldn't join the network :(");
81 if (node->receive_comm)
82 sg_comm_unref(node->receive_comm);
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);
89 /** @brief Main function */
90 int main(int argc, char* argv[])
92 simgrid_init(&argc, argv);
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",
98 simgrid_load_platform(argv[1]);
99 simgrid_register_function("node", node);
100 simgrid_load_deployment(argv[2]);
104 XBT_INFO("Simulated time: %g", simgrid_get_clock());