Refactored GlobalEventBus

This commit is contained in:
lieght
2025-09-10 14:21:01 +02:00
parent 76ddb0d6b8
commit b722e8fd31
11 changed files with 152 additions and 42 deletions

5
.idea/misc.xml generated
View File

@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="com.google.common.eventbus.Subscribe" />
</list>
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager"> <component name="MavenProjectsManager">
<option name="originalFiles"> <option name="originalFiles">

View File

@@ -1,13 +0,0 @@
package org.toop;
import com.google.common.eventbus.EventBus;
public enum GlobalEventBus {
INSTANCE;
private final EventBus eventBus = new EventBus("global-bus");
public EventBus get() {
return eventBus;
}
}

View File

@@ -1,14 +0,0 @@
package org.toop;
import com.google.common.eventbus.Subscribe;
import org.toop.server.*;
public class LoggerListener {
@Subscribe
public void onCommand(CommandEvent event) {
System.out.printf("LOG: %s args=%s -> %s%n",
event.command(),
String.join(",", event.args()),
event.result());
}
}

View File

@@ -1,5 +1,7 @@
package org.toop; package org.toop;
import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
import org.toop.server.Server; import org.toop.server.Server;
public class Main { public class Main {
@@ -8,10 +10,23 @@ public class Main {
Server server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "5000"); Server server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "5000");
server.setBackend(Server.ServerBackend.REMOTE); server.setBackend(Server.ServerBackend.REMOTE);
GlobalEventBus.INSTANCE.get().register(new LoggerListener()); GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnCommand.class, e -> {
if (e.command() == Server.Command.LOGIN) {
System.out.println("LOGIN command -> " + String.join(" ", e.args()));
}
else if (e.command() == Server.Command.HELP) {
System.out.println("HELP command -> " + String.join(" ", e.args()));
}
});
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerBackend.class, e -> {
System.out.println("Server backend has changed to -> " + e.backend());
});
Server.Message msg = server.sendCommand(Server.Command.LOGIN, "move"); Server.Message msg = server.sendCommand(Server.Command.LOGIN, "move");
server.sendCommand(Server.Command.LOGIN, "test"); server.sendCommand(Server.Command.HELP, "test", "test2");
server.setBackend(Server.ServerBackend.REMOTE);
System.out.println(msg); System.out.println(msg);
System.out.println(server); System.out.println(server);

View File

@@ -0,0 +1,23 @@
package org.toop.eventbus;
import org.toop.server.Server;
/**
* Events that are used in the GlobalEventBus class.
*/
public class Events {
public static class ServerEvents {
/**
* Triggers when a command is sent to a server.
*/
public record OnCommand(Server.Command command, String[] args, Server.Message result) {}
/**
* Triggers on changing the server backend.
*/
public record OnChangingServerBackend(Server.ServerBackend backend) {}
}
}

View File

@@ -0,0 +1,90 @@
package org.toop.eventbus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
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;
/**
* Singleton event bus.
*/
private final EventBus eventBus = new EventBus("global-bus");
/**
* Wraps a Consumer into a Guava @Subscribe-compatible listener.
*
* @return Singleton Event Bus
*/
public EventBus get() {
return eventBus;
}
/**
* Wraps a Consumer into a Guava @Subscribe-compatible listener.
*
* @param type The event to be used. (e.g. Events.ServerCommand.class)
* @param action The function, or lambda to run when fired.
* @return Object to be used for registering an event.
*/
public static <T> Object subscribe(Class<T> type, Consumer<T> action) {
return new Object() {
@Subscribe
public void handle(T event) {
action.accept(event);
}
};
}
/**
* Wraps a Consumer into a Guava @Subscribe-compatible listener and registers it.
*
* @param type The event to be used. (e.g. Events.ServerCommand.class)
* @param action The function, or lambda to run when fired.
* @return Object to be used for registering an event.
*/
public static <T> Object subscribeAndRegister(Class<T> type, Consumer<T> action) {
Object listener = new Object() {
@Subscribe
public void handle(Object event) {
if (type.isInstance(event)) {
action.accept(type.cast(event));
}
}
};
register(listener);
return listener;
}
/**
* Wrapper for registering a listener.
*
* @param listener The event listener to register.
*/
public static void register(Object listener) {
GlobalEventBus.INSTANCE.get().register(listener);
}
/**
* Wrapper for unregistering a listener.
*
* @param listener The event listener to unregister.
*/
public static void unregister(Object listener) {
GlobalEventBus.INSTANCE.get().unregister(listener);
}
public static void post(Object event) {
GlobalEventBus.INSTANCE.get().post(event);
}
}

View File

@@ -1,3 +0,0 @@
package org.toop.server;
public record CommandEvent(Server.Command command, String[] args, Server.Message result) {}

View File

@@ -1,6 +1,7 @@
package org.toop.server; package org.toop.server;
import org.toop.GlobalEventBus; import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
import org.toop.server.backend.*; import org.toop.server.backend.*;
import java.util.EnumSet; import java.util.EnumSet;
@@ -48,7 +49,7 @@ public class Server {
String ip; String ip;
String port; String port;
Backend backend; IBackend backend;
public Server(ServerBackend set_backend, String set_ip, String set_port) { public Server(ServerBackend set_backend, String set_ip, String set_port) {
ip = set_ip; ip = set_ip;
@@ -57,13 +58,19 @@ public class Server {
} }
public Backend getBackend() { public IBackend getBackend() {
return backend; return backend;
} }
public void setBackend(ServerBackend backend) { public void setBackend(ServerBackend backend) {
if (backend == ServerBackend.LOCAL) { this.backend = new Local(); } if (backend == ServerBackend.LOCAL) {
else { this.backend = new Remote(); } this.backend = new Local();
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerBackend(ServerBackend.LOCAL));
}
else {
this.backend = new Remote();
GlobalEventBus.post(new Events.ServerEvents.OnChangingServerBackend(ServerBackend.REMOTE));
}
} }
public String getIp() { public String getIp() {
@@ -102,7 +109,7 @@ public class Server {
} }
Message result = sendCommandString(command.toString()); Message result = sendCommandString(command.toString());
GlobalEventBus.INSTANCE.get().post(new CommandEvent(command, new String[0], result)); GlobalEventBus.post(new Events.ServerEvents.OnCommand(command, new String[0], result));
return sendCommandString(command.toString()); return sendCommandString(command.toString());
} }
@@ -132,7 +139,7 @@ public class Server {
Message result = sendCommandString(String.join(" ", fullCommand)); Message result = sendCommandString(String.join(" ", fullCommand));
GlobalEventBus.INSTANCE.get().post(new CommandEvent(command, args, result)); GlobalEventBus.post(new Events.ServerEvents.OnCommand(command, args, result));
return result; return result;
} }

View File

@@ -2,7 +2,7 @@ package org.toop.server.backend;
import org.toop.server.Server; import org.toop.server.Server;
public interface Backend { public interface IBackend {
Server.Message login(String username); Server.Message login(String username);
} }

View File

@@ -2,7 +2,7 @@ package org.toop.server.backend;
import org.toop.server.Server; import org.toop.server.Server;
public class Local implements Backend { public class Local implements IBackend {
@Override @Override
public Server.Message login(String username) { public Server.Message login(String username) {
return null; return null;

View File

@@ -2,7 +2,7 @@ package org.toop.server.backend;
import org.toop.server.Server; import org.toop.server.Server;
public class Remote implements Backend { public class Remote implements IBackend {
@Override @Override
public Server.Message login(String username) { public Server.Message login(String username) {
return null; return null;