GamManager added lock handling.

This commit is contained in:
LevelX2 2017-09-01 23:22:39 +02:00
parent 7019db9479
commit 4c5e5d1367
2 changed files with 112 additions and 27 deletions

View file

@ -31,6 +31,9 @@ import java.io.*;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import mage.MageException; import mage.MageException;
import mage.abilities.Ability; import mage.abilities.Ability;
@ -80,7 +83,11 @@ public class GameController implements GameCallback {
protected static final ScheduledExecutorService timeoutIdleExecutor = ThreadExecutor.instance.getTimeoutIdleExecutor(); protected static final ScheduledExecutorService timeoutIdleExecutor = ThreadExecutor.instance.getTimeoutIdleExecutor();
private final ConcurrentHashMap<UUID, GameSessionPlayer> gameSessions = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, GameSessionPlayer> gameSessions = new ConcurrentHashMap<>();
private final ReadWriteLock gameSessionsLock = new ReentrantReadWriteLock();
private final ConcurrentHashMap<UUID, GameSessionWatcher> watchers = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, GameSessionWatcher> watchers = new ConcurrentHashMap<>();
private final ReadWriteLock gameWatchersLock = new ReentrantReadWriteLock();
private final ConcurrentHashMap<UUID, PriorityTimer> timers = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, PriorityTimer> timers = new ConcurrentHashMap<>();
private final ConcurrentHashMap<UUID, UUID> userPlayerMap; private final ConcurrentHashMap<UUID, UUID> userPlayerMap;
@ -114,7 +121,7 @@ public class GameController implements GameCallback {
public void cleanUp() { public void cleanUp() {
cancelTimeout(); cancelTimeout();
for (GameSessionPlayer gameSessionPlayer : gameSessions.values()) { for (GameSessionPlayer gameSessionPlayer : getGameSessions()) {
gameSessionPlayer.cleanUp(); gameSessionPlayer.cleanUp();
} }
ChatManager.instance.destroyChatSession(chatId); ChatManager.instance.destroyChatSession(chatId);
@ -301,7 +308,13 @@ public class GameController implements GameCallback {
String joinType; String joinType;
if (gameSession == null) { if (gameSession == null) {
gameSession = new GameSessionPlayer(game, userId, playerId); gameSession = new GameSessionPlayer(game, userId, playerId);
final Lock w = gameSessionsLock.writeLock();
w.lock();
try {
gameSessions.put(playerId, gameSession); gameSessions.put(playerId, gameSession);
} finally {
w.unlock();
}
joinType = "joined"; joinType = "joined";
} else { } else {
joinType = "rejoined"; joinType = "rejoined";
@ -314,8 +327,8 @@ public class GameController implements GameCallback {
private synchronized void startGame() { private synchronized void startGame() {
if (gameFuture == null) { if (gameFuture == null) {
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) { for (GameSessionPlayer gameSessionPlayer : getGameSessions()) {
entry.getValue().init(); gameSessionPlayer.init();
} }
GameWorker worker = new GameWorker(game, choosingPlayerId, this); GameWorker worker = new GameWorker(game, choosingPlayerId, this);
@ -413,7 +426,13 @@ public class GameController implements GameCallback {
} }
UserManager.instance.getUser(userId).ifPresent(user -> { UserManager.instance.getUser(userId).ifPresent(user -> {
GameSessionWatcher gameWatcher = new GameSessionWatcher(userId, game, false); GameSessionWatcher gameWatcher = new GameSessionWatcher(userId, game, false);
final Lock w = gameWatchersLock.writeLock();
w.lock();
try {
watchers.put(userId, gameWatcher); watchers.put(userId, gameWatcher);
} finally {
w.unlock();
}
gameWatcher.init(); gameWatcher.init();
user.addGameWatchInfo(game.getId()); user.addGameWatchInfo(game.getId());
ChatManager.instance.broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null); ChatManager.instance.broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
@ -422,7 +441,13 @@ public class GameController implements GameCallback {
} }
public void stopWatching(UUID userId) { public void stopWatching(UUID userId) {
final Lock w = gameWatchersLock.writeLock();
w.lock();
try {
watchers.remove(userId); watchers.remove(userId);
} finally {
w.unlock();
}
UserManager.instance.getUser(userId).ifPresent(user -> { UserManager.instance.getUser(userId).ifPresent(user -> {
ChatManager.instance.broadcast(chatId, user.getName(), " has stopped watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null); ChatManager.instance.broadcast(chatId, user.getName(), " has stopped watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
}); });
@ -673,11 +698,11 @@ public class GameController implements GameCallback {
} }
public void endGame(final String message) throws MageException { public void endGame(final String message) throws MageException {
for (final GameSessionPlayer gameSession : gameSessions.values()) { for (final GameSessionPlayer gameSession : getGameSessions()) {
gameSession.gameOver(message); gameSession.gameOver(message);
gameSession.removeGame(); gameSession.removeGame();
} }
for (final GameSessionWatcher gameWatcher : watchers.values()) { for (final GameSessionWatcher gameWatcher : getGameSessionWatchers()) {
gameWatcher.gameOver(message); gameWatcher.gameOver(message);
} }
TableManager.instance.endGame(tableId); TableManager.instance.endGame(tableId);
@ -722,10 +747,10 @@ public class GameController implements GameCallback {
} }
} }
} }
for (final GameSessionPlayer gameSession : gameSessions.values()) { for (final GameSessionPlayer gameSession : getGameSessions()) {
gameSession.update(); gameSession.update();
} }
for (final GameSessionWatcher gameWatcher : watchers.values()) { for (final GameSessionWatcher gameWatcher : getGameSessionWatchers()) {
gameWatcher.update(); gameWatcher.update();
} }
} }
@ -734,13 +759,13 @@ public class GameController implements GameCallback {
Table table = TableManager.instance.getTable(tableId); Table table = TableManager.instance.getTable(tableId);
if (table != null) { if (table != null) {
if (table.getMatch() != null) { if (table.getMatch() != null) {
for (final GameSessionPlayer gameSession : gameSessions.values()) { for (final GameSessionPlayer gameSession : getGameSessions()) {
gameSession.endGameInfo(table); gameSession.endGameInfo(table);
} }
}
}
// TODO: inform watchers about game end and who won // TODO: inform watchers about game end and who won
} }
}
}
private synchronized void ask(UUID playerId, final String question, final Map<String, Serializable> options) throws MageException { private synchronized void ask(UUID playerId, final String question, final Map<String, Serializable> options) throws MageException {
perform(playerId, playerId1 -> getGameSession(playerId1).ask(question, options)); perform(playerId, playerId1 -> getGameSession(playerId1).ask(question, options));
@ -814,12 +839,12 @@ public class GameController implements GameCallback {
message.append(game.getStep().getType().toString()).append(" - "); message.append(game.getStep().getType().toString()).append(" - ");
} }
message.append("Waiting for ").append(game.getPlayer(playerId).getLogName()); message.append("Waiting for ").append(game.getPlayer(playerId).getLogName());
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) { for (final Entry<UUID, GameSessionPlayer> entry : getGameSessionsMap().entrySet()) {
if (!entry.getKey().equals(playerId)) { if (!entry.getKey().equals(playerId)) {
entry.getValue().inform(message.toString()); entry.getValue().inform(message.toString());
} }
} }
for (final GameSessionWatcher watcher : watchers.values()) { for (final GameSessionWatcher watcher : getGameSessionWatchers()) {
watcher.inform(message.toString()); watcher.inform(message.toString());
} }
} }
@ -834,14 +859,13 @@ public class GameController implements GameCallback {
return; return;
} }
final String message = new StringBuilder(game.getStep().getType().toString()).append(" - Waiting for ").append(controller.getName()).toString(); final String message = new StringBuilder(game.getStep().getType().toString()).append(" - Waiting for ").append(controller.getName()).toString();
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) { for (final Entry<UUID, GameSessionPlayer> entry : getGameSessionsMap().entrySet()) {
boolean skip = players.stream().anyMatch(playerId -> entry.getKey().equals(playerId)); boolean skip = players.stream().anyMatch(playerId -> entry.getKey().equals(playerId));
if (!skip) { if (!skip) {
entry.getValue().inform(message); entry.getValue().inform(message);
} }
} }
for (final GameSessionWatcher watcher : watchers.values()) { for (final GameSessionWatcher watcher : getGameSessionWatchers()) {
watcher.inform(message); watcher.inform(message);
} }
} }
@ -858,7 +882,7 @@ public class GameController implements GameCallback {
for (StackTraceElement e : ex.getStackTrace()) { for (StackTraceElement e : ex.getStackTrace()) {
sb.append(e.toString()).append('\n'); sb.append(e.toString()).append('\n');
} }
for (final Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) { for (final Entry<UUID, GameSessionPlayer> entry : getGameSessionsMap().entrySet()) {
entry.getValue().gameError(sb.toString()); entry.getValue().gameError(sb.toString());
} }
} }
@ -995,6 +1019,42 @@ public class GameController implements GameCallback {
void execute(UUID player); void execute(UUID player);
} }
private Map<UUID, GameSessionPlayer> getGameSessionsMap() {
Map<UUID, GameSessionPlayer> newGameSessionsMap = new HashMap<>();
final Lock r = gameSessionsLock.readLock();
r.lock();
try {
newGameSessionsMap.putAll(gameSessions);
} finally {
r.unlock();
}
return newGameSessionsMap;
}
private List<GameSessionPlayer> getGameSessions() {
List<GameSessionPlayer> newGameSessions = new ArrayList<>();
final Lock r = gameSessionsLock.readLock();
r.lock();
try {
newGameSessions.addAll(gameSessions.values());
} finally {
r.unlock();
}
return newGameSessions;
}
private List<GameSessionWatcher> getGameSessionWatchers() {
List<GameSessionWatcher> newGameSessionWatchers = new ArrayList<>();
final Lock r = gameSessionsLock.readLock();
r.lock();
try {
newGameSessionWatchers.addAll(watchers.values());
} finally {
r.unlock();
}
return newGameSessionWatchers;
}
private GameSessionPlayer getGameSession(UUID playerId) { private GameSessionPlayer getGameSession(UUID playerId) {
if (!timers.isEmpty()) { if (!timers.isEmpty()) {
Player player = game.getState().getPlayer(playerId); Player player = game.getState().getPlayer(playerId);

View file

@ -24,13 +24,17 @@
* The views and conclusions contained in the software and documentation are those of the * The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed * authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com. * or implied, of BetaSteward_at_googlemail.com.
*/ */
package mage.server.game; package mage.server.game;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
import mage.constants.ManaType; import mage.constants.ManaType;
import mage.constants.PlayerAction; import mage.constants.PlayerAction;
@ -46,10 +50,17 @@ public enum GameManager {
instance; instance;
private final ConcurrentHashMap<UUID, GameController> gameControllers = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, GameController> gameControllers = new ConcurrentHashMap<>();
private final ReadWriteLock gameControllersLock = new ReentrantReadWriteLock();
public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId, UUID choosingPlayerId, GameOptions gameOptions) { public UUID createGameSession(Game game, ConcurrentHashMap<UUID, UUID> userPlayerMap, UUID tableId, UUID choosingPlayerId, GameOptions gameOptions) {
GameController gameController = new GameController(game, userPlayerMap, tableId, choosingPlayerId, gameOptions); GameController gameController = new GameController(game, userPlayerMap, tableId, choosingPlayerId, gameOptions);
final Lock w = gameControllersLock.writeLock();
w.lock();
try {
gameControllers.put(game.getId(), gameController); gameControllers.put(game.getId(), gameController);
} finally {
w.unlock();
}
return gameController.getSessionId(); return gameController.getSessionId();
} }
@ -151,7 +162,13 @@ public enum GameManager {
GameController gameController = gameControllers.get(gameId); GameController gameController = gameControllers.get(gameId);
if (gameController != null) { if (gameController != null) {
gameController.cleanUp(); gameController.cleanUp();
final Lock w = gameControllersLock.writeLock();
w.lock();
try {
gameControllers.remove(gameId); gameControllers.remove(gameId);
} finally {
w.unlock();
}
} }
} }
@ -175,7 +192,15 @@ public enum GameManager {
return gameControllers.size(); return gameControllers.size();
} }
public ConcurrentHashMap<UUID, GameController> getGameController() { public Map<UUID, GameController> getGameController() {
return gameControllers; Map<UUID, GameController> newControllers = new HashMap<>();
final Lock r = gameControllersLock.readLock();
r.lock();
try {
newControllers.putAll(gameControllers);
} finally {
r.unlock();
}
return newControllers;
} }
} }