Add: complete JavaDOC to ClientHandler

This commit is contained in:
2026-04-26 16:40:49 +02:00
parent 07584ec5a3
commit bce90c2640
2 changed files with 73 additions and 41 deletions
@@ -8,66 +8,98 @@ import java.net.*;
import java.util.List;
import java.util.concurrent.BlockingQueue;
/**
* Handles the TCP connection with a single client.
* Each instance runs on a dedicated thread and is responsible for receiving {@link NetworkEvent} from its client and adding them
* to the {@link #actionQueue}.
* It is also responsible to send events and game model updates back to the client.
*/
public class ClientHandler implements Runnable {
/** The TCP socket */
private Socket clientSocket;
/** Input stream used to receive requests from the client */
public ObjectInputStream in;
/** Output stream used to send objects to the client */
public ObjectOutputStream out;
/**
* Shared list of all client handlers.
* This handler removes itself from the list when disconnected.
*/
List<ClientHandler> clientHandlers;
/** Queue containing the events to be applied to the game model */
BlockingQueue<NetworkEvent> actionQueue;
/**
* Class constructor that initializes the attributes.
* @param clientSocket The socket representing the client's TCP connection
* @param clientHandlers The shared list of all active client handlers
* @param actionQueue The queue containing incoming events
*/
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
this.clientSocket = clientSocket;
this.clientHandlers = clientHandlers;
this.actionQueue = actionQueue;
}
/**
* Listens for incoming {@link NetworkEvent}s from the client and adds them to the {@link #actionQueue}.
* Upon disconnection, this handler removes itself from {@link #clientHandlers}.
*/
@Override
public void run() {
try {
in = new ObjectInputStream(clientSocket.getInputStream());
out = new ObjectOutputStream(clientSocket.getOutputStream());
while (true) {
NetworkEvent event = (NetworkEvent) in.readObject();
if (!actionQueue.add(event)) {
System.out.println("Error inserting action into queue");
}
}
}
catch(IOException e){
} catch (IOException e) {
clientHandlers.remove(this);
e.printStackTrace();
}
catch(ClassNotFoundException e){
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* Sends a {@link NetworkEvent} to the client.
* @param event The network event to send to the client.
*/
public void notifyEvent(NetworkEvent event) {
synchronized (out) {
try {
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(event);
}
catch(IOException e){
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Sends the current game model to this client.
* @param game The current state of the game to send to the client.
*/
public void notifyModel(Game game) {
synchronized (out) {
try {
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(game);
}
catch(IOException e){
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@@ -43,6 +43,24 @@ public class TCPServer {
private List<ClientHandler> clientHandlers;
/**
* Class constructor that initializes the attributes.
* @param controller The game controller
* @param port The TCP port
* @param actionQueue The action queue
* @param playerList The player's usernames list
*/
public TCPServer(GameController controller, int port, BlockingQueue<NetworkEvent> actionQueue, LimitedList<String> playerList){
this.port = port;
this.ConnectedPlayers = 0;
this.socketTCP = null;
this.controller = controller;
this.actionQueue = actionQueue;
this.playerList = playerList;
this.clientHandlers = new ArrayList<>();
}
/**
* Starts the TCP server.
* If the first event is not AddPlayer, the request is rejected.
@@ -120,24 +138,6 @@ public class TCPServer {
}
/**
* Class constructor that initializes the attributes.
* @param controller The game controller
* @param port The TCP port
* @param actionQueue The action queue
* @param playerList The player's usernames list
*/
public TCPServer(GameController controller, int port, BlockingQueue<NetworkEvent> actionQueue, LimitedList<String> playerList){
this.port = port;
this.ConnectedPlayers = 0;
this.socketTCP = null;
this.controller = controller;
this.actionQueue = actionQueue;
this.playerList = playerList;
this.clientHandlers = new ArrayList<>();
}
/** Sends an action to all TCP clients */
public void notifyAll(NetworkEvent event){
clientHandlers.forEach((x) -> x.notifyEvent(event));