Permalink
Browse files

Added waitForCompletion() method to block until download is complete

Closes #38.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
  • Loading branch information...
1 parent f55684d commit cf5b72df66aabb5be27d97bd5e52262165c20026 @pankdm pankdm committed with Maxime Petazzoni Apr 8, 2013
Showing with 18 additions and 8 deletions.
  1. +4 −3 README.md
  2. +14 −5 src/main/java/com/turn/ttorrent/client/Client.java
View
7 README.md
@@ -90,10 +90,11 @@ client.download();
// client.share(3600);
// Which would seed the torrent for an hour after the download is complete.
-// At any time you can call client.stop() to interrupt the download.
+// Downloading and seeding is done in background threads.
+// To wait for this process to finish, call:
+client.waitForCompletion();
-// When you're done, call client.finish() to close everything up:
-client.finish();
+// At any time you can call client.stop() to interrupt the download.
```
#### Tracker code
View
19 src/main/java/com/turn/ttorrent/client/Client.java
@@ -269,18 +269,27 @@ public void stop(boolean wait) {
if (this.thread != null && this.thread.isAlive()) {
this.thread.interrupt();
if (wait) {
- try {
- this.thread.join();
- } catch (InterruptedException ie) {
- // Ignore
- }
+ this.waitForCompletion();
}
}
this.thread = null;
}
/**
+ * Wait for downloading (and seeding, if requested) to complete.
+ */
+ public void waitForCompletion() {
+ if (this.thread != null && this.thread.isAlive()) {
+ try {
+ this.thread.join();
+ } catch (InterruptedException ie) {
+ logger.error(ie.getMessage(), ie);
+ }
+ }
+ }
+
+ /**
* Tells whether we are a seed for the torrent we're sharing.
*/
public boolean isSeed() {

0 comments on commit cf5b72d

Please sign in to comment.