Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / c / dht-kademlia / node.h
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 #ifndef _DHT_KADEMLIA_ROUTING_H
8 #define _DHT_KADEMLIA_ROUTING_H
9 #include "simgrid/actor.h"
10 #include "simgrid/comm.h"
11 #include "simgrid/engine.h"
12 #include "simgrid/host.h"
13 #include "simgrid/mailbox.h"
14 #include "xbt/dynar.h"
15 #include "xbt/log.h"
16 #include "xbt/sysdep.h"
17
18 #include "answer.h"
19 #include "common.h"
20 #include "message.h"
21 #include "routing_table.h"
22
23 /* Information about a foreign node */
24 typedef struct s_node_contact {
25   unsigned int id;       // The node identifier
26   unsigned int distance; // The distance from the node
27 } s_node_contact_t;
28
29 typedef s_node_contact_t* node_contact_t;
30 typedef const s_node_contact_t* const_node_contact_t;
31
32 /* Node data */
33 typedef struct s_node {
34   unsigned int id;        // node id - 160 bits
35   routing_table_t table;  // node routing table
36   sg_comm_t receive_comm; // current receiving communication.
37   void* received_msg;     // current task being received
38
39   sg_mailbox_t mailbox;           // node mailbox
40   unsigned int find_node_success; // Number of find_node which have succeeded.
41   unsigned int find_node_failed;  // Number of find_node which have failed.
42 } s_node_t;
43
44 typedef s_node_t* node_t;
45 typedef const s_node_t* const_node_t;
46
47 // node functions
48 node_t node_init(unsigned int id);
49 void node_free(node_t node);
50 void routing_table_update(const_node_t node, unsigned int id);
51 answer_t find_closest(const_node_t node, unsigned int destination_id);
52
53 // identifier functions
54 unsigned int get_id_in_prefix(unsigned int id, unsigned int prefix);
55 unsigned int get_node_prefix(unsigned int id, unsigned int nb_bits);
56 sg_mailbox_t get_node_mailbox(unsigned int id);
57
58 // node contact functions
59 node_contact_t node_contact_new(unsigned int id, unsigned int distance);
60 node_contact_t node_contact_copy(const_node_contact_t node_contact);
61 void node_contact_free(node_contact_t contact);
62
63 kademlia_message_t receive(node_t node, sg_mailbox_t mailbox);
64 unsigned int join(node_t node, unsigned int id_known);
65 unsigned int find_node(node_t node, unsigned int id_to_find, unsigned int count_in_stats);
66 void random_lookup(node_t node);
67
68 void send_find_node(const_node_t node, unsigned int id, unsigned int destination);
69 unsigned int send_find_node_to_best(const_node_t node, const_answer_t node_list);
70
71 void handle_find_node(const_node_t node, const_kademlia_message_t data);
72
73 #endif