mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-24 19:22:18 +00:00
Working on The ContentLookupReceiver
This commit is contained in:
parent
58f9157c9d
commit
a12e075cec
@ -15,7 +15,7 @@ import kademlia.node.Node;
|
||||
public class ContentLookupMessage implements Message
|
||||
{
|
||||
|
||||
private static final byte CODE = 0x47;
|
||||
public static final byte CODE = 0x47;
|
||||
|
||||
private Node origin;
|
||||
private GetParameter params;
|
||||
|
57
src/kademlia/message/ContentLookupReceiver.java
Normal file
57
src/kademlia/message/ContentLookupReceiver.java
Normal file
@ -0,0 +1,57 @@
|
||||
package kademlia.message;
|
||||
|
||||
import java.io.IOException;
|
||||
import kademlia.core.KadServer;
|
||||
import kademlia.dht.DHT;
|
||||
import kademlia.node.Node;
|
||||
import kademlia.operation.Receiver;
|
||||
|
||||
/**
|
||||
* Responds to a ContentLookupMessage by sending a ContentMessage containing the requested content;
|
||||
* if the requested content is not found, a NodeReplyMessage containing the K closest nodes to the request key is sent.
|
||||
*
|
||||
* @author Joshua Kissoon
|
||||
* @since 20140226
|
||||
*/
|
||||
public class ContentLookupReceiver implements Receiver
|
||||
{
|
||||
|
||||
private final KadServer server;
|
||||
private final Node localNode;
|
||||
private final DHT dht;
|
||||
|
||||
public ContentLookupReceiver(KadServer server, Node localNode, DHT dht)
|
||||
{
|
||||
this.server = server;
|
||||
this.localNode = localNode;
|
||||
this.dht = dht;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receive(Message incoming, int comm) throws IOException
|
||||
{
|
||||
ContentLookupMessage msg = (ContentLookupMessage) incoming;
|
||||
|
||||
/* Check if we can have this data */
|
||||
if (this.dht.contains(msg.getParameters()))
|
||||
{
|
||||
/* Return a ContentMessage with the required data */
|
||||
ContentMessage cMsg = new ContentMessage(localNode, this.dht.get(msg.getParameters()));
|
||||
}
|
||||
else
|
||||
{
|
||||
/**
|
||||
* Return a the K closest nodes to this content identifier
|
||||
* We create a NodeLookupReceiver and let this receiver handle this operation
|
||||
*/
|
||||
NodeLookupMessage lkpMsg = new NodeLookupMessage(msg.getOrigin(), msg.getParameters().getKey());
|
||||
new NodeLookupReceiver(server, localNode).receive(lkpMsg, comm);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void timeout(int comm)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -61,6 +61,8 @@ public class MessageFactory
|
||||
return new NodeLookupReceiver(server, this.localNode);
|
||||
case StoreContentMessage.CODE:
|
||||
return new StoreContentReceiver(server, this.localNode, this.dht);
|
||||
case ContentLookupMessage.CODE:
|
||||
return new ContentLookupReceiver(server, localNode, dht);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user