diff --git a/src/main/java/com/sheepit/client/Client.java b/src/main/java/com/sheepit/client/Client.java index 820ed32..2dfd35b 100644 --- a/src/main/java/com/sheepit/client/Client.java +++ b/src/main/java/com/sheepit/client/Client.java @@ -24,6 +24,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.sql.Time; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -36,6 +37,14 @@ import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; import com.sheepit.client.Error.ServerCode; @@ -781,24 +790,51 @@ import okhttp3.HttpUrl; } protected Error.Type downloadSceneFile(Job ajob_) throws SheepItException { - int total = ajob_.getArchiveChunks().size(); + + ExecutorService executor = Executors.newFixedThreadPool(total); + ArrayList> tasks = new ArrayList<>(); + for (int i = 0; i < total; i++) { Chunk chunk = ajob_.getArchiveChunks().get(i); - DownloadManager downloadManager = new DownloadManager( - this.server, - this.gui, - this.log, - String.format(LOCALE, "chunk %d/%d", i + 1, total), - this.directoryManager.getActualStoragePathFor(chunk), - chunk.getMd5(), - String.format(LOCALE, "%s?chunk=%s", this.server.getPage("download-chunk"), chunk.getId()) - ); - Error.Type ret = downloadManager.download(); - if (ret != Type.OK) { + + int finalI = i; + Callable downloadTask = () -> { + DownloadManager downloadManager = new DownloadManager( + this.server, + this.gui, + this.log, + String.format(LOCALE, "chunk %d/%d", finalI + 1, total), + this.directoryManager.getActualStoragePathFor(chunk), + chunk.getMd5(), + String.format(LOCALE, "%s?chunk=%s", this.server.getPage("download-chunk"), chunk.getId()) + ); + Type ret = null; + ret = downloadManager.download(); return ret; + }; + + tasks.add(downloadTask); + } + + try { + var results = executor.invokeAll(tasks); + + for (var result : results) { + if (result.get(35, TimeUnit.MINUTES) != Type.OK) { + executor.shutdown(); + return result.get(35, TimeUnit.MINUTES); + } } } + catch (ExecutionException | InterruptedException | TimeoutException e) { + + executor.shutdown(); + return Type.DOWNLOAD_FILE; + } + + + executor.shutdown(); return Type.OK; }