Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / dht-kademlia / routing_table.c
1 /* Copyright (c) 2012-2016. 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   unsigned int i;
17   routing_table_t table = xbt_new(s_routing_table_t, 1);
18   table->buckets = xbt_new(s_bucket_t, identifier_size + 1);
19   for (i = 0; i < identifier_size + 1; i++) {
20     table->buckets[i].nodes = xbt_dynar_new(sizeof(unsigned int), NULL);
21     table->buckets[i].id = i;
22   }
23   table->id = node_id;
24   return table;
25 }
26
27 /** @brief Frees the routing table */
28 void routing_table_free(routing_table_t table)
29 {
30   unsigned int i;
31   //Free the buckets.
32   for (i = 0; i <= identifier_size; i++) {
33     xbt_dynar_free(&table->buckets[i].nodes);
34   }
35   xbt_free(table->buckets);
36   xbt_free(table);
37 }
38
39 /** Returns if the routing table contains the id. */
40 unsigned int routing_table_contains(routing_table_t table, unsigned int node_id)
41 {
42   bucket_t bucket = routing_table_find_bucket(table, node_id);
43   return bucket_contains(bucket, node_id);
44 }
45
46 /**@brief prints the routing table, to debug stuff. */
47 void routing_table_print(routing_table_t table)
48 {
49   unsigned int i, j, value;
50   XBT_INFO("Routing table of %08x:", table->id);
51
52   for (i = 0; i <= identifier_size; i++) {
53     if (!xbt_dynar_is_empty(table->buckets[i].nodes)) {
54       XBT_INFO("Bucket number %d: ", i);
55       xbt_dynar_foreach(table->buckets[i].nodes, j, value) {
56         XBT_INFO("Element %d: %08x", j, value);
57       }
58     }
59   }
60 }
61
62 /** @brief Finds an identifier in a bucket and returns its position or returns -1 if it doesn't exists
63   * @param bucket the bucket in which we try to find our identifier
64   * @param id the id
65   */
66 unsigned int bucket_find_id(bucket_t bucket, unsigned int id)
67 {
68   unsigned int i, current_id;
69   xbt_dynar_foreach(bucket->nodes, i, current_id){
70     if (id == current_id){
71       return i;
72     }
73   }
74   return -1;
75 }
76
77 /** Returns if the bucket contains an identifier.  */
78 unsigned int bucket_contains(bucket_t bucket, unsigned int id)
79 {
80   return xbt_dynar_member(bucket->nodes, &id);
81 }
82
83 /** @brief Finds the corresponding bucket in a routing table for a given identifier
84   * @param table the routing table
85   * @param id the identifier
86   * @return the bucket in which the the identifier would be.
87   */
88 bucket_t routing_table_find_bucket(routing_table_t table, unsigned int id)
89 {
90   unsigned int xor_number = table->id ^ id;
91   unsigned int prefix = get_node_prefix(xor_number, identifier_size);
92   xbt_assert(prefix >= 0 && prefix <= identifier_size, "Tried to return a  bucket that doesn't exist.");
93   bucket_t bucket = &table->buckets[prefix];
94   return bucket;
95 }