Merge remote-tracking branch 'origin/289-server' into 289-server

This commit is contained in:
2025-12-13 18:53:18 +01:00
3 changed files with 37 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
import org.toop.framework.game.players.ArtificialPlayer;
import org.toop.framework.game.players.OnlinePlayer;
import org.toop.framework.game.players.RandomAI;
import org.toop.framework.networking.server.MasterServer;
import org.toop.local.AppContext;
import java.util.List;
@@ -32,7 +33,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public final class Server {
// TODO: Keep track of listeners. Remove them on Server connection close so reference is deleted.
private MasterServer masterServer;
private String user = "";
private long clientId = -1;
@@ -60,10 +62,13 @@ public final class Server {
return null;
}
public Server(String ip, String port, String user) {
this(ip, port, user, null);
}
// Server has to deal with ALL network related listen events. This "server" can then interact with the manager to make stuff happen.
// This prevents data races where events get sent to the game manager but the manager isn't ready yet.
public Server(String ip, String port, String user) {
public Server(String ip, String port, String user, MasterServer masterServer) {
if (ip.split("\\.").length < 4) {
new ErrorPopup("\"" + ip + "\" " + AppContext.getString("is-not-a-valid-ip-address"));
return;
@@ -83,6 +88,8 @@ public final class Server {
return;
}
this.masterServer = masterServer;
final int reconnectAttempts = 10;
LoadingWidget loading = new LoadingWidget(
@@ -281,6 +288,10 @@ public final class Server {
stopScheduler();
connectFlow.unsubscribeAll();
if (masterServer != null) {
masterServer.stop();
}
WidgetContainer.getCurrentView().transitionPrevious();
}

View File

@@ -48,7 +48,8 @@ public class OnlineView extends ViewWidget {
new Server(
"127.0.0.1",
"6666",
"host"
"host",
a
);
}, false);

View File

@@ -19,7 +19,11 @@ import java.util.Map;
public class MasterServer {
private final int port;
public final Server gs;
private final Server gs;
ChannelFuture future;
EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
public MasterServer(int port, Map<String, Class<? extends TurnBasedGame>> gameTypes, Duration challengeDuration) {
this.port = port;
@@ -28,11 +32,10 @@ public class MasterServer {
public void start() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
bossGroup = new NioEventLoopGroup(1);
workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
@@ -57,8 +60,7 @@ public class MasterServer {
}
);
ChannelFuture future = bootstrap.bind(port).sync();
System.out.println("MasterServer listening on port " + port);
future = bootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} finally {
@@ -66,4 +68,18 @@ public class MasterServer {
workerGroup.shutdownGracefully();
}
}
public void stop() {
if (future == null) {
return;
}
future.channel().close();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
future = null;
bossGroup = null;
workerGroup = null;
}
}