mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-24 19:22:18 +00:00
Added some synchronization to the KadBucket and DHT
This commit is contained in:
parent
3ab6b3d2ab
commit
565bf50ddb
@ -318,7 +318,7 @@ public class KademliaNode
|
|||||||
* @throws java.io.IOException
|
* @throws java.io.IOException
|
||||||
* @throws kademlia.exceptions.ContentNotFoundException
|
* @throws kademlia.exceptions.ContentNotFoundException
|
||||||
*/
|
*/
|
||||||
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException
|
public synchronized StorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException
|
||||||
{
|
{
|
||||||
if (this.dht.contains(param))
|
if (this.dht.contains(param))
|
||||||
{
|
{
|
||||||
@ -340,7 +340,7 @@ public class KademliaNode
|
|||||||
*
|
*
|
||||||
* @throws java.io.IOException
|
* @throws java.io.IOException
|
||||||
*/
|
*/
|
||||||
public void refresh() throws IOException
|
public synchronized void refresh() throws IOException
|
||||||
{
|
{
|
||||||
new KadRefreshOperation(this.server, this, this.dht, this.config).execute();
|
new KadRefreshOperation(this.server, this, this.dht, this.config).execute();
|
||||||
}
|
}
|
||||||
@ -368,7 +368,7 @@ public class KademliaNode
|
|||||||
*
|
*
|
||||||
* @throws java.io.FileNotFoundException
|
* @throws java.io.FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public void shutdown(final boolean saveState) throws IOException
|
public synchronized void shutdown(final boolean saveState) throws IOException
|
||||||
{
|
{
|
||||||
/* Shut down the server */
|
/* Shut down the server */
|
||||||
this.server.shutdown();
|
this.server.shutdown();
|
||||||
@ -388,7 +388,7 @@ public class KademliaNode
|
|||||||
*
|
*
|
||||||
* @throws java.io.FileNotFoundException
|
* @throws java.io.FileNotFoundException
|
||||||
*/
|
*/
|
||||||
private void saveKadState() throws IOException
|
private synchronized void saveKadState() throws IOException
|
||||||
{
|
{
|
||||||
DataOutputStream dout;
|
DataOutputStream dout;
|
||||||
|
|
||||||
@ -459,7 +459,7 @@ public class KademliaNode
|
|||||||
* @return The string representation of this Kad instance
|
* @return The string representation of this Kad instance
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public synchronized String toString()
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder("\n\nPrinting Kad State for instance with owner: ");
|
StringBuilder sb = new StringBuilder("\n\nPrinting Kad State for instance with owner: ");
|
||||||
sb.append(this.ownerId);
|
sb.append(this.ownerId);
|
||||||
|
@ -10,7 +10,7 @@ import java.io.File;
|
|||||||
public class DefaultConfiguration implements KadConfiguration
|
public class DefaultConfiguration implements KadConfiguration
|
||||||
{
|
{
|
||||||
|
|
||||||
private final static long RESTORE_INTERVAL = 30 * 1000; // in milliseconds
|
private final static long RESTORE_INTERVAL = 60 * 1000; // in milliseconds
|
||||||
private final static long RESPONSE_TIMEOUT = 1500;
|
private final static long RESPONSE_TIMEOUT = 1500;
|
||||||
private final static long OPERATION_TIMEOUT = 3000;
|
private final static long OPERATION_TIMEOUT = 3000;
|
||||||
private final static int CONCURRENCY = 10;
|
private final static int CONCURRENCY = 10;
|
||||||
|
@ -25,7 +25,7 @@ import kademlia.util.serializer.KadSerializer;
|
|||||||
public class DHT
|
public class DHT
|
||||||
{
|
{
|
||||||
|
|
||||||
private transient StoredContentManager entriesManager;
|
private transient StoredContentManager contentManager;
|
||||||
private transient KadSerializer<StorageEntry> serializer = null;
|
private transient KadSerializer<StorageEntry> serializer = null;
|
||||||
private transient KadConfiguration config;
|
private transient KadConfiguration config;
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ public class DHT
|
|||||||
*/
|
*/
|
||||||
public final void initialize()
|
public final void initialize()
|
||||||
{
|
{
|
||||||
entriesManager = new StoredContentManager();
|
contentManager = new StoredContentManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,9 +83,9 @@ public class DHT
|
|||||||
public synchronized boolean store(StorageEntry content) throws IOException
|
public synchronized boolean store(StorageEntry content) throws IOException
|
||||||
{
|
{
|
||||||
/* Lets check if we have this content and it's the updated version */
|
/* Lets check if we have this content and it's the updated version */
|
||||||
if (this.entriesManager.contains(content.getContentMetadata()))
|
if (this.contentManager.contains(content.getContentMetadata()))
|
||||||
{
|
{
|
||||||
StorageEntryMetadata current = this.entriesManager.get(content.getContentMetadata());
|
StorageEntryMetadata current = this.contentManager.get(content.getContentMetadata());
|
||||||
|
|
||||||
/* update the last republished time */
|
/* update the last republished time */
|
||||||
current.updateLastRepublished();
|
current.updateLastRepublished();
|
||||||
@ -118,7 +118,7 @@ public class DHT
|
|||||||
{
|
{
|
||||||
//System.out.println("Adding new content.");
|
//System.out.println("Adding new content.");
|
||||||
/* Keep track of this content in the entries manager */
|
/* Keep track of this content in the entries manager */
|
||||||
StorageEntryMetadata sEntry = this.entriesManager.put(content.getContentMetadata());
|
StorageEntryMetadata sEntry = this.contentManager.put(content.getContentMetadata());
|
||||||
|
|
||||||
/* Now we store the content locally in a file */
|
/* Now we store the content locally in a file */
|
||||||
String contentStorageFolder = this.getContentStorageFolderName(content.getContentMetadata().getKey());
|
String contentStorageFolder = this.getContentStorageFolderName(content.getContentMetadata().getKey());
|
||||||
@ -155,7 +155,7 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @return A KadContent object
|
* @return A KadContent object
|
||||||
*/
|
*/
|
||||||
private StorageEntry retrieve(KademliaId key, int hashCode) throws FileNotFoundException, IOException, ClassNotFoundException
|
private synchronized StorageEntry retrieve(KademliaId key, int hashCode) throws FileNotFoundException, IOException, ClassNotFoundException
|
||||||
{
|
{
|
||||||
String folder = this.getContentStorageFolderName(key);
|
String folder = this.getContentStorageFolderName(key);
|
||||||
DataInputStream din = new DataInputStream(new FileInputStream(folder + File.separator + hashCode + ".kct"));
|
DataInputStream din = new DataInputStream(new FileInputStream(folder + File.separator + hashCode + ".kct"));
|
||||||
@ -169,9 +169,9 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @return boolean Whether any content exist that satisfy the criteria
|
* @return boolean Whether any content exist that satisfy the criteria
|
||||||
*/
|
*/
|
||||||
public boolean contains(GetParameter param)
|
public synchronized boolean contains(GetParameter param)
|
||||||
{
|
{
|
||||||
return this.entriesManager.contains(param);
|
return this.contentManager.contains(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,7 +183,7 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @throws java.io.IOException
|
* @throws java.io.IOException
|
||||||
*/
|
*/
|
||||||
public StorageEntry get(StorageEntryMetadata entry) throws IOException, NoSuchElementException
|
public synchronized StorageEntry get(StorageEntryMetadata entry) throws IOException, NoSuchElementException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -212,12 +212,12 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @throws java.io.IOException
|
* @throws java.io.IOException
|
||||||
*/
|
*/
|
||||||
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException
|
public synchronized StorageEntry get(GetParameter param) throws NoSuchElementException, IOException
|
||||||
{
|
{
|
||||||
/* Load a KadContent if any exist for the given criteria */
|
/* Load a KadContent if any exist for the given criteria */
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StorageEntryMetadata e = this.entriesManager.get(param);
|
StorageEntryMetadata e = this.contentManager.get(param);
|
||||||
return this.retrieve(e.getKey(), e.hashCode());
|
return this.retrieve(e.getKey(), e.hashCode());
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException e)
|
catch (FileNotFoundException e)
|
||||||
@ -241,17 +241,17 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @throws kademlia.exceptions.ContentNotFoundException
|
* @throws kademlia.exceptions.ContentNotFoundException
|
||||||
*/
|
*/
|
||||||
public void remove(KadContent content) throws ContentNotFoundException
|
public synchronized void remove(KadContent content) throws ContentNotFoundException
|
||||||
{
|
{
|
||||||
this.remove(new StorageEntryMetadata(content));
|
this.remove(new StorageEntryMetadata(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(StorageEntryMetadata entry) throws ContentNotFoundException
|
public synchronized void remove(StorageEntryMetadata entry) throws ContentNotFoundException
|
||||||
{
|
{
|
||||||
String folder = this.getContentStorageFolderName(entry.getKey());
|
String folder = this.getContentStorageFolderName(entry.getKey());
|
||||||
File file = new File(folder + File.separator + entry.hashCode() + ".kct");
|
File file = new File(folder + File.separator + entry.hashCode() + ".kct");
|
||||||
|
|
||||||
entriesManager.remove(entry);
|
contentManager.remove(entry);
|
||||||
|
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
{
|
{
|
||||||
@ -270,7 +270,7 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @return String The name of the folder
|
* @return String The name of the folder
|
||||||
*/
|
*/
|
||||||
private String getContentStorageFolderName(KademliaId key)
|
private synchronized String getContentStorageFolderName(KademliaId key)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Each content is stored in a folder named after the first 10 characters of the NodeId
|
* Each content is stored in a folder named after the first 10 characters of the NodeId
|
||||||
@ -294,7 +294,7 @@ public class DHT
|
|||||||
*/
|
*/
|
||||||
public synchronized List<StorageEntryMetadata> getStorageEntries()
|
public synchronized List<StorageEntryMetadata> getStorageEntries()
|
||||||
{
|
{
|
||||||
return entriesManager.getAllEntries();
|
return contentManager.getAllEntries();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -303,13 +303,13 @@ public class DHT
|
|||||||
*
|
*
|
||||||
* @param ientries The entries to add
|
* @param ientries The entries to add
|
||||||
*/
|
*/
|
||||||
public void putStorageEntries(List<StorageEntryMetadata> ientries)
|
public synchronized void putStorageEntries(List<StorageEntryMetadata> ientries)
|
||||||
{
|
{
|
||||||
for (StorageEntryMetadata e : ientries)
|
for (StorageEntryMetadata e : ientries)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.entriesManager.put(e);
|
this.contentManager.put(e);
|
||||||
}
|
}
|
||||||
catch (ContentExistException ex)
|
catch (ContentExistException ex)
|
||||||
{
|
{
|
||||||
@ -321,6 +321,6 @@ public class DHT
|
|||||||
@Override
|
@Override
|
||||||
public synchronized String toString()
|
public synchronized String toString()
|
||||||
{
|
{
|
||||||
return this.entriesManager.toString();
|
return this.contentManager.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class KadBucketImpl implements KadBucket
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insert(Contact c)
|
public synchronized void insert(Contact c)
|
||||||
{
|
{
|
||||||
if (this.contacts.contains(c))
|
if (this.contacts.contains(c))
|
||||||
{
|
{
|
||||||
@ -99,25 +99,25 @@ public class KadBucketImpl implements KadBucket
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insert(Node n)
|
public synchronized void insert(Node n)
|
||||||
{
|
{
|
||||||
this.insert(new Contact(n));
|
this.insert(new Contact(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsContact(Contact c)
|
public synchronized boolean containsContact(Contact c)
|
||||||
{
|
{
|
||||||
return this.contacts.contains(c);
|
return this.contacts.contains(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean containsNode(Node n)
|
public synchronized boolean containsNode(Node n)
|
||||||
{
|
{
|
||||||
return this.containsContact(new Contact(n));
|
return this.containsContact(new Contact(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeContact(Contact c)
|
public synchronized boolean removeContact(Contact c)
|
||||||
{
|
{
|
||||||
/* If the contact does not exist, then we failed to remove it */
|
/* If the contact does not exist, then we failed to remove it */
|
||||||
if (!this.contacts.contains(c))
|
if (!this.contacts.contains(c))
|
||||||
@ -142,7 +142,7 @@ public class KadBucketImpl implements KadBucket
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contact getFromContacts(Node n)
|
public synchronized Contact getFromContacts(Node n)
|
||||||
{
|
{
|
||||||
for (Contact c : this.contacts)
|
for (Contact c : this.contacts)
|
||||||
{
|
{
|
||||||
@ -156,7 +156,7 @@ public class KadBucketImpl implements KadBucket
|
|||||||
throw new NoSuchElementException("The contact does not exist in the contacts list.");
|
throw new NoSuchElementException("The contact does not exist in the contacts list.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contact removeFromContacts(Node n)
|
public synchronized Contact removeFromContacts(Node n)
|
||||||
{
|
{
|
||||||
for (Contact c : this.contacts)
|
for (Contact c : this.contacts)
|
||||||
{
|
{
|
||||||
@ -172,19 +172,19 @@ public class KadBucketImpl implements KadBucket
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeNode(Node n)
|
public synchronized boolean removeNode(Node n)
|
||||||
{
|
{
|
||||||
return this.removeContact(new Contact(n));
|
return this.removeContact(new Contact(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int numContacts()
|
public synchronized int numContacts()
|
||||||
{
|
{
|
||||||
return this.contacts.size();
|
return this.contacts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getDepth()
|
public synchronized int getDepth()
|
||||||
{
|
{
|
||||||
return this.depth;
|
return this.depth;
|
||||||
}
|
}
|
||||||
@ -198,7 +198,7 @@ public class KadBucketImpl implements KadBucket
|
|||||||
/**
|
/**
|
||||||
* When the bucket is filled, we keep extra contacts in the replacement cache.
|
* When the bucket is filled, we keep extra contacts in the replacement cache.
|
||||||
*/
|
*/
|
||||||
private void insertIntoReplacementCache(Contact c)
|
private synchronized void insertIntoReplacementCache(Contact c)
|
||||||
{
|
{
|
||||||
/* Just return if this contact is already in our replacement cache */
|
/* Just return if this contact is already in our replacement cache */
|
||||||
if (this.replacementCache.contains(c))
|
if (this.replacementCache.contains(c))
|
||||||
@ -223,7 +223,7 @@ public class KadBucketImpl implements KadBucket
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contact removeFromReplacementCache(Node n)
|
public synchronized Contact removeFromReplacementCache(Node n)
|
||||||
{
|
{
|
||||||
for (Contact c : this.replacementCache)
|
for (Contact c : this.replacementCache)
|
||||||
{
|
{
|
||||||
@ -239,7 +239,7 @@ public class KadBucketImpl implements KadBucket
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public synchronized String toString()
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder("Bucket at depth: ");
|
StringBuilder sb = new StringBuilder("Bucket at depth: ");
|
||||||
sb.append(this.depth);
|
sb.append(this.depth);
|
||||||
|
Loading…
Reference in New Issue
Block a user