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
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)

View File

@ -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) {

View File

@ -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<ClientHandler> clientsOnServer;
private List<Lobby> allLobbies;
private Map<Integer, Lobby> lobbyIDMap;
@ -36,6 +37,7 @@ public class CentralServerData {
/**
* Getter for set of all clients.
*
* @return the set of all clients.
*/
public Set<ClientHandler> 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<Lobby> 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<Integer, Lobby> getLobbyIDMap() {

View File

@ -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);
}