mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 09:01:08 +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 {
|
try {
|
||||||
return new Texture(url, cloak);
|
return new Texture(url, cloak);
|
||||||
} catch (FileNotFoundException ignored) {
|
} catch (FileNotFoundException ignored) {
|
||||||
if (LogHelper.isDebugEnabled()) {
|
|
||||||
LogHelper.subDebug("Texture not found :(");
|
LogHelper.subDebug("Texture not found :(");
|
||||||
}
|
|
||||||
return null; // Simply 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()));
|
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
|
||||||
//return;
|
//return;
|
||||||
} else if ((frame instanceof PongWebSocketFrame)) {
|
} 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)) {
|
} else if ((frame instanceof CloseWebSocketFrame)) {
|
||||||
ctx.channel().close();
|
ctx.channel().close();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.lang.instrument.Instrumentation;
|
import java.lang.instrument.Instrumentation;
|
||||||
import java.lang.management.ManagementFactory;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
|
@ -9,6 +9,9 @@
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
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.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
@ -19,10 +22,16 @@
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
import org.apache.http.impl.client.LaxRedirectStrategy;
|
import org.apache.http.impl.client.LaxRedirectStrategy;
|
||||||
|
|
||||||
import pro.gravit.utils.helper.IOHelper;
|
import pro.gravit.utils.helper.*;
|
||||||
import pro.gravit.utils.helper.LogHelper;
|
|
||||||
|
|
||||||
public class ListDownloader {
|
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
|
@FunctionalInterface
|
||||||
public interface DownloadCallback {
|
public interface DownloadCallback {
|
||||||
void stateChanged(String filename, long downloadedSize, long size);
|
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()
|
try (CloseableHttpClient httpclient = HttpClients.custom()
|
||||||
.setRedirectStrategy(new LaxRedirectStrategy())
|
.setRedirectStrategy(new LaxRedirectStrategy())
|
||||||
.build()) {
|
.build()) {
|
||||||
|
applies.sort((a,b) -> Long.compare(a.size, b.size));
|
||||||
HttpGet get = null;
|
List<Callable<Void>> toExec = new ArrayList<>();
|
||||||
URI baseUri = new URI(base);
|
URI baseUri = new URI(base);
|
||||||
String scheme = baseUri.getScheme();
|
String scheme = baseUri.getScheme();
|
||||||
String host = baseUri.getHost();
|
String host = baseUri.getHost();
|
||||||
|
@ -56,19 +65,34 @@ public void download(String base, List<DownloadTask> applies, Path dstDirFile, D
|
||||||
if (port != -1)
|
if (port != -1)
|
||||||
host = host + ":" + port;
|
host = host + ":" + port;
|
||||||
String path = baseUri.getPath();
|
String path = baseUri.getPath();
|
||||||
|
List<IOException> excs = new CopyOnWriteArrayList<>();
|
||||||
for (DownloadTask apply : applies) {
|
for (DownloadTask apply : applies) {
|
||||||
URI u = new URI(scheme, host, path + apply.apply, "", "");
|
URI u = new URI(scheme, host, path + apply.apply, "", "");
|
||||||
callback.stateChanged(apply.apply, 0L, apply.size);
|
callback.stateChanged(apply.apply, 0L, apply.size);
|
||||||
Path targetPath = dstDirFile.resolve(apply.apply);
|
Path targetPath = dstDirFile.resolve(apply.apply);
|
||||||
if (LogHelper.isDebugEnabled()) {
|
toExec.add(() -> {
|
||||||
|
if (LogHelper.isDebugEnabled())
|
||||||
LogHelper.debug("Download URL: %s to file %s dir: %s", u.toString(), targetPath.toAbsolutePath().toString(), dstDirFile.toAbsolutePath().toString());
|
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);
|
||||||
}
|
}
|
||||||
if (get == null) get = new HttpGet(u);
|
return null;
|
||||||
else {
|
});
|
||||||
get.reset();
|
|
||||||
get.setURI(u);
|
|
||||||
}
|
}
|
||||||
httpclient.execute(get, new FileDownloadResponseHandler(targetPath, apply, callback, totalCallback, false));
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,7 +105,7 @@ public void run(String... args) throws Throwable {
|
||||||
loadConfig();
|
loadConfig();
|
||||||
ServerWrapperSetup setup = new ServerWrapperSetup();
|
ServerWrapperSetup setup = new ServerWrapperSetup();
|
||||||
setup.run();
|
setup.run();
|
||||||
System.exit(1);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
modulesManager = new ModulesManager(wrapper);
|
modulesManager = new ModulesManager(wrapper);
|
||||||
modulesManager.autoload(modulesDir);
|
modulesManager.autoload(modulesDir);
|
||||||
|
|
Loading…
Reference in a new issue