From f21572879ea864c76903b56ba9d0cb47bd1ca0cc Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 11 Nov 2018 16:13:14 +0700 Subject: [PATCH] =?UTF-8?q?=D0=AD=D0=BA=D1=81=D0=BF=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D0=BB=D1=8C=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=84=D0=B8=D1=82=D1=87=D0=B0:=20RequestWorker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Launcher/runtime/engine/api.js | 4 +-- .../ru/gravit/launcher/LauncherEngine.java | 2 ++ .../launcher/client/FunctionalBridge.java | 15 +++++++++++ .../gravit/launcher/client/RequestWorker.java | 27 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java diff --git a/Launcher/runtime/engine/api.js b/Launcher/runtime/engine/api.js index 743170c5..04de7ec2 100644 --- a/Launcher/runtime/engine/api.js +++ b/Launcher/runtime/engine/api.js @@ -95,11 +95,11 @@ function newTask(r) { } function newRequestTask(request) { - return newTask(function() request.request()); + FunctionalBridge.makeRequest(request,) } function startTask(task) { - CommonHelper.newThread("FX Task Thread", true, task).start(); + FunctionalBridge.startTask(task); } function openURL(url) { diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java index 883e7ad8..93dc8385 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java @@ -212,6 +212,8 @@ public void start(String... args) throws Throwable { throw new IllegalStateException("Launcher has been already started"); Launcher.modulesManager.initModules(); // Load init.js script + FunctionalBridge.worker = new RequestWorker(); + CommonHelper.newThread("FX Task Worker", true, FunctionalBridge.worker); loadScript(Launcher.API_SCRIPT_FILE); loadScript(Launcher.CONFIG_SCRIPT_FILE); loadScript(Launcher.INIT_SCRIPT_FILE); diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java b/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java index 7169ff5b..cc9499e7 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java @@ -1,11 +1,14 @@ package ru.gravit.launcher.client; +import javafx.concurrent.Task; import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.hasher.FileNameMatcher; import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.update.LegacyLauncherRequest; +import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.serialize.signed.SignedObjectHolder; +import ru.gravit.utils.helper.CommonHelper; import java.io.IOException; import java.nio.file.Path; @@ -14,6 +17,8 @@ public class FunctionalBridge { @LauncherAPI public static LauncherSettings settings; + @LauncherAPI + public static RequestWorker worker; @LauncherAPI public HashedDirRunnable offlineUpdateRequest(String dirName, Path dir, SignedObjectHolder hdir, FileNameMatcher matcher, boolean digest) throws Exception { @@ -45,4 +50,14 @@ public LegacyLauncherRequest.Result offlineLauncherRequest() throws IOException, public interface HashedDirRunnable { SignedObjectHolder run() throws Exception; } + @LauncherAPI + public void makeJsonRequest(RequestInterface request, Runnable callback) + { + + } + @LauncherAPI + public void startTask(Task task) + { + worker.queue.offer(task); + } } diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java b/Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java new file mode 100644 index 00000000..6cfbd202 --- /dev/null +++ b/Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java @@ -0,0 +1,27 @@ +package ru.gravit.launcher.client; + +import javafx.concurrent.Task; +import ru.gravit.launcher.LauncherAPI; +import ru.gravit.launcher.request.Request; +import ru.gravit.utils.helper.LogHelper; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class RequestWorker implements Runnable { + public RequestWorker() + { + queue = new LinkedBlockingQueue<>(64); + } + public BlockingQueue queue; + @Override + public void run() { + while (!Thread.interrupted()) + { + Task task = queue.poll(); + if (task != null) { + task.run(); + } + } + } +}