Init ServerManager

This commit is contained in:
lieght
2025-09-15 00:16:43 +02:00
parent 8a5dafebc8
commit e2268a5d71
7 changed files with 454 additions and 346 deletions

View File

@@ -1,9 +1,12 @@
package org.toop.eventbus;
import org.toop.server.Server;
import org.toop.server.ServerConnection;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Events that are used in the GlobalEventBus class.
@@ -74,10 +77,42 @@ public class Events implements IEvents {
public static class ServerEvents {
public record RequestsAllConnections(CompletableFuture<String> future) {}
public record ForceCloseAllConnections() {}
/**
*
* Triggers starting a server connection.
*
* @param ip
* @param port
*/
public record StartConnection(String ip, String port) {}
/**
* Triggers starting a server connection, returns a future.
* WARNING: This is a blocking operation.
*
* @param ip
* @param port
* @param future
*/
public record StartConnectionRequest(String ip, String port, CompletableFuture<String> future) {}
/**
* Triggers when a connection to a server is established.
*
* @param connectionId
* @param ip
* @param port
*/
public record ConnectionEstablished(Object connectionId, String ip, String port) {}
/**
* Triggers sending a command to a server.
*/
public record command(String command, String... args) {}
public record Command(Object connectionId, String command, String... args) { }
/**
* Triggers when a command is sent to a server.
@@ -102,12 +137,19 @@ public class Events implements IEvents {
/**
* Triggers reconnecting to previous address.
*/
public record Reconnect() {}
public record Reconnect(Object connectionId) {}
/**
* Triggers changing connection to a new address.
*/
public record ChangeConnection(String ip, String port) { }
public record ChangeConnection(Object connectionId, String ip, String port) {}
/**
* Triggers when the server couldn't connect to the desired address.
*/
public record CouldNotConnect(Object connectionId) {}
public record ClosedConnection() {}
}

View File

@@ -8,26 +8,40 @@ import java.util.function.Consumer;
/**
* A singleton Event Bus to be used for creating, triggering and activating events.
*/
public enum GlobalEventBus {
/**
* The instance of the Event Bus.
*/
INSTANCE;
public class GlobalEventBus {
/**
* Singleton event bus.
*/
private final EventBus eventBus = new EventBus("global-bus");
private static EventBus eventBus = new EventBus("global-bus");
private GlobalEventBus() {}
/**
* Wraps a Consumer into a Guava @Subscribe-compatible listener.
*
* @return Singleton Event Bus
*/
public EventBus get() {
public static EventBus get() {
return eventBus;
}
/**
* ONLY USE FOR TESTING
*
* @param newBus
*/
public static void set(EventBus newBus) {
eventBus = newBus;
}
/**
* Reset back to the default global EventBus.
*/
public static void reset() {
eventBus = new EventBus("global-bus");
}
/**
* Wraps a Consumer into a Guava @Subscribe-compatible listener.
* TODO
@@ -39,8 +53,10 @@ public enum GlobalEventBus {
private static <T> Object subscribe(Class<T> type, Consumer<T> action) {
return new Object() {
@Subscribe
public void handle(T event) {
action.accept(event);
public void handle(Object event) {
if (type.isInstance(event)) {
action.accept(type.cast(event));
}
}
};
}
@@ -73,7 +89,7 @@ public enum GlobalEventBus {
* @param event The ready event to add to register.
*/
public static <T> void register(EventMeta<T> event) {
GlobalEventBus.INSTANCE.get().register(event.getEvent());
GlobalEventBus.get().register(event.getEvent());
event.setReady(true);
EventRegistry.markReady(event.getType());
}
@@ -86,7 +102,7 @@ public enum GlobalEventBus {
public static <T> void unregister(EventMeta<T> event) {
EventRegistry.markNotReady(event.getType());
event.setReady(false);
GlobalEventBus.INSTANCE.get().unregister(event.getEvent());
GlobalEventBus.get().unregister(event.getEvent());
}
/**
@@ -106,7 +122,6 @@ public enum GlobalEventBus {
EventRegistry.storeEvent(eventMeta);
// post to Guava EventBus
GlobalEventBus.INSTANCE.get().post(event);
GlobalEventBus.get().post(event);
}
}