mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
Keeping with some good coding standards - Code to Interfaces not implementations
Changed the use of ArrayList to List and HashMap to Map
This commit is contained in:
parent
9f14c66a31
commit
b5e89c6ddb
@ -14,6 +14,7 @@ import java.net.DatagramPacket;
|
|||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
@ -32,9 +33,9 @@ public class KadServer
|
|||||||
private final int udpPort;
|
private final int udpPort;
|
||||||
private final DatagramSocket socket;
|
private final DatagramSocket socket;
|
||||||
private boolean isRunning;
|
private boolean isRunning;
|
||||||
private final HashMap<Integer, Receiver> receivers;
|
private final Map<Integer, Receiver> receivers;
|
||||||
private final Timer timer; // Schedule future tasks
|
private final Timer timer; // Schedule future tasks
|
||||||
private final HashMap<Integer, TimerTask> tasks; // Keep track of scheduled tasks
|
private final Map<Integer, TimerTask> tasks; // Keep track of scheduled tasks
|
||||||
|
|
||||||
private final Node localNode;
|
private final Node localNode;
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@ package kademlia.dht;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import kademlia.node.NodeId;
|
import kademlia.node.NodeId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -15,7 +17,7 @@ import kademlia.node.NodeId;
|
|||||||
public class StorageEntryManager
|
public class StorageEntryManager
|
||||||
{
|
{
|
||||||
|
|
||||||
private final HashMap<NodeId, ArrayList<StorageEntry>> entries;
|
private final Map<NodeId, List<StorageEntry>> entries;
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -60,7 +62,7 @@ public class StorageEntryManager
|
|||||||
*
|
*
|
||||||
* @return List of content for the specific search parameters
|
* @return List of content for the specific search parameters
|
||||||
*/
|
*/
|
||||||
public ArrayList<StorageEntry> get(NodeId key)
|
public List<StorageEntry> get(NodeId key)
|
||||||
{
|
{
|
||||||
return this.entries.get(key);
|
return this.entries.get(key);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
import kademlia.core.Configuration;
|
import kademlia.core.Configuration;
|
||||||
import kademlia.core.KadServer;
|
import kademlia.core.KadServer;
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
@ -45,7 +45,7 @@ public class NodeLookupReceiver implements Receiver
|
|||||||
this.localNode.getRoutingTable().insert(origin);
|
this.localNode.getRoutingTable().insert(origin);
|
||||||
|
|
||||||
/* Find nodes closest to the LookupId */
|
/* Find nodes closest to the LookupId */
|
||||||
ArrayList<Node> nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K);
|
List<Node> nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K);
|
||||||
|
|
||||||
System.out.println("\nClosest Nodes: ");
|
System.out.println("\nClosest Nodes: ");
|
||||||
for (Node n : nodes)
|
for (Node n : nodes)
|
||||||
|
@ -5,12 +5,11 @@
|
|||||||
*/
|
*/
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.DataInput;
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutput;
|
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
|
|
||||||
public class NodeReplyMessage implements Message
|
public class NodeReplyMessage implements Message
|
||||||
@ -18,9 +17,9 @@ public class NodeReplyMessage implements Message
|
|||||||
|
|
||||||
private Node origin;
|
private Node origin;
|
||||||
public static final byte CODE = 0x04;
|
public static final byte CODE = 0x04;
|
||||||
private ArrayList<Node> nodes;
|
private List<Node> nodes;
|
||||||
|
|
||||||
public NodeReplyMessage(Node origin, ArrayList<Node> nodes)
|
public NodeReplyMessage(Node origin, List<Node> nodes)
|
||||||
{
|
{
|
||||||
this.origin = origin;
|
this.origin = origin;
|
||||||
this.nodes = nodes;
|
this.nodes = nodes;
|
||||||
@ -80,7 +79,7 @@ public class NodeReplyMessage implements Message
|
|||||||
return CODE;
|
return CODE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Node> getNodes()
|
public List<Node> getNodes()
|
||||||
{
|
{
|
||||||
return this.nodes;
|
return this.nodes;
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
private final SortedMap<Node, Byte> nodes;
|
private final SortedMap<Node, Byte> nodes;
|
||||||
|
|
||||||
/* Tracks messages in transit and awaiting reply */
|
/* Tracks messages in transit and awaiting reply */
|
||||||
private final HashMap<Integer, Node> messagesTransiting;
|
private final Map<Integer, Node> messagesTransiting;
|
||||||
|
|
||||||
/* Used to sort nodes */
|
/* Used to sort nodes */
|
||||||
private final Comparator comparator;
|
private final Comparator comparator;
|
||||||
@ -83,7 +83,7 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
* @throws kademlia.exceptions.RoutingException
|
* @throws kademlia.exceptions.RoutingException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized ArrayList<Node> execute() throws IOException, RoutingException
|
public synchronized List<Node> execute() throws IOException, RoutingException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -159,7 +159,7 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Get unqueried nodes among the K closest seen that have not FAILED */
|
/* Get unqueried nodes among the K closest seen that have not FAILED */
|
||||||
ArrayList<Node> unasked = this.closestNodesNotFailed(UNASKED);
|
List<Node> unasked = this.closestNodesNotFailed(UNASKED);
|
||||||
for (Node nn : unasked)
|
for (Node nn : unasked)
|
||||||
{
|
{
|
||||||
System.out.println(nn.getNodeId());
|
System.out.println(nn.getNodeId());
|
||||||
@ -198,9 +198,9 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
*
|
*
|
||||||
* @return The K closest nodes to the target lookupId given that have the specified status
|
* @return The K closest nodes to the target lookupId given that have the specified status
|
||||||
*/
|
*/
|
||||||
private ArrayList<Node> closestNodes(Byte status)
|
private List<Node> closestNodes(Byte status)
|
||||||
{
|
{
|
||||||
ArrayList<Node> closestNodes = new ArrayList<>(Configuration.K);
|
List<Node> closestNodes = new ArrayList<>(Configuration.K);
|
||||||
int remainingSpaces = Configuration.K;
|
int remainingSpaces = Configuration.K;
|
||||||
|
|
||||||
for (Map.Entry e : this.nodes.entrySet())
|
for (Map.Entry e : this.nodes.entrySet())
|
||||||
@ -227,9 +227,9 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
*
|
*
|
||||||
* @return A List of the closest nodes
|
* @return A List of the closest nodes
|
||||||
*/
|
*/
|
||||||
private ArrayList<Node> closestNodesNotFailed(Byte status)
|
private List<Node> closestNodesNotFailed(Byte status)
|
||||||
{
|
{
|
||||||
ArrayList<Node> closestNodes = new ArrayList<>(Configuration.K);
|
List<Node> closestNodes = new ArrayList<>(Configuration.K);
|
||||||
int remainingSpaces = Configuration.K;
|
int remainingSpaces = Configuration.K;
|
||||||
|
|
||||||
for (Map.Entry e : this.nodes.entrySet())
|
for (Map.Entry e : this.nodes.entrySet())
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package kademlia.operation;
|
package kademlia.operation;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
import kademlia.core.KadServer;
|
import kademlia.core.KadServer;
|
||||||
import kademlia.dht.KadContent;
|
import kademlia.dht.KadContent;
|
||||||
import kademlia.message.Message;
|
import kademlia.message.Message;
|
||||||
@ -37,7 +37,7 @@ public class StoreOperation implements Operation
|
|||||||
public synchronized Object execute() throws IOException
|
public synchronized Object execute() throws IOException
|
||||||
{
|
{
|
||||||
/* Get the nodes on which we need to store the content */
|
/* Get the nodes on which we need to store the content */
|
||||||
ArrayList<Node> nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute();
|
List<Node> nodes = new NodeLookupOperation(this.server, this.localNode, this.content.getKey()).execute();
|
||||||
System.out.println("Nodes to put content on: " + nodes);
|
System.out.println("Nodes to put content on: " + nodes);
|
||||||
|
|
||||||
/* Create the message */
|
/* Create the message */
|
||||||
|
@ -7,6 +7,8 @@ package kademlia.routing;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.node.NodeId;
|
import kademlia.node.NodeId;
|
||||||
|
|
||||||
@ -14,7 +16,7 @@ public class KadBucket implements Bucket
|
|||||||
{
|
{
|
||||||
|
|
||||||
private final int depth;
|
private final int depth;
|
||||||
private final HashMap<NodeId, Node> nodes;
|
private final Map<NodeId, Node> nodes;
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -85,7 +87,7 @@ public class KadBucket implements Bucket
|
|||||||
this.nodes.remove(n.getNodeId());
|
this.nodes.remove(n.getNodeId());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Node> getNodes()
|
public List<Node> getNodes()
|
||||||
{
|
{
|
||||||
return new ArrayList<>(this.nodes.values());
|
return new ArrayList<>(this.nodes.values());
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
package kademlia.routing;
|
package kademlia.routing;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.node.NodeId;
|
import kademlia.node.NodeId;
|
||||||
|
|
||||||
@ -72,11 +73,11 @@ public class RoutingTable
|
|||||||
* @param target The NodeId to find contacts close to
|
* @param target The NodeId to find contacts close to
|
||||||
* @param num The number of contacts to find
|
* @param num The number of contacts to find
|
||||||
*
|
*
|
||||||
* @return ArrayList<Contact> An ArrayList of num contacts closest to target
|
* @return List<Contact> A List of num contacts closest to target
|
||||||
*/
|
*/
|
||||||
public ArrayList<Node> findClosest(NodeId target, int num)
|
public List<Node> findClosest(NodeId target, int num)
|
||||||
{
|
{
|
||||||
ArrayList<Node> closest = new ArrayList<>(num);
|
List<Node> closest = new ArrayList<>(num);
|
||||||
|
|
||||||
/* Get the bucket number to search for closest from */
|
/* Get the bucket number to search for closest from */
|
||||||
int bucketNumber = this.localNode.getNodeId().xor(target).getFirstSetBitIndex() - 1;
|
int bucketNumber = this.localNode.getNodeId().xor(target).getFirstSetBitIndex() - 1;
|
||||||
@ -144,9 +145,9 @@ public class RoutingTable
|
|||||||
return closest;
|
return closest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Node> getAllNodes()
|
public List<Node> getAllNodes()
|
||||||
{
|
{
|
||||||
ArrayList<Node> nodes = new ArrayList<>();
|
List<Node> nodes = new ArrayList<>();
|
||||||
|
|
||||||
for (KadBucket b : this.buckets)
|
for (KadBucket b : this.buckets)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user