- /**
- * Bucket list
- */
- private Vector<Bucket> buckets;
- /**
- * Id of the routing table owner
- */
- private int id;
- /**
- * Constructor
- */
- public RoutingTable(int id) {
- this.id = id;
- buckets = new Vector<Bucket>();
- for (int i = 0; i < Common.IDENTIFIER_SIZE + 1; i++) {
- buckets.add(new Bucket(i));
- }
- }
- /**
- * Returns an identifier which is in a specific bucket of a routing table
- * @brief id id of the routing table owner
- * @brief prefix id of the bucket where we want that identifier to be
- */
- public int getIdInPrefix(int id, int prefix) {
- if (prefix == 0) {
- return 0;
- }
- int identifier = 1;
- identifier = identifier << (prefix - 1);
- identifier = identifier ^ id;
- return identifier;
- }
- /**
- * Returns the corresponding node prefix for a given id
- */
- public int getNodePrefix(int id) {
- for (int j = 0; j < 32; j++) {
- if ((id >> (32 - 1 - j) & 0x1) != 0) {
- return 32 - j;
- }
- }
- return 0;
- }
- /**
- * Fins the corresponding bucket in a routing table for a given identifier
- */
- public Bucket findBucket(int id) {
- int xorNumber = id ^ this.id;
-// Msg.info("Number:" + xorNumber.toString(16));
- int prefix = this.getNodePrefix(xorNumber);
-
- return buckets.get(prefix);
- }
- /**
- * Updates the routing table with a new value.
- */
- public void update(int id) {
+ /* Bucket list */
+ private Vector<Bucket> buckets;
+ /* Id of the routing table owner */
+ private int id;
+
+ public RoutingTable(int id) {
+ this.id = id;
+ buckets = new Vector<Bucket>();
+ for (int i = 0; i < Common.IDENTIFIER_SIZE + 1; i++) {
+ buckets.add(new Bucket(i));
+ }
+ }
+
+ /**
+ * @brief Returns an identifier which is in a specific bucket of a routing table
+ * @param id id of the routing table owner
+ * @param prefix id of the bucket where we want that identifier to be
+ */
+ public int getIdInPrefix(int id, int prefix) {
+ if (prefix == 0) {
+ return 0;
+ }
+ int identifier = 1;
+ identifier = identifier << (prefix - 1);
+ identifier = identifier ^ id;
+ return identifier;
+ }
+
+ /* Returns the corresponding node prefix for a given id */
+ public int getNodePrefix(int id) {
+ for (int j = 0; j < 32; j++) {
+ if ((id >> (32 - 1 - j) & 0x1) != 0) {
+ return 32 - j;
+ }
+ }
+ return 0;
+ }
+
+ /* Finds the corresponding bucket in a routing table for a given identifier */
+ public Bucket findBucket(int id) {
+ int xorNumber = id ^ this.id;
+ int prefix = this.getNodePrefix(xorNumber);
+ return buckets.get(prefix);
+ }
+
+ /* Updates the routing table with a new value. */
+ public void update(int id) {
+ Bucket bucket = this.findBucket(id);
+ if (bucket.contains(id)) {
+ Msg.debug("Updating " + Integer.toString(id) + " in my routing table");
+ //If the element is already in the bucket, we update it.
+ bucket.pushToFront(id);
+ } else {
+ Msg.debug("Adding " + id + " to my routing table");
+ bucket.add(id);
+ if (bucket.size() > Common.BUCKET_SIZE) {
+ //TODO: Ping the least seen guy and remove him if he is offline.
+ }
+ }
+ }