feat: switched LobbyRunner implementation to threads again

This commit is contained in:
Yannik Bretschneider 2021-06-06 19:30:17 +02:00
parent c09d407351
commit 2995564cdf
1 changed files with 27 additions and 23 deletions

View File

@ -28,12 +28,13 @@ public class LobbyRunner {
} }
private final ExecutorService lobbyExecutor; private final HashMap<LobbyConnection, Thread> activeLobbies;
private final HashSet<LobbyConnection> activeLobbies;
/**
* Constructs a new LobbyRunner
*/
private LobbyRunner() { private LobbyRunner() {
this.lobbyExecutor = Executors.newFixedThreadPool(Server.getMaxLobbies()); this.activeLobbies = new HashMap<>();
this.activeLobbies = new HashSet<>();
} }
boolean canAddLobby() { boolean canAddLobby() {
@ -43,22 +44,26 @@ public class LobbyRunner {
/** Starts a new thread for the current LobbyConnection, and adds it to the currently active lobbies */ /** Starts a new thread for the current LobbyConnection, and adds it to the currently active lobbies */
void startLobby(LobbyConnection connection) { void startLobby(LobbyConnection connection) {
Logger.trace("Starting lobby connection thread '{}'", connection.gameID); Logger.trace("Starting lobby connection thread '{}'", connection.gameID);
if (activeLobbies.contains(connection)) { synchronized (activeLobbies) {
Logger.warn("Already active lobby was started again. This is probably a bug."); if (activeLobbies.containsKey(connection)) {
} else if (!canAddLobby()) { Logger.warn("Already active lobby was started again. This is probably a bug.");
Logger.warn("Scheduling lobby to be started while no thread is free. This might be a bug."); } else if (!canAddLobby()) {
activeLobbies.add(connection); Logger.warn("Scheduling lobby to be started while max lobbies is reached. This might be a bug.");
lobbyExecutor.execute(connection); Thread lobbyThread = new Thread(connection, "Lobby-" + connection.gameID);
} else { activeLobbies.put(connection, lobbyThread);
Logger.trace("Executing LobbyThread 'Lobby-{}'...", connection.gameID); lobbyThread.start();
activeLobbies.add(connection); } else {
lobbyExecutor.execute(connection); Logger.trace("Executing LobbyThread 'Lobby-{}'...", connection.gameID);
Thread lobbyThread = new Thread(connection, "Lobby-" + connection.gameID);
activeLobbies.put(connection, lobbyThread);
lobbyThread.start();
}
} }
} }
void removeLobby(LobbyConnection lobby) { void removeLobby(LobbyConnection lobby) {
synchronized (activeLobbies) { synchronized (activeLobbies) {
if (!activeLobbies.contains(lobby)) { if (!activeLobbies.containsKey(lobby)) {
Logger.warn("Tried to remove non-existent lobby thread. This is probably a bug."); Logger.warn("Tried to remove non-existent lobby thread. This is probably a bug.");
} else { } else {
Logger.debug("Stopping and removing lobby '{}'", lobby.gameID); Logger.debug("Stopping and removing lobby '{}'", lobby.gameID);
@ -69,18 +74,17 @@ public class LobbyRunner {
} }
boolean isStarted(LobbyConnection connection) { boolean isStarted(LobbyConnection connection) {
return activeLobbies.contains(connection); return activeLobbies.containsKey(connection);
} }
/** Shutdown all threads, destroy the lobbies, and close everything up */ /** Shutdown all threads, destroy the lobbies, and close everything up */
void shutdownAll() { void shutdownAll() {
Logger.info("Stopping and removing all LobbyThreads"); Logger.info("Stopping and removing all LobbyThreads");
activeLobbies.forEach(LobbyConnection::terminateConnection); activeLobbies.keySet().forEach(LobbyConnection::terminateConnection);
try { Logger.debug("All lobby shutdown flags set");
lobbyExecutor.awaitTermination(5, TimeUnit.SECONDS); }
} catch (InterruptedException e) {
e.printStackTrace(); // later...
} void checkThreads() {
lobbyExecutor.shutdownNow();
} }
} }