From fa4b29305eab5008794ca4f20c646add3b9db430 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Tue, 25 Feb 2014 18:58:32 +0530 Subject: [PATCH] Finished implementing a JsonSerializer to serialize Content --- src/kademlia/core/KadServer.java | 6 +-- src/kademlia/routing/RoutingTable.java | 8 +-- src/kademlia/serializer/JsonSerializer.java | 53 +++++++++++++++++++ .../serializer/KadContentSerializer.java | 14 +++-- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index b39de5e..f03ec8e 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -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 */ diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 5be6adb..3feb30c 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -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 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 "); diff --git a/src/kademlia/serializer/JsonSerializer.java b/src/kademlia/serializer/JsonSerializer.java index bcd426e..bc2cd0c 100644 --- a/src/kademlia/serializer/JsonSerializer.java +++ b/src/kademlia/serializer/JsonSerializer.java @@ -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)); + } + } } diff --git a/src/kademlia/serializer/KadContentSerializer.java b/src/kademlia/serializer/KadContentSerializer.java index a0e9788..38f6a84 100644 --- a/src/kademlia/serializer/KadContentSerializer.java +++ b/src/kademlia/serializer/KadContentSerializer.java @@ -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; }