From 3afb316fb5cbbfc561d378ec790496f3fee47a96 Mon Sep 17 00:00:00 2001 From: "Maxim.Zaytsev" Date: Fri, 7 Dec 2018 11:40:58 +0300 Subject: [PATCH] fix client hanging in "download torrent" method when torrent exists. 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 --- .../turn/ttorrent/client/SimpleClient.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ttorrent-client/src/main/java/com/turn/ttorrent/client/SimpleClient.java b/ttorrent-client/src/main/java/com/turn/ttorrent/client/SimpleClient.java index 2421a94e6..533f04017 100644 --- a/ttorrent-client/src/main/java/com/turn/ttorrent/client/SimpleClient.java +++ b/ttorrent-client/src/main/java/com/turn/ttorrent/client/SimpleClient.java @@ -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; @@ -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 listeners = Collections.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(); } @@ -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); } }