From 24dceee1d8b6bf97b89cf36e595a6a391dd4e7eb Mon Sep 17 00:00:00 2001 From: Sebastian Lenzlinger Date: Fri, 8 Apr 2022 17:50:49 +0200 Subject: [PATCH] Added method to remove disconnected client from central data. client handler uses it. --- Meilenstein III/Diary.txt | 4 ++++ .../multiplayer/server/ClientHandler.java | 8 ++++--- .../cs108/sebaschi/CentralServerData.java | 23 ++++++++++++++++--- .../unibas/dmi/dbis/cs108/sebaschi/Lobby.java | 1 - 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Meilenstein III/Diary.txt b/Meilenstein III/Diary.txt index 4520955..41f6472 100644 --- a/Meilenstein III/Diary.txt +++ b/Meilenstein III/Diary.txt @@ -256,6 +256,10 @@ Stand 15:00 Uhr: Versuch meine commits wieder richtig zuorden zu können indem ich die user.name und user.email Variablen von git auf meinem Rechner neu configuriere. +Stand 17:30 Uhr: + Ich habe einen neuen Branch "BudaLobbySebastian" erstellt, worauf ich schaue wie und wo eigentlich eine "Lobby" + lebt. LISTL wurde auch implementiert aber noch nicht getestet. + ToDo: Spiellogik: - Send() methode von Passenger mit Client-Server verknüpfen(Seraina) diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java index d4585f7..5fc6669 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/multiplayer/server/ClientHandler.java @@ -117,6 +117,7 @@ public class ClientHandler implements Runnable { break; } } + LOGGER.debug(this.getClientUserName() + " reached end of run()"); } @@ -224,7 +225,7 @@ public class ClientHandler implements Runnable { LOGGER.debug( this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID()); //TODO add server response. Here a possibility: - sendMsgToClient(Protocol.printToClientConsole + "$New lobby with ID: " + newGame + " created."); + sendMsgToClient(Protocol.printToClientConsole + "$New lobby with ID: " + newGame.getLobbyID() + " created."); } /** @@ -234,7 +235,7 @@ public class ClientHandler implements Runnable { */ public void listAllLobbiesToClient() { StringBuilder response = new StringBuilder(); - response.append(Protocol.pingBack); + response.append(Protocol.printToClientConsole); response.append("$"); if (serverData.getAllLobbies().isEmpty()) { response.append("There are currently no open lobbies"); @@ -250,12 +251,13 @@ public class ClientHandler implements Runnable { } /** - * Closes the client's socket, in, and out. + * Closes the client's socket, in, and out. and removes from global list of clients. */ public void disconnectClient() { socket = this.getSocket(); in = this.getIn(); out = this.getOut(); + serverData.removeClientFromSetOfAllClients(this); try { Thread.sleep(100); if (in != null) { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java index b085635..c6d4bbe 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/CentralServerData.java @@ -22,6 +22,7 @@ public class CentralServerData { public static final Logger LOGGER = LogManager.getLogger(); public static final BudaLogConfig l = new BudaLogConfig(LOGGER); + //TODO which datastructures should be used here? private Set clientsOnServer; private List allLobbies; private Map lobbyIDMap; @@ -36,6 +37,7 @@ public class CentralServerData { /** * Getter for set of all clients. + * * @return the set of all clients. */ public Set getClientsOnServer() { @@ -44,31 +46,46 @@ public class CentralServerData { /** * Used to add the client to the set of all clients on server. + * * @param client */ public synchronized void addClientToSetOfAllClients(ClientHandler client) { this.getClientsOnServer().add(client); } - public synchronized void removeClientFromSetOfAllClients(){ + /** + * Remove a client from the set of clients. Used in ClientHandler.disconnectClient(). + * + * @param client to be removed. + */ + public synchronized void removeClientFromSetOfAllClients(ClientHandler client) { //TODO implement or make sure something equivalent is implemented somewhere else + this.getClientsOnServer().remove(client); + LOGGER.debug(client.getClientUserName() + " removed from CentralServerData list of clients."); } /** * Getter for List of all lobbies. + * * @return a list of all lobbies */ public List getAllLobbies() { return allLobbies; } + /** + * Does exactly what it says on the box. + * + * @param lobby + */ public synchronized void addLobbyToListOfAllLobbies(Lobby lobby) { allLobbies.add(lobby); } /** - * Mapping from an Integer that repesents a LobbyID to the lobby - * should be set in {@link Lobby} and is then used by clients to join a lobby. + * Mapping from an Integer that repesents a LobbyID to the lobby should be set in {@link Lobby} + * and is then used by clients to join a lobby. + * * @return a mapping from Integer to Lobby. */ public Map getLobbyIDMap() { diff --git a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java index d43dbd4..4f83481 100644 --- a/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java +++ b/src/main/java/ch/unibas/dmi/dbis/cs108/sebaschi/Lobby.java @@ -57,7 +57,6 @@ public class Lobby { this.admin = admin; this.players.add(admin); this.numberOfPlayersInLobby = 1; - lobbies++; LOGGER.debug("New Lobby created by " + admin.getClientUserName() + ". This lobby's ID: " + this.lobbyID); }