Added method to remove disconnected client from central data. client handler uses it.

This commit is contained in:
Sebastian Lenzlinger 2022-04-08 17:50:49 +02:00
parent 709e5323e8
commit 24dceee1d8
4 changed files with 29 additions and 7 deletions

View File

@ -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 Versuch meine commits wieder richtig zuorden zu können indem ich die user.name und user.email
Variablen von git Variablen von git
auf meinem Rechner neu configuriere. 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: ToDo:
Spiellogik: - Send() methode von Passenger mit Client-Server verknüpfen(Seraina) Spiellogik: - Send() methode von Passenger mit Client-Server verknüpfen(Seraina)

View File

@ -117,6 +117,7 @@ public class ClientHandler implements Runnable {
break; break;
} }
} }
LOGGER.debug(this.getClientUserName() + " reached end of run()");
} }
@ -224,7 +225,7 @@ public class ClientHandler implements Runnable {
LOGGER.debug( LOGGER.debug(
this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID()); this.getClientUserName() + " created a new lobby with ID: " + newGame.getLobbyID());
//TODO add server response. Here a possibility: //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() { public void listAllLobbiesToClient() {
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
response.append(Protocol.pingBack); response.append(Protocol.printToClientConsole);
response.append("$"); response.append("$");
if (serverData.getAllLobbies().isEmpty()) { if (serverData.getAllLobbies().isEmpty()) {
response.append("There are currently no open lobbies"); 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() { public void disconnectClient() {
socket = this.getSocket(); socket = this.getSocket();
in = this.getIn(); in = this.getIn();
out = this.getOut(); out = this.getOut();
serverData.removeClientFromSetOfAllClients(this);
try { try {
Thread.sleep(100); Thread.sleep(100);
if (in != null) { if (in != null) {

View File

@ -22,6 +22,7 @@ public class CentralServerData {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
//TODO which datastructures should be used here?
private Set<ClientHandler> clientsOnServer; private Set<ClientHandler> clientsOnServer;
private List<Lobby> allLobbies; private List<Lobby> allLobbies;
private Map<Integer, Lobby> lobbyIDMap; private Map<Integer, Lobby> lobbyIDMap;
@ -36,6 +37,7 @@ public class CentralServerData {
/** /**
* Getter for set of all clients. * Getter for set of all clients.
*
* @return the set of all clients. * @return the set of all clients.
*/ */
public Set<ClientHandler> getClientsOnServer() { public Set<ClientHandler> getClientsOnServer() {
@ -44,31 +46,46 @@ public class CentralServerData {
/** /**
* Used to add the client to the set of all clients on server. * Used to add the client to the set of all clients on server.
*
* @param client * @param client
*/ */
public synchronized void addClientToSetOfAllClients(ClientHandler client) { public synchronized void addClientToSetOfAllClients(ClientHandler client) {
this.getClientsOnServer().add(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 //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. * Getter for List of all lobbies.
*
* @return a list of all lobbies * @return a list of all lobbies
*/ */
public List<Lobby> getAllLobbies() { public List<Lobby> getAllLobbies() {
return allLobbies; return allLobbies;
} }
/**
* Does exactly what it says on the box.
*
* @param lobby
*/
public synchronized void addLobbyToListOfAllLobbies(Lobby lobby) { public synchronized void addLobbyToListOfAllLobbies(Lobby lobby) {
allLobbies.add(lobby); allLobbies.add(lobby);
} }
/** /**
* Mapping from an Integer that repesents a LobbyID to the lobby * Mapping from an Integer that repesents a LobbyID to the lobby should be set in {@link Lobby}
* should be set in {@link Lobby} and is then used by clients to join a lobby. * and is then used by clients to join a lobby.
*
* @return a mapping from Integer to Lobby. * @return a mapping from Integer to Lobby.
*/ */
public Map<Integer, Lobby> getLobbyIDMap() { public Map<Integer, Lobby> getLobbyIDMap() {

View File

@ -57,7 +57,6 @@ public class Lobby {
this.admin = admin; this.admin = admin;
this.players.add(admin); this.players.add(admin);
this.numberOfPlayersInLobby = 1; this.numberOfPlayersInLobby = 1;
lobbies++;
LOGGER.debug("New Lobby created by " + admin.getClientUserName() + ". This lobby's ID: " LOGGER.debug("New Lobby created by " + admin.getClientUserName() + ". This lobby's ID: "
+ this.lobbyID); + this.lobbyID);
} }