From 6bf0be8fca8eac397834d789857cab4eb1209a91 Mon Sep 17 00:00:00 2001 From: rogiel Date: Thu, 28 Apr 2011 14:30:33 -0300 Subject: [PATCH] Scoring algorithm --- pom.xml | 30 ++- .../algorithm/impl/TorrentStdAlgorithm.java | 12 +- .../impl/TorrentStdInterestAlgorithm.java | 21 ++- .../TorrentStdPieceDownloadAlgorithm.java | 12 +- .../handler/PeerWireAlgorithmHandler.java | 4 +- .../peerwire/manager/PeerManager.java | 8 +- .../torrent/context/TorrentContext.java | 70 +------ .../torrent/torrent/context/TorrentSwarm.java | 175 ++++++++++++++++++ .../torrent/torrent/piece/PieceSelector.java | 7 + .../torrent/piece/ScoredPieceSelector.java | 77 ++++++++ .../piece/SortedListPieceSelector.java | 34 +++- .../score/PieceRarenessScoreAlgorithm.java | 34 ++++ .../piece/score/PieceScoreAlgorithm.java | 23 +++ .../piece/score/ScoredPieceComparator.java | 27 +++ .../torrent/util/PeerWirePeerCallback.java | 34 ++++ .../java/net/torrent/util/SwarmCallback.java | 22 +++ 16 files changed, 498 insertions(+), 92 deletions(-) create mode 100644 src/main/java/net/torrent/torrent/context/TorrentSwarm.java create mode 100644 src/main/java/net/torrent/torrent/piece/ScoredPieceSelector.java create mode 100644 src/main/java/net/torrent/torrent/piece/score/PieceRarenessScoreAlgorithm.java create mode 100644 src/main/java/net/torrent/torrent/piece/score/PieceScoreAlgorithm.java create mode 100644 src/main/java/net/torrent/torrent/piece/score/ScoredPieceComparator.java create mode 100644 src/main/java/net/torrent/util/PeerWirePeerCallback.java create mode 100644 src/main/java/net/torrent/util/SwarmCallback.java diff --git a/pom.xml b/pom.xml index 57397b9..8570a1c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ libtorrent jar libtorrent - 1.0 + 1.0.0-alpha2 Java library used for downloading and uploading torrent files http://code.google.com/p/libtorrent-java @@ -97,6 +97,34 @@ + + org.apache.maven.plugins + maven-pdf-plugin + 1.1 + + + pdf + package + + pdf + + + + + + org.apache.maven.plugins + maven-clean-plugin + 2.4.1 + + + clean + package + + clean + + + + diff --git a/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdAlgorithm.java b/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdAlgorithm.java index 9b63f01..507eefe 100644 --- a/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdAlgorithm.java +++ b/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdAlgorithm.java @@ -21,6 +21,8 @@ import net.torrent.protocol.algorithm.TorrentPeerAlgorithm; import net.torrent.protocol.algorithm.TorrentPieceDownloadAlgorithm; import net.torrent.protocol.algorithm.TorrentPieceUploadAlgorithm; import net.torrent.protocol.peerwire.manager.TorrentManager; +import net.torrent.torrent.piece.PieceSelector; +import net.torrent.torrent.piece.ScoredPieceSelector; /** * Standard torrent algorithm @@ -28,15 +30,21 @@ import net.torrent.protocol.peerwire.manager.TorrentManager; * @author Rogiel Josias Sulzbach */ public class TorrentStdAlgorithm implements TorrentAlgorithm { + private final PieceSelector pieceSelector; + private final TorrentPeerAlgorithm peerAlgorithm; private final TorrentInterestAlgorithm interestAlgorithm; private final TorrentPieceDownloadAlgorithm downloadAlgorithm; private final TorrentPieceUploadAlgorithm uploadAlgorithm; public TorrentStdAlgorithm(final TorrentManager manager) { + pieceSelector = new ScoredPieceSelector(manager); + peerAlgorithm = new TorrentStdPeerAlgorithm(manager); - interestAlgorithm = new TorrentStdInterestAlgorithm(manager); - downloadAlgorithm = new TorrentStdPieceDownloadAlgorithm(manager); + interestAlgorithm = new TorrentStdInterestAlgorithm(manager, + pieceSelector); + downloadAlgorithm = new TorrentStdPieceDownloadAlgorithm(manager, + pieceSelector); uploadAlgorithm = new TorrentStdPieceUploadAlgorithm(manager); } diff --git a/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdInterestAlgorithm.java b/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdInterestAlgorithm.java index cb512b2..d185f8b 100644 --- a/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdInterestAlgorithm.java +++ b/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdInterestAlgorithm.java @@ -20,6 +20,7 @@ import net.torrent.protocol.peerwire.manager.TorrentManager; import net.torrent.torrent.context.TorrentPeer; import net.torrent.torrent.context.TorrentPeer.ChokingState; import net.torrent.torrent.context.TorrentPeer.InterestState; +import net.torrent.torrent.piece.PieceSelector; /** * Standard torrent interest algorithm @@ -29,15 +30,29 @@ import net.torrent.torrent.context.TorrentPeer.InterestState; public class TorrentStdInterestAlgorithm implements TorrentInterestAlgorithm { @SuppressWarnings("unused") private final TorrentManager manager; + private final PieceSelector selector; - public TorrentStdInterestAlgorithm(TorrentManager manager) { + /** + * Creates a new instance + * + * @param manager + * the manager + * @param pieceSelector + * the piece selector + */ + public TorrentStdInterestAlgorithm(TorrentManager manager, + PieceSelector pieceSelector) { this.manager = manager; + this.selector = pieceSelector; } @Override public InterestState interested(TorrentPeer peer) { - // if(peer.getPort() == 25944) - // return InterestState.UNINTERESTED; + int pieces = selector.countPieces(peer); + if(pieces >= 5) + return InterestState.INTERESTED; + + return InterestState.INTERESTED; } diff --git a/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdPieceDownloadAlgorithm.java b/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdPieceDownloadAlgorithm.java index 084138b..be50b7e 100644 --- a/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdPieceDownloadAlgorithm.java +++ b/src/main/java/net/torrent/protocol/algorithm/impl/TorrentStdPieceDownloadAlgorithm.java @@ -24,7 +24,6 @@ import net.torrent.torrent.TorrentPart; import net.torrent.torrent.TorrentPiece; import net.torrent.torrent.context.TorrentPeer; import net.torrent.torrent.piece.PieceSelector; -import net.torrent.torrent.piece.RandomPieceSelector; /** * This standard implementation of {@link TorrentPieceDownloadAlgorithm} chooses @@ -39,8 +38,6 @@ public class TorrentStdPieceDownloadAlgorithm implements * The torrent manager */ private final TorrentManager manager; - // private final TorrentContext context; - // private final Torrent torrent; /** * This selector is used to find the next piece to be downloaded. Parts are @@ -61,12 +58,13 @@ public class TorrentStdPieceDownloadAlgorithm implements * @param manager * the torrent manager instance. With this object is possible to * retrieve current downloads/uploads and connections. + * @param pieceSelector + * the piece selector */ - public TorrentStdPieceDownloadAlgorithm(TorrentManager manager) { + public TorrentStdPieceDownloadAlgorithm(TorrentManager manager, + PieceSelector pieceSelector) { this.manager = manager; - // this.context = this.manager.getContext(); - // this.torrent = this.manager.getTorrent(); - selector = new RandomPieceSelector(manager); + this.selector = pieceSelector; } @Override diff --git a/src/main/java/net/torrent/protocol/peerwire/handler/PeerWireAlgorithmHandler.java b/src/main/java/net/torrent/protocol/peerwire/handler/PeerWireAlgorithmHandler.java index 27e2e10..7016526 100644 --- a/src/main/java/net/torrent/protocol/peerwire/handler/PeerWireAlgorithmHandler.java +++ b/src/main/java/net/torrent/protocol/peerwire/handler/PeerWireAlgorithmHandler.java @@ -45,7 +45,7 @@ import net.torrent.torrent.context.TorrentPeer; import net.torrent.torrent.context.TorrentPeer.ChokingState; import net.torrent.torrent.context.TorrentPeer.InterestState; import net.torrent.torrent.context.TorrentPeerCapabilities.TorrentPeerCapability; -import net.torrent.util.PeerCallback; +import net.torrent.util.PeerWirePeerCallback; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelHandlerContext; @@ -343,7 +343,7 @@ public class PeerWireAlgorithmHandler extends IdleStateAwareChannelHandler { if (datastore.checksum(part.getPiece())) { manager.getContext().getBitfield().setPiece(part.getPiece(), true); - manager.getPeerManager().executeActive(new PeerCallback() { + manager.getPeerManager().executeActive(new PeerWirePeerCallback() { @Override public void callback(PeerWirePeer peer) { peer.have(part.getPiece().getIndex()); diff --git a/src/main/java/net/torrent/protocol/peerwire/manager/PeerManager.java b/src/main/java/net/torrent/protocol/peerwire/manager/PeerManager.java index bd621c5..5fe308b 100644 --- a/src/main/java/net/torrent/protocol/peerwire/manager/PeerManager.java +++ b/src/main/java/net/torrent/protocol/peerwire/manager/PeerManager.java @@ -25,7 +25,7 @@ import java.util.Set; import net.torrent.protocol.peerwire.PeerWirePeer; import net.torrent.torrent.context.TorrentContext; import net.torrent.torrent.context.TorrentPeer; -import net.torrent.util.PeerCallback; +import net.torrent.util.PeerWirePeerCallback; import org.jboss.netty.channel.Channel; @@ -140,21 +140,21 @@ public class PeerManager implements Iterable { return Collections.unmodifiableSet(inactivePeers.keySet()); } - public void executeActive(PeerCallback callback) { + public void executeActive(PeerWirePeerCallback callback) { for (final Entry entry : this.activePeers .entrySet()) { callback.callback(entry.getValue()); } } - public void executeInactive(PeerCallback callback) { + public void executeInactive(PeerWirePeerCallback callback) { for (final Entry entry : this.inactivePeers .entrySet()) { callback.callback(entry.getValue()); } } - public void execute(PeerCallback callback) { + public void execute(PeerWirePeerCallback callback) { executeActive(callback); executeInactive(callback); } diff --git a/src/main/java/net/torrent/torrent/context/TorrentContext.java b/src/main/java/net/torrent/torrent/context/TorrentContext.java index 8d86ac1..8dc144a 100644 --- a/src/main/java/net/torrent/torrent/context/TorrentContext.java +++ b/src/main/java/net/torrent/torrent/context/TorrentContext.java @@ -40,7 +40,7 @@ public class TorrentContext { private final TorrentPeerCapabilities capabilites = new TorrentPeerCapabilities( TorrentPeerCapability.DHT, TorrentPeerCapability.FAST_PEERS); - private final Set peers = new HashSet(); + private final TorrentSwarm swarm = new TorrentSwarm(this); /** * Unknown peers does not have their IDs, consequently they cannot be * queried using their Id and must be done through IP. @@ -103,8 +103,8 @@ public class TorrentContext { * * @return the list of peers */ - public Set getPeers() { - return Collections.unmodifiableSet(peers); + public TorrentSwarm getSwarm() { + return swarm; } /** @@ -116,73 +116,15 @@ public class TorrentContext { return Collections.unmodifiableSet(unknownPeers); } - /** - * Get an peer by its PeerID - * - * @param peerId - * the peer id - * @return the found peer. Null if not found. - */ public TorrentPeer getPeer(TorrentPeerID peerId) { - for (final TorrentPeer peer : peers) { - if (peer.getPeerID().equals(peerId)) - return peer; - } - return null; + return swarm.getPeer(peerId); } - /** - * Get an peer by its address - * - * @param address - * the address - * @return the found peer. Null if not found. - */ public TorrentPeer getPeer(InetSocketAddress address) { - for (final TorrentPeer peer : peers) { - if (peer.getSocketAddress().equals(address)) - return peer; - } - return null; + return swarm.getPeer(address); } - /** - * Lookup for a peer first by its id, then by address, if still not found, - * creates a new entry. - * - * @param id - * the peer id - * @param address - * the address - * @return the found or newly created peer - */ public TorrentPeer getPeer(TorrentPeerID id, InetSocketAddress address) { - TorrentPeer peer = getPeer(id); - if (peer == null) { - peer = getPeer(address); - if (peer != null) { - if (peers.remove(peer)) - peer = peer.createWithID(id); - } else { - peer = new TorrentPeer(this, id, null); - } - peers.add(peer); - } - return peer; - } - - /** - * If this peer already exists, will update its IP. - * - * @param peerInfo - * the peer info object, returned from the tracker - */ - public TorrentPeer addPeerByPeerInfo(PeerInfo peerInfo) { - final TorrentPeerID id = TorrentPeerID.create(peerInfo.getPeerId()); - final InetSocketAddress address = new InetSocketAddress( - peerInfo.getIp(), peerInfo.getPort()); - TorrentPeer peer = getPeer(id, address); - peer.setSocketAddress(address); - return peer; + return swarm.getPeer(id, address); } } diff --git a/src/main/java/net/torrent/torrent/context/TorrentSwarm.java b/src/main/java/net/torrent/torrent/context/TorrentSwarm.java new file mode 100644 index 0000000..af915a4 --- /dev/null +++ b/src/main/java/net/torrent/torrent/context/TorrentSwarm.java @@ -0,0 +1,175 @@ +/** + * + */ +package net.torrent.torrent.context; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import net.torrent.protocol.tracker.message.PeerListMessage.PeerInfo; +import net.torrent.torrent.TorrentPiece; +import net.torrent.util.SwarmCallback; + +/** + * An torrent swarm is an set of all connected peers. + * + * @author Rogiel Josias Sulzbach + */ +public class TorrentSwarm implements Iterable { + /** + * The torrent context + */ + private final TorrentContext context; + + /** + * The list of active peers + */ + private List peers = new ArrayList(); + + /** + * Creates a new instance + * + * @param context + * the torrent context + */ + public TorrentSwarm(final TorrentContext context) { + this.context = context; + } + + /** + * Add an given peer to the swarm + * + * @param peer + * the peer + * @return true if was not present in swarm + */ + public boolean add(TorrentPeer peer) { + return this.peers.add(peer); + } + + /** + * Removes an given peer from the swarm + * + * @param peer + * the peer + * @return true if peer was in swarm + */ + public boolean remove(TorrentPeer peer) { + return this.peers.remove(peer); + } + + /** + * Executes an callback on each peer in the swarm + * + * @param callback + * the callback + */ + public void execute(SwarmCallback callback) { + for (final TorrentPeer peer : this) { + callback.callback(peer); + } + } + + /** + * Select the peers which have the piece + * + * @param piece + * the piece + * @return list of peers with piece + */ + public List getPeersWithPiece(TorrentPiece piece) { + final List hasPieces = new ArrayList(); + for (final TorrentPeer peer : peers) { + if (!peer.getBitfield().hasPiece(piece)) + continue; + hasPieces.add(peer); + } + return hasPieces; + } + + /** + * Get an peer by its PeerID + * + * @param peerId + * the peer id + * @return the found peer. Null if not found. + */ + public TorrentPeer getPeer(TorrentPeerID peerId) { + for (final TorrentPeer peer : peers) { + if (peer.getPeerID().equals(peerId)) + return peer; + } + return null; + } + + /** + * Get an peer by its address + * + * @param address + * the address + * @return the found peer. Null if not found. + */ + public TorrentPeer getPeer(InetSocketAddress address) { + for (final TorrentPeer peer : peers) { + if (peer.getSocketAddress().equals(address)) + return peer; + } + return null; + } + + /** + * Lookup for a peer first by its id, then by address, if still not found, + * creates a new entry. + * + * @param id + * the peer id + * @param address + * the address + * @return the found or newly created peer + */ + public TorrentPeer getPeer(TorrentPeerID id, InetSocketAddress address) { + TorrentPeer peer = getPeer(id); + if (peer == null) { + peer = getPeer(address); + if (peer != null) { + if (remove(peer)) + peer = peer.createWithID(id); + } else { + peer = new TorrentPeer(context, id, null); + } + add(peer); + } + return peer; + } + + /** + * If this peer already exists, will update its IP. + * + * @param peerInfo + * the peer info object, returned from the tracker + */ + public TorrentPeer addPeerByPeerInfo(PeerInfo peerInfo) { + final TorrentPeerID id = TorrentPeerID.create(peerInfo.getPeerId()); + final InetSocketAddress address = new InetSocketAddress( + peerInfo.getIp(), peerInfo.getPort()); + TorrentPeer peer = getPeer(id, address); + peer.setSocketAddress(address); + return peer; + } + + @Override + public Iterator iterator() { + return peers.iterator(); + } + + /** + * Get the torrent context + * + * @return the torrent context + */ + public TorrentContext getContext() { + return context; + } +} diff --git a/src/main/java/net/torrent/torrent/piece/PieceSelector.java b/src/main/java/net/torrent/torrent/piece/PieceSelector.java index eeeb418..41fdccc 100644 --- a/src/main/java/net/torrent/torrent/piece/PieceSelector.java +++ b/src/main/java/net/torrent/torrent/piece/PieceSelector.java @@ -34,4 +34,11 @@ public interface PieceSelector { * @return the {@link TorrentPiece piece} selected for download. */ public TorrentPiece select(TorrentPeer peer); + + /** + * Return the amount of pieces available to select + * + * @return the amount of pieces available + */ + public int countPieces(TorrentPeer peer); } diff --git a/src/main/java/net/torrent/torrent/piece/ScoredPieceSelector.java b/src/main/java/net/torrent/torrent/piece/ScoredPieceSelector.java new file mode 100644 index 0000000..e025432 --- /dev/null +++ b/src/main/java/net/torrent/torrent/piece/ScoredPieceSelector.java @@ -0,0 +1,77 @@ +/* + * 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.torrent.piece; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import net.torrent.protocol.peerwire.manager.TorrentManager; +import net.torrent.torrent.TorrentPiece; +import net.torrent.torrent.context.TorrentPeer; +import net.torrent.torrent.piece.score.PieceRarenessScoreAlgorithm; +import net.torrent.torrent.piece.score.ScoredPieceComparator; + +/** + * Select pieces in sequential order. Can be used for streaming purposes. + * + * @author Rogiel Josias Sulzbach + */ +public class ScoredPieceSelector extends SortedListPieceSelector { + /** + * Sort again the list each n calls to {@link #select(TorrentPeer)} + */ + private static final int SORT_INTERVAL = 10; + + /** + * Call counter. Used to sort the list. + */ + private int calls = 0; + + /** + * The piece comparator + */ + private final ScoredPieceComparator comparator; + + /** + * Creates a new instance + * + * @param manager + * the torrent manager + */ + public ScoredPieceSelector(TorrentManager manager) { + super(manager, Arrays.asList(manager.getTorrent().getPieces())); + this.comparator = new ScoredPieceComparator(manager.getContext() + .getSwarm(), new PieceRarenessScoreAlgorithm()); + // initial sort will be skipped because comparator is null. + this.sort(pieces); + } + + @Override + protected void sort(List pieces) { + if (comparator != null) + Collections.sort(pieces, comparator); + System.out.println(pieces); + } + + @Override + public synchronized TorrentPiece select(TorrentPeer peer) { + if (calls % SORT_INTERVAL == 0) + this.sort(pieces); + calls++; + return super.select(peer); + } +} diff --git a/src/main/java/net/torrent/torrent/piece/SortedListPieceSelector.java b/src/main/java/net/torrent/torrent/piece/SortedListPieceSelector.java index ceba524..f53bf52 100644 --- a/src/main/java/net/torrent/torrent/piece/SortedListPieceSelector.java +++ b/src/main/java/net/torrent/torrent/piece/SortedListPieceSelector.java @@ -32,11 +32,11 @@ public abstract class SortedListPieceSelector implements PieceSelector { /** * The torrent manager */ - private final TorrentManager manager; + protected final TorrentManager manager; /** * The sorted list of pieces */ - private final List pieces; + protected final List pieces; /** * Creates a new instance @@ -53,6 +53,14 @@ public abstract class SortedListPieceSelector implements PieceSelector { this.sort(this.pieces); } + /** + * Sorts the set using an implementation specific algorithm. + * + * @param pieces + * the unsorted pieces list that will be sorted. + */ + protected abstract void sort(List pieces); + @Override public synchronized TorrentPiece select(TorrentPeer peer) { for (int index = 0; index < pieces.size(); index++) { @@ -68,11 +76,19 @@ public abstract class SortedListPieceSelector implements PieceSelector { return null; } - /** - * Sorts the set using an implementation specific algorithm. - * - * @param pieces - * the unsorted pieces list that will be sorted. - */ - protected abstract void sort(List 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); +}