Add: ClientLauncherTUI. Connection and model sending works.

Note: ClientController.view is commented out since class TUI is still missing
This commit is contained in:
2026-04-29 17:14:45 +02:00
parent a699826137
commit 73b5b6eba5
10 changed files with 91 additions and 47 deletions
@@ -1,28 +0,0 @@
package it.polimi.ingsw.gc14;
import java.util.Scanner;
public class ClientLauncher {
public void main() {
Scanner scanner = new Scanner(System.in);
System.out.println("Selezionare nome utente: ");
String username = scanner.next();
System.out.println(username);
System.out.println("Selezionare numero di giocatori desiderato: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println(proposedNumPlayers);
System.out.println("Selezionare RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println(networkType);
scanner.close(); // chiudi solo alla fine
if (networkType == 0) {
return;
} else if (networkType == 1) {
return;
}
}
}
@@ -0,0 +1,54 @@
package it.polimi.ingsw.gc14;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.View.IView;
import java.util.Scanner;
public class ClientLauncherTUI {
public void main() throws InterruptedException {
ClientController controller = new ClientController();
Scanner scanner = new Scanner(System.in);
System.out.println("Selezionare nome utente: ");
String username = scanner.next();
System.out.println(username);
System.out.println("Selezionare numero di giocatori desiderato: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println(proposedNumPlayers);
System.out.println("Selezionare RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println(networkType);
scanner.close();
if (networkType == 0) {
RMIClient client = new RMIClient("localhost", 1099);
if (client.connect(username, proposedNumPlayers, controller)) {
System.out.println("CLIENT CONNESSO DAJE");
} else {
System.out.println("NON CONNESSO D:");
}
while(true) {
System.out.flush();
if (controller.localModel!=null) {
System.out.println("MODEL SETTATO");
}
Thread.sleep(500);
}
} else if (networkType == 1) {
return;
}
}
}
@@ -6,9 +6,9 @@ import it.polimi.ingsw.gc14.View.IView;
public class ClientController {
private Game localModel;
public Game localModel;
public GameController localController;
public final IView view;
public IView view=null;
public ClientController(IView view,Game localModel) {
this.view = view;
@@ -16,12 +16,18 @@ public class ClientController {
this.localController = new GameController(localModel);
}
public ClientController() {
this.localController = new GameController(localModel);
}
public void setModel(Game model) {
this.localModel = model;
localController.setModel(model);
localModel.addObserver((Observer) view); // registra la view come observer
// localModel.addObserver((Observer) view); // registra la view come observer
}
public void onError(String message) {
view.showError(message);
}
@@ -111,9 +111,9 @@ public class Board implements Serializable {
ArrayList<BuildingCard> buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1));
Collections.shuffle(buildingDeck);
if(nTotem==2)
upperListBuilding= buildingDeck.subList(0,1);
upperListBuilding = new ArrayList<>(buildingDeck.subList(0,1));
else
upperListBuilding= buildingDeck.subList(0,2);
upperListBuilding = new ArrayList<>(buildingDeck.subList(0,2));
}
/**
@@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
@@ -13,7 +14,7 @@ import java.rmi.server.UnicastRemoteObject;
* RMI client callback implementation of {@link IClientCallback}.
* Receives notifications from the server and updates the client game model.
*/
public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCallback {
public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCallback, Serializable {
/** The client controller used to apply events and update the model */
private final ClientController clientController;
@@ -7,7 +7,6 @@ import it.polimi.ingsw.gc14.Controller.ClientController;
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;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
/**
* Client RMI. Uses the methods exposed by the server RMI.
@@ -3,9 +3,10 @@ package it.polimi.ingsw.gc14.Network.RMI.Common;
import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import java.io.Serializable;
import java.rmi.*;
public interface IClientCallback extends Remote {
public interface IClientCallback extends Remote, Serializable {
void onGameInit(Game model) throws RemoteException;
void onAction(NetworkEvent action) throws RemoteException;
}
@@ -21,7 +21,7 @@ import java.rmi.*;
/**
* Server RMI. Exposes a method to join the game and one to execute an event.
*/
public class RMIServer implements IGameServer {
public class RMIServer extends UnicastRemoteObject implements IGameServer {
/** Server game's controller */
private GameController controller;
@@ -88,12 +88,13 @@ public class RMIServer implements IGameServer {
playerList.setLimit(preferredInt);
}
if (controller.addPlayer(username)) {
playerList.add(username);
clients.put(username, callback);
playerList.add(username);
return true;
}
return false;
}
}
@@ -34,7 +34,7 @@ import java.util.concurrent.LinkedBlockingQueue;
public class ServerLauncher {
/**
* Queue containing the events to be applied to the game model.
* Queue containinetworkTypeng the events to be applied to the game model.
* Thread safe by design.
*/
BlockingQueue<NetworkEvent> actionQueue;
@@ -109,13 +109,15 @@ public class ServerLauncher {
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
playerList.setAction(()->{
try {
launcher.run();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
new Thread(()->{
try {
launcher.run();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}).start();
});
serverRMI.start();
@@ -132,6 +134,7 @@ public class ServerLauncher {
*/
public void run() throws InterruptedException, RemoteException {
// Game creation
System.out.println("NOTIFICO MODEL");
serverRMI.notifyAll(gameController.getModel());
serverTCP.notifyAll(gameController.getModel());
+8 -1
View File
@@ -8,7 +8,14 @@ module it.polimi.ingsw.gc14 {
opens it.polimi.ingsw.gc14 to javafx.fxml, com.google.gson;
opens it.polimi.ingsw.gc14.Model to com.google.gson;
opens it.polimi.ingsw.gc14.Model.GamePackage to com.google.gson;
exports it.polimi.ingsw.gc14;
opens it.polimi.ingsw.gc14.Model.GamePackage to com.google.gson;
// RMI
exports it.polimi.ingsw.gc14.Network.RMI.Common to java.rmi;
exports it.polimi.ingsw.gc14.Network.RMI.Server to java.rmi;
exports it.polimi.ingsw.gc14.Network.RMI.Client to java.rmi;
exports it.polimi.ingsw.gc14.Network to java.rmi;
exports it.polimi.ingsw.gc14.Model to java.rmi, com.google.gson;
}