Merge branch 'main' into javadoc-fixes

This commit is contained in:
rubenpirreram
2026-04-30 20:11:10 +02:00
committed by GitHub
19 changed files with 1541 additions and 509 deletions
@@ -0,0 +1,8 @@
package it.polimi.ingsw.gc14.Network;
import java.rmi.RemoteException;
public interface IClient {
public boolean connect(String username,int preferredInt);
public void doEvent(NetworkEvent event) ;
}
@@ -39,6 +39,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
@Override
public void onGameInit(Game model) throws RemoteException {
clientController.setModel(model);
clientController.view.render();
}
@@ -55,7 +56,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
System.out.println(event.toString());
} else {
event.apply(clientController.localController);
//clientController.view.update(); TODO
clientController.view.render();
}
}
}
@@ -4,6 +4,7 @@ import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
@@ -11,7 +12,7 @@ import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
/**
* Client RMI. Uses the methods exposed by the server RMI.
*/
public class RMIClient {
public class RMIClient implements IClient {
/** The host address of the RMI server */
private final String host;
@@ -22,13 +23,18 @@ public class RMIClient {
/** The remote stub used to call methods on the server */
private IGameServer stub;
/** Client game's controller */
ClientController controller;
/**
* Class constructor.
* @param controller the client controller used to create the callback
* @param host the host address of the RMI server
* @param port the port of the RMI server
*/
public RMIClient(String host, int port) {
public RMIClient(ClientController controller, String host, int port) {
this.controller=controller;
this.host = host;
this.port = port;
}
@@ -40,14 +46,13 @@ public class RMIClient {
* Then, creates a {@link ClientCallbackImpl} and calls {@link RMIServer#joinGame(String, int, IClientCallback)}.
* @param username the player's username
* @param preferredInt the desired number of players
* @param clientController the client controller used to create the callback
* @return true if the player successfully joined the game, false otherwise
*/
public boolean connect(String username,int preferredInt, ClientController clientController) {
public boolean connect(String username,int preferredInt) {
try {
Registry registry = LocateRegistry.getRegistry(host, port);
this.stub = (IGameServer) registry.lookup("RMIGameServer");
ClientCallbackImpl callback = new ClientCallbackImpl(clientController);
ClientCallbackImpl callback = new ClientCallbackImpl(controller);
return stub.joinGame(username, preferredInt, callback);
}
@@ -63,8 +68,12 @@ public class RMIClient {
* @param event the event to send
* @throws RemoteException if any RMI error occurs
*/
public void doEvent(NetworkEvent event) throws RemoteException {
stub.doEvent(event);
public void doEvent(NetworkEvent event) {
try {
stub.doEvent(event);
}catch (Exception e) {
}
}
}
@@ -1,7 +1,8 @@
package it.polimi.ingsw.gc14.Network.TCP.Client;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
@@ -11,7 +12,7 @@ import java.net.*;
/**
* Client TCP. Sends and receives messages with the TCP server.
*/
public class TCPClient {
public class TCPClient implements IClient {
/** Socket TCP */
Socket communicationSocket;
@@ -23,7 +24,7 @@ public class TCPClient {
ObjectOutputStream socketSend;
/** Client game's controller */
GameController controller;
ClientController controller;
/** IP address of the server to connect to */
String hostname;
@@ -38,7 +39,7 @@ public class TCPClient {
* @param hostname The IP address of the server
* @param port The TCP port of the server
*/
public TCPClient(GameController controller, String hostname, int port) {
public TCPClient(ClientController controller, String hostname, int port) {
this.controller = controller;
this.hostname = hostname;
this.port = port;
@@ -53,13 +54,15 @@ public class TCPClient {
* @param proposedNPlayers The desired number of players for the game
* @return true if the connection is successful, false otherwise.
*/
public boolean start(String user, int proposedNPlayers) {
public boolean connect(String user, int proposedNPlayers) {
try {
communicationSocket = new Socket(hostname, port);
socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());
socketReceive = new ObjectInputStream(communicationSocket.getInputStream());
sendEvent(new AddPlayer(user, proposedNPlayers));
doEvent(new AddPlayer(user, proposedNPlayers));
if (communicationSocket.getInputStream().read() == -1) {
System.out.println("Could not connect to server");
return false;
@@ -89,11 +92,12 @@ public class TCPClient {
if (event.getIsError()) {
System.out.println(event);
} else {
event.apply(controller);
//clientController.view.update(); TODO
event.apply(controller.localController);
controller.view.render();
}
} else if (read instanceof Game model) {
controller.setModel(model);
controller.view.render();
}
} catch (IOException e) {
e.printStackTrace();
@@ -108,7 +112,7 @@ public class TCPClient {
* Sends a {@link NetworkEvent} to the server.
* @param event The NetworkEvent to send.
*/
private void sendEvent(NetworkEvent event) {
public void doEvent(NetworkEvent event) {
try {
socketSend.writeObject(event);
} catch (IOException e) {
@@ -41,8 +41,10 @@ public class ClientHandler implements Runnable {
* @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, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
this.clientSocket = clientSocket;
this.in = in;
this.out = out;
this.clientHandlers = clientHandlers;
this.actionQueue = actionQueue;
}
@@ -55,8 +57,7 @@ public class ClientHandler implements Runnable {
@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)) {
@@ -76,15 +77,13 @@ public class ClientHandler implements Runnable {
* 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) {
e.printStackTrace();
}
public synchronized void notifyEvent(NetworkEvent event) {
try {
out.writeObject(event);
} catch (IOException e) {
e.printStackTrace();
}
}
@@ -92,14 +91,11 @@ public class ClientHandler implements Runnable {
* 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) {
e.printStackTrace();
}
public synchronized void notifyModel(Game game) {
try {
out.writeObject(game);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -88,8 +88,9 @@ public class TCPServer {
try{
clientSocket = socketTCP.accept();
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
ObjectOutputStream clientSend = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream clientReceive = new ObjectInputStream(clientSocket.getInputStream());
NetworkEvent event = (NetworkEvent) clientReceive.readObject();
if(!(event.getEventType() == EventType.ADD_PLAYER)){
clientSocket.getOutputStream().write((int)(-1));
@@ -114,7 +115,7 @@ public class TCPServer {
clientSocket.getOutputStream().write((int) (1));
System.out.println("Accepted player: " + eventAddPlayer.getUsername());
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, actionQueue);
ClientHandler clientHandler = new ClientHandler(clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
clientHandlers.add(clientHandler);
ConnectedPlayers++;