From 93c1fe20ea3f740e3fd7632aac477fefed7df7cd Mon Sep 17 00:00:00 2001 From: Zaxar163 Date: Fri, 16 Aug 2019 12:49:22 +0300 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=90=D1=81=D0=B8=D0=BD=D1=85?= =?UTF-8?q?=D1=80=D0=BE=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/texture/RequestTextureProvider.java | 4 +- .../handlers/WebSocketFrameHandler.java | 4 +- .../pro/gravit/launcher/LauncherAgent.java | 1 - .../launcher/downloader/ListDownloader.java | 50 ++++++++++++++----- .../java/cpw/mods/fml/SafeExitJVMLegacy.java | 2 +- .../gravit/launcher/server/ServerWrapper.java | 2 +- 6 files changed, 41 insertions(+), 22 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/RequestTextureProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/RequestTextureProvider.java index 9e963f51..3431deeb 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/RequestTextureProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/texture/RequestTextureProvider.java @@ -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 } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java index 071c0fb8..db0c9836 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java @@ -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 { diff --git a/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java b/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java index c0f855bf..af2d39fa 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java +++ b/Launcher/src/main/java/pro/gravit/launcher/LauncherAgent.java @@ -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; diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/downloader/ListDownloader.java b/LauncherAPI/src/main/java/pro/gravit/launcher/downloader/ListDownloader.java index 1145a507..b9fed281 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/downloader/ListDownloader.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/downloader/ListDownloader.java @@ -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 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> 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 applies, Path dstDirFile, D if (port != -1) host = host + ":" + port; String path = baseUri.getPath(); + List 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; } } } diff --git a/LauncherAuthlib/src/main/java/cpw/mods/fml/SafeExitJVMLegacy.java b/LauncherAuthlib/src/main/java/cpw/mods/fml/SafeExitJVMLegacy.java index d80d9c5c..76673b94 100644 --- a/LauncherAuthlib/src/main/java/cpw/mods/fml/SafeExitJVMLegacy.java +++ b/LauncherAuthlib/src/main/java/cpw/mods/fml/SafeExitJVMLegacy.java @@ -6,6 +6,6 @@ // Не входящих в пакеты самого Forge public class SafeExitJVMLegacy { public static void exit(int code) { - JVMHelper.RUNTIME.halt(code); + JVMHelper.RUNTIME.halt(code); } } diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java index a0fd85ea..8c7bba1c 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java @@ -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);