[FEATURE] Переработка системы Request'ов, использование Future

This commit is contained in:
Gravit 2019-04-20 01:42:38 +07:00
parent 4027ec3b91
commit c2f8ee7bab
23 changed files with 137 additions and 155 deletions

View file

@ -2,7 +2,7 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.LogEvent; 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.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.command.Command; import ru.gravit.utils.command.Command;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -35,9 +35,9 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
LogHelper.info("Send log listener request"); 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"); LogHelper.info("Add log handler");
LegacyRequestBridge.service.registerHandler((result) -> { Request.service.registerHandler((result) -> {
if(result instanceof LogEvent) if(result instanceof LogEvent)
{ {
System.out.println(((LogEvent) result).string); System.out.println(((LogEvent) result).string);

View file

@ -10,7 +10,6 @@
import ru.gravit.launcher.profiles.PlayerProfile; import ru.gravit.launcher.profiles.PlayerProfile;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.auth.RestoreSessionRequest; 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.HInput;
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launcher.serialize.stream.StreamObject; import ru.gravit.launcher.serialize.stream.StreamObject;
@ -457,11 +456,11 @@ public static void main(String... args) throws Throwable {
LogHelper.debug("Restore sessions"); LogHelper.debug("Restore sessions");
RestoreSessionRequest request = new RestoreSessionRequest(Request.getSession()); RestoreSessionRequest request = new RestoreSessionRequest(Request.getSession());
request.request(); request.request();
LegacyRequestBridge.service.reconnectCallback = () -> Request.service.reconnectCallback = () ->
{ {
LogHelper.debug("WebSocket connect closed. Try reconnect"); LogHelper.debug("WebSocket connect closed. Try reconnect");
try { 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); LogHelper.debug("Connect to %s", Launcher.getConfig().address);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -1,12 +1,16 @@
package ru.gravit.launcher.request; package ru.gravit.launcher.request;
import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.LauncherAPI; 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 ru.gravit.utils.helper.SecurityHelper;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public abstract class Request<R> { public abstract class Request<R extends ResultInterface> implements RequestInterface {
private static long session = SecurityHelper.secureRandom.nextLong(); private static long session = SecurityHelper.secureRandom.nextLong();
public static StandartClientWebSocketService service;
public static void setSession(long session) { public static void setSession(long session) {
Request.session = session; Request.session = session;
@ -27,9 +31,13 @@ public static void requestError(String message) throws RequestException {
public R request() throws Exception { public R request() throws Exception {
if (!started.compareAndSet(false, true)) if (!started.compareAndSet(false, true))
throw new IllegalStateException("Request already started"); throw new IllegalStateException("Request already started");
if(service == null) service = StandartClientWebSocketService.initWebSockets(Launcher.getConfig().address);
return requestDo(); return requestDo();
} }
protected abstract R requestDo() throws Exception; protected R requestDo() throws Exception
{
return (R) service.sendRequest(this);
}
} }

View file

@ -3,7 +3,6 @@
import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launcher.events.request.ExecCommandRequestEvent; import ru.gravit.launcher.events.request.ExecCommandRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
public class ExecCommandRequest extends Request<ExecCommandRequestEvent> implements RequestInterface { public class ExecCommandRequest extends Request<ExecCommandRequestEvent> implements RequestInterface {
@ -14,11 +13,6 @@ public ExecCommandRequest(String cmd) {
this.cmd = cmd; this.cmd = cmd;
} }
@Override
protected ExecCommandRequestEvent requestDo() throws Exception {
return (ExecCommandRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "execCmd"; return "execCmd";

View file

@ -5,7 +5,6 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.AuthRequestEvent; import ru.gravit.launcher.events.request.AuthRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
@ -77,11 +76,6 @@ public AuthRequest(String login, byte[] encryptedPassword, String auth_id, Conne
this.getSession = false; this.getSession = false;
} }
@Override
public AuthRequestEvent requestDo() throws Exception {
return (AuthRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "auth"; return "auth";

View file

@ -4,7 +4,6 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.CheckServerRequestEvent; import ru.gravit.launcher.events.request.CheckServerRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
@ -20,12 +19,6 @@ public CheckServerRequest(String username, String serverID) {
this.serverID = VerifyHelper.verifyServerID(serverID); this.serverID = VerifyHelper.verifyServerID(serverID);
} }
@Override
public CheckServerRequestEvent requestDo() throws Exception
{
return (CheckServerRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "checkServer"; return "checkServer";

View file

@ -2,14 +2,9 @@
import ru.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; import ru.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
public class GetAvailabilityAuthRequest extends Request<GetAvailabilityAuthRequestEvent> implements RequestInterface { public class GetAvailabilityAuthRequest extends Request<GetAvailabilityAuthRequestEvent> implements RequestInterface {
@Override
protected GetAvailabilityAuthRequestEvent requestDo() throws Exception {
return (GetAvailabilityAuthRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {

View file

@ -4,13 +4,10 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.JoinServerRequestEvent; import ru.gravit.launcher.events.request.JoinServerRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import java.io.IOException;
public final class JoinServerRequest extends Request<JoinServerRequestEvent> implements RequestInterface { public final class JoinServerRequest extends Request<JoinServerRequestEvent> implements RequestInterface {
// Instance // Instance
@ -28,11 +25,6 @@ public JoinServerRequest(String username, String accessToken, String serverID) {
this.serverID = VerifyHelper.verifyServerID(serverID); this.serverID = VerifyHelper.verifyServerID(serverID);
} }
@Override
public JoinServerRequestEvent requestDo() throws IOException, InterruptedException {
return (JoinServerRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "joinServer"; return "joinServer";

View file

@ -3,11 +3,8 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.RestoreSessionRequestEvent; import ru.gravit.launcher.events.request.RestoreSessionRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import java.io.IOException;
public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements RequestInterface { public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent> implements RequestInterface {
@LauncherNetworkAPI @LauncherNetworkAPI
public long session; public long session;
@ -15,10 +12,6 @@ public class RestoreSessionRequest extends Request<RestoreSessionRequestEvent>
public RestoreSessionRequest(long session) { public RestoreSessionRequest(long session) {
this.session = session; this.session = session;
} }
@Override
public RestoreSessionRequestEvent requestDo() throws IOException, InterruptedException {
return (RestoreSessionRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {

View file

@ -4,7 +4,6 @@
import ru.gravit.launcher.events.request.SetProfileRequestEvent; import ru.gravit.launcher.events.request.SetProfileRequestEvent;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
public class SetProfileRequest extends Request<SetProfileRequestEvent> implements RequestInterface { public class SetProfileRequest extends Request<SetProfileRequestEvent> implements RequestInterface {
@ -15,11 +14,6 @@ public SetProfileRequest(ClientProfile profile) {
this.client = profile.getTitle(); this.client = profile.getTitle();
} }
@Override
public SetProfileRequestEvent requestDo() throws Exception {
return (SetProfileRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "setProfile"; return "setProfile";

View file

@ -6,7 +6,6 @@
import ru.gravit.launcher.downloader.ListDownloader; import ru.gravit.launcher.downloader.ListDownloader;
import ru.gravit.launcher.events.request.LauncherRequestEvent; import ru.gravit.launcher.events.request.LauncherRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.JVMHelper; import ru.gravit.utils.helper.JVMHelper;
@ -67,7 +66,7 @@ public static void update(LauncherRequestEvent result) throws IOException {
@Override @Override
public LauncherRequestEvent requestDo() throws Exception { public LauncherRequestEvent requestDo() throws Exception {
LauncherRequestEvent result = (LauncherRequestEvent) LegacyRequestBridge.sendRequest(this); LauncherRequestEvent result = (LauncherRequestEvent) service.sendRequest(this);
if (result.needUpdate) update(result); if (result.needUpdate) update(result);
return result; return result;
} }

View file

@ -2,16 +2,10 @@
import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.events.request.ProfilesRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
public final class ProfilesRequest extends Request<ProfilesRequestEvent> implements RequestInterface { public final class ProfilesRequest extends Request<ProfilesRequestEvent> implements RequestInterface {
@Override
public ProfilesRequestEvent requestDo() throws Exception {
return (ProfilesRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "profiles"; return "profiles";

View file

@ -2,16 +2,10 @@
import ru.gravit.launcher.events.request.UpdateListRequestEvent; import ru.gravit.launcher.events.request.UpdateListRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
public final class UpdateListRequest extends Request<UpdateListRequestEvent> implements RequestInterface { public final class UpdateListRequest extends Request<UpdateListRequestEvent> implements RequestInterface {
@Override
public UpdateListRequestEvent requestDo() throws Exception {
return (UpdateListRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "updateList"; return "updateList";

View file

@ -11,7 +11,6 @@
import ru.gravit.launcher.hasher.HashedFile; import ru.gravit.launcher.hasher.HashedFile;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.update.UpdateRequest.State.Callback; 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.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -169,7 +168,7 @@ public double getTotalSizeMiB() {
@Override @Override
public UpdateRequestEvent requestDo() throws Exception { public UpdateRequestEvent requestDo() throws Exception {
LogHelper.debug("Start update request"); LogHelper.debug("Start update request");
UpdateRequestEvent e = (UpdateRequestEvent) LegacyRequestBridge.sendRequest(this); UpdateRequestEvent e = (UpdateRequestEvent) service.sendRequest(this);
LogHelper.debug("Start update"); LogHelper.debug("Start update");
Launcher.profile.pushOptionalFile(e.hdir, !Launcher.profile.isUpdateFastCheck()); Launcher.profile.pushOptionalFile(e.hdir, !Launcher.profile.isUpdateFastCheck());
HashedDir.Diff diff = e.hdir.diff(localDir, matcher); HashedDir.Diff diff = e.hdir.diff(localDir, matcher);

View file

@ -4,7 +4,6 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.BatchProfileByUsernameRequestEvent; import ru.gravit.launcher.events.request.BatchProfileByUsernameRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.launcher.serialize.SerializeLimits; import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
@ -34,10 +33,6 @@ public BatchProfileByUsernameRequest(String... usernames) throws IOException {
for (String username : usernames) for (String username : usernames)
VerifyHelper.verifyUsername(username); VerifyHelper.verifyUsername(username);
} }
@Override
public BatchProfileByUsernameRequestEvent requestDo() throws IOException, InterruptedException {
return (BatchProfileByUsernameRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {

View file

@ -4,10 +4,8 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.ProfileByUUIDRequestEvent; import ru.gravit.launcher.events.request.ProfileByUUIDRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@ -20,11 +18,6 @@ public ProfileByUUIDRequest(UUID uuid) {
this.uuid = Objects.requireNonNull(uuid, "uuid"); this.uuid = Objects.requireNonNull(uuid, "uuid");
} }
@Override
public ProfileByUUIDRequestEvent requestDo() throws IOException, InterruptedException {
return (ProfileByUUIDRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "profileByUUID"; return "profileByUUID";

View file

@ -4,12 +4,9 @@
import ru.gravit.launcher.LauncherNetworkAPI; import ru.gravit.launcher.LauncherNetworkAPI;
import ru.gravit.launcher.events.request.ProfileByUsernameRequestEvent; import ru.gravit.launcher.events.request.ProfileByUsernameRequestEvent;
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.request.websockets.RequestInterface;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import java.io.IOException;
public final class ProfileByUsernameRequest extends Request<ProfileByUsernameRequestEvent> implements RequestInterface { public final class ProfileByUsernameRequest extends Request<ProfileByUsernameRequestEvent> implements RequestInterface {
@LauncherNetworkAPI @LauncherNetworkAPI
private final String username; private final String username;
@ -19,11 +16,6 @@ public ProfileByUsernameRequest(String username) {
this.username = VerifyHelper.verifyUsername(username); this.username = VerifyHelper.verifyUsername(username);
} }
@Override
public ProfileByUsernameRequestEvent requestDo() throws IOException, InterruptedException {
return (ProfileByUsernameRequestEvent) LegacyRequestBridge.sendRequest(this);
}
@Override @Override
public String getType() { public String getType() {
return "profileByUsername"; return "profileByUsername";

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -12,7 +12,6 @@ public class WaitEventHandler implements ClientWebSocketService.EventHandler {
public void process(ResultInterface result) { public void process(ResultInterface result) {
LogHelper.debug("Processing event %s type", result.getType()); LogHelper.debug("Processing event %s type", result.getType());
for (ResultEvent r : requests) { for (ResultEvent r : requests) {
LogHelper.subDebug("Processing %s", r.type);
if (r.type.equals(result.getType()) || result.getType().equals("error")) { if (r.type.equals(result.getType()) || result.getType().equals("error")) {
LogHelper.debug("Event %s type", r.type); LogHelper.debug("Event %s type", r.type);
synchronized (r) { synchronized (r) {

2
Radon

@ -1 +1 @@
Subproject commit 07581407e7b214b7dff8a256247dde62bba1697d Subproject commit e6974532efc7228a8b826a8cd9e1f1f7b609659f

View file

@ -7,9 +7,9 @@
import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.LauncherConfig;
import ru.gravit.launcher.events.request.ProfilesRequestEvent; import ru.gravit.launcher.events.request.ProfilesRequestEvent;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.auth.AuthRequest; import ru.gravit.launcher.request.auth.AuthRequest;
import ru.gravit.launcher.request.update.ProfilesRequest; import ru.gravit.launcher.request.update.ProfilesRequest;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.server.setup.ServerWrapperSetup; import ru.gravit.launcher.server.setup.ServerWrapperSetup;
import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.PublicURLClassLoader;
import ru.gravit.utils.config.JsonConfigurable; import ru.gravit.utils.config.JsonConfigurable;
@ -166,11 +166,11 @@ public void run(String... args) throws Throwable {
modulesManager.postInitModules(); modulesManager.postInitModules();
if(config.websocket.enabled) if(config.websocket.enabled)
{ {
LegacyRequestBridge.service.reconnectCallback = () -> Request.service.reconnectCallback = () ->
{ {
LogHelper.debug("WebSocket connect closed. Try reconnect"); LogHelper.debug("WebSocket connect closed. Try reconnect");
try { 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); LogHelper.debug("Connect to %s", config.websocket.address);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

@ -1 +1 @@
Subproject commit 2d7c696aebf750f29b0a185faea25a3262fa905e Subproject commit 571a9bfe1b79548d1fb1617264ef33be976504b3