mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-12-23 08:11:47 +00:00
Fixed the NodeId Prefix Length method to give the correct prefixLength
This commit is contained in:
parent
ed1ac876f3
commit
6ff6e8dc21
@ -35,7 +35,7 @@ public class NodeId implements Streamable
|
||||
*/
|
||||
public NodeId()
|
||||
{
|
||||
keyBytes = new byte[ID_LENGTH];
|
||||
keyBytes = new byte[ID_LENGTH / 8];
|
||||
new Random().nextBytes(keyBytes);
|
||||
}
|
||||
|
||||
@ -104,7 +104,7 @@ public class NodeId implements Streamable
|
||||
*/
|
||||
public NodeId xor(NodeId nid)
|
||||
{
|
||||
byte[] result = new byte[ID_LENGTH];
|
||||
byte[] result = new byte[ID_LENGTH / 8];
|
||||
byte[] nidBytes = nid.getBytes();
|
||||
for (int i = 0; i < ID_LENGTH / 8; i++)
|
||||
{
|
||||
@ -122,20 +122,39 @@ public class NodeId implements Streamable
|
||||
public int prefixLength()
|
||||
{
|
||||
int prefixLength = 0;
|
||||
System.out.println("Bytes: ");
|
||||
|
||||
for (byte b : this.keyBytes)
|
||||
{
|
||||
if (b == 0)
|
||||
{
|
||||
prefixLength++;
|
||||
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;
|
||||
return ID_LENGTH - prefixLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,20 +18,21 @@ public class NodeConnectionTest
|
||||
{
|
||||
/* Setting up 2 Kad networks */
|
||||
Kademlia kad1 = new Kademlia("Joshua", new NodeId("12345678901234567890"), 7574);
|
||||
System.out.println("Kad 1 Before: ");
|
||||
System.out.println(kad1.getNode().getRoutingTable());
|
||||
|
||||
Kademlia kad2 = new Kademlia("Crystal", new NodeId("12345678901234567891"), 7572);
|
||||
System.out.println("Kad 2 Before: ");
|
||||
System.out.println(kad2.getNode().getRoutingTable());
|
||||
|
||||
/* Connecting 2 to 1 */
|
||||
kad1.connect(kad2.getNode());
|
||||
|
||||
System.out.println("Kad 1 After: ");
|
||||
|
||||
System.out.println("Kad 1: ");
|
||||
System.out.println(kad1.getNode().getRoutingTable());
|
||||
System.out.println("Kad 2 After: ");
|
||||
System.out.println("Kad 2: ");
|
||||
System.out.println(kad2.getNode().getRoutingTable());
|
||||
|
||||
/* Creating a new node 3 and connecting it to 1, hoping it'll get onto 2 also */
|
||||
Kademlia kad3 = new Kademlia("Jessica", new NodeId("88888736882323647625"), 7783);
|
||||
kad3.connect(kad1.getNode());
|
||||
System.out.println("Kad 3: ");
|
||||
System.out.println(kad3.getNode().getRoutingTable());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user