summaryrefslogtreecommitdiff
path: root/sw-server/src/main/kotlin/org/luxons
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@booking.com>2020-05-15 03:49:12 +0200
committerJoffrey Bion <joffrey.bion@booking.com>2020-05-15 03:49:12 +0200
commita68eda7bb7388901f4ac8e377b14bc291cb5666e (patch)
treea4a71e184f65135de533b6b6ce6931d1c0d6b902 /sw-server/src/main/kotlin/org/luxons
parentFix polymorphic serialization (diff)
downloadseven-wonders-a68eda7bb7388901f4ac8e377b14bc291cb5666e.tar.gz
seven-wonders-a68eda7bb7388901f4ac8e377b14bc291cb5666e.tar.bz2
seven-wonders-a68eda7bb7388901f4ac8e377b14bc291cb5666e.zip
Make Lobby/player repository more thread safe
Diffstat (limited to 'sw-server/src/main/kotlin/org/luxons')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt9
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt24
2 files changed, 10 insertions, 23 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt
index ff06f549..5036266d 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt
@@ -5,19 +5,20 @@ import org.luxons.sevenwonders.server.lobby.Lobby
import org.luxons.sevenwonders.server.lobby.Player
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
-import java.util.HashMap
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.atomic.AtomicLong
@Repository
class LobbyRepository @Autowired constructor() {
- private val lobbies = HashMap<Long, Lobby>()
+ private val lobbies = ConcurrentHashMap<Long, Lobby>()
- private var lastGameId: Long = 0
+ private var lastGameId: AtomicLong = AtomicLong(0)
fun list(): Collection<Lobby> = lobbies.values
fun create(gameName: String, owner: Player): Lobby {
- val id = lastGameId++
+ val id = lastGameId.getAndIncrement()
val lobby = Lobby(id, gameName, owner, GameDefinition.load())
lobbies[id] = lobby
return lobby
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
index 00badf75..b31d2cee 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
@@ -3,33 +3,19 @@ package org.luxons.sevenwonders.server.repositories
import org.luxons.sevenwonders.server.ApiMisuseException
import org.luxons.sevenwonders.server.lobby.Player
import org.springframework.stereotype.Repository
-import java.util.HashMap
+import java.util.concurrent.ConcurrentHashMap
@Repository
class PlayerRepository {
- private val players = HashMap<String, Player>()
+ private val players = ConcurrentHashMap<String, Player>()
operator fun contains(username: String): Boolean = players.containsKey(username)
fun createOrUpdate(username: String, displayName: String): Player {
- return if (players.containsKey(username)) {
- update(username, displayName)
- } else {
- create(username, displayName)
- }
- }
-
- private fun create(username: String, displayName: String): Player {
- val player = Player(username, displayName)
- players[username] = player
- return player
- }
-
- private fun update(username: String, displayName: String): Player {
- val player = find(username)
- player.displayName = displayName
- return player
+ val p = players.computeIfAbsent(username) { Player(username, displayName) }
+ p.displayName = displayName
+ return p
}
fun find(username: String): Player = players[username] ?: throw PlayerNotFoundException(username)
bgstack15