From 514e8e353560e1450c75b8401cd62a6e69bd936c Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 20 Apr 2019 02:16:31 +0700 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=BE=D0=B5=20=D0=BF=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launcher/LauncherEngine.java | 8 ++++++ .../gravit/launcher/request/PingRequest.java | 8 ++++-- .../ru/gravit/launcher/request/Request.java | 2 +- .../websockets/ClientWebSocketService.java | 28 +++++++++++++++++++ .../StandartClientWebSocketService.java | 22 ++++++++++----- 5 files changed, 58 insertions(+), 10 deletions(-) diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java index 89e98d0e..6638f4b7 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java @@ -9,6 +9,8 @@ import ru.gravit.launcher.gui.RuntimeProvider; import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; +import ru.gravit.launcher.request.Request; +import ru.gravit.launcher.request.websockets.StandartClientWebSocketService; import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.EnvHelper; import ru.gravit.utils.helper.JVMHelper; @@ -64,6 +66,12 @@ public void start(String... args) throws Throwable { if (runtimeProvider == null) runtimeProvider = new JSRuntimeProvider(); runtimeProvider.init(false); runtimeProvider.preLoad(); + if(Request.service != null) + { + String address = Launcher.getConfig().address; + LogHelper.debug("Start async connection to %s", address); + Request.service = StandartClientWebSocketService.initWebSockets(address, true); + } LauncherGuardManager.initGuard(false); Objects.requireNonNull(args, "args"); if (started.getAndSet(true)) diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/PingRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/PingRequest.java index 1813e3dc..ae3c8ea7 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/PingRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/PingRequest.java @@ -1,10 +1,14 @@ package ru.gravit.launcher.request; -public final class PingRequest extends Request { +public final class PingRequest extends Request { @Override - protected Void requestDo() throws Exception { + protected ResultInterface requestDo() throws Exception { return null; } + @Override + public String getType() { + return null; + } } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/Request.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/Request.java index adb3c996..7567d4f7 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/Request.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/Request.java @@ -31,7 +31,7 @@ public static void requestError(String message) throws RequestException { public R request() throws Exception { if (!started.compareAndSet(false, true)) throw new IllegalStateException("Request already started"); - if(service == null) service = StandartClientWebSocketService.initWebSockets(Launcher.getConfig().address); + if(service == null) service = StandartClientWebSocketService.initWebSockets(Launcher.getConfig().address, false); return requestDo(); } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java index 927e30e5..143a7ae0 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -2,6 +2,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import org.java_websocket.handshake.ServerHandshake; import ru.gravit.launcher.events.request.*; import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; @@ -19,6 +20,7 @@ public class ClientWebSocketService extends ClientJSONPoint { public final GsonBuilder gsonBuilder; public final Gson gson; public OnCloseCallback onCloseCallback; + public final Boolean onConnect; public ReconnectCallback reconnectCallback; private HashMap> requests; private HashMap> results; @@ -34,6 +36,7 @@ public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) { this.gsonBuilder.registerTypeAdapter(ResultInterface.class, new JsonResultAdapter(this)); this.gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); this.gson = gsonBuilder.create(); + this.onConnect = true; } private static URI createURL(String address) { @@ -58,6 +61,14 @@ public void onMessage(String message) { public void onError(Exception e) { LogHelper.error(e); } + @Override + public void onOpen(ServerHandshake handshakedata) { + //Notify open + synchronized (onConnect) + { + onConnect.notifyAll(); + } + } @Override public void onClose(int code, String reason, boolean remote) @@ -121,14 +132,31 @@ public void registerResults() { public void registerHandler(EventHandler eventHandler) { handlers.add(eventHandler); } + public void waitIfNotConnected() + { + if(!isOpen() && !isClosed() && !isClosing()) + { + LogHelper.warning("WebSocket not connected. Try wait onConnect object"); + synchronized (onConnect) + { + try { + onConnect.wait(5000); + } catch (InterruptedException e) { + LogHelper.error(e); + } + } + } + } public void sendObject(Object obj) throws IOException { + waitIfNotConnected(); if(isClosed() && reconnectCallback != null) reconnectCallback.onReconnect(); send(gson.toJson(obj, RequestInterface.class)); } public void sendObject(Object obj, Type type) throws IOException { + waitIfNotConnected(); if(isClosed() && reconnectCallback != null) reconnectCallback.onReconnect(); send(gson.toJson(obj, type)); diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/StandartClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/StandartClientWebSocketService.java index 304b59b3..4ed7284d 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/StandartClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/StandartClientWebSocketService.java @@ -92,20 +92,28 @@ public RequestFuture asyncSendRequest(RequestInterface request) throws IOExcepti return new RequestFuture(request); } - public static StandartClientWebSocketService initWebSockets(String address) { + public static StandartClientWebSocketService initWebSockets(String address, boolean async) { StandartClientWebSocketService service = new StandartClientWebSocketService(new GsonBuilder(), address, 5000); service.registerResults(); service.registerRequests(); service.registerHandler(service.waitEventHandler); - try { - if (!service.connectBlocking()) LogHelper.error("Error connecting"); - LogHelper.debug("Connect to %s", address); - } catch (InterruptedException e) { - e.printStackTrace(); + if(!async) + { + try { + if (!service.connectBlocking()) LogHelper.error("Error connecting"); + LogHelper.debug("Connect to %s", address); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + else + { + service.connect(); } JVMHelper.RUNTIME.addShutdownHook(new Thread(() -> { try { - service.closeBlocking(); + if(service.isOpen()) + service.closeBlocking(); } catch (InterruptedException e) { LogHelper.error(e); }