mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
NodeId.getFirstSetBitIndex was actually returning a distance value, but without a comparison, was confusing things.
Created a distance function to replace that. Still working on Refresh Operations
This commit is contained in:
parent
825c2ca13f
commit
f91dea9e5f
@ -1,8 +1,6 @@
|
|||||||
package kademlia.node;
|
package kademlia.node;
|
||||||
|
|
||||||
import java.io.DataInput;
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutput;
|
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -180,13 +178,13 @@ public class Node implements Streamable
|
|||||||
|
|
||||||
//System.out.println("\n **************** Compare Starting **************** ");
|
//System.out.println("\n **************** Compare Starting **************** ");
|
||||||
//System.out.println("Comparing to: " + this.nodeId);
|
//System.out.println("Comparing to: " + this.nodeId);
|
||||||
int index1 = nodeId.xor(n1.getNodeId()).getFirstSetBitIndex();
|
int distance1 = nodeId.getDistance(n1.getNodeId());
|
||||||
//System.out.println("Node " + n1.getNodeId() + " distance: " + index1);
|
//System.out.println("Node " + n1.getNodeId() + " distance: " + index1);
|
||||||
int index2 = nodeId.xor(n2.getNodeId()).getFirstSetBitIndex();
|
int distance2 = nodeId.getDistance(n2.getNodeId());
|
||||||
//System.out.println("Node " + n2.getNodeId() + " distance: " + index2);
|
//System.out.println("Node " + n2.getNodeId() + " distance: " + index2);
|
||||||
|
|
||||||
int retval;
|
int retval;
|
||||||
if (index1 < index2)
|
if (distance1 < distance2)
|
||||||
{
|
{
|
||||||
/* If the first node is closer to the given node, return 1 */
|
/* If the first node is closer to the given node, return 1 */
|
||||||
retval = 1;
|
retval = 1;
|
||||||
|
@ -83,7 +83,6 @@ public class NodeId implements Streamable
|
|||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o)
|
public boolean equals(Object o)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (o instanceof NodeId)
|
if (o instanceof NodeId)
|
||||||
{
|
{
|
||||||
NodeId nid = (NodeId) o;
|
NodeId nid = (NodeId) o;
|
||||||
@ -100,28 +99,6 @@ public class NodeId implements Streamable
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a given NodeId is less than this NodeId
|
|
||||||
*
|
|
||||||
* @param nid The NodeId to compare to this NodeId
|
|
||||||
*
|
|
||||||
* @return boolean Whether the given NodeId is less than this NodeId
|
|
||||||
*/
|
|
||||||
public boolean lessThan(NodeId nid)
|
|
||||||
{
|
|
||||||
byte[] nidBytes = nid.getBytes();
|
|
||||||
for (int i = 0; i < ID_LENGTH; i++)
|
|
||||||
{
|
|
||||||
if (this.keyBytes[i] != nidBytes[i])
|
|
||||||
{
|
|
||||||
return this.keyBytes[i] < nidBytes[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We got here means they're equal */
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the distance between this and another NodeId
|
* Checks the distance between this and another NodeId
|
||||||
*
|
*
|
||||||
@ -147,9 +124,22 @@ public class NodeId implements Streamable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the number of leading 0's in this NodeId
|
* Generates a NodeId that is some distance away from this NodeId
|
||||||
*
|
*
|
||||||
* @return int The number of leading 0's
|
* @param distance
|
||||||
|
*
|
||||||
|
* @return NodeId The newly generated NodeId
|
||||||
|
*/
|
||||||
|
public NodeId generateNodeIdByDistance(int distance)
|
||||||
|
{
|
||||||
|
byte[] result = new byte[ID_LENGTH / 8];
|
||||||
|
int emptyBytes = distance / 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts the number of leading 0's in this NodeId
|
||||||
|
*
|
||||||
|
* @return Integer The number of leading 0's
|
||||||
*/
|
*/
|
||||||
public int getFirstSetBitIndex()
|
public int getFirstSetBitIndex()
|
||||||
{
|
{
|
||||||
@ -185,7 +175,24 @@ public class NodeId implements Streamable
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ID_LENGTH - prefixLength;
|
return prefixLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the distance from this NodeId to another NodeId
|
||||||
|
*
|
||||||
|
* @param to
|
||||||
|
*
|
||||||
|
* @return Integer The distance
|
||||||
|
*/
|
||||||
|
public int getDistance(NodeId 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 ID_LENGTH - this.xor(to).getFirstSetBitIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -36,7 +36,7 @@ public class BucketRefreshOperation implements Operation
|
|||||||
/* Test whether each nodeId in this list is a different distance from our current NID */
|
/* Test whether each nodeId in this list is a different distance from our current NID */
|
||||||
for (NodeId nid : refreshIds)
|
for (NodeId nid : refreshIds)
|
||||||
{
|
{
|
||||||
System.out.println(nid.xor(localNode.getNodeId()).getFirstSetBitIndex());
|
System.out.println(localNode.getNodeId().getDistance(nid));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @todo Do a Node Lookup operation to refresh K-Buckets */
|
/* @todo Do a Node Lookup operation to refresh K-Buckets */
|
||||||
|
@ -40,10 +40,8 @@ public class RoutingTable
|
|||||||
*/
|
*/
|
||||||
public void insert(Node n)
|
public void insert(Node n)
|
||||||
{
|
{
|
||||||
/* Find the first set bit: how far this node is away from the contact node */
|
/* bucketId is the distance between these nodes */
|
||||||
NodeId id = this.localNode.getNodeId().xor(n.getNodeId());
|
int bucketId = this.localNode.getNodeId().getDistance(n.getNodeId()) - 1;
|
||||||
//System.out.println(" First Bit Set: " + id.getFirstSetBitIndex());
|
|
||||||
int bucketId = id.getFirstSetBitIndex();
|
|
||||||
|
|
||||||
System.out.println(this.localNode.getNodeId() + " Adding Node " + n.getNodeId() + " to bucket at depth: " + bucketId);
|
System.out.println(this.localNode.getNodeId() + " Adding Node " + n.getNodeId() + " to bucket at depth: " + bucketId);
|
||||||
|
|
||||||
@ -59,7 +57,7 @@ public class RoutingTable
|
|||||||
public void remove(Node n)
|
public void remove(Node n)
|
||||||
{
|
{
|
||||||
/* Find the first set bit: how far this node is away from the contact node */
|
/* Find the first set bit: how far this node is away from the contact node */
|
||||||
int bucketId = this.localNode.getNodeId().xor(n.getNodeId()).getFirstSetBitIndex();
|
int bucketId = this.localNode.getNodeId().getDistance(n.getNodeId());
|
||||||
|
|
||||||
/* If the bucket has the contact, remove it */
|
/* If the bucket has the contact, remove it */
|
||||||
if (this.buckets[bucketId].containNode(n))
|
if (this.buckets[bucketId].containNode(n))
|
||||||
@ -80,11 +78,11 @@ public class RoutingTable
|
|||||||
{
|
{
|
||||||
List<Node> closest = new ArrayList<>(num);
|
List<Node> closest = new ArrayList<>(num);
|
||||||
|
|
||||||
/* Get the bucket number to search for closest from */
|
/* Get the bucket index to search for closest from */
|
||||||
int bucketNumber = this.localNode.getNodeId().xor(target).getFirstSetBitIndex() - 1;
|
int bucketIndex = this.localNode.getNodeId().getDistance(target) - 1;
|
||||||
|
|
||||||
/* Add the contacts from this bucket to the return contacts */
|
/* Add the contacts from this bucket to the return contacts */
|
||||||
for (Node c : this.buckets[bucketNumber].getNodes())
|
for (Node c : this.buckets[bucketIndex].getNodes())
|
||||||
{
|
{
|
||||||
if (closest.size() < num)
|
if (closest.size() < num)
|
||||||
{
|
{
|
||||||
@ -102,12 +100,12 @@ public class RoutingTable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* If we still need more nodes, we add from buckets on either side of the closest bucket */
|
/* If we still need more nodes, we add from buckets on either side of the closest bucket */
|
||||||
for (int i = 1; ((bucketNumber - i) >= 0 || (bucketNumber + i) < NodeId.ID_LENGTH); i++)
|
for (int i = 1; ((bucketIndex - i) >= 0 || (bucketIndex + i) < NodeId.ID_LENGTH); i++)
|
||||||
{
|
{
|
||||||
/* Check the bucket on the left side */
|
/* Check the bucket on the left side */
|
||||||
if (bucketNumber - i > 0)
|
if (bucketIndex - i > 0)
|
||||||
{
|
{
|
||||||
for (Node c : this.buckets[bucketNumber - i].getNodes())
|
for (Node c : this.buckets[bucketIndex - i].getNodes())
|
||||||
{
|
{
|
||||||
if (closest.size() < num)
|
if (closest.size() < num)
|
||||||
{
|
{
|
||||||
@ -121,9 +119,9 @@ public class RoutingTable
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Check the bucket on the right side */
|
/* Check the bucket on the right side */
|
||||||
if (bucketNumber + i < NodeId.ID_LENGTH)
|
if (bucketIndex + i < NodeId.ID_LENGTH)
|
||||||
{
|
{
|
||||||
for (Node c : this.buckets[bucketNumber + i].getNodes())
|
for (Node c : this.buckets[bucketIndex + i].getNodes())
|
||||||
{
|
{
|
||||||
if (closest.size() < num)
|
if (closest.size() < num)
|
||||||
{
|
{
|
||||||
@ -179,28 +177,28 @@ public class RoutingTable
|
|||||||
{
|
{
|
||||||
/* Construct a NodeId that is i bits away from the current node Id */
|
/* Construct a NodeId that is i bits away from the current node Id */
|
||||||
System.out.println("\nGenerating a new NodeId ");
|
System.out.println("\nGenerating a new NodeId ");
|
||||||
BitSet temp = new BitSet(NodeId.ID_LENGTH);
|
BitSet bits = new BitSet(160);
|
||||||
|
|
||||||
/* Fill the first i parts with 1 */
|
/* Fill the first i parts with 1 */
|
||||||
for (int j = 0; j < i; j++)
|
for (int j = 0; j < i + 10; j++)
|
||||||
{
|
{
|
||||||
System.out.println("Got here 1 - j: " + j);
|
bits.set(j);
|
||||||
temp.set(j);
|
System.out.println("Got here 1 - j: " + j + "; bits: " + bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fill the last parts with 0 */
|
/* Fill the last parts with 0 */
|
||||||
for (int j = i; j < NodeId.ID_LENGTH; j++)
|
for (int j = i; j < NodeId.ID_LENGTH; j++)
|
||||||
{
|
{
|
||||||
System.out.println("Got here 2 - j: " + j);
|
bits.clear(j);
|
||||||
temp.set(j, false);
|
System.out.println("Got here 2 - j: " + j + "; bits: " + bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LocalNode NodeId xor the Bits we generated will give a new NodeId
|
* LocalNode NodeId xor the Bits we generated will give a new NodeId
|
||||||
* i distance away from our LocalNode NodeId, we add this to our refreshList
|
* i distance away from our LocalNode NodeId, we add this to our refreshList
|
||||||
*/
|
*/
|
||||||
System.out.println("Bits: " + temp);
|
System.out.println("Bits: " + bits.toByteArray());
|
||||||
NodeId nid = this.localNode.getNodeId().xor(new NodeId(temp.toByteArray()));
|
NodeId nid = this.localNode.getNodeId().xor(new NodeId(bits.toByteArray()));
|
||||||
System.out.println("NodeId: " + nid);
|
System.out.println("NodeId: " + nid);
|
||||||
refreshList.add(nid);
|
refreshList.add(nid);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user