Skip to content

Commit

Permalink
kademlia protocol finding by distance or by node id . solves issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
srenevic14 committed Aug 25, 2022
1 parent db6abad commit e4e8df5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions simulator/config/example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protocol.3kademlia peersim.kademlia.KademliaProtocol
protocol.3kademlia.transport 2unreltr
protocol.3kademlia.BITS 256
protocol.3kademlia.NBUCKETS 17
protocol.3kademlia.FINDMODE 0


# ::::: INITIALIZERS :::::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class KademliaCommonConfig {
public static int MAXREPLACEMENT = 10; // number of items in the replacement list for each bucket

public static int FINDMODE = 0; // find mode: 0 find by node id / 1 find by distance to node

/**
* short information about current mspastry configuration
*
Expand Down
24 changes: 19 additions & 5 deletions simulator/src/main/java/peersim/kademlia/KademliaProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,13 @@ private void handleResponse(Message m, int myPid) {
if (neighbour != null) {
// create a new request to send to neighbour
Message request = new Message(Message.MSG_FIND);
if (KademliaCommonConfig.FINDMODE == 1) request = new Message(Message.MSG_FIND_DIST);
request.operationId = m.operationId;
request.src = this.getNode();
request.dest = m.dest;
request.body = fop.destNode;
if (KademliaCommonConfig.FINDMODE == 1)
request.body = Util.distance(fop.destNode, (BigInteger) fop.body);
// increment hop count
fop.nrHops++;

Expand Down Expand Up @@ -241,10 +244,14 @@ private void handleResponse(Message m, int myPid) {
private void handleFind(Message m, int myPid) {
// get the ALPHA closest node to destNode

logger.info("handleFind " + (BigInteger) m.body);

BigInteger[] neighbours = this.routingTable.getNeighbours((BigInteger) m.body, m.src.getId());
logger.info("handleFind received");

BigInteger[] neighbours = new BigInteger[KademliaCommonConfig.K];
if (m.getType() == Message.MSG_FIND) {
neighbours = this.routingTable.getNeighbours((BigInteger) m.body, m.src.getId());
} else if (m.getType() == Message.MSG_FIND_DIST) {
neighbours = this.routingTable.getNeighbours((int) m.body);
}
// for (BigInteger neigh : neighbours) logger.warning("Neighbours " + neigh);
// create a response message containing the neighbours (with the same id of the request)
Message response = new Message(Message.MSG_RESPONSE, neighbours);
Expand Down Expand Up @@ -284,7 +291,7 @@ private void handleInitFind(Message m, int myPid) {

// set message operation id
m.operationId = fop.operationId;
m.type = Message.MSG_FIND;

m.src = this.getNode();

// send ALPHA messages
Expand All @@ -293,6 +300,12 @@ private void handleInitFind(Message m, int myPid) {
if (nextNode != null) {
m.dest =
nodeIdtoNode(nextNode).getKademliaProtocol().getNode(); // new KademliaNode(nextNode);
// set message type depending on find mode
if (KademliaCommonConfig.FINDMODE == 0) m.type = Message.MSG_FIND;
else {
m.type = Message.MSG_FIND_DIST;
m.body = Util.distance(nextNode, (BigInteger) fop.body);
}

logger.info("sendMessage to " + nextNode);

Expand Down Expand Up @@ -325,7 +338,7 @@ public void sendMessage(Message m, BigInteger destId, int myPid) {
transport = (UnreliableTransport) (Network.prototype).getProtocol(tid);
transport.send(src, dest, m, kademliaid);

if (m.getType() == Message.MSG_FIND) { // is a request
if (m.getType() == Message.MSG_FIND || m.getType() == Message.MSG_FIND_DIST) { // is a request
Timeout t = new Timeout(destId, m.id, m.operationId);
long latency = transport.getLatency(src, dest);

Expand Down Expand Up @@ -362,6 +375,7 @@ public void processEvent(Node myNode, int myPid, Object event) {
break;

case Message.MSG_FIND:
case Message.MSG_FIND_DIST:
m = (Message) event;
handleFind(m, myPid);
break;
Expand Down
19 changes: 18 additions & 1 deletion simulator/src/main/java/peersim/kademlia/RoutingTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ public void removeNeighbour(BigInteger node) {
bucketAtDistance(Util.distance(nodeId, node)).removeNeighbour(node);
}

// return the neighbours with a specific common prefix len
public BigInteger[] getNeighbours(final int dist) {
BigInteger[] result = new BigInteger[0];
ArrayList<BigInteger> resultList = new ArrayList<BigInteger>();
resultList.addAll(bucketAtDistance(dist).neighbours.keySet());

if (resultList.size() < k && (dist + 1) <= 256) {
resultList.addAll(bucketAtDistance(dist + 1).neighbours.keySet());
while (resultList.size() > k) resultList.remove(resultList.size() - 1);
}
if (resultList.size() < k & (dist - 1) >= 0) {
resultList.addAll(bucketAtDistance(dist - 1).neighbours.keySet());
while (resultList.size() > k) resultList.remove(resultList.size() - 1);
}
return resultList.toArray(result);
}

// return the closest neighbour to a key from the correct k-bucket
public BigInteger[] getNeighbours(final BigInteger key, final BigInteger src) {
// resulting neighbours
Expand All @@ -85,7 +102,7 @@ public BigInteger[] getNeighbours(final BigInteger key, final BigInteger src) {
// else get k closest node from all k-buckets
prefix_len = 0;
while (prefix_len < KademliaCommonConfig.BITS) {
neighbour_candidates.addAll(k_buckets.get(prefix_len).neighbours.keySet());
neighbour_candidates.addAll(bucketAtDistance(prefix_len).neighbours.keySet());
// remove source id
neighbour_candidates.remove(src);
prefix_len++;
Expand Down

0 comments on commit e4e8df5

Please sign in to comment.