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
|
* @param model The desired model
|
||||||
*/
|
*/
|
||||||
public void notifyAll(Game model) throws RemoteException {
|
public void notifyAll(Game model) throws RemoteException {
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ public class ClientHandler implements Runnable {
|
|||||||
List<ClientHandler> clientHandlers;
|
List<ClientHandler> clientHandlers;
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
|
|
||||||
public Socket getClientSocket() {
|
|
||||||
return clientSocket;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
|
|||||||
@@ -13,24 +13,50 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server TCP. Accepts connections and manages all client handlers.
|
||||||
|
*/
|
||||||
public class TCPServer {
|
public class TCPServer {
|
||||||
|
|
||||||
|
/** TCP port */
|
||||||
int port;
|
int port;
|
||||||
|
|
||||||
|
/** Number of currently connected clients */
|
||||||
int ConnectedPlayers;
|
int ConnectedPlayers;
|
||||||
ServerSocket serverTCP;
|
|
||||||
|
/** Socket TCP */
|
||||||
|
ServerSocket socketTCP;
|
||||||
|
|
||||||
|
/** Server game's controller */
|
||||||
GameController controller;
|
GameController controller;
|
||||||
|
|
||||||
|
/** Queue containing the events to be applied to the game model */
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
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;
|
private LimitedList<String> playerList;
|
||||||
|
|
||||||
|
/** List containing all client's handlers */
|
||||||
private List<ClientHandler> clientHandlers;
|
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(){
|
public void start(){
|
||||||
|
|
||||||
try{
|
try{
|
||||||
serverTCP = new ServerSocket(port);
|
socketTCP = new ServerSocket(port);
|
||||||
}
|
}
|
||||||
catch (IOException e){
|
catch (IOException e){
|
||||||
System.out.println("Could not start the server TCP on port: " + port);
|
System.out.println("Could not start the server TCP on port: " + port);
|
||||||
@@ -38,12 +64,12 @@ public class TCPServer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Server TCP started on port: " + port);
|
System.out.println("Server TCP started on port: " + port);
|
||||||
|
Socket clientSocket;
|
||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
Socket clientSocket = null;
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
clientSocket = serverTCP.accept();
|
clientSocket = socketTCP.accept();
|
||||||
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
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.port = port;
|
||||||
this.ConnectedPlayers = 0;
|
this.ConnectedPlayers = 0;
|
||||||
this.serverTCP = null;
|
this.socketTCP = null;
|
||||||
this.controller = gameController;
|
this.controller = controller;
|
||||||
this.actionQueue = actionQueue;
|
this.actionQueue = actionQueue;
|
||||||
this.playerList = players;
|
this.playerList = playerList;
|
||||||
this.clientHandlers = new ArrayList<>();
|
this.clientHandlers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Sends a game model to all TCP clients */
|
||||||
public void notifyAll(Game model){
|
public void notifyAll(Game model){
|
||||||
clientHandlers.forEach((x) -> x.notifyModel(model));
|
clientHandlers.forEach((x) -> x.notifyModel(model));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user