Update of /cvsroot/freenet/freenet/src/freenet/node/rt
In directory sc8-pr-cvs1:/tmp/cvs-serv15670/src/freenet/node/rt
Modified Files:
ResponseTimeEstimator.java
Log Message:
Use Math.max() and Math.min() instead of homegrown versions of the same thing.
Removed unused serialize() method.
Moved keyspace wrapping handling into a method.
Move misc. code fragments into separate methods.
Indenting.
Index: ResponseTimeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/ResponseTimeEstimator.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -w -r1.21 -r1.22
--- ResponseTimeEstimator.java 30 Oct 2003 13:09:57 -0000 1.21
+++ ResponseTimeEstimator.java 31 Oct 2003 13:53:41 -0000 1.22
@@ -147,8 +147,13 @@
public ResponseTimeEstimator(int initTime, int accuracy) {
this(accuracy);
if(initTime < 0) throw new IllegalArgumentException("negative initTime");
- for(int i=0;i<accuracy;i++) {
- time[i] = initTime;
+ setAllTimes(initTime);
+ }
+
+ //Sets all times to the supplied value
+ private void setAllTimes(int newtime) {
+ for(int i=0;i<time.length;i++) {
+ time[i] = newtime;
}
}
@@ -195,12 +200,16 @@
// Let's evenly distribute the keys across the whole
// keyspace and leave the time values at zero.
+ dristributeKeysEvenly();
+ recent = new RecentReports();
+ }
+ //distributes the keys evenly all through the keyspace
+ private void dristributeKeysEvenly() {
BigInteger a = keyspace.subtract(BigInteger.ONE);
- BigInteger b = a.divide(BigInteger.valueOf(accuracy));
+ BigInteger b = a.divide(BigInteger.valueOf(key.length));
for (int i = key.length; --i >= 0; a = a.subtract(b))
key[i] = a;
- recent = new RecentReports();
}
// Return offset of the biggest key that's still less than n.
@@ -230,8 +239,7 @@
b = ob;
}
if(b.length > KEYBYTES) {
- Core.logger.log(this, "Very long key detected!: "+k,
- new Exception("debug"), Logger.NORMAL);
+ Core.logger.log(this, "Very long key detected!: " + k, new
Exception("debug"), Logger.NORMAL);
byte[] ob = new byte[KEYBYTES];
System.arraycopy(b,0,ob,0,KEYBYTES);
}
@@ -277,14 +285,10 @@
if (logDEBUG)
Core.logger.log(this, "reportDecreasing(" +
lowerBound.toString(16) + "," + upperBound.toString(16) + "," + center.toString(16) +
"," + initialSteps + "," + lowerBoundPos + "," + upperBoundPos + ")", new
Exception("debug"), Logger.DEBUG);
dumpLog();
- if (upperBoundPos >= key.length - 1)
- upperBoundPos = key.length - 1;
- if (lowerBoundPos >= key.length - 1)
- lowerBoundPos = key.length - 1;
- if (lowerBoundPos < 0)
- lowerBoundPos = 0;
- if (upperBoundPos < 0)
- upperBoundPos = 0;
+ upperBoundPos = Math.min(upperBoundPos,key.length - 1);
+ lowerBoundPos = Math.min(lowerBoundPos,key.length - 1);
+ lowerBoundPos = Math.max(lowerBoundPos,0);
+ upperBoundPos = Math.max(upperBoundPos,0);
if (usec < 0)
throw new IllegalArgumentException("usec " + usec + "
negative!");
// Loop from lowerBoundPos to upperBoundPos, inclusive
@@ -308,12 +312,9 @@
Core.logger.log(this, "time[" + i + "] = " + time[i] +
" - NEGATIVE TIME!", Logger.ERROR);
if (logDEBUG)
Core.logger.log(this, "Trying key[" + i + "]: " +
key[i].toString(16) + "," + time[i], Logger.DEBUG);
- if (sensitivity[i] > SENSITIVITY_MAX)
- sensitivity[i] = SENSITIVITY_MAX;
+ ensureSensitivityBelowMax(i);
double sens = sensitivity[i];
- BigInteger diff = key[i].subtract(center);
- if (diff.signum() == -1)
- diff = diff.add(keyspace);
+ BigInteger diff = handleWrap(key[i].subtract(center));
// Result = old position + ((old position * sensitivity) /
(sensitivity + 1)
// >> shiftAmount
@@ -331,11 +332,8 @@
BigDecimal resultDiff = fracDiff.divide(fracSens.add(DEC_ONE),
BigDecimal.ROUND_DOWN);
diff = resultDiff.toBigInteger().shiftRight(shiftAmount);
diff = diff.multiply(THREE).divide(FOUR);
- key[i] = key[i].subtract(diff);
- if (key[i].signum() == -1)
- key[i] = key[i].add(keyspace);
- if (key[i].compareTo(keyspace) == 1)
- key[i] = key[i].subtract(keyspace);
+ key[i] = handleWrap(key[i].subtract(diff));
+
int timediff = usec - time[i];
if (logDEBUG)
Core.logger.log(this, "usec=" + usec + ", time[" + i +
"]=" + time[i] + ", timediff=" + timediff, Logger.DEBUG);
@@ -363,6 +361,19 @@
return shiftAmount;
}
+ //Handles keyspace wrapping, always returns a properly wrapped value
+ private BigInteger handleWrap(BigInteger key) {
+ //TODO: Can we really jump two whole keyspaces in
+ //one calculation anywhere? If not then the recursion
+ //is unnessecary
+ if (key.signum() == -1)
//If the value has fallen below 0
+ return handleWrap(key.add(keyspace)); //Then move
upwards (at least) one keyspace
+ else if (key.compareTo(keyspace) == 1) //else, if the
value has become larger than the keyspace max
+ return handleWrap(key.subtract(keyspace)); //then move
downwards (at least) one keyspace
+ else
+ return key;
//else do nothing
+ }
+
protected synchronized int reportIncreasing(BigInteger lowerBound,
BigInteger upperBound,
BigInteger center,
@@ -376,14 +387,10 @@
initialSteps+","+lowerBoundPos+","+upperBoundPos+")",
new Exception("debug"), Logger.DEBUG);
dumpLog();
- if(upperBoundPos >= key.length-1)
- upperBoundPos = key.length - 1;
- if(lowerBoundPos >= key.length-1)
- lowerBoundPos = key.length - 1;
- if(lowerBoundPos < 0)
- lowerBoundPos = 0;
- if(upperBoundPos < 0)
- upperBoundPos = 0;
+ upperBoundPos = Math.min(upperBoundPos,key.length - 1);
+ lowerBoundPos = Math.min(lowerBoundPos,key.length - 1);
+ lowerBoundPos = Math.max(lowerBoundPos,0);
+ upperBoundPos = Math.max(upperBoundPos,0);
if(usec < 0) throw new IllegalArgumentException("usec "+usec+" negative!");
// Loop from lowerBoundPos to upperBoundPos, inclusive
// We are increasing
@@ -406,11 +413,9 @@
if(time[i] < 0)
Core.logger.log(this, "time["+i+"] = "+time[i]+
" - NEGATIVE TIME!", Logger.ERROR);
- if(sensitivity[i] > SENSITIVITY_MAX)
- sensitivity[i] = SENSITIVITY_MAX;
+ ensureSensitivityBelowMax(i);
double sens = sensitivity[i];
- BigInteger diff = center.subtract(key[i]);
- if(diff.signum() == -1) diff = diff.add(keyspace);
+ BigInteger diff = handleWrap(center.subtract(key[i]));
BigDecimal fracDiff = new BigDecimal(diff);
BigDecimal fracSens = new BigDecimal(sens);
BigDecimal fracOrig = new BigDecimal(center);
@@ -418,11 +423,7 @@
BigDecimal.ROUND_DOWN);
diff = resultDiff.toBigInteger().shiftRight(shiftAmount);
diff = diff.multiply(THREE).divide(FOUR);
- key[i] = key[i].add(diff.shiftRight(shiftAmount));
- if(key[i].signum() == -1)
- key[i] = key[i].add(keyspace);
- if(key[i].compareTo(keyspace) == 1)
- key[i] = key[i].subtract(keyspace);
+ key[i] = handleWrap(key[i].add(diff.shiftRight(shiftAmount)));
int timediff = usec - time[i];
double fracTimediff = (double)timediff;
double resultTimediff = fracTimediff/(sens+1.0);
@@ -441,6 +442,11 @@
dumpLog();
return shiftAmount;
}
+ //Ensures that the sensitivity at the given position is below the allowed
threshold
+ private void ensureSensitivityBelowMax(int index) {
+ if(sensitivity[index] > SENSITIVITY_MAX)
+ sensitivity[index] = SENSITIVITY_MAX;
+ }
public synchronized void report(Key k, int usec) {
logDEBUG = Core.logger.shouldLog(Logger.DEBUG,this);
@@ -666,7 +672,7 @@
// 2^31 microseconds is 36 minutes.
return i.intValue();
}
-
+/*
public byte[] serialize() {
byte[] b = new byte[key.length * BYTES];
@@ -690,6 +696,7 @@
return b;
}
+ */
public void print() {
for (int i = 0; i < key.length; i++)
@@ -765,7 +772,7 @@
public long highestRaw() {
int highest = 0;
for(int x=0;x<time.length;x++) {
- if(time[x] > highest) highest = time[x];
+ highest = Math.max(time[x],highest);
}
return highest;
}
@@ -773,7 +780,7 @@
public long lowestRaw() {
int lowest = Integer.MAX_VALUE;
for(int x=0;x<time.length;x++) {
- if(time[x] < lowest) lowest = time[x];
+ lowest = Math.min(time[x],lowest);
}
return lowest;
}
_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs