Logo AND Algorithmique Numérique Distribuée

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