Finished implementing a JsonSerializer to serialize Content

This commit is contained in:
Joshua Kissoon 2014-02-25 18:58:32 +05:30
parent 995da2cffb
commit fa4b29305e
4 changed files with 70 additions and 11 deletions

View File

@ -119,9 +119,9 @@ public class KadServer
private void sendMessage(Node to, Message msg, int comm) throws IOException
{
final Class<?> clazz = msg.getClass();
System.out.println(clazz.getSimpleName());
System.out.println(clazz);
Class c = msg.getClass();
System.out.println(c.getSimpleName());
System.out.println(c.getName());
/* Setup the message for transmission */

View File

@ -39,10 +39,10 @@ public class RoutingTable
public void insert(Node n)
{
/* Find the first set bit: how far this node is away from the contact node */
NodeId id = this.localNode.getNodeId().xor(n.getNodeId());
NodeId id = this.localNode.getNodeId().xor(n.getNodeId());
//System.out.println(" First Bit Set: " + id.getFirstSetBitIndex());
int bucketId = id.getFirstSetBitIndex();
System.out.println(this.localNode.getNodeId() + " Adding Node " + n.getNodeId() + " to bucket at depth: " + bucketId);
/* Put this contact to the bucket that stores contacts prefixLength distance away */
@ -79,7 +79,7 @@ public class RoutingTable
ArrayList<Node> closest = new ArrayList<>(num);
/* Get the bucket number to search for closest from */
int bucketNumber = this.localNode.getNodeId().xor(target).getFirstSetBitIndex();
int bucketNumber = this.localNode.getNodeId().xor(target).getFirstSetBitIndex() - 1;
/* Add the contacts from this bucket to the return contacts */
for (Node c : this.buckets[bucketNumber].getNodes())
@ -162,7 +162,7 @@ public class RoutingTable
StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n");
for (KadBucket b : this.buckets)
{
// System.out.println("Bucket: " + b);
// System.out.println("Bucket: " + b);
if (b.numNodes() > 0)
{
sb.append("# nodes in Bucket with depth ");

View File

@ -1,5 +1,17 @@
package kademlia.serializer;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import kademlia.dht.KadContent;
/**
* A KadContentSerializer that serializes content to JSON format
*
@ -9,4 +21,45 @@ package kademlia.serializer;
public class JsonSerializer implements KadContentSerializer
{
private final Gson gson;
{
gson = new Gson();
}
@Override
public void write(KadContent content, OutputStream out) throws IOException
{
try (DataOutputStream dout = new DataOutputStream(out);
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out)))
{
writer.beginArray();
/* Store the content type */
gson.toJson(content.getClass().getName(), String.class, writer);
/* Now Store the content */
gson.toJson(content, content.getClass(), writer);
writer.endArray();
}
}
@Override
public KadContent read(InputStream in) throws IOException, ClassNotFoundException
{
try (DataInputStream din = new DataInputStream(in);
JsonReader reader = new JsonReader(new InputStreamReader(in)))
{
reader.beginArray();
/* Read the class name */
String className = gson.fromJson(reader, String.class);
/* Read and return the Content*/
return gson.fromJson(reader, Class.forName(className));
}
}
}

View File

@ -1,7 +1,8 @@
package kademlia.serializer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import kademlia.dht.KadContent;
/**
@ -20,8 +21,10 @@ public interface KadContentSerializer
*
* @param content The content to write
* @param out The output Stream to write to
*
* @throws java.io.IOException
*/
public void write(KadContent content, DataOutput out);
public void write(KadContent content, OutputStream out) throws IOException;
/**
* Read a KadContent from a DataInput Stream
@ -29,6 +32,9 @@ public interface KadContentSerializer
* @param in The InputStream to read the data from
*
* @return KadContent
*
* @throws java.io.IOException
* @throws java.lang.ClassNotFoundException
*/
public KadContent read(DataInput in);
public KadContent read(InputStream in) throws IOException, ClassNotFoundException;
}