Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add sg_mailbox_put_init
[simgrid.git] / examples / deprecated / msg / dht-kademlia / routing_table.c
1 /* Copyright (c) 2012-2020. 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 "routing_table.h"
8 #include "node.h"
9 #include "simgrid/msg.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_kademlia_routing_table, "Messages specific for this msg example");
12
13 /** @brief Initialization of a node routing table.  */
14 routing_table_t routing_table_init(unsigned int node_id)
15 {
16   routing_table_t table = xbt_new(s_routing_table_t, 1);
17   table->buckets        = xbt_new(s_bucket_t, IDENTIFIER_SIZE + 1);
18   for (unsigned int i = 0; i < IDENTIFIER_SIZE + 1; i++) {
19     table->buckets[i].nodes = xbt_dynar_new(sizeof(unsigned int), NULL);
20     table->buckets[i].id = i;
21   }
22   table->id = node_id;
23   return table;
24 }
25
26 /** @brief Frees the routing table */
27 void routing_table_free(routing_table_t table)
28 {
29   unsigned int i;
30   //Free the buckets.
31   for (i = 0; i <= IDENTIFIER_SIZE; i++) {
32     xbt_dynar_free(&table->buckets[i].nodes);
33   }
34   xbt_free(table->buckets);
35   xbt_free(table);
36 }
37
38 /**@brief prints the routing table, to debug stuff. */
39 void routing_table_print(const_routing_table_t table)
40 {
41   unsigned int j;
42   unsigned int value;
43   XBT_INFO("Routing table of %08x:", table->id);
44
45   for (unsigned int i = 0; i <= IDENTIFIER_SIZE; i++) {
46     if (!xbt_dynar_is_empty(table->buckets[i].nodes)) {
47       XBT_INFO("Bucket number %u: ", i);
48       xbt_dynar_foreach(table->buckets[i].nodes, j, value) {
49         XBT_INFO("Element %u: %08x", j, value);
50       }
51     }
52   }
53 }
54
55 /** @brief Finds an identifier in a bucket and returns its position or returns -1 if it doesn't exists
56   * @param bucket the bucket in which we try to find our identifier
57   * @param id the id
58   */
59 unsigned int bucket_find_id(const_bucket_t bucket, unsigned int id)
60 {
61   unsigned int i;
62   unsigned int current_id;
63   xbt_dynar_foreach(bucket->nodes, i, current_id){
64     if (id == current_id){
65       return i;
66     }
67   }
68   return -1;
69 }
70
71 /** @brief Finds the corresponding bucket in a routing table for a given identifier
72   * @param table the routing table
73   * @param id the identifier
74   * @return the bucket in which the the identifier would be.
75   */
76 bucket_t routing_table_find_bucket(const_routing_table_t table, unsigned int id)
77 {
78   unsigned int xor_number = table->id ^ id;
79   unsigned int prefix     = get_node_prefix(xor_number, IDENTIFIER_SIZE);
80   xbt_assert(prefix <= IDENTIFIER_SIZE, "Tried to return a  bucket that doesn't exist.");
81   bucket_t bucket = &table->buckets[prefix];
82   return bucket;
83 }