1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 import java.util.Collections;
9 import java.util.Vector;
11 import org.simgrid.msg.Msg;
13 * @brief Contains the various data of a routing table.
15 public class RoutingTable {
19 private Vector<Bucket> buckets;
21 * Id of the routing table owner
27 public RoutingTable(int id) {
29 buckets = new Vector<Bucket>();
30 for (int i = 0; i < Common.IDENTIFIER_SIZE + 1; i++) {
31 buckets.add(new Bucket(i));
35 * Returns an identifier which is in a specific bucket of a routing table
36 * @brief id id of the routing table owner
37 * @brief prefix id of the bucket where we want that identifier to be
39 public int getIdInPrefix(int id, int prefix) {
44 identifier = identifier << (prefix - 1);
45 identifier = identifier ^ id;
49 * Returns the corresponding node prefix for a given id
51 public int getNodePrefix(int id) {
52 for (int j = 0; j < 32; j++) {
53 if ((id >> (32 - 1 - j) & 0x1) != 0) {
60 * Fins the corresponding bucket in a routing table for a given identifier
62 public Bucket findBucket(int id) {
63 int xorNumber = id ^ this.id;
64 // Msg.info("Number:" + xorNumber.toString(16));
65 int prefix = this.getNodePrefix(xorNumber);
67 return buckets.get(prefix);
70 * Updates the routing table with a new value.
72 public void update(int id) {
74 Bucket bucket = this.findBucket(id);
75 if (bucket.contains(id)) {
76 Msg.debug("Updating " + Integer.toString(id) + " in my routing table");
77 //If the element is already in the bucket, we update it.
78 bucket.pushToFront(id);
81 Msg.debug("Adding " + id + " to my routing table");
83 if (bucket.size() > Common.BUCKET_SIZE) {
84 //TODO: Ping the least seen guy and remove him if he is offline.
89 * Returns the closest notes we know to a given id
91 public Answer findClosest(int destinationId) {
92 Answer answer = new Answer(destinationId);
95 Bucket bucket = this.findBucket(destinationId);
96 bucket.addToAnswer(answer,destinationId);
98 for (int i = 1; answer.size() < Common.BUCKET_SIZE &&
99 ((bucket.getId() - i) >= 0 ||
100 (bucket.getId() + i) <= Common.IDENTIFIER_SIZE); i++) {
101 //Check the previous buckets
102 if (bucket.getId() - i >= 0) {
103 Bucket bucketP = this.buckets.get(bucket.getId() - i);
104 bucketP.addToAnswer(answer,destinationId);
106 //Check the next buckets
107 if (bucket.getId() + i <= Common.IDENTIFIER_SIZE) {
108 Bucket bucketN = this.buckets.get(bucket.getId() + i);
109 bucketN.addToAnswer(answer, destinationId);
113 Collections.sort(answer.getNodes());
115 while (answer.size() > Common.BUCKET_SIZE) {
116 answer.remove(answer.size() - 1); //TODO: Not the best thing.
123 public String toString() {
124 String string = "RoutingTable [ id=" + id + " " ;
125 for (int i = 0; i < buckets.size(); i++) {
126 if (buckets.get(i).size() > 0) {
127 string += buckets.get(i) + " ";