Changeset d2a7af2


Ignore:
Timestamp:
Aug 29, 2012 2:05:02 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
17b4ab6
Parents:
d05f1ca
Message:

refactor trimmers to their own files

Files:
4 added
5 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/net/i2p/kademlia/KBucketImpl.java

    rd05f1ca rd2a7af2  
    5454    private final int _end;
    5555    private final int _max;
    56     private final KBucketSet.KBucketTrimmer _trimmer;
     56    private final KBucketTrimmer _trimmer;
    5757    /** when did we last shake things up */
    5858    private long _lastChanged;
     
    6363     *  from us in the range [begin, end] inclusive.
    6464     */
    65     public KBucketImpl(I2PAppContext context, int begin, int end, int max, KBucketSet.KBucketTrimmer trimmer) {
     65    public KBucketImpl(I2PAppContext context, int begin, int end, int max, KBucketTrimmer trimmer) {
    6666        if (begin > end)
    6767            throw new IllegalArgumentException(begin + " > " + end);
  • apps/i2psnark/java/src/net/i2p/kademlia/KBucketSet.java

    rd05f1ca rd2a7af2  
    769769    }
    770770
    771     /**
    772      *  Called when a kbucket can no longer be split and is too big
    773      */
    774     public interface KBucketTrimmer<K extends SimpleDataStructure> {
    775         /**
    776          *  Called from add() just before adding the entry.
    777          *  You may call getEntries() and/or remove() from here.
    778          *  Do NOT call add().
    779          *  To always discard a newer entry, always return false.
    780          *
    781          *  @param kbucket the kbucket that is now too big
    782          *  @return true to actually add the entry.
    783          */
    784         public boolean trim(KBucket<K> kbucket, K toAdd);
    785     }
    786 
    787     /**
    788      *  Removes a random element. Not resistant to flooding.
    789      */
    790     public static class RandomTrimmer<T extends SimpleDataStructure> implements KBucketTrimmer<T> {
    791         protected final I2PAppContext _ctx;
    792         private final int _max;
    793 
    794         public RandomTrimmer(I2PAppContext ctx, int max) {
    795             _ctx = ctx;
    796             _max = max;
    797         }
    798 
    799         public boolean trim(KBucket<T> kbucket, T toAdd) {
    800             List<T> e = new ArrayList(kbucket.getEntries());
    801             int sz = e.size();
    802             // concurrency
    803             if (sz < _max)
    804                 return true;
    805             T toRemove = e.get(_ctx.random().nextInt(sz));
    806             return kbucket.remove(toRemove);
    807         }
    808     }
    809 
    810     /**
    811      *  Removes a random element, but only if the bucket hasn't changed in 5 minutes.
    812      */
    813     public static class RandomIfOldTrimmer<T extends SimpleDataStructure> extends RandomTrimmer<T> {
    814 
    815         public RandomIfOldTrimmer(I2PAppContext ctx, int max) {
    816             super(ctx, max);
    817         }
    818 
    819         public boolean trim(KBucket<T> kbucket, T toAdd) {
    820             if (kbucket.getLastChanged() > _ctx.clock().now() - 5*60*1000)
    821                 return false;
    822             return super.trim(kbucket, toAdd);
    823         }
    824     }
    825 
    826     /**
    827      *  Removes nothing and always rejects the add. Flood resistant..
    828      */
    829     public static class RejectTrimmer<T extends SimpleDataStructure> implements KBucketTrimmer<T> {
    830         public boolean trim(KBucket<T> kbucket, T toAdd) {
    831             return false;
    832         }
    833     }
    834 
    835771    @Override
    836772    public String toString() {
  • apps/i2psnark/java/src/org/klomp/snark/dht/KBTrimmer.java

    rd05f1ca rd2a7af2  
    55import net.i2p.I2PAppContext;
    66import net.i2p.kademlia.KBucket;
    7 import net.i2p.kademlia.KBucketSet;
     7import net.i2p.kademlia.KBucketTrimmer;
    88
    99/**
    1010 *  Removes an element older than 15 minutes, but only if the bucket hasn't changed in 5 minutes.
    1111 */
    12 class KBTrimmer implements KBucketSet.KBucketTrimmer<NID> {
     12class KBTrimmer implements KBucketTrimmer<NID> {
    1313    private final I2PAppContext _ctx;
    1414    private final int _max;
  • history.txt

    rd05f1ca rd2a7af2  
     12012-08-297 zzz
     2 * ClientManager: Cleanups
     3 * i2psnark:
     4   - Fix NPE on destroy() if init() failed
     5   - Add new flood-resistant KBucket trim policy
     6   - Limit received MsgID size
     7 * NTCP: Reduce lock contention (ticket #697)
     8 * RandomIterator: Workaround for Android bug (ticket #703)
     9
    1102012-08-27 zzz
    211 * i2psnark:
  • router/java/src/net/i2p/router/RouterVersion.java

    rd05f1ca rd2a7af2  
    1919    public final static String ID = "Monotone";
    2020    public final static String VERSION = CoreVersion.VERSION;
    21     public final static long BUILD = 20;
     21    public final static long BUILD = 21;
    2222
    2323    /** for example "-test" */
Note: See TracChangeset for help on using the changeset viewer.