From 0ee264502ca8a31cd9220592dd1e6becd326fd43 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 6 Jun 2021 03:49:48 +0200 Subject: [PATCH] feat: converted UserManager to singleton object --- .../server/netconnector/UserManager.java | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java b/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java index 7ee9a5f..d4fec75 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/netconnector/UserManager.java @@ -20,6 +20,19 @@ import java.util.*; */ public class UserManager { + private static UserManager instance; + + /** + * @return the current instance of the UserManager + */ + public static UserManager getInstance() { + if (instance == null) { + Logger.debug("No instance of UserManager found. Creating new instance..."); + instance = new UserManager(); + } + return instance; + } + /** A set of users that aren't assigned to lobbies or character selection yet */ private final HashSet newUsers; @@ -45,7 +58,7 @@ public class UserManager { private final JSON json; /** Constructs a new, empty UserManager */ - public UserManager() { + private UserManager() { this.newUsers = new HashSet<>(); this.readyToConnect = new HashMap<>(); this.readyToReconnect = new HashMap<>(); @@ -55,7 +68,7 @@ public class UserManager { } /** Called on a new WebSocket connection. Places the WebSocket and its ResourceDescriptor in a HashMap. */ - public void connectUser(WebSocket conn) { + void connectUser(WebSocket conn) { newUsers.add(conn); } @@ -66,7 +79,7 @@ public class UserManager { * @param conn is the {@link WebSocket} that sent the message * @param message is the {@link String} sent by the connection */ - public void messageReceived(WebSocket conn, String message) { + void messageReceived(WebSocket conn, String message) { Logger.trace("Parsing message..."); var parsedMessageOptional = json.parse(message); if (parsedMessageOptional.isEmpty()) { @@ -148,8 +161,13 @@ public class UserManager { var participantToRestore = activeParticipants.get(clientID); participantToRestore.setConnection(conn); - readyToReconnect.remove(conn); - inGame.put(conn, participantToRestore); + + synchronized (readyToReconnect) { + readyToReconnect.remove(conn); + } + synchronized (inGame) { + inGame.put(conn, participantToRestore); + } // activeParticipants remains the same, as no players have been removed from the game } else { Logger.debug( @@ -157,8 +175,13 @@ public class UserManager { conn); var clientID = readyToReconnect.get(conn); - readyToConnect.put(conn, clientID); - readyToReconnect.remove(conn); + + synchronized (readyToConnect) { + readyToConnect.put(conn, clientID); + } + synchronized (readyToReconnect) { + readyToReconnect.remove(conn); + } } } @@ -172,6 +195,16 @@ public class UserManager { void relayRequestMessage(Participant conn, RequestMessage message) { } + void disconnectUser(WebSocket conn, boolean closedByRemote) { + // TODO: notify clients and such if the connection was in fact closed by the remote. Also remove participant + synchronized (newUsers) { + newUsers.remove(conn); + } + synchronized (inGame) { + inGame.remove(conn); + } + } + public void sendMessage(WebSocket conn, BasicMessage message) { var jsonRepresentingMessage = json.stringify(message); if (jsonRepresentingMessage.isEmpty()) { @@ -189,16 +222,6 @@ public class UserManager { sendMessage(conn, errorMessage); } - public void disconnectUser(WebSocket conn, boolean closedByRemote) { - // TODO: notify clients and such if the connection was in fact closed by the remote. Also remove participant - synchronized (newUsers) { - newUsers.remove(conn); - } - synchronized (inGame) { - inGame.remove(conn); - } - } - public boolean isUserConnected(WebSocket user) { return newUsers.contains(user) || readyToReconnect.containsKey(user) ||