Skip to content

Commit

Permalink
fix client hanging in "download torrent" method when torrent exists.
Browse files Browse the repository at this point in the history
reason: "downloadComplete" callback will be invoked only when last piece is received and validated. This callback will not be invoked if torrent exists. So it's needed interrupting method in "validation complete" callback if all pieces are valid
  • Loading branch information
Dead-off committed Dec 7, 2018
1 parent 7078339 commit 3afb316
Showing 1 changed file with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -55,14 +57,23 @@ public void stop(int timeout, TimeUnit timeUnit) {
}

public void downloadTorrent(String torrentFile, String downloadDir, InetAddress iPv4Address) throws IOException, InterruptedException {
TorrentManager torrentManager = startDownloading(torrentFile, downloadDir, iPv4Address);
communicationManager.start(iPv4Address);
final Semaphore semaphore = new Semaphore(0);
torrentManager.addListener(new TorrentListenerWrapper() {
@Override
public void downloadComplete() {
semaphore.release();
}
});
List<TorrentListener> listeners = Collections.<TorrentListener>singletonList(
new TorrentListenerWrapper() {

@Override
public void validationComplete(int validpieces, int totalpieces) {
if (validpieces == totalpieces) semaphore.release();
}

@Override
public void downloadComplete() {
semaphore.release();
}
}
);
TorrentManager torrentManager = communicationManager.addTorrent(torrentFile, downloadDir, listeners);
semaphore.acquire();
}

Expand All @@ -72,8 +83,8 @@ private TorrentManager startDownloading(String torrentFile, String downloadDir,
}

public TorrentManager downloadTorrentAsync(String torrentFile,
String downloadDir,
InetAddress iPv4Address) throws IOException {
String downloadDir,
InetAddress iPv4Address) throws IOException {
return startDownloading(torrentFile, downloadDir, iPv4Address);
}
}

0 comments on commit 3afb316

Please sign in to comment.