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 * @return byte[] The content in byte format
*/ */
public byte[] toBytes(); public byte[] toSerializedForm();
/** /**
* Given the Content in byte format, read it * Given the Content in byte format, read it
@ -61,5 +61,5 @@ public interface KadContent
* *
* @return A new object from the given byte[] * @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 public class StorageEntry
{ {
private byte[] content; private String content;
private final StorageEntryMetadata metadata; private final StorageEntryMetadata metadata;
public StorageEntry(KadContent content) public StorageEntry(final KadContent content)
{ {
this(content, new StorageEntryMetadata(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; 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; return this.metadata;
} }

View File

@ -48,7 +48,7 @@ public class ContentSendingTest
gp.setOwnerId(c.getOwnerId()); gp.setOwnerId(c.getOwnerId());
System.out.println("Get Parameter: " + gp); System.out.println("Get Parameter: " + gp);
StorageEntry conte = kad2.get(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()); System.out.println("Content Metadata: " + conte.getContentMetadata());
} }

View File

@ -37,7 +37,7 @@ public class ContentUpdatingTest
System.out.println("Get Parameter: " + gp); System.out.println("Get Parameter: " + gp);
StorageEntry conte = kad2.get(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()); System.out.println("Content Metadata: " + conte.getContentMetadata());
/* Lets update the content and put it again */ /* Lets update the content and put it again */
@ -47,7 +47,7 @@ public class ContentUpdatingTest
/* Lets retrieve the content */ /* Lets retrieve the content */
System.out.println("Retrieving Content Again"); System.out.println("Retrieving Content Again");
conte = kad2.get(gp); 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()); System.out.println("Content Metadata: " + conte.getContentMetadata());
} }

View File

@ -89,14 +89,14 @@ public class DHTContentImpl implements KadContent
} }
@Override @Override
public byte[] toBytes() public byte[] toSerializedForm()
{ {
Gson gson = new Gson(); Gson gson = new Gson();
return gson.toJson(this).getBytes(); return gson.toJson(this).getBytes();
} }
@Override @Override
public DHTContentImpl fromBytes(byte[] data) public DHTContentImpl fromSerializedForm(byte[] data)
{ {
Gson gson = new Gson(); Gson gson = new Gson();
DHTContentImpl val = gson.fromJson(new String(data), DHTContentImpl.class); 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 */ /* Trying to get a content stored on the restored node */
GetParameter gp = new GetParameter(c.getKey(), kad2.getOwnerId(), c.getType()); GetParameter gp = new GetParameter(c.getKey(), kad2.getOwnerId(), c.getType());
StorageEntry content = kad2.get(gp); 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); System.out.println("Content received: " + cc);
} }