From 5b458eff274dd7f94969a4cdedf58c078ceff530 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 24 Apr 2019 12:42:54 +0400 Subject: [PATCH] Added getDistance() method to Number160. Added .gradle to .gitignore --- .gitignore | 3 +- .../influencedht/core/Number160.java | 52 ++----------------- .../core/routing/KademliaRoutingTable.kt | 14 ++--- 3 files changed, 13 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index d4f654f..751c924 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea /build -/out \ No newline at end of file +/out +/.gradle \ No newline at end of file diff --git a/src/main/java/io/gitub/chronosx88/influencedht/core/Number160.java b/src/main/java/io/gitub/chronosx88/influencedht/core/Number160.java index 0f1c4ee..dce342d 100644 --- a/src/main/java/io/gitub/chronosx88/influencedht/core/Number160.java +++ b/src/main/java/io/gitub/chronosx88/influencedht/core/Number160.java @@ -475,60 +475,16 @@ public final class Number160 extends Number implements Comparable { return Utils.makeSHAHash(string); } - /** - * Counts the number of leading 0's in this NodeId - * - * @return Integer The number of leading 0's - */ - public int getFirstSetBitIndex() - { - int prefixLength = 0; - - for (byte b : this.toByteArray()) - { - if (b == 0) - { - prefixLength += 8; - } - else - { - /* If the byte is not 0, we need to count how many MSBs are 0 */ - int count = 0; - for (int i = 7; i >= 0; i--) - { - boolean a = (b & (1 << i)) == 0; - if (a) - { - count++; - } - else - { - break; // Reset the count if we encounter a non-zero number - } - } - - /* Add the count of MSB 0s to the prefix length */ - prefixLength += count; - - /* Break here since we've now covered the MSB 0s */ - break; - } - } - return prefixLength; - } - /** * Gets the distance from this ID to another ID * - * @param to + * @param to Another ID * - * @return Integer The distance + * @return The distance */ public int getDistance(Number160 to) { - // Compute the xor of this and to - // Get the index i of the first set bit of the xor returned NodeId - // The distance between them is ID_LENGTH - i - return BITS - this.xor(to).getFirstSetBitIndex(); + // Compute the xor of this and get bit length + return this.xor(to).bitLength(); } } diff --git a/src/main/kotlin/io/github/chronosx88/influencedht/core/routing/KademliaRoutingTable.kt b/src/main/kotlin/io/github/chronosx88/influencedht/core/routing/KademliaRoutingTable.kt index c91c3ff..ee31abf 100644 --- a/src/main/kotlin/io/github/chronosx88/influencedht/core/routing/KademliaRoutingTable.kt +++ b/src/main/kotlin/io/github/chronosx88/influencedht/core/routing/KademliaRoutingTable.kt @@ -33,7 +33,7 @@ class KademliaRoutingTable( @Synchronized get() { val nodes = ArrayList() - for (b in this.buckets!!) { + for (b in this.buckets) { for (c in b.getContacts()) { nodes.add(c.getPeerAddress()) } @@ -49,7 +49,7 @@ class KademliaRoutingTable( get() { val contacts = ArrayList() - for (b in this.buckets!!) { + for (b in this.buckets) { contacts.addAll(b.getContacts()) } @@ -86,7 +86,7 @@ class KademliaRoutingTable( */ @Synchronized fun insert(c: PeerContact) { - this.buckets!![this.getBucketId(c.getPeerAddress().nodeId)].insert(c) + this.buckets[this.getBucketId(c.getPeerAddress().nodeId)].insert(c) } /** @@ -96,7 +96,7 @@ class KademliaRoutingTable( */ @Synchronized fun insert(n: PeerAddress) { - this.buckets!![this.getBucketId(n.nodeId)].insert(n) + this.buckets[this.getBucketId(n.nodeId)].insert(n) } /** @@ -107,7 +107,7 @@ class KademliaRoutingTable( * @return Integer The bucket ID in which the given node should be placed. */ fun getBucketId(nid: Number160?): Int { - val bId = this.localNode.nodeId!!.xor(nid).bitLength() - 1 + val bId = this.localNode.nodeId!!.getDistance(nid) - 1 /* If we are trying to insert a node into it's own routing table, then the bucket ID will be -1, so let's just keep it in bucket 0 */ return if (bId < 0) 0 else bId @@ -163,14 +163,14 @@ class KademliaRoutingTable( val bucketId = this.getBucketId(n.nodeId) /* Remove the contact from the bucket */ - this.buckets!![bucketId].removeNode(n) + this.buckets[bucketId].removeNode(n) } @Synchronized override fun toString(): String { val sb = StringBuilder("\n ***************** \n") var totalContacts = 0 - for (b in this.buckets!!) { + for (b in this.buckets) { if (b.numContacts() > 0) { totalContacts += b.numContacts() sb.append("# nodes in Bucket with depth ")