Fix: removed unused class in ClientHandler
Add: complete JavaDOC to TCPServer
This commit is contained in:
@@ -122,7 +122,7 @@ public class RMIServer implements IGameServer {
|
||||
|
||||
|
||||
/**
|
||||
* Sends a model game to all RMI clients.
|
||||
* Sends a game model to all RMI clients.
|
||||
* @param model The desired model
|
||||
*/
|
||||
public void notifyAll(Game model) throws RemoteException {
|
||||
|
||||
@@ -15,9 +15,6 @@ public class ClientHandler implements Runnable {
|
||||
List<ClientHandler> clientHandlers;
|
||||
BlockingQueue<NetworkEvent> actionQueue;
|
||||
|
||||
public Socket getClientSocket() {
|
||||
return clientSocket;
|
||||
}
|
||||
|
||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
|
||||
this.clientSocket = clientSocket;
|
||||
|
||||
@@ -13,24 +13,50 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
|
||||
/**
|
||||
* Server TCP. Accepts connections and manages all client handlers.
|
||||
*/
|
||||
public class TCPServer {
|
||||
|
||||
/** TCP port */
|
||||
int port;
|
||||
|
||||
/** Number of currently connected clients */
|
||||
int ConnectedPlayers;
|
||||
ServerSocket serverTCP;
|
||||
|
||||
/** Socket TCP */
|
||||
ServerSocket socketTCP;
|
||||
|
||||
/** Server game's controller */
|
||||
GameController controller;
|
||||
|
||||
/** Queue containing the events to be applied to the game model */
|
||||
BlockingQueue<NetworkEvent> actionQueue;
|
||||
|
||||
/**
|
||||
* List containing the usernames of joined players.
|
||||
* {@link LimitedList}'s limit defines at which size the list calls its action. The limit can be set using {@link LimitedList#setLimit(int)}.
|
||||
*/
|
||||
private LimitedList<String> playerList;
|
||||
|
||||
/** List containing all client's handlers */
|
||||
private List<ClientHandler> clientHandlers;
|
||||
|
||||
private int getConnectedPlayers(){
|
||||
return ConnectedPlayers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the TCP server.
|
||||
* If the first event is not AddPlayer, the request is rejected.
|
||||
* If the desired number of player is invalid, the request is rejected.
|
||||
*
|
||||
* If this is the first player to connect, a new game model is created and passed to the controller. Additionally, the playerList's limit is set.
|
||||
* If the controller successfully adds the player, the username is added to {@link #playerList} and the handler is added to {@link #clientHandlers}.
|
||||
*
|
||||
* If any error occurs, the server sends -1 back to the client. Otherwise, it sends 1.
|
||||
*/
|
||||
public void start(){
|
||||
|
||||
try{
|
||||
serverTCP = new ServerSocket(port);
|
||||
socketTCP = new ServerSocket(port);
|
||||
}
|
||||
catch (IOException e){
|
||||
System.out.println("Could not start the server TCP on port: " + port);
|
||||
@@ -38,12 +64,12 @@ public class TCPServer {
|
||||
return;
|
||||
}
|
||||
System.out.println("Server TCP started on port: " + port);
|
||||
Socket clientSocket;
|
||||
|
||||
while(true){
|
||||
Socket clientSocket = null;
|
||||
|
||||
try{
|
||||
clientSocket = serverTCP.accept();
|
||||
clientSocket = socketTCP.accept();
|
||||
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
||||
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
||||
|
||||
@@ -93,20 +119,32 @@ public class TCPServer {
|
||||
}
|
||||
}
|
||||
|
||||
public TCPServer(GameController gameController, int port, BlockingQueue<NetworkEvent> actionQueue, LimitedList<String> players){
|
||||
|
||||
/**
|
||||
* 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.serverTCP = null;
|
||||
this.controller = gameController;
|
||||
this.socketTCP = null;
|
||||
this.controller = controller;
|
||||
this.actionQueue = actionQueue;
|
||||
this.playerList = players;
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
/** Sends a game model to all TCP clients */
|
||||
public void notifyAll(Game model){
|
||||
clientHandlers.forEach((x) -> x.notifyModel(model));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user