diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java index 385c6cf3..02a68acf 100644 --- a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java @@ -2,7 +2,7 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.LogEvent; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; +import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.command.Command; import ru.gravit.utils.helper.LogHelper; @@ -35,9 +35,9 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { LogHelper.info("Send log listener request"); - LegacyRequestBridge.service.sendObject(new LogListenerRequest(LogHelper.JANSI ? LogHelper.OutputTypes.JANSI : LogHelper.OutputTypes.PLAIN)); + Request.service.sendObject(new LogListenerRequest(LogHelper.JANSI ? LogHelper.OutputTypes.JANSI : LogHelper.OutputTypes.PLAIN)); LogHelper.info("Add log handler"); - LegacyRequestBridge.service.registerHandler((result) -> { + Request.service.registerHandler((result) -> { if(result instanceof LogEvent) { System.out.println(((LogEvent) result).string); diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java index bf856d3f..013c7a33 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java @@ -10,7 +10,6 @@ import ru.gravit.launcher.profiles.PlayerProfile; import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.auth.RestoreSessionRequest; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.stream.StreamObject; @@ -457,11 +456,11 @@ public static void main(String... args) throws Throwable { LogHelper.debug("Restore sessions"); RestoreSessionRequest request = new RestoreSessionRequest(Request.getSession()); request.request(); - LegacyRequestBridge.service.reconnectCallback = () -> + Request.service.reconnectCallback = () -> { LogHelper.debug("WebSocket connect closed. Try reconnect"); try { - if (!LegacyRequestBridge.service.reconnectBlocking()) LogHelper.error("Error connecting"); + if (!Request.service.reconnectBlocking()) LogHelper.error("Error connecting"); LogHelper.debug("Connect to %s", Launcher.getConfig().address); } catch (InterruptedException e) { e.printStackTrace(); 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 a6774a41..adb3c996 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/Request.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/Request.java @@ -1,12 +1,16 @@ package ru.gravit.launcher.request; +import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherAPI; +import ru.gravit.launcher.request.websockets.RequestInterface; +import ru.gravit.launcher.request.websockets.StandartClientWebSocketService; import ru.gravit.utils.helper.SecurityHelper; import java.util.concurrent.atomic.AtomicBoolean; -public abstract class Request { +public abstract class Request implements RequestInterface { private static long session = SecurityHelper.secureRandom.nextLong(); + public static StandartClientWebSocketService service; public static void setSession(long session) { Request.session = session; @@ -27,9 +31,13 @@ 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); return requestDo(); } - protected abstract R requestDo() throws Exception; + protected R requestDo() throws Exception + { + return (R) service.sendRequest(this); + } } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java index a1da5e1c..24a8dbb0 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java @@ -3,7 +3,6 @@ import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.events.request.ExecCommandRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; public class ExecCommandRequest extends Request implements RequestInterface { @@ -14,11 +13,6 @@ public ExecCommandRequest(String cmd) { this.cmd = cmd; } - @Override - protected ExecCommandRequestEvent requestDo() throws Exception { - return (ExecCommandRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "execCmd"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java index 4e02baae..2200d67c 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/AuthRequest.java @@ -5,7 +5,6 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.AuthRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.helper.VerifyHelper; @@ -77,11 +76,6 @@ public AuthRequest(String login, byte[] encryptedPassword, String auth_id, Conne this.getSession = false; } - @Override - public AuthRequestEvent requestDo() throws Exception { - return (AuthRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "auth"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/CheckServerRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/CheckServerRequest.java index 39a64279..ea351617 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/CheckServerRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/CheckServerRequest.java @@ -4,7 +4,6 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.CheckServerRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.helper.VerifyHelper; @@ -20,12 +19,6 @@ public CheckServerRequest(String username, String serverID) { this.serverID = VerifyHelper.verifyServerID(serverID); } - @Override - public CheckServerRequestEvent requestDo() throws Exception - { - return (CheckServerRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "checkServer"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java index 6713134f..f3bcd1e6 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/GetAvailabilityAuthRequest.java @@ -2,14 +2,9 @@ import ru.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; public class GetAvailabilityAuthRequest extends Request implements RequestInterface { - @Override - protected GetAvailabilityAuthRequestEvent requestDo() throws Exception { - return (GetAvailabilityAuthRequestEvent) LegacyRequestBridge.sendRequest(this); - } @Override public String getType() { diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/JoinServerRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/JoinServerRequest.java index c649c025..94354865 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/JoinServerRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/JoinServerRequest.java @@ -4,13 +4,10 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.JoinServerRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.VerifyHelper; -import java.io.IOException; - public final class JoinServerRequest extends Request implements RequestInterface { // Instance @@ -28,11 +25,6 @@ public JoinServerRequest(String username, String accessToken, String serverID) { this.serverID = VerifyHelper.verifyServerID(serverID); } - @Override - public JoinServerRequestEvent requestDo() throws IOException, InterruptedException { - return (JoinServerRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "joinServer"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/RestoreSessionRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/RestoreSessionRequest.java index bb9b890e..56a93d5f 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/RestoreSessionRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/RestoreSessionRequest.java @@ -3,11 +3,8 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.RestoreSessionRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; -import java.io.IOException; - public class RestoreSessionRequest extends Request implements RequestInterface { @LauncherNetworkAPI public long session; @@ -15,10 +12,6 @@ public class RestoreSessionRequest extends Request public RestoreSessionRequest(long session) { this.session = session; } - @Override - public RestoreSessionRequestEvent requestDo() throws IOException, InterruptedException { - return (RestoreSessionRequestEvent) LegacyRequestBridge.sendRequest(this); - } @Override public String getType() { diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/SetProfileRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/SetProfileRequest.java index 8c391fe6..b8d116af 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/SetProfileRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/auth/SetProfileRequest.java @@ -4,7 +4,6 @@ import ru.gravit.launcher.events.request.SetProfileRequestEvent; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; public class SetProfileRequest extends Request implements RequestInterface { @@ -15,11 +14,6 @@ public SetProfileRequest(ClientProfile profile) { this.client = profile.getTitle(); } - @Override - public SetProfileRequestEvent requestDo() throws Exception { - return (SetProfileRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "setProfile"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/LauncherRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/LauncherRequest.java index 2fad0c61..3c0be801 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/LauncherRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/LauncherRequest.java @@ -6,7 +6,6 @@ import ru.gravit.launcher.downloader.ListDownloader; import ru.gravit.launcher.events.request.LauncherRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.JVMHelper; @@ -67,7 +66,7 @@ public static void update(LauncherRequestEvent result) throws IOException { @Override public LauncherRequestEvent requestDo() throws Exception { - LauncherRequestEvent result = (LauncherRequestEvent) LegacyRequestBridge.sendRequest(this); + LauncherRequestEvent result = (LauncherRequestEvent) service.sendRequest(this); if (result.needUpdate) update(result); return result; } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/ProfilesRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/ProfilesRequest.java index 550f146a..fcb70c01 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/ProfilesRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/ProfilesRequest.java @@ -2,16 +2,10 @@ import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; public final class ProfilesRequest extends Request implements RequestInterface { - @Override - public ProfilesRequestEvent requestDo() throws Exception { - return (ProfilesRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "profiles"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateListRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateListRequest.java index 131269c3..725f0492 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateListRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateListRequest.java @@ -2,16 +2,10 @@ import ru.gravit.launcher.events.request.UpdateListRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; public final class UpdateListRequest extends Request implements RequestInterface { - @Override - public UpdateListRequestEvent requestDo() throws Exception { - return (UpdateListRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "updateList"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateRequest.java index 68401d5b..0e652a40 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/update/UpdateRequest.java @@ -11,7 +11,6 @@ import ru.gravit.launcher.hasher.HashedFile; import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.update.UpdateRequest.State.Callback; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; @@ -169,7 +168,7 @@ public double getTotalSizeMiB() { @Override public UpdateRequestEvent requestDo() throws Exception { LogHelper.debug("Start update request"); - UpdateRequestEvent e = (UpdateRequestEvent) LegacyRequestBridge.sendRequest(this); + UpdateRequestEvent e = (UpdateRequestEvent) service.sendRequest(this); LogHelper.debug("Start update"); Launcher.profile.pushOptionalFile(e.hdir, !Launcher.profile.isUpdateFastCheck()); HashedDir.Diff diff = e.hdir.diff(localDir, matcher); diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java index f1c80a91..5fe332c3 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/BatchProfileByUsernameRequest.java @@ -4,7 +4,6 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.BatchProfileByUsernameRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.serialize.SerializeLimits; import ru.gravit.utils.helper.IOHelper; @@ -34,10 +33,6 @@ public BatchProfileByUsernameRequest(String... usernames) throws IOException { for (String username : usernames) VerifyHelper.verifyUsername(username); } - @Override - public BatchProfileByUsernameRequestEvent requestDo() throws IOException, InterruptedException { - return (BatchProfileByUsernameRequestEvent) LegacyRequestBridge.sendRequest(this); - } @Override public String getType() { diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUUIDRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUUIDRequest.java index 87ebc328..fe99ec0d 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUUIDRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUUIDRequest.java @@ -4,10 +4,8 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.ProfileByUUIDRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; -import java.io.IOException; import java.util.Objects; import java.util.UUID; @@ -20,11 +18,6 @@ public ProfileByUUIDRequest(UUID uuid) { this.uuid = Objects.requireNonNull(uuid, "uuid"); } - @Override - public ProfileByUUIDRequestEvent requestDo() throws IOException, InterruptedException { - return (ProfileByUUIDRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "profileByUUID"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUsernameRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUsernameRequest.java index 0ad63999..b0308f23 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUsernameRequest.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/uuid/ProfileByUsernameRequest.java @@ -4,12 +4,9 @@ import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.events.request.ProfileByUsernameRequestEvent; import ru.gravit.launcher.request.Request; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.utils.helper.VerifyHelper; -import java.io.IOException; - public final class ProfileByUsernameRequest extends Request implements RequestInterface { @LauncherNetworkAPI private final String username; @@ -19,11 +16,6 @@ public ProfileByUsernameRequest(String username) { this.username = VerifyHelper.verifyUsername(username); } - @Override - public ProfileByUsernameRequestEvent requestDo() throws IOException, InterruptedException { - return (ProfileByUsernameRequestEvent) LegacyRequestBridge.sendRequest(this); - } - @Override public String getType() { return "profileByUsername"; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java deleted file mode 100644 index ee3e7f1b..00000000 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.gravit.launcher.request.websockets; - -import com.google.gson.GsonBuilder; -import ru.gravit.launcher.Launcher; -import ru.gravit.launcher.events.request.ErrorRequestEvent; -import ru.gravit.launcher.request.RequestException; -import ru.gravit.launcher.request.ResultInterface; -import ru.gravit.utils.helper.JVMHelper; -import ru.gravit.utils.helper.LogHelper; - -import java.io.IOException; - -public class LegacyRequestBridge { - public static WaitEventHandler waitEventHandler = new WaitEventHandler(); - public static ClientWebSocketService service; - - public static ResultInterface sendRequest(RequestInterface request) throws IOException, InterruptedException { - WaitEventHandler.ResultEvent e = new WaitEventHandler.ResultEvent(); - e.type = request.getType(); - waitEventHandler.requests.add(e); - service.sendObject(request); - while (!e.ready) { - synchronized (e) { - e.wait(); - LogHelper.debug("WAIT OK"); - } - } - ResultInterface result = e.result; - waitEventHandler.requests.remove(e); - if (e.result.getType().equals("error")) { - ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) e.result; - throw new RequestException(errorRequestEvent.error); - } - return result; - } - - public static void initWebSockets(String address) { - service = new ClientWebSocketService(new GsonBuilder(), address, 5000); - service.registerResults(); - service.registerRequests(); - service.registerHandler(waitEventHandler); - try { - if (!service.connectBlocking()) LogHelper.error("Error connecting"); - LogHelper.debug("Connect to %s", address); - } catch (InterruptedException e) { - e.printStackTrace(); - } - JVMHelper.RUNTIME.addShutdownHook(new Thread(() -> { - try { - service.closeBlocking(); - } catch (InterruptedException e) { - LogHelper.error(e); - } - })); - } - - static { - initWebSockets(Launcher.getConfig().address); - } -} 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 new file mode 100644 index 00000000..304b59b3 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/StandartClientWebSocketService.java @@ -0,0 +1,115 @@ +package ru.gravit.launcher.request.websockets; + +import com.google.gson.GsonBuilder; +import ru.gravit.launcher.events.request.ErrorRequestEvent; +import ru.gravit.launcher.request.RequestException; +import ru.gravit.launcher.request.ResultInterface; +import ru.gravit.utils.helper.JVMHelper; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; +import java.util.concurrent.*; + +public class StandartClientWebSocketService extends ClientWebSocketService { + public WaitEventHandler waitEventHandler = new WaitEventHandler(); + public StandartClientWebSocketService(GsonBuilder gsonBuilder, String address, int i) { + super(gsonBuilder, address, i); + } + public class RequestFuture implements Future + { + public final WaitEventHandler.ResultEvent event; + public boolean isCanceled = false; + + public RequestFuture(RequestInterface request) throws IOException { + event = new WaitEventHandler.ResultEvent(); + event.type = request.getType(); + waitEventHandler.requests.add(event); + sendObject(request); + } + + @Override + public boolean cancel(boolean mayInterruptIfRunning) { + waitEventHandler.requests.remove(event); + isCanceled = true; + return true; + } + + @Override + public boolean isCancelled() { + return isCanceled; + } + + @Override + public boolean isDone() { + return event.ready; + } + + @Override + public ResultInterface get() throws InterruptedException, ExecutionException { + if(isCanceled) return null; + while (!event.ready) { + synchronized (event) { + event.wait(); + } + } + ResultInterface result = event.result; + waitEventHandler.requests.remove(event); + if (event.result.getType().equals("error")) { + ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) event.result; + throw new ExecutionException(new RequestException(errorRequestEvent.error)); + } + return result; + } + + @Override + public ResultInterface get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException { + if(isCanceled) return null; + while (!event.ready) { + synchronized (event) { + event.wait(timeout); + } + } + ResultInterface result = event.result; + waitEventHandler.requests.remove(event); + if (event.result.getType().equals("error")) { + ErrorRequestEvent errorRequestEvent = (ErrorRequestEvent) event.result; + throw new ExecutionException(new RequestException(errorRequestEvent.error)); + } + return result; + } + } + public ResultInterface sendRequest(RequestInterface request) throws IOException, InterruptedException { + RequestFuture future = new RequestFuture(request); + ResultInterface result; + try { + result = future.get(); + } catch (ExecutionException e) { + throw (RequestException) e.getCause(); + } + return result; + } + public RequestFuture asyncSendRequest(RequestInterface request) throws IOException { + return new RequestFuture(request); + } + + public static StandartClientWebSocketService initWebSockets(String address) { + 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(); + } + JVMHelper.RUNTIME.addShutdownHook(new Thread(() -> { + try { + service.closeBlocking(); + } catch (InterruptedException e) { + LogHelper.error(e); + } + })); + return service; + } +} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java index 0a89e127..72211e20 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/WaitEventHandler.java @@ -12,7 +12,6 @@ public class WaitEventHandler implements ClientWebSocketService.EventHandler { public void process(ResultInterface result) { LogHelper.debug("Processing event %s type", result.getType()); for (ResultEvent r : requests) { - LogHelper.subDebug("Processing %s", r.type); if (r.type.equals(result.getType()) || result.getType().equals("error")) { LogHelper.debug("Event %s type", r.type); synchronized (r) { diff --git a/Radon b/Radon index 07581407..e6974532 160000 --- a/Radon +++ b/Radon @@ -1 +1 @@ -Subproject commit 07581407e7b214b7dff8a256247dde62bba1697d +Subproject commit e6974532efc7228a8b826a8cd9e1f1f7b609659f diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java index ec2974cc..bf1593b9 100644 --- a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java @@ -7,9 +7,9 @@ import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.profiles.ClientProfile; +import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.auth.AuthRequest; import ru.gravit.launcher.request.update.ProfilesRequest; -import ru.gravit.launcher.request.websockets.LegacyRequestBridge; import ru.gravit.launcher.server.setup.ServerWrapperSetup; import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.config.JsonConfigurable; @@ -166,11 +166,11 @@ public void run(String... args) throws Throwable { modulesManager.postInitModules(); if(config.websocket.enabled) { - LegacyRequestBridge.service.reconnectCallback = () -> + Request.service.reconnectCallback = () -> { LogHelper.debug("WebSocket connect closed. Try reconnect"); try { - if (!LegacyRequestBridge.service.reconnectBlocking()) LogHelper.error("Error connecting"); + if (!Request.service.reconnectBlocking()) LogHelper.error("Error connecting"); LogHelper.debug("Connect to %s", config.websocket.address); } catch (InterruptedException e) { e.printStackTrace(); diff --git a/modules b/modules index 2d7c696a..571a9bfe 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit 2d7c696aebf750f29b0a185faea25a3262fa905e +Subproject commit 571a9bfe1b79548d1fb1617264ef33be976504b3