feat: switched LobbyRunner implementation to threads again
This commit is contained in:
parent
c09d407351
commit
2995564cdf
@ -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) {
|
||||||
|
if (activeLobbies.containsKey(connection)) {
|
||||||
Logger.warn("Already active lobby was started again. This is probably a bug.");
|
Logger.warn("Already active lobby was started again. This is probably a bug.");
|
||||||
} else if (!canAddLobby()) {
|
} else if (!canAddLobby()) {
|
||||||
Logger.warn("Scheduling lobby to be started while no thread is free. This might be a bug.");
|
Logger.warn("Scheduling lobby to be started while max lobbies is reached. This might be a bug.");
|
||||||
activeLobbies.add(connection);
|
Thread lobbyThread = new Thread(connection, "Lobby-" + connection.gameID);
|
||||||
lobbyExecutor.execute(connection);
|
activeLobbies.put(connection, lobbyThread);
|
||||||
|
lobbyThread.start();
|
||||||
} else {
|
} else {
|
||||||
Logger.trace("Executing LobbyThread 'Lobby-{}'...", connection.gameID);
|
Logger.trace("Executing LobbyThread 'Lobby-{}'...", connection.gameID);
|
||||||
activeLobbies.add(connection);
|
Thread lobbyThread = new Thread(connection, "Lobby-" + connection.gameID);
|
||||||
lobbyExecutor.execute(connection);
|
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();
|
|
||||||
}
|
}
|
||||||
lobbyExecutor.shutdownNow();
|
|
||||||
|
// later...
|
||||||
|
void checkThreads() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user