Again set our storageentry to store a string

- we'll have to encode any byte array to base64 if we need to
This commit is contained in:
Joshua Kissoon 2014-05-12 17:34:41 +05:30
parent cc0cdc42b5
commit fca4a5fde5
6 changed files with 17 additions and 17 deletions

View File

@ -52,7 +52,7 @@ public interface KadContent
*
* @return byte[] The content in byte format
*/
public byte[] toBytes();
public byte[] toSerializedForm();
/**
* Given the Content in byte format, read it
@ -61,5 +61,5 @@ public interface KadContent
*
* @return A new object from the given byte[]
*/
public KadContent fromBytes(byte[] data);
public KadContent fromSerializedForm(byte[] data);
}

View File

@ -9,31 +9,31 @@ package kademlia.dht;
public class StorageEntry
{
private byte[] content;
private String content;
private final StorageEntryMetadata metadata;
public StorageEntry(KadContent content)
public StorageEntry(final KadContent content)
{
this(content, new StorageEntryMetadata(content));
}
public StorageEntry(KadContent content, StorageEntryMetadata metadata)
public StorageEntry(final KadContent content, final StorageEntryMetadata metadata)
{
this.content = content.toBytes();
this.setContent(content.toSerializedForm());
this.metadata = metadata;
}
public void setContent(byte[] data)
public final void setContent(final byte[] data)
{
this.content = data;
this.content = new String(data);
}
public byte[] getContent()
public final byte[] getContent()
{
return this.content;
return this.content.getBytes();
}
public StorageEntryMetadata getContentMetadata()
public final StorageEntryMetadata getContentMetadata()
{
return this.metadata;
}

View File

@ -48,7 +48,7 @@ public class ContentSendingTest
gp.setOwnerId(c.getOwnerId());
System.out.println("Get Parameter: " + gp);
StorageEntry conte = kad2.get(gp);
System.out.println("Content Found: " + new DHTContentImpl().fromBytes(conte.getContent()));
System.out.println("Content Found: " + new DHTContentImpl().fromSerializedForm(conte.getContent()));
System.out.println("Content Metadata: " + conte.getContentMetadata());
}

View File

@ -37,7 +37,7 @@ public class ContentUpdatingTest
System.out.println("Get Parameter: " + gp);
StorageEntry conte = kad2.get(gp);
System.out.println("Content Found: " + new DHTContentImpl().fromBytes(conte.getContent()));
System.out.println("Content Found: " + new DHTContentImpl().fromSerializedForm(conte.getContent()));
System.out.println("Content Metadata: " + conte.getContentMetadata());
/* Lets update the content and put it again */
@ -47,7 +47,7 @@ public class ContentUpdatingTest
/* Lets retrieve the content */
System.out.println("Retrieving Content Again");
conte = kad2.get(gp);
System.out.println("Content Found: " + new DHTContentImpl().fromBytes(conte.getContent()));
System.out.println("Content Found: " + new DHTContentImpl().fromSerializedForm(conte.getContent()));
System.out.println("Content Metadata: " + conte.getContentMetadata());
}

View File

@ -89,14 +89,14 @@ public class DHTContentImpl implements KadContent
}
@Override
public byte[] toBytes()
public byte[] toSerializedForm()
{
Gson gson = new Gson();
return gson.toJson(this).getBytes();
}
@Override
public DHTContentImpl fromBytes(byte[] data)
public DHTContentImpl fromSerializedForm(byte[] data)
{
Gson gson = new Gson();
DHTContentImpl val = gson.fromJson(new String(data), DHTContentImpl.class);

View File

@ -53,7 +53,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);
DHTContentImpl cc = new DHTContentImpl().fromBytes(content.getContent());
DHTContentImpl cc = new DHTContentImpl().fromSerializedForm(content.getContent());
System.out.println("Content received: " + cc);
}