Skip to content

Commit

Permalink
fix incorrect calculating left bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dead-off committed Oct 12, 2018
1 parent 029334d commit 350c88f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,19 @@ public TorrentManager addTorrent(TorrentMetadataProvider metadataProvider,
PieceStorage pieceStorage,
List<TorrentListener> listeners) throws IOException {
CopyOnWriteArrayList<TorrentListener> cowListeners = new CopyOnWriteArrayList<TorrentListener>(listeners);
TorrentMetadata torrentMetadata = metadataProvider.getTorrentMetadata();
final LoadedTorrentImpl loadedTorrent = new LoadedTorrentImpl(
new TorrentStatistic(),
metadataProvider,
torrentMetadata,
pieceStorage,
new EventDispatcher(cowListeners));

if (pieceStorage.isFinished()) {
loadedTorrent.getTorrentStatistic().setLeft(0);
} else {
long size = 0;
for (TorrentFile torrentFile : loadedTorrent.getMetadata().getFiles()) {
size += torrentFile.size;
}
loadedTorrent.getTorrentStatistic().setLeft(size);
long left = calculateLeft(pieceStorage, torrentMetadata);
loadedTorrent.getTorrentStatistic().setLeft(left);
}

this.torrentsStorage.addTorrent(loadedTorrent.getTorrentHash().getHexInfoHash(), loadedTorrent);
Expand All @@ -224,6 +223,25 @@ public TorrentManager addTorrent(TorrentMetadataProvider metadataProvider,
return new TorrentManagerImpl(cowListeners, loadedTorrent.getTorrentHash());
}

private long calculateLeft(PieceStorage pieceStorage, TorrentMetadata torrentMetadata) {

long size = 0;
for (TorrentFile torrentFile : torrentMetadata.getFiles()) {
size += torrentFile.size;
}

int pieceLength = torrentMetadata.getPieceLength();
long result = 0;
BitSet availablePieces = pieceStorage.getAvailablePieces();
for (int i = 0; i < torrentMetadata.getPiecesCount(); i++) {
if (availablePieces.get(i)) {
continue;
}
result += Math.min(pieceLength, size - i * pieceLength);
}
return result;
}

private void forceAnnounceAndLogError(LoadedTorrent torrent, AnnounceRequestMessage.RequestEvent event) {
try {
this.announce.forceAnnounce(torrent.createAnnounceableInformation(), this, event);
Expand Down Expand Up @@ -609,7 +627,7 @@ public void handleDiscoveredPeers(List<Peer> peers, String hexInfoHash) {

if (announceableTorrent.getPieceStorage().isFinished()) return;

logger.info("Got {} peer(s) ({}) for {} in tracker response", new Object[]{peers.size(),
logger.debug("Got {} peer(s) ({}) for {} in tracker response", new Object[]{peers.size(),
Arrays.toString(peers.toArray()), hexInfoHash});

Map<PeerUID, Peer> uniquePeers = new HashMap<PeerUID, Peer>();
Expand Down Expand Up @@ -742,7 +760,7 @@ private void validatePieceAsync(final SharedTorrent torrent, final Piece piece,
isTorrentComplete = torrent.isComplete();

if (isTorrentComplete) {
logger.debug("Download of {} complete.", torrent.getDirectoryName());
logger.info("Download of {} complete.", torrent.getDirectoryName());

torrent.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class LoadedTorrentImpl implements LoadedTorrent {

LoadedTorrentImpl(TorrentStatistic torrentStatistic,
TorrentMetadataProvider metadataProvider,
TorrentMetadata torrentMetadata,
PieceStorage pieceStorage,
EventDispatcher eventDispatcher) throws IOException {
EventDispatcher eventDispatcher) {
this.torrentStatistic = torrentStatistic;
this.metadataProvider = metadataProvider;
final TorrentMetadata torrentMetadata = metadataProvider.getTorrentMetadata();
this.eventDispatcher = eventDispatcher;
torrentHash = new ImmutableTorrentHash(torrentMetadata.getInfoHash());
if (torrentMetadata.getAnnounceList() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,9 @@ private void hashSingleThread() {
);
this.pieces[idx] = piece;
piece.setValid(pieceStorage.getAvailablePieces().get(idx));
}

for (Piece piece : pieces) {
if (piece.isValid()) {
this.completedPieces.set(piece.getIndex());
myTorrentStatistic.addLeft(-piece.size());
}
}
}
Expand Down

0 comments on commit 350c88f

Please sign in to comment.