mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +03:00
[FEATURE] RequestService
This commit is contained in:
parent
fbb24bb904
commit
111639b963
9 changed files with 132 additions and 33 deletions
|
@ -5,11 +5,12 @@
|
|||
import pro.gravit.launcher.events.NotificationEvent;
|
||||
import pro.gravit.launcher.events.request.SecurityReportRequestEvent;
|
||||
import pro.gravit.launcher.request.Request;
|
||||
import pro.gravit.launcher.request.RequestService;
|
||||
import pro.gravit.launcher.request.WebSocketEvent;
|
||||
import pro.gravit.launcher.request.websockets.ClientWebSocketService;
|
||||
import pro.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class BasicLauncherEventHandler implements ClientWebSocketService.EventHandler {
|
||||
public class BasicLauncherEventHandler implements RequestService.EventHandler {
|
||||
|
||||
@Override
|
||||
public <T extends WebSocketEvent> boolean eventHandle(T event) {
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
import pro.gravit.launcher.request.RequestException;
|
||||
import pro.gravit.launcher.request.auth.AuthRequest;
|
||||
import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest;
|
||||
import pro.gravit.launcher.request.websockets.ClientWebSocketService;
|
||||
import pro.gravit.launcher.request.websockets.StdWebSocketService;
|
||||
import pro.gravit.launcher.utils.NativeJVMHalt;
|
||||
import pro.gravit.utils.helper.*;
|
||||
|
@ -189,11 +190,12 @@ public void start(String... args) throws Throwable {
|
|||
if (runtimeProvider == null) runtimeProvider = new NoRuntimeProvider();
|
||||
runtimeProvider.init(clientInstance);
|
||||
//runtimeProvider.preLoad();
|
||||
if (Request.service == null) {
|
||||
if (!Request.isAvailable()) {
|
||||
String address = Launcher.getConfig().address;
|
||||
LogHelper.debug("Start async connection to %s", address);
|
||||
Request.service = StdWebSocketService.initWebSockets(address, true);
|
||||
Request.service.reconnectCallback = () ->
|
||||
StdWebSocketService service = StdWebSocketService.initWebSockets(address, true);
|
||||
Request.setRequestService(service);
|
||||
service.reconnectCallback = () ->
|
||||
{
|
||||
LogHelper.debug("WebSocket connect closed. Try reconnect");
|
||||
try {
|
||||
|
@ -203,7 +205,7 @@ public void start(String... args) throws Throwable {
|
|||
throw new RequestException("Connection failed", e);
|
||||
}
|
||||
};
|
||||
Request.service.registerEventHandler(new BasicLauncherEventHandler());
|
||||
service.registerEventHandler(new BasicLauncherEventHandler());
|
||||
}
|
||||
Objects.requireNonNull(args, "args");
|
||||
if (started.getAndSet(true))
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
import pro.gravit.launcher.request.RequestException;
|
||||
import pro.gravit.launcher.request.auth.AuthRequest;
|
||||
import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest;
|
||||
import pro.gravit.launcher.request.websockets.StdWebSocketService;
|
||||
import pro.gravit.launcher.serialize.HInput;
|
||||
import pro.gravit.launcher.utils.DirWatcher;
|
||||
import pro.gravit.utils.helper.*;
|
||||
|
@ -112,10 +113,12 @@ public static void main(String[] args) throws Throwable {
|
|||
List<URL> classpath = resolveClassPath(clientDir, params.actions, params.profile).map(IOHelper::toURL).collect(Collectors.toList());
|
||||
// Start client with WatchService monitoring
|
||||
boolean digest = !profile.isUpdateFastCheck();
|
||||
LogHelper.debug("Restore sessions");
|
||||
StdWebSocketService service = StdWebSocketService.initWebSockets(Launcher.getConfig().address, false);
|
||||
LogHelper.debug("Restore sessions");
|
||||
Request.restore();
|
||||
Request.service.registerEventHandler(new BasicLauncherEventHandler());
|
||||
Request.service.reconnectCallback = () ->
|
||||
|
||||
service.registerEventHandler(new BasicLauncherEventHandler());
|
||||
service.reconnectCallback = () ->
|
||||
{
|
||||
LogHelper.debug("WebSocket connect closed. Try reconnect");
|
||||
try {
|
||||
|
@ -125,6 +128,7 @@ public static void main(String[] args) throws Throwable {
|
|||
throw new RequestException("Connection failed", e);
|
||||
}
|
||||
};
|
||||
Request.setRequestService(service);
|
||||
ClientProfile.ClassLoaderConfig classLoaderConfig = profile.getClassLoaderConfig();
|
||||
if (classLoaderConfig == ClientProfile.ClassLoaderConfig.LAUNCHER) {
|
||||
ClientClassLoader classLoader = new ClientClassLoader(classpath.toArray(new URL[0]), ClassLoader.getSystemClassLoader());
|
||||
|
|
|
@ -19,7 +19,9 @@
|
|||
public abstract class Request<R extends WebSocketEvent> implements WebSocketRequest {
|
||||
private static final List<ExtendedTokenCallback> extendedTokenCallbacks = new ArrayList<>(4);
|
||||
private static final List<BiConsumer<String, AuthRequestEvent.OAuthRequestEvent>> oauthChangeHandlers = new ArrayList<>(4);
|
||||
@Deprecated
|
||||
public static StdWebSocketService service;
|
||||
private static RequestService requestService;
|
||||
private static UUID session;
|
||||
private static AuthRequestEvent.OAuthRequestEvent oauth;
|
||||
private static Map<String, String> extendedTokens;
|
||||
|
@ -29,6 +31,21 @@ public abstract class Request<R extends WebSocketEvent> implements WebSocketRequ
|
|||
public final UUID requestUUID = UUID.randomUUID();
|
||||
private transient final AtomicBoolean started = new AtomicBoolean(false);
|
||||
|
||||
public static void setRequestService(RequestService service) {
|
||||
requestService = service;
|
||||
if(service instanceof StdWebSocketService) {
|
||||
Request.service = (StdWebSocketService) service;
|
||||
}
|
||||
}
|
||||
|
||||
public static RequestService getRequestService() {
|
||||
return requestService;
|
||||
}
|
||||
|
||||
public static boolean isAvailable() {
|
||||
return requestService != null;
|
||||
}
|
||||
|
||||
public static UUID getSession() {
|
||||
return Request.session;
|
||||
}
|
||||
|
@ -197,18 +214,25 @@ public void removeOAuthChangeHandler(BiConsumer<String, AuthRequestEvent.OAuthRe
|
|||
public R request() throws Exception {
|
||||
if (!started.compareAndSet(false, true))
|
||||
throw new IllegalStateException("Request already started");
|
||||
if (service == null)
|
||||
service = StdWebSocketService.initWebSockets(Launcher.getConfig().address, false);
|
||||
return requestDo(service);
|
||||
if (!isAvailable())
|
||||
setRequestService(StdWebSocketService.initWebSockets(Launcher.getConfig().address, false));
|
||||
return requestDo(requestService);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public R request(StdWebSocketService service) throws Exception {
|
||||
if (!started.compareAndSet(false, true))
|
||||
throw new IllegalStateException("Request already started");
|
||||
return requestDo(service);
|
||||
}
|
||||
|
||||
protected R requestDo(StdWebSocketService service) throws Exception {
|
||||
public R request(RequestService service) throws Exception {
|
||||
if (!started.compareAndSet(false, true))
|
||||
throw new IllegalStateException("Request already started");
|
||||
return requestDo(service);
|
||||
}
|
||||
|
||||
protected R requestDo(RequestService service) throws Exception {
|
||||
return service.requestSync(this);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package pro.gravit.launcher.request;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public interface RequestService {
|
||||
<T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException;
|
||||
void registerEventHandler(EventHandler handler);
|
||||
void unregisterEventHandler(EventHandler handler);
|
||||
default <T extends WebSocketEvent> T requestSync(Request<T> request) throws IOException {
|
||||
try {
|
||||
return request(request).get();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RequestException("Request interrupted");
|
||||
} catch (ExecutionException e) {
|
||||
Throwable cause = e.getCause();
|
||||
if (cause instanceof IOException)
|
||||
throw (IOException) e.getCause();
|
||||
else {
|
||||
throw new RequestException(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean isClosed();
|
||||
|
||||
@FunctionalInterface
|
||||
public interface EventHandler {
|
||||
/**
|
||||
* @param event processing event
|
||||
* @param <T> event type
|
||||
* @return false - continue, true - stop
|
||||
*/
|
||||
<T extends WebSocketEvent> boolean eventHandle(T event);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
import pro.gravit.launcher.LauncherNetworkAPI;
|
||||
import pro.gravit.launcher.events.request.LauncherRequestEvent;
|
||||
import pro.gravit.launcher.request.Request;
|
||||
import pro.gravit.launcher.request.RequestService;
|
||||
import pro.gravit.launcher.request.websockets.StdWebSocketService;
|
||||
import pro.gravit.launcher.request.websockets.WebSocketRequest;
|
||||
import pro.gravit.utils.helper.IOHelper;
|
||||
|
@ -88,7 +89,7 @@ public static void update(LauncherRequestEvent result) throws IOException {
|
|||
}
|
||||
|
||||
@Override
|
||||
public LauncherRequestEvent requestDo(StdWebSocketService service) throws Exception {
|
||||
public LauncherRequestEvent requestDo(RequestService service) throws Exception {
|
||||
LauncherRequestEvent result = super.request(service);
|
||||
if (result.needUpdate) update(result);
|
||||
return result;
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
import pro.gravit.launcher.events.request.ErrorRequestEvent;
|
||||
import pro.gravit.launcher.request.Request;
|
||||
import pro.gravit.launcher.request.RequestException;
|
||||
import pro.gravit.launcher.request.RequestService;
|
||||
import pro.gravit.launcher.request.WebSocketEvent;
|
||||
import pro.gravit.utils.helper.JVMHelper;
|
||||
import pro.gravit.utils.helper.LogHelper;
|
||||
|
@ -17,10 +18,11 @@
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class StdWebSocketService extends ClientWebSocketService {
|
||||
public class StdWebSocketService extends ClientWebSocketService implements RequestService {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private final ConcurrentHashMap<UUID, CompletableFuture> futureMap = new ConcurrentHashMap<>();
|
||||
private final HashSet<EventHandler> eventHandlers = new HashSet<>();
|
||||
private final HashSet<RequestService.EventHandler> eventHandlers = new HashSet<>();
|
||||
private final HashSet<ClientWebSocketService.EventHandler> legacyEventHandlers = new HashSet<>();
|
||||
|
||||
public StdWebSocketService(String address) throws SSLException {
|
||||
super(address);
|
||||
|
@ -57,16 +59,21 @@ public static StdWebSocketService initWebSockets(String address, boolean async)
|
|||
return service;
|
||||
}
|
||||
|
||||
public void registerEventHandler(EventHandler handler) {
|
||||
eventHandlers.add(handler);
|
||||
@Deprecated
|
||||
public void registerEventHandler(ClientWebSocketService.EventHandler handler) {
|
||||
legacyEventHandlers.add(handler);
|
||||
}
|
||||
|
||||
public void unregisterEventHandler(EventHandler handler) {
|
||||
eventHandlers.remove(handler);
|
||||
@Deprecated
|
||||
public void unregisterEventHandler(ClientWebSocketService.EventHandler handler) {
|
||||
legacyEventHandlers.remove(handler);
|
||||
}
|
||||
|
||||
public <T extends WebSocketEvent> void processEventHandlers(T event) {
|
||||
for (EventHandler handler : eventHandlers) {
|
||||
for (RequestService.EventHandler handler : eventHandlers) {
|
||||
if (handler.eventHandle(event)) return;
|
||||
}
|
||||
for (ClientWebSocketService.EventHandler handler : legacyEventHandlers) {
|
||||
if (handler.eventHandle(event)) return;
|
||||
}
|
||||
}
|
||||
|
@ -110,6 +117,16 @@ public <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> reques
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerEventHandler(RequestService.EventHandler handler) {
|
||||
eventHandlers.add(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterEventHandler(RequestService.EventHandler handler) {
|
||||
eventHandlers.remove(handler);
|
||||
}
|
||||
|
||||
public <T extends WebSocketEvent> T requestSync(Request<T> request) throws IOException {
|
||||
try {
|
||||
return request(request).get();
|
||||
|
@ -124,4 +141,9 @@ public <T extends WebSocketEvent> T requestSync(Request<T> request) throws IOExc
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return isClosed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest;
|
||||
import pro.gravit.launcher.request.auth.RestoreRequest;
|
||||
import pro.gravit.launcher.request.update.ProfilesRequest;
|
||||
import pro.gravit.launcher.request.websockets.StdWebSocketService;
|
||||
import pro.gravit.launcher.server.setup.ServerWrapperSetup;
|
||||
import pro.gravit.utils.PublicURLClassLoader;
|
||||
import pro.gravit.utils.helper.IOHelper;
|
||||
|
@ -112,6 +113,18 @@ public void run(String... args) throws Throwable {
|
|||
updateLauncherConfig();
|
||||
if (config.env != null) Launcher.applyLauncherEnv(config.env);
|
||||
else Launcher.applyLauncherEnv(LauncherConfig.LauncherEnvironment.STD);
|
||||
StdWebSocketService service = StdWebSocketService.initWebSockets(config.address, false);
|
||||
service.reconnectCallback = () ->
|
||||
{
|
||||
LogHelper.debug("WebSocket connect closed. Try reconnect");
|
||||
try {
|
||||
Request.reconnect();
|
||||
getProfiles();
|
||||
} catch (Exception e) {
|
||||
LogHelper.error(e);
|
||||
}
|
||||
};
|
||||
Request.setRequestService(service);
|
||||
if (config.logFile != null) LogHelper.addOutput(IOHelper.newWriter(Paths.get(config.logFile), true));
|
||||
{
|
||||
restore();
|
||||
|
@ -153,16 +166,6 @@ public void run(String... args) throws Throwable {
|
|||
if (loader != null) mainClass = Class.forName(classname, true, loader);
|
||||
else mainClass = Class.forName(classname);
|
||||
MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class));
|
||||
Request.service.reconnectCallback = () ->
|
||||
{
|
||||
LogHelper.debug("WebSocket connect closed. Try reconnect");
|
||||
try {
|
||||
Request.reconnect();
|
||||
getProfiles();
|
||||
} catch (Exception e) {
|
||||
LogHelper.error(e);
|
||||
}
|
||||
};
|
||||
LogHelper.info("ServerWrapper: LaunchServer address: %s. Title: %s", config.address, Launcher.profile != null ? Launcher.profile.getTitle() : "unknown");
|
||||
LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile == null ? "unknown" : wrapper.profile.getVersion().name);
|
||||
LogHelper.info("Start Minecraft Server");
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package pro.gravit.launcher.server.setup;
|
||||
|
||||
import pro.gravit.launcher.request.Request;
|
||||
import pro.gravit.launcher.request.websockets.StdWebSocketService;
|
||||
import pro.gravit.launcher.server.ServerWrapper;
|
||||
import pro.gravit.utils.PublicURLClassLoader;
|
||||
import pro.gravit.utils.helper.IOHelper;
|
||||
import pro.gravit.utils.helper.JVMHelper;
|
||||
import pro.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.net.URL;
|
||||
|
@ -56,9 +58,11 @@ public void run() throws Exception {
|
|||
wrapper.config.serverName = commands.commandHandler.readLine();
|
||||
wrapper.config.mainclass = mainClassName;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if(Request.service == null || Request.service.isClosed) {
|
||||
if(!Request.isAvailable() || Request.getRequestService().isClosed()) {
|
||||
System.out.println("Print launchserver websocket host( ws://host:port/api ):");
|
||||
wrapper.config.address = commands.commandHandler.readLine();
|
||||
StdWebSocketService service = StdWebSocketService.initWebSockets(wrapper.config.address, false);
|
||||
Request.setRequestService(service);
|
||||
}
|
||||
System.out.println("Print server token:");
|
||||
String checkServerToken = commands.commandHandler.readLine();
|
||||
|
@ -70,8 +74,8 @@ public void run() throws Exception {
|
|||
break;
|
||||
} catch (Throwable e) {
|
||||
LogHelper.error(e);
|
||||
if(!Request.service.isClosed) {
|
||||
Request.service.close();
|
||||
if(Request.isAvailable() && Request.getRequestService() instanceof AutoCloseable) {
|
||||
((AutoCloseable) Request.getRequestService()).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue