Added a message factory interface

This commit is contained in:
Joshua Kissoon 2014-05-23 20:29:43 +05:30
parent b594b91adf
commit 720b062646
2 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,37 @@
package kademlia.message;
import java.io.DataInputStream;
import java.io.IOException;
import kademlia.core.KadServer;
/**
* A factory that handles creating messages and receivers
*
* @author Joshua Kissoon
* @since 20140523
*/
public interface KademliaMessageFactory
{
/**
* Method that creates a message based on the code and input stream
*
* @param code The message code
* @param in An input stream with the message data
*
* @return A message
*
* @throws java.io.IOException
*/
public Message createMessage(byte code, DataInputStream in) throws IOException;
/**
* Method that returns a receiver to handle a specific type of message
*
* @param code The message code
* @param server
*
* @return A receiver
*/
public Receiver createReceiver(byte code, KadServer server);
}

View File

@ -13,7 +13,7 @@ import kademlia.dht.DHT;
* @author Joshua Kissoon
* @since 20140202
*/
public class MessageFactory
public class MessageFactory implements KademliaMessageFactory
{
private final KademliaNode localNode;
@ -27,6 +27,7 @@ public class MessageFactory
this.config = config;
}
@Override
public Message createMessage(byte code, DataInputStream in) throws IOException
{
switch (code)
@ -54,6 +55,7 @@ public class MessageFactory
}
}
@Override
public Receiver createReceiver(byte code, KadServer server)
{
switch (code)