From 860e29b47ccc20f5b277608051445ca51f6281fb Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Sun, 20 Jan 2013 16:15:56 +0000 Subject: [PATCH] Thread safety in LotroGameMediator. --- .../lotro/game/LotroGameMediator.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java index 67d67e500..707b2e500 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/LotroGameMediator.java @@ -23,7 +23,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; public class LotroGameMediator { private static final Logger LOG = Logger.getLogger(LotroGameMediator.class); - private Map _communicationChannels = new HashMap(); + private Map _communicationChannels = Collections.synchronizedMap(new HashMap()); private DefaultUserFeedback _userFeedback; private DefaultLotroGame _lotroGame; private Map _playerClocks = new HashMap(); @@ -292,7 +292,7 @@ public class LotroGameMediator { } } - public void playerAnswered(Player player, int channelNumber, int decisionId, String answer) throws SubscriptionConflictException, SubscriptionExpiredException { + public synchronized void playerAnswered(Player player, int channelNumber, int decisionId, String answer) throws SubscriptionConflictException, SubscriptionExpiredException { String playerName = player.getName(); _writeLock.lock(); try { @@ -333,7 +333,7 @@ public class LotroGameMediator { } } - public GameCommunicationChannel getCommunicationChannel(Player player, int channelNumber) throws PrivateInformationException, SubscriptionConflictException, SubscriptionExpiredException { + public GameCommunicationChannel getCommunicationChannel(Player player, int channelNumber) throws PrivateInformationException, SubscriptionConflictException, SubscriptionExpiredException { String playerName = player.getName(); if (!player.getType().contains("a") && !_allowSpectators && !_playersPlaying.contains(playerName)) throw new PrivateInformationException(); @@ -356,20 +356,25 @@ public class LotroGameMediator { } public void processVisitor(GameCommunicationChannel communicationChannel, int channelNumber, String playerName, ParticipantCommunicationVisitor visitor) { - visitor.visitChannelNumber(channelNumber); - for (GameEvent gameEvent : communicationChannel.consumeGameEvents()) - visitor.visitGameEvent(gameEvent); + _readLock.lock(); + try { + visitor.visitChannelNumber(channelNumber); + for (GameEvent gameEvent : communicationChannel.consumeGameEvents()) + visitor.visitGameEvent(gameEvent); - String warning = _userFeedback.consumeWarning(playerName); - if (warning != null) - visitor.visitGameEvent(new GameEvent(GameEvent.Type.W).message(warning)); + String warning = _userFeedback.consumeWarning(playerName); + if (warning != null) + visitor.visitGameEvent(new GameEvent(GameEvent.Type.W).message(warning)); - Map secondsLeft = new HashMap(); - for (Map.Entry playerClock : _playerClocks.entrySet()) { - String playerClockName = playerClock.getKey(); - secondsLeft.put(playerClockName, _maxSecondsForGamePerPlayer - playerClock.getValue() - getCurrentUserPendingTime(playerClockName)); + Map secondsLeft = new HashMap(); + for (Map.Entry playerClock : _playerClocks.entrySet()) { + String playerClockName = playerClock.getKey(); + secondsLeft.put(playerClockName, _maxSecondsForGamePerPlayer - playerClock.getValue() - getCurrentUserPendingTime(playerClockName)); + } + visitor.visitClock(secondsLeft); + } finally { + _readLock.unlock(); } - visitor.visitClock(secondsLeft); } public void singupUserForGame(Player player, ParticipantCommunicationVisitor visitor) throws PrivateInformationException {