Merge pull request #92 from rubenpirreram/server-resilance

server-resilance
This commit is contained in:
rubenpirreram
2026-05-10 17:16:08 +02:00
committed by GitHub
2 changed files with 130 additions and 44 deletions
@@ -88,12 +88,13 @@ public class TCPServer {
continue;
}
if (playerList.isEmpty()) {
if(controller.getModel() == null){
Game model = new Game(eventAddPlayer.getProposedNPlayer());
controller.setModel(model);
playerList.setLimit(eventAddPlayer.getProposedNPlayer());
}
}
String username = eventAddPlayer.getUsername();
@@ -114,7 +115,8 @@ public class TCPServer {
// metti in attesa del socket heartbeat
} else if (playerList.containsKey(username) && !playerList.get(username)) {
}
else if(playerList.containsKey(username) && !playerList.get(username)){
// riconnessione
playerList.put(username, true);
System.out.println("Reconnected player: " + username);
@@ -131,14 +133,16 @@ public class TCPServer {
clientHandlers.add(handler);
connectedPlayers++;
actionQueue.add(new ReconnectPlayer(username));
} else {
}
else{
clientSocket.getOutputStream().write(-1);
clientSocket.close();
System.out.println("Player could not be added. Connection terminated.");
}
} catch (IOException | ClassNotFoundException e) {
}
catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
@@ -3,6 +3,7 @@ package it.polimi.ingsw.gc14;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Network.ClientPlayer;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
@@ -11,6 +12,10 @@ import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.BlockingQueue;
@@ -74,6 +79,7 @@ public class ServerLauncher {
this.actionQueue = actionQueue;
this.serverRMI = serverRMI;
this.gameController = gameController;
this.gameController.setModel(loadSave());
this.serverTCP = serverTCP;
}
@@ -92,6 +98,15 @@ public class ServerLauncher {
event.setIsError(!event.apply(gameController));
serverRMI.notifyAll(event);
serverTCP.notifyAll(event);
if(!event.getIsError()){
if(this.gameController.getModel().getCurrentState().getGameStage() == GameStages.ENDED){
this.deleteSave();
}
else if(!this.gameSave() ){
System.out.println("\n!!! Save failed !!!\n");
}
}
if (!playerList.get(gameController.getModel().getCurrentState().getCurrentPlayer().getUserName())) {
actionQueue.offer(new SkipPlayerDisconnected(gameController.getModel().getCurrentState().getCurrentPlayer().getUserName()));
}
@@ -114,6 +129,7 @@ public class ServerLauncher {
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController();
String IP;
try {
IP=chooseNetworkInterface(new Scanner(System.in));
} catch (Exception e) {
@@ -161,14 +177,17 @@ public class ServerLauncher {
try{
this.doFirstEvent();
this.view.fullRender();
} catch (InterruptedException e) {
}
catch(InterruptedException e){
Thread.currentThread().interrupt();
break;
} catch (RemoteException e) {
}
catch(RemoteException e){
throw new RuntimeException(e);
}
}
}
public static String chooseNetworkInterface(Scanner scanner) throws Exception {
List<String> ips = new ArrayList<>();
@@ -201,4 +220,67 @@ public class ServerLauncher {
int choice = Integer.parseInt(scanner.nextLine().trim());
return ips.get(choice);
}
private boolean gameSave(){
try{
Path jarPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
Path filePath = jarPath.resolve("GameSaves/save.dat");
Files.createDirectories(filePath.getParent());
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath.toFile()))){
oos.writeObject(this.gameController.getModel());
System.out.println("Game saved to: " + filePath.toAbsolutePath());
return true;
}
catch(IOException e){
e.printStackTrace();
return false;
}
}
catch(IOException e){
System.out.println("Couldn't create directory.");
e.printStackTrace();
return false;
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private Game loadSave(){
try {
Path jarPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
Path filePath = jarPath.resolve("GameSaves/save.dat");
try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath.toFile()))){
return (Game)(ois.readObject());
}
catch(FileNotFoundException e){
return null;
}
catch(IOException e){
e.printStackTrace();
return null;
}
catch(ClassNotFoundException e){
throw new RuntimeException(e);
}
}
catch(URISyntaxException e){
throw new RuntimeException(e);
}
}
private boolean deleteSave(){
try{
Path jarPath = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).getParent();
Path filePath = jarPath.resolve("GameSaves/save.dat");
Files.delete(filePath);
return true;
}
catch(URISyntaxException e){
return false;
}
catch (IOException e){
return false;
}
}
}