From 8f31713f6a8ad7d012d7a9b14e7e934d7553e52a Mon Sep 17 00:00:00 2001 From: zzz Date: Sat, 29 Oct 2011 14:05:39 +0000 Subject: [PATCH] * CapacityCalculator: Small adjustment for XOR distance to break ties and encourage closeness --- history.txt | 2 ++ .../router/peermanager/CapacityCalculator.java | 3 +++ .../net/i2p/router/peermanager/PeerProfile.java | 15 +++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/history.txt b/history.txt index 07af3dc81..9579fcd51 100644 --- a/history.txt +++ b/history.txt @@ -1,5 +1,7 @@ 2011-10-29 zzz * BuildHandler: Add router.buildHandlerThreads config setting + * CapacityCalculator: Small adjustment for XOR distance to + break ties and encourage closeness * ProfileOrganizer: Reduce min expire time * SSU: Limit max peers to use as introducers diff --git a/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java b/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java index 76a6f1b58..839674967 100644 --- a/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java +++ b/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java @@ -21,6 +21,7 @@ class CapacityCalculator { private static final double BONUS_NEW = 1.25; private static final double BONUS_ESTABLISHED = 1; private static final double BONUS_SAME_COUNTRY = .85; + private static final double BONUS_XOR = .25; private static final double PENALTY_UNREACHABLE = 2; public static double calc(PeerProfile profile) { @@ -69,6 +70,8 @@ class CapacityCalculator { // penalize unreachable peers if (profile.wasUnreachable()) capacity -= PENALTY_UNREACHABLE; + // a tiny tweak to break ties and encourage closeness, -.25 to +.25 + capacity -= profile.getXORDistance() * (BONUS_XOR / 128); capacity += profile.getCapacityBonus(); if (capacity < 0) diff --git a/router/java/src/net/i2p/router/peermanager/PeerProfile.java b/router/java/src/net/i2p/router/peermanager/PeerProfile.java index e21c7c383..2b57f377b 100644 --- a/router/java/src/net/i2p/router/peermanager/PeerProfile.java +++ b/router/java/src/net/i2p/router/peermanager/PeerProfile.java @@ -58,6 +58,7 @@ public class PeerProfile { private boolean _expanded; private boolean _expandedDB; private int _consecutiveShitlists; + private final int _distance; public PeerProfile(RouterContext context, Hash peer) { this(context, peer, true); @@ -74,6 +75,11 @@ public class PeerProfile { // if it is false, so all need to be fixed before we can have non-expanded profiles if (expand) expandProfile(); + Hash us = _context.routerHash(); + if (us != null) + _distance = ((_peer.getData()[0] & 0xff) ^ (us.getData()[0] & 0xff)) - 127; + else + _distance = 0; } /** what peer is being profiled */ @@ -115,6 +121,15 @@ public class PeerProfile { return us != null && us.equals(_context.commSystem().getCountry(_peer)); } + /** + * For now, just a one-byte comparison + * @return -127 to +128, lower is closer + * @since 0.8.11 + */ + public int getXORDistance() { + return _distance; + } + /** * Is this peer active at the moment (sending/receiving messages within the * given period?)