* CapacityCalculator: Small adjustment for XOR distance to

break ties and encourage closeness
This commit is contained in:
zzz
2011-10-29 14:05:39 +00:00
parent a9698dd89e
commit 8f31713f6a
3 changed files with 20 additions and 0 deletions

View File

@ -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)

View File

@ -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?)