mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Added logging.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
package org.toop;
|
||||
|
||||
public class Client {
|
||||
|
||||
}
|
||||
|
||||
@@ -5,47 +5,64 @@ import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
import org.toop.server.Server;
|
||||
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
public class Main {
|
||||
private static final Logger logger = LogManager.getLogger(Main.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerBackend.class, e -> {
|
||||
System.out.println("Server backend has changed to -> " + e.backend());
|
||||
});
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerIp.class, e -> {
|
||||
System.out.println("Server ip has changed to -> " + e.ip());
|
||||
});
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerPort.class, e -> {
|
||||
System.out.println("Server port has changed to -> " + e.port());
|
||||
});
|
||||
if (!initEvents()) {
|
||||
throw new RuntimeException("A event could not be initialized");
|
||||
}
|
||||
|
||||
Server server = new Server(Server.ServerBackend.LOCAL, "127.0.0.1", "5000");
|
||||
server.setBackend(Server.ServerBackend.REMOTE);
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnCommand.class, e -> {
|
||||
if (e.command() == Server.Command.LOGIN) {
|
||||
System.out.println("LOGIN command -> " + String.join(" ", e.args()));
|
||||
logger.info("LOGIN command -> {}", String.join(" ", e.args()));
|
||||
}
|
||||
else if (e.command() == Server.Command.HELP) {
|
||||
System.out.println("HELP command -> " + String.join(" ", e.args()));
|
||||
logger.info("HELP command -> {}", String.join(" ", e.args()));
|
||||
}
|
||||
else {
|
||||
System.out.println(e.command().toString());
|
||||
logger.info(e.command().toString());
|
||||
}
|
||||
});
|
||||
|
||||
Server.Message msg = server.sendCommand(Server.Command.LOGIN, "move");
|
||||
server.sendCommand(Server.Command.HELP, "test", "test2");
|
||||
server.sendCommand(Server.Command.BYE);
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.command(Server.Command.HELP, "test", "test2"));
|
||||
GlobalEventBus.post(new Events.ServerEvents.command(Server.Command.BYE));
|
||||
GlobalEventBus.post(new Events.ServerEvents.changeServerIp("127.1.1.1"));
|
||||
GlobalEventBus.post(new Events.ServerEvents.changeServerPort("5003"));
|
||||
server.setBackend(Server.ServerBackend.REMOTE);
|
||||
|
||||
System.out.println(msg);
|
||||
System.out.println(server);
|
||||
|
||||
Window.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false if any event could not be initialized.
|
||||
*/
|
||||
private static boolean initEvents() {
|
||||
try {
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerBackend.class, e ->
|
||||
logger.info("Changing server backend state to {}", e.backend())
|
||||
);
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerIp.class, e ->
|
||||
logger.info("Changing server ip to {}", e.ip())
|
||||
);
|
||||
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerPort.class, e ->
|
||||
logger.info("Changing server port to {}", e.port())
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
package org.toop.eventbus;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.Main;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -10,6 +14,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
*/
|
||||
public class EventRegistry {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(Main.class);
|
||||
|
||||
private static final Map<Class<?>, CopyOnWriteArrayList<EventEntry<?>>> eventHistory =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
@@ -19,6 +25,7 @@ public class EventRegistry {
|
||||
* Stores an event in the registry. Safe for concurrent use.
|
||||
*/
|
||||
public static <T> void storeEvent(EventMeta<T> eventMeta) {
|
||||
logger.info("Storing event: {}", eventMeta.toString());
|
||||
eventHistory
|
||||
.computeIfAbsent(eventMeta.getType(), k -> new CopyOnWriteArrayList<>())
|
||||
.add(new EventEntry<>(eventMeta));
|
||||
@@ -28,6 +35,7 @@ public class EventRegistry {
|
||||
* Marks a specific event type as ready (safe to post).
|
||||
*/
|
||||
public static <T> void markReady(Class<T> type) {
|
||||
logger.info("Marking event as ready: {}", type.toString());
|
||||
readyStates.put(type, true);
|
||||
}
|
||||
|
||||
@@ -35,6 +43,7 @@ public class EventRegistry {
|
||||
* Marks a specific event type as not ready (posting will fail).
|
||||
*/
|
||||
public static <T> void markNotReady(Class<T> type) {
|
||||
logger.info("Marking event as not ready: {}", type.toString());
|
||||
readyStates.put(type, false);
|
||||
}
|
||||
|
||||
@@ -70,6 +79,7 @@ public class EventRegistry {
|
||||
* Clears the stored events for a given type.
|
||||
*/
|
||||
public static <T> void clearEvents(Class<T> type) {
|
||||
logger.info("Clearing events: {}", type.toString());
|
||||
eventHistory.remove(type);
|
||||
}
|
||||
|
||||
@@ -77,6 +87,7 @@ public class EventRegistry {
|
||||
* Clears all events and resets readiness.
|
||||
*/
|
||||
public static void reset() {
|
||||
logger.info("Resetting event registry events");
|
||||
eventHistory.clear();
|
||||
readyStates.clear();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,12 @@ import org.toop.server.Server;
|
||||
public class Events implements IEvents {
|
||||
|
||||
public static class ServerEvents {
|
||||
|
||||
/**
|
||||
* Triggers sending a command to a server.
|
||||
*/
|
||||
public record command(Server.Command command, String... args) {}
|
||||
|
||||
/**
|
||||
* Triggers when a command is sent to a server.
|
||||
*/
|
||||
|
||||
@@ -89,6 +89,11 @@ public enum GlobalEventBus {
|
||||
GlobalEventBus.INSTANCE.get().unregister(event.getEvent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for posting events.
|
||||
*
|
||||
* @param event The event to post.
|
||||
*/
|
||||
public static <T> void post(T event) {
|
||||
Class<T> type = (Class<T>) event.getClass();
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@ public class Server {
|
||||
}
|
||||
|
||||
private void initEvents() {
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.command.class, e -> this.sendCommand(e.command(), e.args()));
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.changeServerIp.class, e -> this.setIp(e.ip()));
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.changeServerPort.class, e -> this.setPort(e.port()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user