mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
Created a new storage entry interface,
Let the storage entry class and the rest of this system use this interface
This commit is contained in:
parent
9fb139571a
commit
a1faffb4d9
@ -15,6 +15,7 @@ import kademlia.dht.GetParameter;
|
||||
import kademlia.dht.DHT;
|
||||
import kademlia.dht.KadContent;
|
||||
import kademlia.dht.KademliaDHT;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.exceptions.ContentNotFoundException;
|
||||
import kademlia.exceptions.RoutingException;
|
||||
@ -265,7 +266,7 @@ public class JKademliaNode implements KademliaNode
|
||||
}
|
||||
|
||||
@Override
|
||||
public int put(StorageEntry entry) throws IOException
|
||||
public int put(KademliaStorageEntry entry) throws IOException
|
||||
{
|
||||
StoreOperation sop = new StoreOperation(this.server, this, entry, this.dht, this.config);
|
||||
sop.execute();
|
||||
@ -281,7 +282,7 @@ public class JKademliaNode implements KademliaNode
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException
|
||||
public KademliaStorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException
|
||||
{
|
||||
if (this.dht.contains(param))
|
||||
{
|
||||
|
@ -5,7 +5,7 @@ import java.util.NoSuchElementException;
|
||||
import kademlia.dht.GetParameter;
|
||||
import kademlia.dht.KadContent;
|
||||
import kademlia.dht.KademliaDHT;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.exceptions.ContentNotFoundException;
|
||||
import kademlia.exceptions.RoutingException;
|
||||
import kademlia.node.Node;
|
||||
@ -86,7 +86,7 @@ public interface KademliaNode
|
||||
* @throws java.io.IOException
|
||||
*
|
||||
*/
|
||||
public int put(StorageEntry entry) throws IOException;
|
||||
public int put(KademliaStorageEntry entry) throws IOException;
|
||||
|
||||
/**
|
||||
* Store a content on the local node's DHT
|
||||
@ -107,7 +107,7 @@ public interface KademliaNode
|
||||
* @throws java.io.IOException
|
||||
* @throws kademlia.exceptions.ContentNotFoundException
|
||||
*/
|
||||
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException;
|
||||
public KademliaStorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException;
|
||||
|
||||
/**
|
||||
* Allow the user of the System to call refresh even out of the normal Kad refresh timing
|
||||
|
@ -26,7 +26,7 @@ public class DHT implements KademliaDHT
|
||||
{
|
||||
|
||||
private transient StoredContentManager contentManager;
|
||||
private transient KadSerializer<StorageEntry> serializer = null;
|
||||
private transient KadSerializer<KademliaStorageEntry> serializer = null;
|
||||
private transient KadConfiguration config;
|
||||
|
||||
private final String ownerId;
|
||||
@ -51,7 +51,7 @@ public class DHT implements KademliaDHT
|
||||
}
|
||||
|
||||
@Override
|
||||
public KadSerializer<StorageEntry> getSerializer()
|
||||
public KadSerializer<KademliaStorageEntry> getSerializer()
|
||||
{
|
||||
if (null == serializer)
|
||||
{
|
||||
@ -62,7 +62,7 @@ public class DHT implements KademliaDHT
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean store(StorageEntry content) throws IOException
|
||||
public boolean store(KademliaStorageEntry content) throws IOException
|
||||
{
|
||||
/* Lets check if we have this content and it's the updated version */
|
||||
if (this.contentManager.contains(content.getContentMetadata()))
|
||||
@ -130,7 +130,7 @@ public class DHT implements KademliaDHT
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageEntry retrieve(KademliaId key, int hashCode) throws FileNotFoundException, IOException, ClassNotFoundException
|
||||
public KademliaStorageEntry retrieve(KademliaId key, int hashCode) throws FileNotFoundException, IOException, ClassNotFoundException
|
||||
{
|
||||
String folder = this.getContentStorageFolderName(key);
|
||||
DataInputStream din = new DataInputStream(new FileInputStream(folder + File.separator + hashCode + ".kct"));
|
||||
@ -144,7 +144,7 @@ public class DHT implements KademliaDHT
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageEntry get(StorageEntryMetadata entry) throws IOException, NoSuchElementException
|
||||
public KademliaStorageEntry get(StorageEntryMetadata entry) throws IOException, NoSuchElementException
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -164,7 +164,7 @@ public class DHT implements KademliaDHT
|
||||
}
|
||||
|
||||
@Override
|
||||
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException
|
||||
public KademliaStorageEntry get(GetParameter param) throws NoSuchElementException, IOException
|
||||
{
|
||||
/* Load a KadContent if any exist for the given criteria */
|
||||
try
|
||||
|
@ -35,7 +35,7 @@ public interface KademliaDHT
|
||||
*
|
||||
* @return The new ContentSerializer
|
||||
*/
|
||||
public KadSerializer<StorageEntry> getSerializer();
|
||||
public KadSerializer<KademliaStorageEntry> getSerializer();
|
||||
|
||||
/**
|
||||
* Handle storing content locally
|
||||
@ -46,7 +46,7 @@ public interface KademliaDHT
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public boolean store(StorageEntry content) throws IOException;
|
||||
public boolean store(KademliaStorageEntry content) throws IOException;
|
||||
|
||||
public boolean store(KadContent content) throws IOException;
|
||||
|
||||
@ -61,7 +61,7 @@ public interface KademliaDHT
|
||||
* @throws java.io.FileNotFoundException
|
||||
* @throws java.lang.ClassNotFoundException
|
||||
*/
|
||||
public StorageEntry retrieve(KademliaId key, int hashCode) throws FileNotFoundException, IOException, ClassNotFoundException;
|
||||
public KademliaStorageEntry retrieve(KademliaId key, int hashCode) throws FileNotFoundException, IOException, ClassNotFoundException;
|
||||
|
||||
/**
|
||||
* Check if any content for the given criteria exists in this DHT
|
||||
@ -81,7 +81,7 @@ public interface KademliaDHT
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public StorageEntry get(StorageEntryMetadata entry) throws IOException, NoSuchElementException;
|
||||
public KademliaStorageEntry get(StorageEntryMetadata entry) throws IOException, NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Get the StorageEntry for the content if any exist.
|
||||
@ -92,7 +92,7 @@ public interface KademliaDHT
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException;
|
||||
public KademliaStorageEntry get(GetParameter param) throws NoSuchElementException, IOException;
|
||||
|
||||
/**
|
||||
* Delete a content from local storage
|
||||
|
17
src/kademlia/dht/KademliaStorageEntry.java
Normal file
17
src/kademlia/dht/KademliaStorageEntry.java
Normal file
@ -0,0 +1,17 @@
|
||||
package kademlia.dht;
|
||||
|
||||
/**
|
||||
* A StorageEntry interface for the storage entry class used to store a content on the DHT
|
||||
*
|
||||
* @author Joshua Kissoon
|
||||
* @since 20140523
|
||||
*/
|
||||
public interface KademliaStorageEntry
|
||||
{
|
||||
|
||||
public void setContent(final byte[] data);
|
||||
|
||||
public byte[] getContent();
|
||||
|
||||
public StorageEntryMetadata getContentMetadata();
|
||||
}
|
@ -6,7 +6,7 @@ package kademlia.dht;
|
||||
* @author Joshua Kissoon
|
||||
* @since 20140402
|
||||
*/
|
||||
public class StorageEntry
|
||||
public class StorageEntry implements KademliaStorageEntry
|
||||
{
|
||||
|
||||
private String content;
|
||||
@ -23,16 +23,19 @@ public class StorageEntry
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void setContent(final byte[] data)
|
||||
{
|
||||
this.content = new String(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final byte[] getContent()
|
||||
{
|
||||
return this.content.getBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final StorageEntryMetadata getContentMetadata()
|
||||
{
|
||||
return this.metadata;
|
||||
|
@ -3,7 +3,7 @@ package kademlia.message;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.node.Node;
|
||||
import kademlia.util.serializer.JsonSerializer;
|
||||
|
||||
@ -18,7 +18,7 @@ public class ContentMessage implements Message
|
||||
|
||||
public static final byte CODE = 0x04;
|
||||
|
||||
private StorageEntry content;
|
||||
private KademliaStorageEntry content;
|
||||
private Node origin;
|
||||
|
||||
/**
|
||||
@ -26,7 +26,7 @@ public class ContentMessage implements Message
|
||||
* @param content The content to be stored
|
||||
*
|
||||
*/
|
||||
public ContentMessage(Node origin, StorageEntry content)
|
||||
public ContentMessage(Node origin, KademliaStorageEntry content)
|
||||
{
|
||||
this.content = content;
|
||||
this.origin = origin;
|
||||
@ -43,7 +43,7 @@ public class ContentMessage implements Message
|
||||
this.origin.toStream(out);
|
||||
|
||||
/* Serialize the KadContent, then send it to the stream */
|
||||
new JsonSerializer<StorageEntry>().write(content, out);
|
||||
new JsonSerializer<KademliaStorageEntry>().write(content, out);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -53,7 +53,7 @@ public class ContentMessage implements Message
|
||||
|
||||
try
|
||||
{
|
||||
this.content = new JsonSerializer<StorageEntry>().read(in);
|
||||
this.content = new JsonSerializer<KademliaStorageEntry>().read(in);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
@ -66,7 +66,7 @@ public class ContentMessage implements Message
|
||||
return this.origin;
|
||||
}
|
||||
|
||||
public StorageEntry getContent()
|
||||
public KademliaStorageEntry getContent()
|
||||
{
|
||||
return this.content;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package kademlia.message;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.node.Node;
|
||||
import kademlia.util.serializer.JsonSerializer;
|
||||
|
||||
@ -18,7 +18,7 @@ public class StoreContentMessage implements Message
|
||||
|
||||
public static final byte CODE = 0x08;
|
||||
|
||||
private StorageEntry content;
|
||||
private KademliaStorageEntry content;
|
||||
private Node origin;
|
||||
|
||||
/**
|
||||
@ -26,7 +26,7 @@ public class StoreContentMessage implements Message
|
||||
* @param content The content to be stored
|
||||
*
|
||||
*/
|
||||
public StoreContentMessage(Node origin, StorageEntry content)
|
||||
public StoreContentMessage(Node origin, KademliaStorageEntry content)
|
||||
{
|
||||
this.content = content;
|
||||
this.origin = origin;
|
||||
@ -43,7 +43,7 @@ public class StoreContentMessage implements Message
|
||||
this.origin.toStream(out);
|
||||
|
||||
/* Serialize the KadContent, then send it to the stream */
|
||||
new JsonSerializer<StorageEntry>().write(content, out);
|
||||
new JsonSerializer<KademliaStorageEntry>().write(content, out);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -52,7 +52,7 @@ public class StoreContentMessage implements Message
|
||||
this.origin = new Node(in);
|
||||
try
|
||||
{
|
||||
this.content = new JsonSerializer<StorageEntry>().read(in);
|
||||
this.content = new JsonSerializer<KademliaStorageEntry>().read(in);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
@ -65,7 +65,7 @@ public class StoreContentMessage implements Message
|
||||
return this.origin;
|
||||
}
|
||||
|
||||
public StorageEntry getContent()
|
||||
public KademliaStorageEntry getContent()
|
||||
{
|
||||
return this.content;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import kademlia.JKademliaNode;
|
||||
import kademlia.dht.GetParameter;
|
||||
import kademlia.KadConfiguration;
|
||||
import kademlia.KadServer;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.exceptions.ContentNotFoundException;
|
||||
import kademlia.exceptions.RoutingException;
|
||||
import kademlia.exceptions.UnknownMessageException;
|
||||
@ -43,7 +43,7 @@ public class ContentLookupOperation implements Operation, Receiver
|
||||
|
||||
private final KadServer server;
|
||||
private final JKademliaNode localNode;
|
||||
private StorageEntry contentFound = null;
|
||||
private KademliaStorageEntry contentFound = null;
|
||||
private final KadConfiguration config;
|
||||
|
||||
private final ContentLookupMessage lookupMessage;
|
||||
@ -253,7 +253,7 @@ public class ContentLookupOperation implements Operation, Receiver
|
||||
this.localNode.getRoutingTable().insert(msg.getOrigin());
|
||||
|
||||
/* Get the Content and check if it satisfies the required parameters */
|
||||
StorageEntry content = msg.getContent();
|
||||
KademliaStorageEntry content = msg.getContent();
|
||||
this.contentFound = content;
|
||||
this.isContentFound = true;
|
||||
}
|
||||
@ -320,7 +320,7 @@ public class ContentLookupOperation implements Operation, Receiver
|
||||
*
|
||||
* @throws kademlia.exceptions.ContentNotFoundException
|
||||
*/
|
||||
public StorageEntry getContentFound() throws ContentNotFoundException
|
||||
public KademliaStorageEntry getContentFound() throws ContentNotFoundException
|
||||
{
|
||||
if (this.isContentFound)
|
||||
{
|
||||
|
@ -6,7 +6,7 @@ import kademlia.JKademliaNode;
|
||||
import kademlia.KadConfiguration;
|
||||
import kademlia.KadServer;
|
||||
import kademlia.dht.KademliaDHT;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.message.Message;
|
||||
import kademlia.message.StoreContentMessage;
|
||||
import kademlia.node.Node;
|
||||
@ -22,7 +22,7 @@ public class StoreOperation implements Operation
|
||||
|
||||
private final KadServer server;
|
||||
private final JKademliaNode localNode;
|
||||
private final StorageEntry storageEntry;
|
||||
private final KademliaStorageEntry storageEntry;
|
||||
private final KademliaDHT localDht;
|
||||
private final KadConfiguration config;
|
||||
|
||||
@ -33,7 +33,7 @@ public class StoreOperation implements Operation
|
||||
* @param localDht The local DHT
|
||||
* @param config
|
||||
*/
|
||||
public StoreOperation(KadServer server, JKademliaNode localNode, StorageEntry storageEntry, KademliaDHT localDht, KadConfiguration config)
|
||||
public StoreOperation(KadServer server, JKademliaNode localNode, KademliaStorageEntry storageEntry, KademliaDHT localDht, KadConfiguration config)
|
||||
{
|
||||
this.server = server;
|
||||
this.localNode = localNode;
|
||||
|
@ -4,7 +4,7 @@ import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import kademlia.dht.GetParameter;
|
||||
import kademlia.JKademliaNode;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.exceptions.ContentNotFoundException;
|
||||
import kademlia.node.KademliaId;
|
||||
|
||||
@ -47,7 +47,7 @@ public class ContentSendingTest
|
||||
GetParameter gp = new GetParameter(c.getKey(), DHTContentImpl.TYPE);
|
||||
gp.setOwnerId(c.getOwnerId());
|
||||
System.out.println("Get Parameter: " + gp);
|
||||
StorageEntry conte = kad2.get(gp);
|
||||
KademliaStorageEntry conte = kad2.get(gp);
|
||||
System.out.println("Content Found: " + new DHTContentImpl().fromSerializedForm(conte.getContent()));
|
||||
System.out.println("Content Metadata: " + conte.getContentMetadata());
|
||||
|
||||
|
@ -3,7 +3,7 @@ package kademlia.simulations;
|
||||
import java.io.IOException;
|
||||
import kademlia.dht.GetParameter;
|
||||
import kademlia.JKademliaNode;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.exceptions.ContentNotFoundException;
|
||||
import kademlia.node.KademliaId;
|
||||
|
||||
@ -36,7 +36,7 @@ public class ContentUpdatingTest
|
||||
GetParameter gp = new GetParameter(c.getKey(), DHTContentImpl.TYPE, c.getOwnerId());
|
||||
|
||||
System.out.println("Get Parameter: " + gp);
|
||||
StorageEntry conte = kad2.get(gp);
|
||||
KademliaStorageEntry conte = kad2.get(gp);
|
||||
System.out.println("Content Found: " + new DHTContentImpl().fromSerializedForm(conte.getContent()));
|
||||
System.out.println("Content Metadata: " + conte.getContentMetadata());
|
||||
|
||||
|
@ -3,7 +3,7 @@ package kademlia.simulations;
|
||||
import java.io.IOException;
|
||||
import kademlia.dht.GetParameter;
|
||||
import kademlia.JKademliaNode;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.exceptions.ContentNotFoundException;
|
||||
import kademlia.node.KademliaId;
|
||||
|
||||
@ -33,7 +33,7 @@ public class RefreshOperationTest
|
||||
GetParameter gp = new GetParameter(c.getKey(), DHTContentImpl.TYPE);
|
||||
gp.setType(DHTContentImpl.TYPE);
|
||||
gp.setOwnerId(c.getOwnerId());
|
||||
StorageEntry conte = kad2.get(gp);
|
||||
KademliaStorageEntry conte = kad2.get(gp);
|
||||
|
||||
kad2.refresh();
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package kademlia.simulations;
|
||||
|
||||
import kademlia.JKademliaNode;
|
||||
import kademlia.dht.GetParameter;
|
||||
import kademlia.dht.StorageEntry;
|
||||
import kademlia.dht.KademliaStorageEntry;
|
||||
import kademlia.node.KademliaId;
|
||||
|
||||
/**
|
||||
@ -52,7 +52,7 @@ public class SaveStateTest2
|
||||
|
||||
/* Trying to get a content stored on the restored node */
|
||||
GetParameter gp = new GetParameter(c.getKey(), kad2.getOwnerId(), c.getType());
|
||||
StorageEntry content = kad2.get(gp);
|
||||
KademliaStorageEntry content = kad2.get(gp);
|
||||
DHTContentImpl cc = new DHTContentImpl().fromSerializedForm(content.getContent());
|
||||
System.out.println("Content received: " + cc);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user