pieces);
+ @Override
+ public int countPieces(TorrentPeer peer) {
+ int count = 0;
+ for (int index = 0; index < pieces.size(); index++) {
+ final TorrentPiece piece = pieces.get(index);
+ if (manager.getContext().getBitfield().hasPiece(piece))
+ continue;
+ if (!peer.getBitfield().hasPiece(piece))
+ continue;
+ if (manager.getDownloadManager().isDownloading(piece))
+ continue;
+ count++;
+ }
+ return count;
+ }
}
diff --git a/src/main/java/net/torrent/torrent/piece/score/PieceRarenessScoreAlgorithm.java b/src/main/java/net/torrent/torrent/piece/score/PieceRarenessScoreAlgorithm.java
new file mode 100644
index 0000000..b608eea
--- /dev/null
+++ b/src/main/java/net/torrent/torrent/piece/score/PieceRarenessScoreAlgorithm.java
@@ -0,0 +1,34 @@
+/**
+ *
+ */
+package net.torrent.torrent.piece.score;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import net.torrent.torrent.TorrentPiece;
+import net.torrent.torrent.context.TorrentPeer;
+import net.torrent.torrent.context.TorrentSwarm;
+import net.torrent.util.SwarmCallback;
+
+/**
+ * @author Rogiel Josias Sulzbach
+ */
+public class PieceRarenessScoreAlgorithm implements PieceScoreAlgorithm {
+ @Override
+ public int calculate(TorrentSwarm swarm, final TorrentPiece piece) {
+ final AtomicInteger score = new AtomicInteger();
+ swarm.execute(new SwarmCallback() {
+ @Override
+ public void callback(TorrentPeer peer) {
+ int peerScore = (int) (Math.random() * 10); // bit of randomness
+ if (!peer.isAccessible()) {
+ peerScore += 100;
+ }
+ if (!peer.getBitfield().hasPiece(piece))
+ peerScore += 1000;
+ score.addAndGet(peerScore);
+ }
+ });
+ return score.get();
+ }
+}
diff --git a/src/main/java/net/torrent/torrent/piece/score/PieceScoreAlgorithm.java b/src/main/java/net/torrent/torrent/piece/score/PieceScoreAlgorithm.java
new file mode 100644
index 0000000..9041e29
--- /dev/null
+++ b/src/main/java/net/torrent/torrent/piece/score/PieceScoreAlgorithm.java
@@ -0,0 +1,23 @@
+/**
+ *
+ */
+package net.torrent.torrent.piece.score;
+
+import net.torrent.torrent.TorrentPiece;
+import net.torrent.torrent.context.TorrentSwarm;
+
+/**
+ * Computes the score of an piece inside the swarm.
+ *
+ * Pieces with higher scores will be more suitable to download than others.
+ *
+ * @author Rogiel Josias Sulzbach
+ */
+public interface PieceScoreAlgorithm {
+ /**
+ * Computes the score of an piece inside an swarm.
+ *
+ * @return
+ */
+ int calculate(TorrentSwarm swarm, TorrentPiece piece);
+}
diff --git a/src/main/java/net/torrent/torrent/piece/score/ScoredPieceComparator.java b/src/main/java/net/torrent/torrent/piece/score/ScoredPieceComparator.java
new file mode 100644
index 0000000..87154ae
--- /dev/null
+++ b/src/main/java/net/torrent/torrent/piece/score/ScoredPieceComparator.java
@@ -0,0 +1,27 @@
+/**
+ *
+ */
+package net.torrent.torrent.piece.score;
+
+import java.util.Comparator;
+
+import net.torrent.torrent.TorrentPiece;
+import net.torrent.torrent.context.TorrentSwarm;
+
+/**
+ * @author Rogiel Josias Sulzbach
+ */
+public class ScoredPieceComparator implements Comparator {
+ private final PieceScoreAlgorithm score;
+ private final TorrentSwarm swarm;
+
+ public ScoredPieceComparator(TorrentSwarm swarm, PieceScoreAlgorithm score) {
+ this.swarm = swarm;
+ this.score = score;
+ }
+
+ @Override
+ public int compare(TorrentPiece piece1, TorrentPiece piece2) {
+ return score.calculate(swarm, piece1) - score.calculate(swarm, piece2);
+ }
+}
diff --git a/src/main/java/net/torrent/util/PeerWirePeerCallback.java b/src/main/java/net/torrent/util/PeerWirePeerCallback.java
new file mode 100644
index 0000000..ba1d82b
--- /dev/null
+++ b/src/main/java/net/torrent/util/PeerWirePeerCallback.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2011 Rogiel Josias Sulzbach
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.torrent.util;
+
+import net.torrent.protocol.peerwire.PeerWirePeer;
+import net.torrent.protocol.peerwire.manager.PeerManager;
+
+/**
+ * Callback used in {@link PeerManager#execute(PeerCallback)}
+ *
+ * @author Rogiel Josias Sulzbach
+ */
+public interface PeerWirePeerCallback {
+ /**
+ * Execute the desired action for peer
+ *
+ * @param peer
+ * the peer
+ */
+ void callback(PeerWirePeer peer);
+}
diff --git a/src/main/java/net/torrent/util/SwarmCallback.java b/src/main/java/net/torrent/util/SwarmCallback.java
new file mode 100644
index 0000000..82cf055
--- /dev/null
+++ b/src/main/java/net/torrent/util/SwarmCallback.java
@@ -0,0 +1,22 @@
+/**
+ *
+ */
+package net.torrent.util;
+
+import net.torrent.torrent.context.TorrentPeer;
+import net.torrent.torrent.context.TorrentSwarm;
+
+/**
+ * Callback used in {@link TorrentSwarm#execute(SwarmCallback)}
+ *
+ * @author Rogiel Josias Sulzbach
+ */
+public interface SwarmCallback {
+ /**
+ * Execute the desired action for peer
+ *
+ * @param peer
+ * the peer
+ */
+ void callback(TorrentPeer peer);
+}