mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Refactored GlobalEventBus
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.toop;
|
||||
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
import org.toop.server.Server;
|
||||
|
||||
public class Main {
|
||||
@@ -8,10 +10,23 @@ public class Main {
|
||||
Server server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "5000");
|
||||
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.sendCommand(Server.Command.LOGIN, "test");
|
||||
server.sendCommand(Server.Command.HELP, "test", "test2");
|
||||
|
||||
server.setBackend(Server.ServerBackend.REMOTE);
|
||||
|
||||
System.out.println(msg);
|
||||
System.out.println(server);
|
||||
|
||||
23
src/main/java/org/toop/eventbus/Events.java
Normal file
23
src/main/java/org/toop/eventbus/Events.java
Normal 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) {}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
90
src/main/java/org/toop/eventbus/GlobalEventBus.java
Normal file
90
src/main/java/org/toop/eventbus/GlobalEventBus.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
package org.toop.server;
|
||||
|
||||
public record CommandEvent(Server.Command command, String[] args, Server.Message result) {}
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.toop.server;
|
||||
|
||||
import org.toop.GlobalEventBus;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
import org.toop.server.backend.*;
|
||||
import java.util.EnumSet;
|
||||
|
||||
@@ -48,7 +49,7 @@ public class Server {
|
||||
|
||||
String ip;
|
||||
String port;
|
||||
Backend backend;
|
||||
IBackend backend;
|
||||
|
||||
public Server(ServerBackend set_backend, String set_ip, String set_port) {
|
||||
ip = set_ip;
|
||||
@@ -57,13 +58,19 @@ public class Server {
|
||||
|
||||
}
|
||||
|
||||
public Backend getBackend() {
|
||||
public IBackend getBackend() {
|
||||
return backend;
|
||||
}
|
||||
|
||||
public void setBackend(ServerBackend backend) {
|
||||
if (backend == ServerBackend.LOCAL) { this.backend = new Local(); }
|
||||
else { this.backend = new Remote(); }
|
||||
if (backend == ServerBackend.LOCAL) {
|
||||
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() {
|
||||
@@ -102,7 +109,7 @@ public class Server {
|
||||
}
|
||||
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());
|
||||
}
|
||||
@@ -132,7 +139,7 @@ public class Server {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.toop.server.backend;
|
||||
|
||||
import org.toop.server.Server;
|
||||
|
||||
public interface Backend {
|
||||
public interface IBackend {
|
||||
Server.Message login(String username);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.toop.server.backend;
|
||||
|
||||
import org.toop.server.Server;
|
||||
|
||||
public class Local implements Backend {
|
||||
public class Local implements IBackend {
|
||||
@Override
|
||||
public Server.Message login(String username) {
|
||||
return null;
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.toop.server.backend;
|
||||
|
||||
import org.toop.server.Server;
|
||||
|
||||
public class Remote implements Backend {
|
||||
public class Remote implements IBackend {
|
||||
@Override
|
||||
public Server.Message login(String username) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user