synchronized adding to shared resources

This commit is contained in:
Sebastian Lenzlinger 2022-04-08 16:42:04 +02:00
parent b0ebbc9c77
commit 8ea39356f2
3 changed files with 15 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger;
import ch.unibas.dmi.dbis.cs108.sebaschi.CentralServerData;
import ch.unibas.dmi.dbis.cs108.sebaschi.Lobby; import ch.unibas.dmi.dbis.cs108.sebaschi.Lobby;
import java.io.*; import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
@ -16,6 +17,8 @@ public class ClientHandler implements Runnable {
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);
CentralServerData serverData;
private String clientUserName; private String clientUserName;
private BufferedWriter out; private BufferedWriter out;
private BufferedReader in; private BufferedReader in;
@ -41,8 +44,9 @@ public class ClientHandler implements Runnable {
* @param ip the ip of the client, used for re-connection. * @param ip the ip of the client, used for re-connection.
* @param socket the socket on which to make the connection. * @param socket the socket on which to make the connection.
*/ */
public ClientHandler(Socket socket, InetAddress ip) { public ClientHandler(Socket socket, InetAddress ip, CentralServerData serverData) {
try { try {
this.serverData = serverData;
this.ip = ip; this.ip = ip;
this.socket = socket; this.socket = socket;
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

View File

@ -13,7 +13,7 @@ import org.apache.logging.log4j.Logger;
public class Server { public class Server {
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);
private static CentralServerData allData; private static CentralServerData allData = new CentralServerData();
private static final int gamePort = 42069; private static final int gamePort = 42069;
private HashSet<ClientHandler> connectedClients = new HashSet<>(); private HashSet<ClientHandler> connectedClients = new HashSet<>();
@ -32,9 +32,10 @@ public class Server {
System.out.println("Port 42069 is open."); System.out.println("Port 42069 is open.");
while (!serverSocket.isClosed()) { while (!serverSocket.isClosed()) {
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();
ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress()); ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress(), allData);
Thread th = new Thread(nextClient); Thread th = new Thread(nextClient);
connectedClients.add(nextClient); connectedClients.add(nextClient); // will leave be for now
allData.addClientToSetOfAllClients(nextClient);
th.start(); th.start();
} }
} catch (IOException e) { } catch (IOException e) {

View File

@ -1,8 +1,6 @@
package ch.unibas.dmi.dbis.cs108.sebaschi; package ch.unibas.dmi.dbis.cs108.sebaschi;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,7 +23,6 @@ public class CentralServerData {
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
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;
@ -49,11 +46,11 @@ 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 void addClientToSetOfAllClients(ClientHandler client) { public synchronized void addClientToSetOfAllClients(ClientHandler client) {
this.getClientsOnServer().add(client); this.getClientsOnServer().add(client);
} }
public void removeClientFromSetOfAllClients(){ public synchronized void removeClientFromSetOfAllClients(){
//TODO implement or make sure something equivalent is implemented somewhere else //TODO implement or make sure something equivalent is implemented somewhere else
} }
@ -65,6 +62,10 @@ public class CentralServerData {
return allLobbies; return allLobbies;
} }
public synchronized void addLobbyToListOfAllLobbies(Lobby 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} and is then used by clients to join a lobby. * should be set in {@link Lobby} and is then used by clients to join a lobby.