Skip to content

Commit

Permalink
Fixed RateComparator to never return 0
Browse files Browse the repository at this point in the history
The rate comparator could return 0, but since Rate doesn't implement
equals() it would lead to strange behavior when building ordered sets
using a RateComparator. This could lead to the number of bound peers
available for unchoked to be incorrect which could prevent the start of
the download when the swarm gets large.

Signed-off-by: Maxime Petazzoni <[email protected]>
  • Loading branch information
Maxime Petazzoni committed Dec 3, 2012
1 parent ae5af44 commit 9f9d412
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/main/java/com/turn/ttorrent/client/peer/Rate.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.turn.ttorrent.client.peer;

import java.io.Serializable;
import java.util.Comparator;


Expand All @@ -29,7 +30,10 @@
*
* @author mpetazzoni
*/
public class Rate {
public class Rate implements Comparable<Rate> {

public static final Comparator<Rate> RATE_COMPARATOR =
new RateComparator();

private long bytes = 0;
private long reset = 0;
Expand Down Expand Up @@ -73,6 +77,11 @@ public synchronized void reset() {
this.last = this.reset;
}

@Override
public int compareTo(Rate other) {
return RATE_COMPARATOR.compare(this, other);
}

/**
* A rate comparator.
*
Expand All @@ -90,7 +99,10 @@ public synchronized void reset() {
*
* @author mpetazzoni
*/
public static class RateComparator implements Comparator<Rate> {
private static class RateComparator
implements Comparator<Rate>, Serializable {

private static final long serialVersionUID = 72460233003600L;

/**
* Compare two rates together.
Expand All @@ -108,11 +120,9 @@ public static class RateComparator implements Comparator<Rate> {
public int compare(Rate a, Rate b) {
if (a.get() > b.get()) {
return 1;
} else if (a.get() < b.get()) {
return -1;
}

return 0;
return -1;
}
}
}
19 changes: 13 additions & 6 deletions src/main/java/com/turn/ttorrent/client/peer/SharingPeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.turn.ttorrent.client.SharedTorrent;

import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -725,11 +726,14 @@ private void fireIOException(IOException ioe) {
* @author mpetazzoni
* @see Rate.RateComparator
*/
public static class DLRateComparator implements Comparator<SharingPeer> {
public static class DLRateComparator
implements Comparator<SharingPeer>, Serializable {

private static final long serialVersionUID = 96307229964730L;

@Override
public int compare(SharingPeer a, SharingPeer b) {
return new Rate.RateComparator()
.compare(a.getDLRate(), b.getDLRate());
return Rate.RATE_COMPARATOR.compare(a.getDLRate(), b.getDLRate());
}
}

Expand All @@ -743,11 +747,14 @@ public int compare(SharingPeer a, SharingPeer b) {
* @author mpetazzoni
* @see Rate.RateComparator
*/
public static class ULRateComparator implements Comparator<SharingPeer> {
public static class ULRateComparator
implements Comparator<SharingPeer>, Serializable {

private static final long serialVersionUID = 38794949747717L;

@Override
public int compare(SharingPeer a, SharingPeer b) {
return new Rate.RateComparator()
.compare(a.getULRate(), b.getULRate());
return Rate.RATE_COMPARATOR.compare(a.getULRate(), b.getULRate());
}
}

Expand Down

0 comments on commit 9f9d412

Please sign in to comment.