2014-02-24 15:56:49 +00:00
|
|
|
package kademlia.operation;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-02-26 06:27:59 +00:00
|
|
|
import java.util.List;
|
2014-02-24 15:56:49 +00:00
|
|
|
import kademlia.core.KadServer;
|
2014-02-25 08:12:08 +00:00
|
|
|
import kademlia.dht.KadContent;
|
2014-02-25 17:27:46 +00:00
|
|
|
import kademlia.message.Message;
|
|
|
|
import kademlia.message.StoreContentMessage;
|
2014-02-24 15:56:49 +00:00
|
|
|
import kademlia.node.Node;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operation that stores a DHT Content onto the K closest nodes to the content Key
|
|
|
|
*
|
|
|
|
* @author Joshua Kissoon
|
|
|
|
* @since 20140224
|
|
|
|
*/
|
|
|
|
public class StoreOperation implements Operation
|
|
|
|
{
|
|
|
|
|
|
|
|
private final KadServer server;
|
|
|
|
private final Node localNode;
|
2014-02-25 08:12:08 +00:00
|
|
|
private final KadContent content;
|
2014-02-24 15:56:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param server
|
|
|
|
* @param localNode
|
|
|
|
* @param content The content to be stored on the DHT
|
|
|
|
*/
|
2014-02-25 08:12:08 +00:00
|
|
|
public StoreOperation(KadServer server, Node localNode, KadContent content)
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
|
|
|
this.server = server;
|
|
|
|
this.localNode = localNode;
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-26 13:28:55 +00:00
|
|
|
public synchronized void execute() throws IOException
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
|
|
|
/* Get the nodes on which we need to store the content */
|
2014-02-26 13:28:55 +00:00
|
|
|
NodeLookupOperation ndlo = new NodeLookupOperation(this.server, this.localNode, this.content.getKey());
|
|
|
|
ndlo.execute();
|
|
|
|
List<Node> nodes = ndlo.getClosestNodes();
|
|
|
|
|
2014-02-25 07:31:06 +00:00
|
|
|
System.out.println("Nodes to put content on: " + nodes);
|
|
|
|
|
2014-02-25 17:27:46 +00:00
|
|
|
/* Create the message */
|
|
|
|
Message msg = new StoreContentMessage(this.localNode, this.content);
|
|
|
|
|
|
|
|
/*Store the message on all of the K-Nodes*/
|
|
|
|
for (Node n : nodes)
|
|
|
|
{
|
|
|
|
if (n.equals(this.localNode))
|
|
|
|
{
|
2014-03-07 05:44:45 +00:00
|
|
|
/* @todo Store the content locally */
|
2014-02-25 17:27:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-07 05:44:45 +00:00
|
|
|
/**
|
|
|
|
* @todo Create a receiver that recieves a store acknowledgement message to count how many nodes a content have been stored at
|
|
|
|
*/
|
2014-02-25 17:27:46 +00:00
|
|
|
this.server.sendMessage(n, msg, null);
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|
2014-03-07 05:44:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The number of nodes that have stored this content
|
|
|
|
*
|
|
|
|
* @todo Implement this method
|
|
|
|
*/
|
|
|
|
public int numNodesStoredAt()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|