Added getDistance() method to Number160. Added .gradle to .gitignore

This commit is contained in:
ChronosX88 2019-04-24 12:42:54 +04:00
parent a598cbb9cb
commit 5b458eff27
3 changed files with 13 additions and 56 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
/.idea /.idea
/build /build
/out /out
/.gradle

View File

@ -475,60 +475,16 @@ public final class Number160 extends Number implements Comparable<Number160> {
return Utils.makeSHAHash(string); 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 * 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) public int getDistance(Number160 to)
{ {
// Compute the xor of this and to // Compute the xor of this and get bit length
// Get the index i of the first set bit of the xor returned NodeId return this.xor(to).bitLength();
// The distance between them is ID_LENGTH - i
return BITS - this.xor(to).getFirstSetBitIndex();
} }
} }

View File

@ -33,7 +33,7 @@ class KademliaRoutingTable(
@Synchronized get() { @Synchronized get() {
val nodes = ArrayList<PeerAddress>() val nodes = ArrayList<PeerAddress>()
for (b in this.buckets!!) { for (b in this.buckets) {
for (c in b.getContacts()) { for (c in b.getContacts()) {
nodes.add(c.getPeerAddress()) nodes.add(c.getPeerAddress())
} }
@ -49,7 +49,7 @@ class KademliaRoutingTable(
get() { get() {
val contacts = ArrayList<PeerContact>() val contacts = ArrayList<PeerContact>()
for (b in this.buckets!!) { for (b in this.buckets) {
contacts.addAll(b.getContacts()) contacts.addAll(b.getContacts())
} }
@ -86,7 +86,7 @@ class KademliaRoutingTable(
*/ */
@Synchronized @Synchronized
fun insert(c: PeerContact) { 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 @Synchronized
fun insert(n: PeerAddress) { 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. * @return Integer The bucket ID in which the given node should be placed.
*/ */
fun getBucketId(nid: Number160?): Int { 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 */ /* 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 return if (bId < 0) 0 else bId
@ -163,14 +163,14 @@ class KademliaRoutingTable(
val bucketId = this.getBucketId(n.nodeId) val bucketId = this.getBucketId(n.nodeId)
/* Remove the contact from the bucket */ /* Remove the contact from the bucket */
this.buckets!![bucketId].removeNode(n) this.buckets[bucketId].removeNode(n)
} }
@Synchronized @Synchronized
override fun toString(): String { override fun toString(): String {
val sb = StringBuilder("\n ***************** \n") val sb = StringBuilder("\n ***************** \n")
var totalContacts = 0 var totalContacts = 0
for (b in this.buckets!!) { for (b in this.buckets) {
if (b.numContacts() > 0) { if (b.numContacts() > 0) {
totalContacts += b.numContacts() totalContacts += b.numContacts()
sb.append("# nodes in Bucket with depth ") sb.append("# nodes in Bucket with depth ")