Fixed the NodeId Prefix Length method to give the correct prefixLength

This commit is contained in:
Joshua Kissoon 2014-02-19 11:38:27 +05:30
parent ed1ac876f3
commit 6ff6e8dc21
2 changed files with 33 additions and 13 deletions

View File

@ -35,7 +35,7 @@ public class NodeId implements Streamable
*/ */
public NodeId() public NodeId()
{ {
keyBytes = new byte[ID_LENGTH]; keyBytes = new byte[ID_LENGTH / 8];
new Random().nextBytes(keyBytes); new Random().nextBytes(keyBytes);
} }
@ -104,7 +104,7 @@ public class NodeId implements Streamable
*/ */
public NodeId xor(NodeId nid) public NodeId xor(NodeId nid)
{ {
byte[] result = new byte[ID_LENGTH]; byte[] result = new byte[ID_LENGTH / 8];
byte[] nidBytes = nid.getBytes(); byte[] nidBytes = nid.getBytes();
for (int i = 0; i < ID_LENGTH / 8; i++) for (int i = 0; i < ID_LENGTH / 8; i++)
{ {
@ -122,20 +122,39 @@ public class NodeId implements Streamable
public int prefixLength() public int prefixLength()
{ {
int prefixLength = 0; int prefixLength = 0;
System.out.println("Bytes: ");
for (byte b : this.keyBytes) for (byte b : this.keyBytes)
{ {
if (b == 0) if (b == 0)
{ {
prefixLength++; prefixLength += 8;
} }
else 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; break;
} }
} }
return ID_LENGTH - prefixLength;
return prefixLength;
} }
@Override @Override

View File

@ -18,20 +18,21 @@ public class NodeConnectionTest
{ {
/* Setting up 2 Kad networks */ /* Setting up 2 Kad networks */
Kademlia kad1 = new Kademlia("Joshua", new NodeId("12345678901234567890"), 7574); 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); 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 */ /* Connecting 2 to 1 */
kad1.connect(kad2.getNode()); kad1.connect(kad2.getNode());
System.out.println("Kad 1 After: "); System.out.println("Kad 1: ");
System.out.println(kad1.getNode().getRoutingTable()); System.out.println(kad1.getNode().getRoutingTable());
System.out.println("Kad 2 After: "); System.out.println("Kad 2: ");
System.out.println(kad2.getNode().getRoutingTable()); 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) catch (IOException e)
{ {