Networking moved to netty. Added a EventPublisher class for easy building of events.

This commit is contained in:
lieght
2025-09-22 04:04:52 +02:00
parent efd485852c
commit 3fa0bae46a
28 changed files with 1429 additions and 483 deletions

View File

@@ -1,13 +1,22 @@
package org.toop;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.google.common.base.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.backend.ServerManager;
import org.toop.eventbus.Events;
import org.toop.eventbus.EventPublisher;
import org.toop.eventbus.EventRegistry;
import org.toop.eventbus.events.Events;
import org.toop.eventbus.GlobalEventBus;
import org.toop.frontend.ConnectionManager;
import org.toop.eventbus.events.NetworkEvents;
import org.toop.frontend.UI.LocalServerSelector;
import org.toop.frontend.networking.NetworkingClientManager;
import org.toop.frontend.networking.NetworkingGameClientHandler;
public class Main {
private static final Logger logger = LogManager.getLogger(Main.class);
@@ -15,14 +24,55 @@ public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// Logging.disableAllLogs();
// Logging.enableAllLogsForClass(LocalTicTacToe.class);
Logging.enableAllLogsForClass(EventRegistry.class);
// Logging.enableLogsForClass(ServerManager.class, Level.ALL);
// Logging.enableLogsForClass(TicTacToeServer.class, Level.ALL);
// Logging.enableLogsForClass(TcpClient.class, Level.ALL);
// Logging.enableLogsForClass(ConnectionManager.class, Level.ALL);
// Logging.enableLogsForClass(NetworkingClientManager.class, Level.ALL);
initSystems();
registerEvents();
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
GlobalEventBus.post(
new Events.ServerEvents.StartServerRequest(5001, "tictactoe", serverIdFuture));
var serverId = serverIdFuture.get();
// CompletableFuture<String> conIdFuture = new CompletableFuture<>();
// GlobalEventBus.post(
// new NetworkEvents.StartClientRequest(NetworkingGameClientHandler::new,
// "127.0.0.1", 5001, conIdFuture));
// var conId = conIdFuture.get();
int numThreads = 100; // how many EventPublisher tests you want
ExecutorService executor = Executors.newFixedThreadPool(200); // 20 threads in pool
for (int i = 0; i < numThreads; i++) {
executor.submit(() -> {
new EventPublisher<>(
NetworkEvents.StartClient.class,
(Supplier<NetworkingGameClientHandler>) NetworkingGameClientHandler::new,
"127.0.0.1",
5001
).onEventById(
NetworkEvents.StartClientSuccess.class,
event -> GlobalEventBus.post(
new NetworkEvents.CloseClient((String) event.connectionId()))
).unregisterAfterSuccess()
.postEvent();
});
}
// Shutdown after tasks complete
executor.shutdown();
// GlobalEventBus.post(new NetworkEvents.SendCommand(conId, "move", "5"));
// GlobalEventBus.post(new NetworkEvents.ForceCloseAllClients());
// GlobalEventBus.post(new NetworkEvents.StartClient(
// NetworkingGameClientHandler::new, "127.0.0.1", 5001, serverId
// ));
// JFrame frame = new JFrame("Server Settings");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(800, 600);
@@ -49,7 +99,7 @@ public class Main {
public static void initSystems() {
new ServerManager();
new ConnectionManager();
new NetworkingClientManager();
}
private static void quit() {