From 995da2cffbc47d29de337bfb4e745d88ffb7a42b Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Tue, 25 Feb 2014 13:42:08 +0530 Subject: [PATCH] Continuing work on message sending --- src/kademlia/core/KadServer.java | 8 +++-- src/kademlia/core/Kademlia.java | 6 ++-- .../dht/{DHTContent.java => KadContent.java} | 2 +- src/kademlia/message/ContentStoreMessage.java | 8 ++--- src/kademlia/operation/StoreOperation.java | 9 +++-- src/kademlia/serializer/JsonSerializer.java | 12 +++++++ .../serializer/KadContentSerializer.java | 34 +++++++++++++++++++ src/kademlia/tests/DHTContentImpl.java | 4 +-- 8 files changed, 68 insertions(+), 15 deletions(-) rename src/kademlia/dht/{DHTContent.java => KadContent.java} (96%) create mode 100644 src/kademlia/serializer/JsonSerializer.java create mode 100644 src/kademlia/serializer/KadContentSerializer.java diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 3028605..b39de5e 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -35,7 +35,7 @@ public class KadServer private final HashMap receivers; private final Timer timer; // Schedule future tasks private final HashMap tasks; // Keep track of scheduled tasks - + private final Node localNode; /* Factories */ @@ -53,7 +53,7 @@ public class KadServer { this.udpPort = udpPort; this.socket = new DatagramSocket(udpPort); - + this.localNode = localNode; this.messageFactory = mFactory; @@ -119,6 +119,10 @@ 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); + /* Setup the message for transmission */ ByteArrayOutputStream bout = new ByteArrayOutputStream(); diff --git a/src/kademlia/core/Kademlia.java b/src/kademlia/core/Kademlia.java index 2e733fc..7823920 100644 --- a/src/kademlia/core/Kademlia.java +++ b/src/kademlia/core/Kademlia.java @@ -4,7 +4,7 @@ import java.io.IOException; import java.net.InetAddress; import java.util.Timer; import java.util.TimerTask; -import kademlia.dht.DHTContent; +import kademlia.dht.KadContent; import kademlia.exceptions.RoutingException; import kademlia.message.MessageFactory; import kademlia.node.Node; @@ -125,7 +125,7 @@ public class Kademlia * @throws java.io.IOException * */ - public int put(DHTContent content) throws IOException + public int put(KadContent content) throws IOException { return (int) new StoreOperation(server, localNode, content).execute(); } @@ -139,7 +139,7 @@ public class Kademlia * * @return DHTContent The content */ - public DHTContent get(GetParameter param, Class c) + public KadContent get(GetParameter param, Class c) { return null; } diff --git a/src/kademlia/dht/DHTContent.java b/src/kademlia/dht/KadContent.java similarity index 96% rename from src/kademlia/dht/DHTContent.java rename to src/kademlia/dht/KadContent.java index 1681992..4ba274b 100644 --- a/src/kademlia/dht/DHTContent.java +++ b/src/kademlia/dht/KadContent.java @@ -8,7 +8,7 @@ import kademlia.node.NodeId; * @author Joshua Kissoon * @since 20140224 */ -public interface DHTContent +public interface KadContent { /** diff --git a/src/kademlia/message/ContentStoreMessage.java b/src/kademlia/message/ContentStoreMessage.java index 4a2071f..80d6810 100644 --- a/src/kademlia/message/ContentStoreMessage.java +++ b/src/kademlia/message/ContentStoreMessage.java @@ -2,7 +2,7 @@ package kademlia.message; import java.io.DataInput; import java.io.DataOutput; -import kademlia.dht.DHTContent; +import kademlia.dht.KadContent; import kademlia.node.Node; /** @@ -15,7 +15,7 @@ public class ContentStoreMessage implements Message { private final Node origin; - private final DHTContent content; + private final KadContent content; public final static byte CODE = 0x23; @@ -23,7 +23,7 @@ public class ContentStoreMessage implements Message * @param origin Where did this content come from - it'll always be the local node * @param content The Content to send */ - public ContentStoreMessage(Node origin, DHTContent content) + public ContentStoreMessage(Node origin, KadContent content) { this.origin = origin; this.content = content; @@ -52,7 +52,7 @@ public class ContentStoreMessage implements Message return this.origin; } - public DHTContent getContent() + public KadContent getContent() { return this.content; } diff --git a/src/kademlia/operation/StoreOperation.java b/src/kademlia/operation/StoreOperation.java index 76733ff..c65dbd6 100644 --- a/src/kademlia/operation/StoreOperation.java +++ b/src/kademlia/operation/StoreOperation.java @@ -3,7 +3,7 @@ package kademlia.operation; import java.io.IOException; import java.util.ArrayList; import kademlia.core.KadServer; -import kademlia.dht.DHTContent; +import kademlia.dht.KadContent; import kademlia.node.Node; /** @@ -17,14 +17,14 @@ public class StoreOperation implements Operation private final KadServer server; private final Node localNode; - private final DHTContent content; + private final KadContent content; /** * @param server * @param localNode * @param content The content to be stored on the DHT */ - public StoreOperation(KadServer server, Node localNode, DHTContent content) + public StoreOperation(KadServer server, Node localNode, KadContent content) { this.server = server; this.localNode = localNode; @@ -36,6 +36,9 @@ public class StoreOperation implements Operation { /* Get the nodes on which we need to store the content */ ArrayList nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute(); + + + System.out.println("Nodes to put content on: " + nodes); /* Return how many nodes the content was stored on */ diff --git a/src/kademlia/serializer/JsonSerializer.java b/src/kademlia/serializer/JsonSerializer.java new file mode 100644 index 0000000..bcd426e --- /dev/null +++ b/src/kademlia/serializer/JsonSerializer.java @@ -0,0 +1,12 @@ +package kademlia.serializer; + +/** + * A KadContentSerializer that serializes content to JSON format + * + * @author Joshua Kissoon + * @since 20140225 + */ +public class JsonSerializer implements KadContentSerializer +{ + +} diff --git a/src/kademlia/serializer/KadContentSerializer.java b/src/kademlia/serializer/KadContentSerializer.java new file mode 100644 index 0000000..a0e9788 --- /dev/null +++ b/src/kademlia/serializer/KadContentSerializer.java @@ -0,0 +1,34 @@ +package kademlia.serializer; + +import java.io.DataInput; +import java.io.DataOutput; +import kademlia.dht.KadContent; + +/** + * A Serializer is used to transform data to and from a specified form. + * + * Here we define the structure of any Serializer used in Kademlia + * + * @author Joshua Kissoon + * @since 20140225 + */ +public interface KadContentSerializer +{ + + /** + * Write a KadContent to a DataOutput stream + * + * @param content The content to write + * @param out The output Stream to write to + */ + public void write(KadContent content, DataOutput out); + + /** + * Read a KadContent from a DataInput Stream + * + * @param in The InputStream to read the data from + * + * @return KadContent + */ + public KadContent read(DataInput in); +} diff --git a/src/kademlia/tests/DHTContentImpl.java b/src/kademlia/tests/DHTContentImpl.java index d9e6539..f5f4d15 100644 --- a/src/kademlia/tests/DHTContentImpl.java +++ b/src/kademlia/tests/DHTContentImpl.java @@ -1,6 +1,6 @@ package kademlia.tests; -import kademlia.dht.DHTContent; +import kademlia.dht.KadContent; import kademlia.node.NodeId; /** @@ -9,7 +9,7 @@ import kademlia.node.NodeId; * @author Joshua Kissoon * @since 20140224 */ -public class DHTContentImpl implements DHTContent +public class DHTContentImpl implements KadContent { private final NodeId key;