Add: complete JavaDOC to ClientHandler
This commit is contained in:
@@ -8,66 +8,98 @@ import java.net.*;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.BlockingQueue;
|
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 {
|
public class ClientHandler implements Runnable {
|
||||||
|
|
||||||
|
/** The TCP socket */
|
||||||
private Socket clientSocket;
|
private Socket clientSocket;
|
||||||
|
|
||||||
|
/** Input stream used to receive requests from the client */
|
||||||
public ObjectInputStream in;
|
public ObjectInputStream in;
|
||||||
|
|
||||||
|
/** Output stream used to send objects to the client */
|
||||||
public ObjectOutputStream out;
|
public ObjectOutputStream out;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shared list of all client handlers.
|
||||||
|
* This handler removes itself from the list when disconnected.
|
||||||
|
*/
|
||||||
List<ClientHandler> clientHandlers;
|
List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
|
/** Queue containing the events to be applied to the game model */
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
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) {
|
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
||||||
this.clientSocket = clientSocket;
|
this.clientSocket = clientSocket;
|
||||||
this.clientHandlers = clientHandlers;
|
this.clientHandlers = clientHandlers;
|
||||||
this.actionQueue = actionQueue;
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
in = new ObjectInputStream(clientSocket.getInputStream());
|
in = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
NetworkEvent event = (NetworkEvent) in.readObject();
|
NetworkEvent event = (NetworkEvent) in.readObject();
|
||||||
if (!actionQueue.add(event)) {
|
if (!actionQueue.add(event)) {
|
||||||
System.out.println("Error inserting action into queue");
|
System.out.println("Error inserting action into queue");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(IOException e){
|
|
||||||
clientHandlers.remove(this);
|
clientHandlers.remove(this);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
} catch (ClassNotFoundException e) {
|
||||||
catch(ClassNotFoundException e){
|
|
||||||
throw new RuntimeException(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) {
|
public void notifyEvent(NetworkEvent event) {
|
||||||
synchronized (out) {
|
synchronized (out) {
|
||||||
try {
|
try {
|
||||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
out.writeObject(event);
|
out.writeObject(event);
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
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) {
|
public void notifyModel(Game game) {
|
||||||
synchronized (out) {
|
synchronized (out) {
|
||||||
try {
|
try {
|
||||||
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
out.writeObject(game);
|
out.writeObject(game);
|
||||||
}
|
} catch (IOException e) {
|
||||||
catch(IOException e){
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,24 @@ public class TCPServer {
|
|||||||
private List<ClientHandler> clientHandlers;
|
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.
|
* Starts the TCP server.
|
||||||
* If the first event is not AddPlayer, the request is rejected.
|
* 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 */
|
/** Sends an action to all TCP clients */
|
||||||
public void notifyAll(NetworkEvent event){
|
public void notifyAll(NetworkEvent event){
|
||||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||||
|
|||||||
Reference in New Issue
Block a user