mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE] Асинхронное поделючение к серверу
This commit is contained in:
parent
c2f8ee7bab
commit
514e8e3535
5 changed files with 58 additions and 10 deletions
|
@ -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))
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package ru.gravit.launcher.request;
|
||||
|
||||
public final class PingRequest extends Request<Void> {
|
||||
public final class PingRequest extends Request<ResultInterface> {
|
||||
|
||||
@Override
|
||||
protected Void requestDo() throws Exception {
|
||||
protected ResultInterface requestDo() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String, Class<? extends RequestInterface>> requests;
|
||||
private HashMap<String, Class<? extends ResultInterface>> 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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue