Feature/multidownloader

This commit is contained in:
harlekin
2024-02-25 18:31:16 +00:00
parent e27fc3076f
commit fdadb13cae

View File

@@ -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<Callable<Error.Type>> tasks = new ArrayList<>();
for (int i = 0; i < total; i++) {
Chunk chunk = ajob_.getArchiveChunks().get(i);
int finalI = i;
Callable<Type> downloadTask = () -> {
DownloadManager downloadManager = new DownloadManager(
this.server,
this.gui,
this.log,
String.format(LOCALE, "chunk %d/%d", i + 1, total),
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())
);
Error.Type ret = downloadManager.download();
if (ret != Type.OK) {
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;
}