Add: complete JavaDOC to ServerLauncher

This commit is contained in:
2026-04-25 18:16:13 +02:00
parent 27903f1738
commit 6ebfd4d026
3 changed files with 38 additions and 26 deletions
@@ -52,7 +52,7 @@ public class RMIServer implements IGameServer {
if (controller.addPlayer(username)) {
if(playerList.size()==0)
{
Game game= new Game(preferredInt);
Game game = new Game(preferredInt);
controller.setModel(game);
playerList.setLimit(preferredInt);
}
@@ -118,7 +118,7 @@ public class TCPServer {
this.playerList = players;
}
public void notifAll(NetworkEvent event){
public void notifyAll(NetworkEvent event){
clientHandlers.forEach((x) -> x.notifyEvent(event));
}
@@ -23,18 +23,18 @@ import java.util.concurrent.LinkedBlockingQueue;
* - The TCP/RMI server creates the {@link Game} with the requested number of players and adds the player to {@link #playerList}
* - Other players request to join the game (their requested number of players is ignored)
* - When the number of players in {@link #playerList} reaches the {@link LimitedList}'s limit, the list calls {@link #run()}
* - All players are notified with the {@link Game}
* - All players are notified of the {@link Game}
*
* The process flow for game execution is as follows:
* - The TCP/RMI server receives a {@link NetworkEvent} from a client and adds it to the {@link #actionQueue}
* - The {@link #run()} method repeatedly calls {@link #doFirstEvent()}, which takes the first event in the {@link #actionQueue} and tries to apply it
* - If the event is successfully applied to the model, all players receive the event
* - Otherwise, the player who sent the action receives an error notification
* - If the event cannot be successfully applied to the model, its {@code isError} flag is set to {@code true}
* - All players are notified of the event
*/
public class ServerLauncher {
/**
* List containing the events that have to be applied to the game's model.
* Queue containing the events to be applied to the game model.
* Thread safe by design.
*/
BlockingQueue<NetworkEvent> actionQueue;
@@ -49,9 +49,9 @@ public class ServerLauncher {
TCPServer serverTCP;
/**
* List containing the username of joined players.
* List containing the usernames of joined players.
* {@link LimitedList}'s limit defines at which size the list calls its action
* Both limit and action can be set with {@link LimitedList#setLimit(int)} and {@link LimitedList#setAction(Runnable)}
* Both the limit and the action can be set using {@link LimitedList#setLimit(int)} and {@link LimitedList#setAction(Runnable)}
* The limit is set by the first player joining the game. The action consists in calling {@link #run()}
*/
static LimitedList<String> playerList;
@@ -59,10 +59,10 @@ public class ServerLauncher {
/**
* Class constructor that initializes the attributes.
* @param actionQueue is the list containing the events
* @param gameController is the game controller
* @param serverRMI is the server RMI
* @param serverTCP is the server TCP
* @param actionQueue The queue containing the events
* @param gameController The game controller
* @param serverRMI The server RMI
* @param serverTCP The server TCP
*/
public ServerLauncher(BlockingQueue<NetworkEvent> actionQueue, GameController gameController, RMIServer serverRMI, TCPServer serverTCP) {
this.actionQueue = actionQueue;
@@ -73,29 +73,34 @@ public class ServerLauncher {
/**
* Takes the first event in the actionQueue and attempt to apply it to the game controller.
* If the event can be applied, all clients (both TCP and RMI) are notified with the event. Otherwise, the user who sent the action will be notified with an error.
* @return the outcome of attempting to apply the event to the controller
* @throws InterruptedException if any problem in accessing actionQueue is issued
* @throws RemoteException if any RMI problem is issued
* Takes the first event in the actionQueue and attempts to apply it to the game controller.
* If the event cannot be applied, its isError flag is set to true; otherwise, it is set to false.
* All clients (both TCP and RMI) are notified of the event
* @return the outcome of applying the event to the controller
* @throws InterruptedException if an error occurs while accessing the actionQueue
* @throws RemoteException if an RMI error occurs
*/
public boolean doFirstEvent() throws InterruptedException, RemoteException {
NetworkEvent event = actionQueue.take();
event.setIsError(!event.apply(gameController));
serverRMI.notifyAll(event);
serverTCP.notifAll(event);
serverTCP.notifyAll(event);
return !event.getIsError();
}
public static void main() throws RemoteException, InterruptedException {
// TODO
// Deve avere una coda con gli eventi.
// Il server RMI e il server TCP quando ricevono un doEvent devono aggiungere l'evento alla coda condivisa
// Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP
/**
* The first method executed when the server program is launched.
* It creates all the objects needed: playerList, actionQueue, gameController, serverRMI, serverTCP, launcher.
* Then sets the playerList's action to execute launcher.run() and starts the TCP/RMI servers.
* Note: the model is initialized and set in the controller in TCP/RMI servers when the first user decides the number of players.
*
* @throws InterruptedException if this exception is issued by run method
* @throws RemoteException if this exception is issued by run method
*/
public static void main(String[] args) throws InterruptedException, RemoteException {
playerList = new LimitedList<>(5, ()->{});
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController();
@@ -117,12 +122,19 @@ public class ServerLauncher {
new Thread(()->{serverTCP.start();}).start();
}
public void run() throws InterruptedException, RemoteException {
/**
* Creates and executes the game.
* Game creation: TCP/RMI servers send the game model to all players.
* Game execution: repeatedly calls doFirstEvent() to process the events in the actionQueue.
* @throws InterruptedException if the TCP server thread is interrupted
* @throws RemoteException if an RMI error occurs
*/
public void run() throws InterruptedException, RemoteException {
// Game creation
serverRMI.notifyAll(gameController.getModel());
serverTCP.notifyAll(gameController.getModel());
// Game execution
while (true) {
try {