mirror of
https://github.com/ChronosX88/InfluenceDHT-Kotlin.git
synced 2024-12-04 07:42:19 +00:00
Added getDistance() method to Number160. Added .gradle to .gitignore
This commit is contained in:
parent
a598cbb9cb
commit
5b458eff27
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
/.idea
|
||||
/build
|
||||
/out
|
||||
/out
|
||||
/.gradle
|
@ -475,60 +475,16 @@ public final class Number160 extends Number implements Comparable<Number160> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ class KademliaRoutingTable(
|
||||
@Synchronized get() {
|
||||
val nodes = ArrayList<PeerAddress>()
|
||||
|
||||
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<PeerContact>()
|
||||
|
||||
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 ")
|
||||
|
Loading…
Reference in New Issue
Block a user