diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketImpl.java b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketImpl.java index 2653fe5d9..e99b51e84 100644 --- a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketImpl.java +++ b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketImpl.java @@ -53,7 +53,7 @@ class KBucketImpl implements KBucket { /** include if no bits higher than this bit (inclusive) are set */ private final int _end; private final int _max; - private final KBucketSet.KBucketTrimmer _trimmer; + private final KBucketTrimmer _trimmer; /** when did we last shake things up */ private long _lastChanged; private final I2PAppContext _context; @@ -62,7 +62,7 @@ class KBucketImpl implements KBucket { * All entries in this bucket will have at least one bit different * from us in the range [begin, end] inclusive. */ - public KBucketImpl(I2PAppContext context, int begin, int end, int max, KBucketSet.KBucketTrimmer trimmer) { + public KBucketImpl(I2PAppContext context, int begin, int end, int max, KBucketTrimmer trimmer) { if (begin > end) throw new IllegalArgumentException(begin + " > " + end); _context = context; diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java index b0b6d9055..3b8b09db6 100644 --- a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java +++ b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java @@ -768,70 +768,6 @@ public class KBucketSet { } } - /** - * Called when a kbucket can no longer be split and is too big - */ - public interface KBucketTrimmer { - /** - * Called from add() just before adding the entry. - * You may call getEntries() and/or remove() from here. - * Do NOT call add(). - * To always discard a newer entry, always return false. - * - * @param kbucket the kbucket that is now too big - * @return true to actually add the entry. - */ - public boolean trim(KBucket kbucket, K toAdd); - } - - /** - * Removes a random element. Not resistant to flooding. - */ - public static class RandomTrimmer implements KBucketTrimmer { - protected final I2PAppContext _ctx; - private final int _max; - - public RandomTrimmer(I2PAppContext ctx, int max) { - _ctx = ctx; - _max = max; - } - - public boolean trim(KBucket kbucket, T toAdd) { - List e = new ArrayList(kbucket.getEntries()); - int sz = e.size(); - // concurrency - if (sz < _max) - return true; - T toRemove = e.get(_ctx.random().nextInt(sz)); - return kbucket.remove(toRemove); - } - } - - /** - * Removes a random element, but only if the bucket hasn't changed in 5 minutes. - */ - public static class RandomIfOldTrimmer extends RandomTrimmer { - - public RandomIfOldTrimmer(I2PAppContext ctx, int max) { - super(ctx, max); - } - - public boolean trim(KBucket kbucket, T toAdd) { - if (kbucket.getLastChanged() > _ctx.clock().now() - 5*60*1000) - return false; - return super.trim(kbucket, toAdd); - } - } - - /** - * Removes nothing and always rejects the add. Flood resistant.. - */ - public static class RejectTrimmer implements KBucketTrimmer { - public boolean trim(KBucket kbucket, T toAdd) { - return false; - } - } - @Override public String toString() { StringBuilder buf = new StringBuilder(1024); diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/KBucketTrimmer.java b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketTrimmer.java new file mode 100644 index 000000000..b33f85ddb --- /dev/null +++ b/apps/i2psnark/java/src/net/i2p/kademlia/KBucketTrimmer.java @@ -0,0 +1,20 @@ +package net.i2p.kademlia; + +import net.i2p.data.SimpleDataStructure; + +/** + * Called when a kbucket can no longer be split and is too big + * @since 0.9.2 + */ +public interface KBucketTrimmer { + /** + * Called from add() just before adding the entry. + * You may call getEntries() and/or remove() from here. + * Do NOT call add(). + * To always discard a newer entry, always return false. + * + * @param kbucket the kbucket that is now too big + * @return true to actually add the entry. + */ + public boolean trim(KBucket kbucket, K toAdd); +} diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/RandomIfOldTrimmer.java b/apps/i2psnark/java/src/net/i2p/kademlia/RandomIfOldTrimmer.java new file mode 100644 index 000000000..ade28ce50 --- /dev/null +++ b/apps/i2psnark/java/src/net/i2p/kademlia/RandomIfOldTrimmer.java @@ -0,0 +1,21 @@ +package net.i2p.kademlia; + +import net.i2p.I2PAppContext; +import net.i2p.data.SimpleDataStructure; + +/** + * Removes a random element, but only if the bucket hasn't changed in 5 minutes. + * @since 0.9.2 + */ +public class RandomIfOldTrimmer extends RandomTrimmer { + + public RandomIfOldTrimmer(I2PAppContext ctx, int max) { + super(ctx, max); + } + + public boolean trim(KBucket kbucket, T toAdd) { + if (kbucket.getLastChanged() > _ctx.clock().now() - 5*60*1000) + return false; + return super.trim(kbucket, toAdd); + } +} diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/RandomTrimmer.java b/apps/i2psnark/java/src/net/i2p/kademlia/RandomTrimmer.java new file mode 100644 index 000000000..b0a802072 --- /dev/null +++ b/apps/i2psnark/java/src/net/i2p/kademlia/RandomTrimmer.java @@ -0,0 +1,31 @@ +package net.i2p.kademlia; + +import java.util.ArrayList; +import java.util.List; + +import net.i2p.I2PAppContext; +import net.i2p.data.SimpleDataStructure; + +/** + * Removes a random element. Not resistant to flooding. + * @since 0.9.2 + */ +public class RandomTrimmer implements KBucketTrimmer { + protected final I2PAppContext _ctx; + private final int _max; + + public RandomTrimmer(I2PAppContext ctx, int max) { + _ctx = ctx; + _max = max; + } + + public boolean trim(KBucket kbucket, T toAdd) { + List e = new ArrayList(kbucket.getEntries()); + int sz = e.size(); + // concurrency + if (sz < _max) + return true; + T toRemove = e.get(_ctx.random().nextInt(sz)); + return kbucket.remove(toRemove); + } +} diff --git a/apps/i2psnark/java/src/net/i2p/kademlia/RejectTrimmer.java b/apps/i2psnark/java/src/net/i2p/kademlia/RejectTrimmer.java new file mode 100644 index 000000000..2e29f28e2 --- /dev/null +++ b/apps/i2psnark/java/src/net/i2p/kademlia/RejectTrimmer.java @@ -0,0 +1,13 @@ +package net.i2p.kademlia; + +import net.i2p.data.SimpleDataStructure; + +/** + * Removes nothing and always rejects the add. Flood resistant.. + * @since 0.9.2 + */ +public class RejectTrimmer implements KBucketTrimmer { + public boolean trim(KBucket kbucket, T toAdd) { + return false; + } +} diff --git a/apps/i2psnark/java/src/org/klomp/snark/dht/KBTrimmer.java b/apps/i2psnark/java/src/org/klomp/snark/dht/KBTrimmer.java index 96809cbc2..872652c05 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/dht/KBTrimmer.java +++ b/apps/i2psnark/java/src/org/klomp/snark/dht/KBTrimmer.java @@ -4,12 +4,12 @@ import java.util.Set; import net.i2p.I2PAppContext; import net.i2p.kademlia.KBucket; -import net.i2p.kademlia.KBucketSet; +import net.i2p.kademlia.KBucketTrimmer; /** * Removes an element older than 15 minutes, but only if the bucket hasn't changed in 5 minutes. */ -class KBTrimmer implements KBucketSet.KBucketTrimmer { +class KBTrimmer implements KBucketTrimmer { private final I2PAppContext _ctx; private final int _max; diff --git a/history.txt b/history.txt index f7493794c..3280c0687 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,12 @@ +2012-08-297 zzz + * ClientManager: Cleanups + * i2psnark: + - Fix NPE on destroy() if init() failed + - Add new flood-resistant KBucket trim policy + - Limit received MsgID size + * NTCP: Reduce lock contention (ticket #697) + * RandomIterator: Workaround for Android bug (ticket #703) + 2012-08-27 zzz * i2psnark: - Notify threads awaiting DHT replies at shutdown diff --git a/router/java/src/net/i2p/router/RouterVersion.java b/router/java/src/net/i2p/router/RouterVersion.java index f5c926454..43dca9240 100644 --- a/router/java/src/net/i2p/router/RouterVersion.java +++ b/router/java/src/net/i2p/router/RouterVersion.java @@ -18,7 +18,7 @@ public class RouterVersion { /** deprecated */ public final static String ID = "Monotone"; public final static String VERSION = CoreVersion.VERSION; - public final static long BUILD = 20; + public final static long BUILD = 21; /** for example "-test" */ public final static String EXTRA = "";