Init server code

This commit is contained in:
Bas de Jong
2025-12-12 15:15:55 +01:00
parent 0132981d94
commit 66cb000fad
49 changed files with 475 additions and 92 deletions

View File

@@ -147,7 +147,6 @@
<version>2.42.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>

View File

@@ -0,0 +1,3 @@
package org.toop.framework.game;
// TODO: Remove this, only used in ReversiCanvas. Needs to not
public record Move(int position, char value) {}

View File

@@ -0,0 +1,88 @@
package org.toop.framework.game.gameThreads;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.gameFramework.GameState;
import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.function.Consumer;
/**
* Handles local turn-based game logic at a fixed update rate.
* <p>
* Runs a separate thread that executes game turns at a fixed frequency (default 60 updates/sec),
* applying player moves, updating the game state, and dispatching UI events.
*/
public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
/**
* Creates a fixed-rate behaviour for a local turn-based game.
*
* @param game the game instance
*/
public LocalFixedRateThreadBehaviour(T game) {
super(game);
}
/** Starts the game loop thread if not already running. */
@Override
public void start() {
if (isRunning.compareAndSet(false, true)) {
new Thread(this).start();
}
}
/** Stops the game loop after the current iteration. */
@Override
public void stop() {
isRunning.set(false);
}
/**
* Main loop running at a fixed rate.
* <p>
* Fetches the current player's move, applies it to the game,
* updates the UI, and handles game-ending states.
*/
@Override
public void run() {
final int UPS = 1;
final long UPDATE_INTERVAL = 1_000_000_000L / UPS;
long nextUpdate = System.nanoTime();
while (isRunning.get()) {
long now = System.nanoTime();
if (now >= nextUpdate) {
nextUpdate += UPDATE_INTERVAL;
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
long move = currentPlayer.getMove(game.deepCopy());
PlayResult result = game.play(move);
updateUI();
GameState state = result.state();
switch (state) {
case WIN, DRAW -> {
isRunning.set(false);
new EventFlow().addPostEvent(GUIEvents.GameEnded.class, state == GameState.WIN, result.player()).postEvent();
}
case NORMAL, TURN_SKIPPED -> { /* continue */ }
default -> {
logger.error("Unexpected state {}", state);
isRunning.set(false);
throw new RuntimeException("Unknown state: " + state);
}
}
} else {
try {
Thread.sleep(10);
} catch (InterruptedException ignored) {}
}
}
}
}

View File

@@ -0,0 +1,76 @@
package org.toop.framework.game.gameThreads;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.GameState;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.Player;
import java.util.function.Consumer;
/**
* Handles local turn-based game logic in its own thread.
* <p>
* Repeatedly gets the current player's move, applies it to the game,
* updates the UI, and stops when the game ends or {@link #stop()} is called.
*/
public class LocalThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
/**
* Creates a new behaviour for a local turn-based game.
*
* @param game the game instance
*/
public LocalThreadBehaviour(T game) {
super(game);
}
/** Starts the game loop in a new thread. */
@Override
public void start() {
if (isRunning.compareAndSet(false, true)) {
new Thread(this).start();
}
}
/** Stops the game loop after the current iteration. */
@Override
public void stop() {
isRunning.set(false);
}
/**
* Main game loop: gets the current player's move, applies it,
* updates the UI, and handles end-of-game states.
*/
@Override
public void run() {
while (isRunning.get()) {
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
long move = currentPlayer.getMove(game.deepCopy());
PlayResult result = game.play(move);
updateUI();
GameState state = result.state();
switch (state) {
case WIN, DRAW -> {
isRunning.set(false);
new EventFlow().addPostEvent(
GUIEvents.GameEnded.class,
state == GameState.WIN,
result.player()
).postEvent();
}
case NORMAL, TURN_SKIPPED -> { /* continue normally */ }
default -> {
logger.error("Unexpected state {}", state);
isRunning.set(false);
throw new RuntimeException("Unknown state: " + state);
}
}
}
}
}

View File

@@ -0,0 +1,84 @@
package org.toop.framework.game.gameThreads;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
import org.toop.framework.gameFramework.view.GUIEvents;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.player.Player;
import org.toop.framework.game.players.OnlinePlayer;
/**
* Handles online multiplayer game logic.
* <p>
* Reacts to server events, sending moves and updating the game state
* for the local player while receiving moves from other players.
*/
public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements SupportsOnlinePlay {
/**
* Creates behaviour and sets the first local player
* (non-online player) from the given array.
*/
public OnlineThreadBehaviour(T game) {
super(game);
}
/** Finds the first non-online player in the array. */
private int getFirstNotOnlinePlayer(Player<T>[] players) {
for (int i = 0; i < players.length; i++) {
if (!(players[i] instanceof OnlinePlayer)) {
return i;
}
}
throw new RuntimeException("All players are online players");
}
/** Starts processing network events for the local player. */
@Override
public void start() {
isRunning.set(true);
}
/** Stops processing network events. */
@Override
public void stop() {
isRunning.set(false);
}
/**
* Called when the server notifies that it is the local player's turn.
* Sends the generated move back to the server.
*/
@Override
public void onYourTurn(long clientId) {
if (!isRunning.get()) return;
long move = game.getPlayer(game.getCurrentTurn()).getMove(game.deepCopy());
sendMove(clientId, move);
}
/**
* Handles a move received from the server for any player.
* Updates the game state and triggers a UI refresh.
*/
public void onMoveReceived(long move) {
if (!isRunning.get()) return;
game.play(move);
updateUI();
}
/**
* Handles the end of the game as notified by the server.
* Updates the UI to show a win or draw result for the local player.
*/
public void gameFinished(String condition) {
switch(condition.toUpperCase()){
case "WIN", "LOSS" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, true, game.getWinner()).postEvent();
case "DRAW" -> new EventFlow().addPostEvent(GUIEvents.GameEnded.class, false, -1).postEvent();
default -> {
logger.error("Invalid condition");
throw new RuntimeException("Unknown condition");
}
}
}
}

View File

@@ -0,0 +1,39 @@
package org.toop.framework.game.gameThreads;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
/**
* Online thread behaviour that adds a fixed delay before processing
* the local player's turn.
* <p>
* This is identical to {@link OnlineThreadBehaviour}, but inserts a
* short sleep before delegating to the base implementation.
*/
public class OnlineWithSleepThreadBehaviour<T extends TurnBasedGame<T>> extends OnlineThreadBehaviour<T> {
/**
* Creates the behaviour and forwards the players to the base class.
*
* @param game the online-capable turn-based game
*/
public OnlineWithSleepThreadBehaviour(T game) {
super(game);
}
/**
* Waits briefly before handling the "your turn" event.
*
* @param event the network event indicating it's this client's turn
*/
@Override
public void onYourTurn(long clientId) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.onYourTurn(clientId);
}
}

View File

@@ -0,0 +1,85 @@
package org.toop.framework.game.players;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
// Future can be used with event system, IF unsubscribeAfterSuccess works...
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
private CompletableFuture<Long> LastMove;
public LocalPlayer(String name) {
super(name);
}
public LocalPlayer(LocalPlayer<T> other) {
super(other);
}
@Override
public long getMove(T gameCopy) {
return getValidMove(gameCopy);
}
public void setMove(long move) {
LastMove.complete(move);
}
// TODO: helper function, would like to replace to get rid of this method
public static boolean contains(int[] array, int value){
for (int i : array) if (i == value) return true;
return false;
}
private long getMove2(T gameCopy) {
LastMove = new CompletableFuture<>();
long move = 0;
try {
move = LastMove.get();
System.out.println(Long.toBinaryString(move));
} catch (InterruptedException | ExecutionException e) {
// TODO: Add proper logging.
e.printStackTrace();
}
return move;
}
protected long getValidMove(T gameCopy){
// Get this player's valid moves
long validMoves = gameCopy.getLegalMoves();
// Make sure provided move is valid
// TODO: Limit amount of retries?
// TODO: Stop copying game so many times
long move = getMove2(gameCopy.deepCopy());
while ((validMoves & move) == 0) {
System.out.println("Not a valid move, try again");
move = getMove2(gameCopy.deepCopy());
}
return move;
}
@Override
public LocalPlayer<T> deepCopy() {
return new LocalPlayer<T>(this.getName());
}
/*public void register() {
// Listening to PlayerAttemptedMove
new EventFlow().listen(GUIEvents.PlayerAttemptedMove.class, event -> {
if (!LastMove.isDone()) {
LastMove.complete(event.move()); // complete the future
}
}, true); // auto-unsubscribe
}
// This blocks until the next move arrives
public int take() throws ExecutionException, InterruptedException {
int move = LastMove.get(); // blocking
LastMove = new CompletableFuture<>(); // reset for next move
return move;
}*/
}

View File

@@ -0,0 +1,165 @@
package org.toop.framework.game.players;
import org.toop.framework.gameFramework.GameState;
import org.toop.framework.gameFramework.model.game.PlayResult;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.AbstractAI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
private final int maxDepth;
private final Random random = new Random();
public MiniMaxAI(int depth) {
this.maxDepth = depth;
}
public MiniMaxAI(MiniMaxAI<T> other) {
this.maxDepth = other.maxDepth;
}
@Override
public MiniMaxAI<T> deepCopy() {
return new MiniMaxAI<>(this);
}
@Override
public long getMove(T game) {
long legalMoves = game.getLegalMoves();
if (legalMoves == 0) return 0;
List<Long> bestMoves = new ArrayList<>();
int bestScore = Integer.MIN_VALUE;
int aiPlayer = game.getCurrentTurn();
long movesLoop = legalMoves;
while (movesLoop != 0) {
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
T copy = game.deepCopy();
PlayResult result = copy.play(move);
int score;
switch (result.state()) {
case WIN -> score = (result.player() == aiPlayer ? maxDepth : -maxDepth);
case DRAW -> score = 0;
default -> score = getMoveScore(copy, maxDepth - 1, false, aiPlayer, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
if (score > bestScore) {
bestScore = score;
bestMoves.clear();
bestMoves.add(move);
} else if (score == bestScore) {
bestMoves.add(move);
}
movesLoop &= movesLoop - 1;
}
long chosenMove = bestMoves.get(random.nextInt(bestMoves.size()));
return chosenMove;
}
/**
* Recursive minimax with alpha-beta pruning and heuristic evaluation.
*
* @param game Current game state
* @param depth Remaining depth
* @param maximizing True if AI is maximizing, false if opponent
* @param aiPlayer AI's player index
* @param alpha Alpha value
* @param beta Beta value
* @return score of the position
*/
private int getMoveScore(T game, int depth, boolean maximizing, int aiPlayer, int alpha, int beta) {
long legalMoves = game.getLegalMoves();
// Terminal state
PlayResult lastResult = null;
if (legalMoves == 0) {
lastResult = new PlayResult(GameState.DRAW, -1);
}
// If the game is over or depth limit reached, evaluate
if (depth <= 0 || legalMoves == 0) {
if (lastResult != null) return 0;
return evaluateBoard(game, aiPlayer);
}
int bestScore = maximizing ? Integer.MIN_VALUE : Integer.MAX_VALUE;
long movesLoop = legalMoves;
while (movesLoop != 0) {
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
T copy = game.deepCopy();
PlayResult result = copy.play(move);
int score;
switch (result.state()) {
case WIN -> score = (result.player() == aiPlayer ? depth : -depth);
case DRAW -> score = 0;
default -> score = getMoveScore(copy, depth - 1, !maximizing, aiPlayer, alpha, beta);
}
if (maximizing) {
bestScore = Math.max(bestScore, score);
alpha = Math.max(alpha, bestScore);
} else {
bestScore = Math.min(bestScore, score);
beta = Math.min(beta, bestScore);
}
// Alpha-beta pruning
if (beta <= alpha) break;
movesLoop &= movesLoop - 1;
}
return bestScore;
}
/**
* Simple heuristic evaluation for Reversi-like games.
* Positive = good for AI, Negative = good for opponent.
*
* @param game Game state
* @param aiPlayer AI's player index
* @return heuristic score
*/
private int evaluateBoard(T game, int aiPlayer) {
long[] board = game.getBoard();
int aiCount = 0;
int opponentCount = 0;
// Count pieces for AI vs opponent
for (int i = 0; i < board.length; i++) {
long bits = board[i];
for (int j = 0; j < 64; j++) {
if ((bits & (1L << j)) != 0) {
// Assume player 0 occupies even indices, player 1 occupies odd
if ((i * 64 + j) % game.getPlayerCount() == aiPlayer) aiCount++;
else opponentCount++;
}
}
}
// Mobility (number of legal moves)
int mobility = Long.bitCount(game.getLegalMoves());
// Corner control (top-left, top-right, bottom-left, bottom-right)
int corners = 0;
long[] cornerMasks = {1L << 0, 1L << 7, 1L << 56, 1L << 63};
for (long mask : cornerMasks) {
for (long b : board) {
if ((b & mask) != 0) corners += 1;
}
}
// Weighted sum
return (aiCount - opponentCount) + 2 * mobility + 5 * corners;
}
}

View File

@@ -0,0 +1,36 @@
package org.toop.framework.game.players;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
import org.toop.framework.gameFramework.model.player.Player;
/**
* Represents a player controlled remotely or over a network.
* <p>
* This class extends {@link AbstractPlayer} and can be used to implement game logic
* where moves are provided by an external source (e.g., another user or a server).
* Currently, this class is a placeholder and does not implement move logic.
* </p>
*/
public class OnlinePlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
/**
* Constructs a new OnlinePlayer.
* <p>
* Currently, no additional initialization is performed. Subclasses or
* future implementations should provide mechanisms to receive moves from
* an external source.
*/
public OnlinePlayer(String name) {
super(name);
}
public OnlinePlayer(OnlinePlayer<T> other) {
super(other);
}
@Override
public Player<T> deepCopy() {
return new OnlinePlayer<>(this);
}
}

View File

@@ -0,0 +1,38 @@
package org.toop.framework.game.players;
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
import org.toop.framework.gameFramework.model.player.AbstractAI;
import java.util.Random;
public class RandomAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
public RandomAI() {
super();
}
@Override
public RandomAI<T> deepCopy() {
return new RandomAI<T>();
}
@Override
public long getMove(T game) {
long legalMoves = game.getLegalMoves();
int move = new Random().nextInt(Long.bitCount(legalMoves));
return nthBitIndex(legalMoves, move);
}
public static long nthBitIndex(long bb, int n) {
while (bb != 0) {
int tz = Long.numberOfTrailingZeros(bb);
if (n == 0) {
return 1L << tz;
}
bb &= bb - 1; // clear the least significant 1
n--;
}
return 0L; // not enough 1s
}
}

View File

@@ -1,8 +1,7 @@
package org.toop.framework.gameFramework.controller;
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
import org.toop.framework.gameFramework.model.game.threadBehaviour.Controllable;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.networking.connection.events.NetworkEvents;
public interface GameController extends Controllable, UpdatesGameUI {
/** Called when it is this player's turn to make a move. */

View File

@@ -1,7 +1,5 @@
package org.toop.framework.gameFramework.model.game;
import org.toop.framework.networking.events.NetworkEvents;
/**
* Interface for games that support online multiplayer play.
* <p>

View File

@@ -1,13 +1,13 @@
package org.toop.framework.networking;
package org.toop.framework.networking.connection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.SnowflakeGenerator;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.eventbus.bus.EventBus;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.networking.exceptions.ClientNotFoundException;
import org.toop.framework.networking.interfaces.NetworkingClientManager;
import org.toop.framework.networking.connection.events.NetworkEvents;
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
import org.toop.framework.networking.connection.interfaces.NetworkingClientManager;
public class NetworkingClientEventListener {
private static final Logger logger = LogManager.getLogger(NetworkingClientEventListener.class);

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking;
package org.toop.framework.networking.connection;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -10,13 +10,13 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.eventbus.bus.EventBus;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.networking.exceptions.ClientNotFoundException;
import org.toop.framework.networking.exceptions.CouldNotConnectException;
import org.toop.framework.networking.interfaces.NetworkingClient;
import org.toop.framework.networking.types.NetworkingConnector;
import org.toop.framework.networking.connection.events.NetworkEvents;
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
import org.toop.framework.networking.connection.types.NetworkingConnector;
public class NetworkingClientManager implements org.toop.framework.networking.interfaces.NetworkingClientManager {
public class NetworkingClientManager implements org.toop.framework.networking.connection.interfaces.NetworkingClientManager {
private static final Logger logger = LogManager.getLogger(NetworkingClientManager.class);
private final EventBus eventBus;

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.clients;
package org.toop.framework.networking.connection.clients;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
@@ -12,9 +12,9 @@ import io.netty.util.CharsetUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.bus.EventBus;
import org.toop.framework.networking.exceptions.CouldNotConnectException;
import org.toop.framework.networking.handlers.NetworkingGameClientHandler;
import org.toop.framework.networking.interfaces.NetworkingClient;
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
import org.toop.framework.networking.connection.handlers.NetworkingGameClientHandler;
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
import java.net.InetSocketAddress;

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.events;
package org.toop.framework.networking.connection.events;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@@ -6,8 +6,8 @@ import java.util.concurrent.CompletableFuture;
import org.toop.annotations.AutoResponseResult;
import org.toop.framework.eventbus.GlobalEventBus;
import org.toop.framework.eventbus.events.*;
import org.toop.framework.networking.interfaces.NetworkingClient;
import org.toop.framework.networking.types.NetworkingConnector;
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
import org.toop.framework.networking.connection.types.NetworkingConnector;
/**
* Defines all event types related to the networking subsystem.

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.exceptions;
package org.toop.framework.networking.connection.exceptions;
/**
* Thrown when an operation is attempted on a networking client

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.exceptions;
package org.toop.framework.networking.connection.exceptions;
public class CouldNotConnectException extends RuntimeException {

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.exceptions;
package org.toop.framework.networking.connection.exceptions;
public class NetworkingInitializationException extends RuntimeException {
public NetworkingInitializationException(String message, Throwable cause) {

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.handlers;
package org.toop.framework.networking.connection.handlers;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -9,7 +9,7 @@ import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.bus.EventBus;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.networking.connection.events.NetworkEvents;
public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = LogManager.getLogger(NetworkingGameClientHandler.class);

View File

@@ -1,6 +1,6 @@
package org.toop.framework.networking.interfaces;
package org.toop.framework.networking.connection.interfaces;
import org.toop.framework.networking.exceptions.CouldNotConnectException;
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
import java.net.InetSocketAddress;

View File

@@ -1,8 +1,8 @@
package org.toop.framework.networking.interfaces;
package org.toop.framework.networking.connection.interfaces;
import org.toop.framework.networking.exceptions.ClientNotFoundException;
import org.toop.framework.networking.exceptions.CouldNotConnectException;
import org.toop.framework.networking.types.NetworkingConnector;
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
import org.toop.framework.networking.connection.types.NetworkingConnector;
public interface NetworkingClientManager {
void startClient(

View File

@@ -1,4 +1,4 @@
package org.toop.framework.networking.types;
package org.toop.framework.networking.connection.types;
import java.util.concurrent.TimeUnit;

View File

@@ -1,3 +1,3 @@
package org.toop.framework.networking.types;
package org.toop.framework.networking.connection.types;
public record ServerCommand(long clientId, String command) {}

View File

@@ -0,0 +1,3 @@
package org.toop.framework.networking.connection.types;
public record ServerMessage(String message) {}

View File

@@ -0,0 +1,30 @@
package org.toop.framework.networking.server;
import org.toop.framework.game.BitboardGame;
public class Game implements OnlineGame {
private long id;
private User[] users;
private GameDefinition<BitboardGame<?>> game;
public Game(GameDefinition game, User... users) {
this.game = game;
this.users = users;
}
@Override
public long id() {
return id;
}
@Override
public GameDefinition game() {
return game;
}
@Override
public User[] users() {
return users;
}
}

View File

@@ -0,0 +1,26 @@
package org.toop.framework.networking.server;
import java.lang.reflect.InvocationTargetException;
public class GameDefinition<T> {
private final String name;
private final Class<T> game;
public GameDefinition(String name, Class<T> game) {
this.name = name;
this.game = game;
}
public String name() {
return name;
}
public T create(String... users) {
try {
return game.getDeclaredConstructor().newInstance(users);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,8 @@
package org.toop.framework.networking.server;
public interface GameServer {
// List<?> gameTypes();
// List<?> ongoingGames();
// void startGame(String gameType, User... users);
// String[] onlineUsers();
}

View File

@@ -0,0 +1,68 @@
package org.toop.framework.networking.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.toop.framework.SnowflakeGenerator;
import org.toop.framework.game.BitboardGame;
import java.util.Map;
public class MasterServer {
private final int port;
public final Server gs;
public MasterServer(int port, Map<String, GameDefinition<BitboardGame<?>>> gameTypes) {
this.port = port;
this.gs = new Server(gameTypes);
}
public void start() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.option(ChannelOption.SO_BACKLOG, 128);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
bootstrap.handler(new LoggingHandler(LogLevel.INFO));
bootstrap.childHandler(
new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LineBasedFrameDecoder(8192));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
long userid = SnowflakeGenerator.nextId();
User user = new User(userid, ""+userid);
pipeline.addLast(new ServerHandler(user, gs));
}
}
);
ChannelFuture future = bootstrap.bind(port).sync();
System.out.println("MasterServer listening on port " + port);
future.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}

View File

@@ -0,0 +1,7 @@
package org.toop.framework.networking.server;
public interface MessageStore {
void add(String message);
String get();
void reset();
}

View File

@@ -0,0 +1,7 @@
package org.toop.framework.networking.server;
public interface OnlineGame {
long id();
GameDefinition game();
User[] users();
}

View File

@@ -0,0 +1,4 @@
package org.toop.framework.networking.server;
public record ParsedMessage(String command, String... args) {}

View File

@@ -0,0 +1,4 @@
package org.toop.framework.networking.server;
public class Parser {
}

View File

@@ -0,0 +1,4 @@
package org.toop.framework.networking.server;
public interface ServableGame {
}

View File

@@ -0,0 +1,51 @@
package org.toop.framework.networking.server;
import org.toop.framework.game.BitboardGame;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Server implements GameServer {
final private Map<String, GameDefinition<BitboardGame<?>>> gameTypes;
public List<OnlineGame> games = new ArrayList<>();
final private Map<Long, ServerUser> users = new ConcurrentHashMap<>();
public Server(Map<String, GameDefinition<BitboardGame<?>>> gameTypes) {
this.gameTypes = gameTypes;
}
public void addUser(ServerUser user) {
users.putIfAbsent(user.id(), user);
}
public void removeUser(ServerUser user) {
users.remove(user.id());
}
public String[] gameTypes() {
return gameTypes.keySet().toArray(new String[0]);
}
// public List<OnlineGame<BitboardGame<?>>> ongoingGames() {
// return List.of();
// }
public void startGame(String gameType, User... users) {
if (!gameTypes.containsKey(gameType)) return;
try {
var game = new Game(gameTypes.get(gameType).create(), users);
games.addLast(new Game(game, users));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String[] onlineUsers() {
return users.values().stream().map(ServerUser::name).toArray(String[]::new);
}
}

View File

@@ -0,0 +1,117 @@
package org.toop.framework.networking.server;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class ServerHandler extends SimpleChannelInboundHandler<String> {
private final User user;
private final Server server;
public ServerHandler(User user, Server server) {
this.user = user;
this.server = server;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush("WELCOME " + user.id() + "\n");
user.setCtx(ctx);
server.addUser(user); // TODO set correct name on login
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
ParsedMessage p = parse(msg);
if (p == null) return;
IO.println(p.command() + " " + Arrays.toString(p.args()));
switch (p.command()) {
case "ping" -> ctx.writeAndFlush("PONG\n");
case "login" -> handleLogin(p);
case "get" -> handleGet(p);
case "subscribe" -> handleSubscribe(p);
case "move" -> handleMove(p);
case "challenge" -> handleChallenge(p);
case "message" -> handleMessage(p);
case "help" -> handleHelp(p);
default -> ctx.writeAndFlush("ERROR Unknown command\n");
}
}
private boolean allowedArgs(String... args) {
if (args.length < 1) return false;
return true;
}
private void handleLogin(ParsedMessage p) {
if (!allowedArgs(p.args())) return;
user.setName(p.args()[0]);
}
private void handleGet(ParsedMessage p) {
if (!allowedArgs(p.args())) return;
switch (p.args()[0]) {
case "playerlist" -> user.ctx().writeAndFlush(Arrays.toString(server.onlineUsers()));
case "gamelist" -> user.ctx().writeAndFlush(Arrays.toString(server.gameTypes()));
}
}
private void handleSubscribe(ParsedMessage p) {
// TODO
}
private void handleHelp(ParsedMessage p) {
// TODO
}
private void handleMessage(ParsedMessage p) {
// TODO
}
private void handleChallenge(ParsedMessage p) {
// TODO
}
private void handleMove(ParsedMessage p) {
// TODO
}
private ParsedMessage parse(String msg) {
// TODO, what if empty string.
if (msg.isEmpty()) return null;
msg = msg.trim().toLowerCase();
List<String> parts = new LinkedList<>(List.of(msg.split(" ")));
if (parts.size() > 1) {
String command = parts.removeFirst();
return new ParsedMessage(command, parts.toArray(String[]::new));
}
else {
return new ParsedMessage(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
server.removeUser(user);
}
}

View File

@@ -0,0 +1,27 @@
package org.toop.framework.networking.server;
import java.util.Queue;
public class ServerMessageStore implements MessageStore {
Queue<String> messageQueue;
public ServerMessageStore(Queue<String> messageQueue) {
this.messageQueue = messageQueue;
}
@Override
public void add(String message) {
messageQueue.offer(message);
}
@Override
public String get() {
return messageQueue.poll();
}
@Override
public void reset() {
messageQueue.clear();
}
}

View File

@@ -0,0 +1,9 @@
package org.toop.framework.networking.server;
import java.net.InetSocketAddress;
public interface ServerUser {
long id();
String name();
void setName(String name);
}

View File

@@ -0,0 +1,40 @@
package org.toop.framework.networking.server;
import io.netty.channel.ChannelHandlerContext;
import java.net.InetSocketAddress;
public class User implements ServerUser {
final private long id;
private String name;
private ChannelHandlerContext connectionContext;
public User(long userId, String name) {
this.id = userId;
this.name = name;
}
@Override
public long id() {
return id;
}
@Override
public String name() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
public ChannelHandlerContext ctx() {
return connectionContext;
}
public void setCtx(ChannelHandlerContext ctx) {
this.connectionContext = ctx;
}
}

View File

@@ -1,3 +0,0 @@
package org.toop.framework.networking.types;
public record ServerMessage(String message) {}

View File

@@ -9,7 +9,7 @@
//import org.mockito.*;
//import org.toop.framework.SnowflakeGenerator;
//import org.toop.framework.eventbus.EventFlow;
//import org.toop.framework.networking.events.NetworkEvents;
//import org.toop.framework.networking.connection.events.NetworkEvents;
//
//class NetworkingClientManagerTest {
//

View File

@@ -1,4 +1,4 @@
//package org.toop.framework.networking.events;
//package org.toop.framework.networking.connection.events;
//
//import static org.junit.jupiter.api.Assertions.*;
//