Skip to content

Commit cad22ea

Browse files
authored
Merge pull request #3 from jasperkamerling/spectating
Spectating
2 parents da52886 + 11f5a8d commit cad22ea

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

.github/workflows/docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Docker
33
on:
44
push:
55
branches: [ "main" ]
6-
tags: [ 'v*.*.*' ]
6+
tags: [ 'v*.*.*', 'v*.*' ]
77
pull_request:
88
branches: [ "main" ]
99

src/main/kotlin/com/alliander/Application.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ fun Application.module() {
3030
anyHost()
3131
allowHeaders { true }
3232
allowMethod(HttpMethod.Put)
33+
allowMethod(HttpMethod.Delete)
3334
}
35+
3436
install(WebSockets) {
3537
pingPeriod = 15.seconds
36-
timeout = 30.seconds
3738
contentConverter = KotlinxWebsocketSerializationConverter(Json)
3839
}
3940

src/main/kotlin/com/alliander/rooms/RoomService.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.less
1010
import org.jetbrains.exposed.sql.kotlin.datetime.CurrentTimestamp
1111
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp
1212
import org.jetbrains.exposed.sql.transactions.transaction
13+
import org.slf4j.LoggerFactory
1314
import java.util.*
1415
import kotlin.time.Duration.Companion.days
1516

1617

1718
class RoomService {
19+
private val logger = LoggerFactory.getLogger(RoomService::class.java)
20+
1821
object Room : Table() {
1922
val roomId = uuid("room_id")
2023
val revealed = bool("revealed")
@@ -129,6 +132,7 @@ class RoomService {
129132
* Cleanup old rooms after 1 day
130133
*/
131134
fun pruneOldRooms() = transaction {
132-
Room.deleteWhere { created less Clock.System.now().plus(1.days) }
135+
Room.deleteWhere { created less Clock.System.now().minus(1.days) }
136+
.let { if (it != 0) logger.info("Deleted $it rooms from the database") }
133137
}
134138
}

src/main/kotlin/com/alliander/rooms/RoomsRouter.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,27 @@ import io.ktor.websocket.*
1010
import kotlinx.coroutines.cancel
1111
import java.util.*
1212

13-
fun Application.configureRooms(roomService: RoomService,stateService: StateService) {
13+
fun Application.configureRooms(roomService: RoomService, stateService: StateService) {
14+
fun String.toUUID(): UUID = UUID.fromString(this)
1415
routing {
15-
webSocket("/api/rooms/{roomId}/ws") {
16+
webSocket("/api/rooms/{roomId}/{userId}/ws") {
1617
val roomId = call.parameters["roomId"]?.toUUID() ?: throw IllegalArgumentException("Invalid Room ID")
18+
val userId = call.parameters["userId"]?.toUUID() ?: throw IllegalArgumentException("Invalid User ID")
19+
20+
try {
21+
if (!roomService.roomExists(roomId)) {
22+
close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, "Unknown Room ID"))
23+
cancel()
24+
return@webSocket
25+
}
1726

18-
if (!roomService.roomExists(roomId)) {
19-
close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, "Unknown Room ID"))
20-
cancel()
21-
} else {
2227
// Subscribe to the flow
2328
stateService.getOrCreateState(roomId).stateFlow.collect { room ->
2429
sendSerialized(room)
2530
}
31+
} catch (e: Throwable) {
32+
roomService.deleteRoomUser(roomId, userId)
33+
stateService.updateRoom(roomId)
2634
}
2735
}
2836

@@ -129,4 +137,3 @@ fun Application.configureRooms(roomService: RoomService,stateService: StateServi
129137
}
130138
}
131139

132-
fun String.toUUID(): UUID = UUID.fromString(this)

src/main/kotlin/com/alliander/rooms/StateService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.alliander.rooms
22

33
import com.alliander.rooms.dto.ExternalRoom
44
import kotlinx.coroutines.flow.MutableStateFlow
5+
import org.slf4j.LoggerFactory
56
import java.time.LocalDateTime
67
import java.util.UUID
78

89
class StateService(private val roomService: RoomService) {
910
data class State(val stateFlow: MutableStateFlow<ExternalRoom>, val lastUpdated: LocalDateTime)
1011

11-
val flows: MutableMap<UUID, State> = mutableMapOf()
12+
private val logger = LoggerFactory.getLogger(RoomService::class.java)
13+
private val flows: MutableMap<UUID, State> = mutableMapOf()
1214

1315
fun getOrCreateState(roomId: UUID): State {
1416
if (!flows.containsKey(roomId)) {
@@ -29,5 +31,6 @@ class StateService(private val roomService: RoomService) {
2931
fun pruneOldRooms() =
3032
flows.filterValues { it.lastUpdated < LocalDateTime.now().minusHours(1) }
3133
.keys
34+
.also { if(it.isNotEmpty()) logger.info("Deleted $it.size from cache") }
3235
.forEach { flows.remove(it)?.stateFlow }
3336
}

0 commit comments

Comments
 (0)