Fix: client TCP complete connection and receive game model

This commit is contained in:
2026-04-30 16:08:13 +02:00
parent 61caba4b47
commit fe5e08e5cd
5 changed files with 55 additions and 33 deletions
@@ -1,6 +1,7 @@
package it.polimi.ingsw.gc14; package it.polimi.ingsw.gc14;
import it.polimi.ingsw.gc14.Controller.ClientController; import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient; import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
import java.util.Scanner; import java.util.Scanner;
@@ -13,21 +14,18 @@ public class ClientLauncherTUI {
System.out.println("Selezionare nome utente: "); System.out.println("Selezionare nome utente: ");
String username = scanner.next(); String username = scanner.next();
System.out.println(username);
System.out.println("Selezionare numero di giocatori desiderato: "); System.out.println("Selezionare numero di giocatori desiderato: ");
int proposedNumPlayers = scanner.nextInt(); int proposedNumPlayers = scanner.nextInt();
System.out.println(proposedNumPlayers);
System.out.println("Selezionare RMI[0] o TCP[1]: "); System.out.println("Selezionare RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt(); int networkType = scanner.nextInt();
System.out.println(networkType);
scanner.close(); scanner.close();
if (networkType == 0) { if (networkType == 0) {
RMIClient client = new RMIClient("localhost", 1099); RMIClient client = new RMIClient(controller, "localhost", 1099);
if (client.connect(username, proposedNumPlayers, controller)) { if (client.connect(username, proposedNumPlayers)) {
System.out.println("Succesfully connected to RMI server\n\n"); System.out.println("Succesfully connected to RMI server\n\n");
} else { } else {
System.out.println("RMI connection refused\n\n"); System.out.println("RMI connection refused\n\n");
@@ -49,7 +47,26 @@ public class ClientLauncherTUI {
} else if (networkType == 1) { } else if (networkType == 1) {
return; TCPClient client = new TCPClient(controller, "localhost", 8080);
if (client.start(username, proposedNumPlayers)) {
System.out.println("Succesfully connected to TCP server\n\n");
} else {
System.out.println("TCP connection refused\n\n");
}
while(true) {
System.out.flush();
if (controller.localModel!=null) {
break;
}
Thread.sleep(500);
}
System.out.println("Model set\n\n");
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println(controller.localModel);
} }
} }
} }
@@ -22,13 +22,18 @@ public class RMIClient {
/** The remote stub used to call methods on the server */ /** The remote stub used to call methods on the server */
private IGameServer stub; private IGameServer stub;
/** Client game's controller */
ClientController controller;
/** /**
* Class constructor. * Class constructor.
* @param controller the client controller used to create the callback
* @param host the host address of the RMI server * @param host the host address of the RMI server
* @param port the port 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.host = host;
this.port = port; this.port = port;
} }
@@ -40,14 +45,13 @@ public class RMIClient {
* Then, creates a {@link ClientCallbackImpl} and calls {@link RMIServer#joinGame(String, int, IClientCallback)}. * Then, creates a {@link ClientCallbackImpl} and calls {@link RMIServer#joinGame(String, int, IClientCallback)}.
* @param username the player's username * @param username the player's username
* @param preferredInt the desired number of players * @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 * @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 { try {
Registry registry = LocateRegistry.getRegistry(host, port); Registry registry = LocateRegistry.getRegistry(host, port);
this.stub = (IGameServer) registry.lookup("RMIGameServer"); this.stub = (IGameServer) registry.lookup("RMIGameServer");
ClientCallbackImpl callback = new ClientCallbackImpl(clientController); ClientCallbackImpl callback = new ClientCallbackImpl(controller);
return stub.joinGame(username, preferredInt, callback); return stub.joinGame(username, preferredInt, callback);
} }
@@ -1,5 +1,6 @@
package it.polimi.ingsw.gc14.Network.TCP.Client; 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.Controller.GameController;
import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
@@ -23,7 +24,7 @@ public class TCPClient {
ObjectOutputStream socketSend; ObjectOutputStream socketSend;
/** Client game's controller */ /** Client game's controller */
GameController controller; ClientController controller;
/** IP address of the server to connect to */ /** IP address of the server to connect to */
String hostname; String hostname;
@@ -38,7 +39,7 @@ public class TCPClient {
* @param hostname The IP address of the server * @param hostname The IP address of the server
* @param port The TCP port 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.controller = controller;
this.hostname = hostname; this.hostname = hostname;
this.port = port; this.port = port;
@@ -55,10 +56,12 @@ public class TCPClient {
*/ */
public boolean start(String user, int proposedNPlayers) { public boolean start(String user, int proposedNPlayers) {
try { try {
communicationSocket = new Socket(hostname, port); communicationSocket = new Socket(hostname, port);
socketSend = new ObjectOutputStream(communicationSocket.getOutputStream()); socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());
socketReceive = new ObjectInputStream(communicationSocket.getInputStream()); socketReceive = new ObjectInputStream(communicationSocket.getInputStream());
sendEvent(new AddPlayer(user, proposedNPlayers)); sendEvent(new AddPlayer(user, proposedNPlayers));
if (communicationSocket.getInputStream().read() == -1) { if (communicationSocket.getInputStream().read() == -1) {
System.out.println("Could not connect to server"); System.out.println("Could not connect to server");
@@ -89,7 +92,7 @@ public class TCPClient {
if (event.getIsError()) { if (event.getIsError()) {
System.out.println(event); System.out.println(event);
} else { } else {
event.apply(controller); event.apply(controller.localController);
//clientController.view.update(); TODO //clientController.view.update(); TODO
} }
} else if (read instanceof Game model) { } else if (read instanceof Game model) {
@@ -41,8 +41,10 @@ public class ClientHandler implements Runnable {
* @param clientHandlers The shared list of all active client handlers * @param clientHandlers The shared list of all active client handlers
* @param actionQueue The queue containing incoming events * @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.clientSocket = clientSocket;
this.in = in;
this.out = out;
this.clientHandlers = clientHandlers; this.clientHandlers = clientHandlers;
this.actionQueue = actionQueue; this.actionQueue = actionQueue;
} }
@@ -76,15 +78,13 @@ public class ClientHandler implements Runnable {
* Sends a {@link NetworkEvent} to the client. * Sends a {@link NetworkEvent} to the client.
* @param event The network event to send to the client. * @param event The network event to send to the client.
*/ */
public void notifyEvent(NetworkEvent event) { public synchronized void notifyEvent(NetworkEvent event) {
synchronized (out) {
try { try {
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(event); out.writeObject(event);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
} }
@@ -92,14 +92,11 @@ public class ClientHandler implements Runnable {
* Sends the current game model to this client. * Sends the current game model to this client.
* @param game The current state of the game to send to the client. * @param game The current state of the game to send to the client.
*/ */
public void notifyModel(Game game) { public synchronized void notifyModel(Game game) {
synchronized (out) {
try { try {
out = new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(game); out.writeObject(game);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
}
@@ -88,8 +88,9 @@ public class TCPServer {
try{ try{
clientSocket = socketTCP.accept(); clientSocket = socketTCP.accept();
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream()); ObjectOutputStream clientSend = new ObjectOutputStream(clientSocket.getOutputStream());
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject(); ObjectInputStream clientReceive = new ObjectInputStream(clientSocket.getInputStream());
NetworkEvent event = (NetworkEvent) clientReceive.readObject();
if(!(event.getEventType() == EventType.ADD_PLAYER)){ if(!(event.getEventType() == EventType.ADD_PLAYER)){
clientSocket.getOutputStream().write((int)(-1)); clientSocket.getOutputStream().write((int)(-1));
@@ -114,7 +115,7 @@ public class TCPServer {
clientSocket.getOutputStream().write((int) (1)); clientSocket.getOutputStream().write((int) (1));
System.out.println("Accepted player: " + eventAddPlayer.getUsername()); 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); clientHandlers.add(clientHandler);
ConnectedPlayers++; ConnectedPlayers++;