Fix: MVC (Client) Add:IClient

This commit is contained in:
rubenpirreram
2026-04-30 18:34:40 +02:00
parent d66af3a112
commit 5246818fca
6 changed files with 127 additions and 15 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) ;
}
@@ -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;
@@ -67,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,8 +1,8 @@
package it.polimi.ingsw.gc14.Network.TCP.Client;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Controller.GameController;
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;
@@ -12,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;
@@ -54,7 +54,7 @@ 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);
@@ -62,7 +62,7 @@ public class TCPClient {
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;
@@ -112,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) {