mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] Асинхронная загрузка файлов.
This commit is contained in:
parent
073b00c58e
commit
93c1fe20ea
6 changed files with 41 additions and 22 deletions
|
@ -26,9 +26,7 @@ private static Texture getTexture(String url, boolean cloak) throws IOException
|
|||
try {
|
||||
return new Texture(url, cloak);
|
||||
} catch (FileNotFoundException ignored) {
|
||||
if (LogHelper.isDebugEnabled()) {
|
||||
LogHelper.subDebug("Texture not found :(");
|
||||
}
|
||||
LogHelper.subDebug("Texture not found :(");
|
||||
return null; // Simply not found
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,9 +62,7 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
|||
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
|
||||
//return;
|
||||
} else if ((frame instanceof PongWebSocketFrame)) {
|
||||
if (LogHelper.isDevEnabled()) {
|
||||
LogHelper.dev("WebSocket Client received pong");
|
||||
}
|
||||
LogHelper.dev("WebSocket Client received pong");
|
||||
} else if ((frame instanceof CloseWebSocketFrame)) {
|
||||
ctx.channel().close();
|
||||
} else {
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
|
|
@ -9,6 +9,9 @@
|
|||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
|
@ -19,10 +22,16 @@
|
|||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.client.LaxRedirectStrategy;
|
||||
|
||||
import pro.gravit.utils.helper.IOHelper;
|
||||
import pro.gravit.utils.helper.LogHelper;
|
||||
import pro.gravit.utils.helper.*;
|
||||
|
||||
public class ListDownloader {
|
||||
private static final AtomicInteger COUNTER_THR = new AtomicInteger(0);
|
||||
private static final ThreadFactory FACTORY = r -> CommonHelper.newThread("Downloader Thread #" + COUNTER_THR.incrementAndGet(), true, r);
|
||||
|
||||
private static ExecutorService newExecutor() {
|
||||
return new ThreadPoolExecutor(0, VerifyHelper.verifyInt(Integer.parseInt(System.getProperty("launcher.downloadThreads", "3")), VerifyHelper.POSITIVE, "Thread max count must be positive."), 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), FACTORY);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface DownloadCallback {
|
||||
void stateChanged(String filename, long downloadedSize, long size);
|
||||
|
@ -47,8 +56,8 @@ public void download(String base, List<DownloadTask> applies, Path dstDirFile, D
|
|||
try (CloseableHttpClient httpclient = HttpClients.custom()
|
||||
.setRedirectStrategy(new LaxRedirectStrategy())
|
||||
.build()) {
|
||||
|
||||
HttpGet get = null;
|
||||
applies.sort((a,b) -> Long.compare(a.size, b.size));
|
||||
List<Callable<Void>> toExec = new ArrayList<>();
|
||||
URI baseUri = new URI(base);
|
||||
String scheme = baseUri.getScheme();
|
||||
String host = baseUri.getHost();
|
||||
|
@ -56,19 +65,34 @@ public void download(String base, List<DownloadTask> applies, Path dstDirFile, D
|
|||
if (port != -1)
|
||||
host = host + ":" + port;
|
||||
String path = baseUri.getPath();
|
||||
List<IOException> excs = new CopyOnWriteArrayList<>();
|
||||
for (DownloadTask apply : applies) {
|
||||
URI u = new URI(scheme, host, path + apply.apply, "", "");
|
||||
callback.stateChanged(apply.apply, 0L, apply.size);
|
||||
Path targetPath = dstDirFile.resolve(apply.apply);
|
||||
if (LogHelper.isDebugEnabled()) {
|
||||
LogHelper.debug("Download URL: %s to file %s dir: %s", u.toString(), targetPath.toAbsolutePath().toString(), dstDirFile.toAbsolutePath().toString());
|
||||
}
|
||||
if (get == null) get = new HttpGet(u);
|
||||
else {
|
||||
get.reset();
|
||||
get.setURI(u);
|
||||
}
|
||||
httpclient.execute(get, new FileDownloadResponseHandler(targetPath, apply, callback, totalCallback, false));
|
||||
toExec.add(() -> {
|
||||
if (LogHelper.isDebugEnabled())
|
||||
LogHelper.debug("Download URL: %s to file %s dir: %s", u.toString(), targetPath.toAbsolutePath().toString(), dstDirFile.toAbsolutePath().toString());
|
||||
try {
|
||||
httpclient.execute(new HttpGet(u), new FileDownloadResponseHandler(targetPath, apply, callback, totalCallback, false));
|
||||
} catch (IOException e) {
|
||||
excs.add(e);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
try {
|
||||
ExecutorService e = newExecutor();
|
||||
e.invokeAll(toExec);
|
||||
e.shutdown();
|
||||
e.awaitTermination(4, TimeUnit.HOURS);
|
||||
} catch (InterruptedException t) {
|
||||
LogHelper.error(t);
|
||||
}
|
||||
if (!excs.isEmpty()) {
|
||||
IOException toThrow = excs.remove(0);
|
||||
excs.forEach(toThrow::addSuppressed);
|
||||
throw toThrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,6 @@
|
|||
// Не входящих в пакеты самого Forge
|
||||
public class SafeExitJVMLegacy {
|
||||
public static void exit(int code) {
|
||||
JVMHelper.RUNTIME.halt(code);
|
||||
JVMHelper.RUNTIME.halt(code);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,7 +105,7 @@ public void run(String... args) throws Throwable {
|
|||
loadConfig();
|
||||
ServerWrapperSetup setup = new ServerWrapperSetup();
|
||||
setup.run();
|
||||
System.exit(1);
|
||||
System.exit(0);
|
||||
}
|
||||
modulesManager = new ModulesManager(wrapper);
|
||||
modulesManager.autoload(modulesDir);
|
||||
|
|
Loading…
Reference in a new issue