Skip to content

Commit

Permalink
Add RegisterAsPeer logic to TelemetryClient (#16)
Browse files Browse the repository at this point in the history
* Implement RegisterAsPeer.

* Define and get IceAdapterVersion.

* Remove unnecessarey UUID.random() from message

* Set version in gradle.properties and add to manifest

* Use version instead of archiveVersion

---------

Co-authored-by: Ryan Hieber <[email protected]>
  • Loading branch information
2 people authored and Brutus5000 committed Apr 21, 2024
1 parent 6bc7581 commit 28e4b49
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

subprojects {
group = "com.faforever.ice"
version = "1.0-SNAPSHOT"

// Read from gradle.properties
version = "$version"

tasks {
withType<KotlinCompile> {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version=unspecified
kotlin.code.style=official
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
Expand Down
5 changes: 4 additions & 1 deletion kia-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ tasks {

named<ShadowJar>("shadowJar") {
manifest {
attributes(mapOf("Main-Class" to "com.faforever.ice.KiaApplication"))
attributes(mapOf(
"Main-Class" to "com.faforever.ice.KiaApplication",
"Implementation-Version" to version,
))
}
}
}
5 changes: 5 additions & 0 deletions kia-lib/src/main/kotlin/com/faforever/ice/IceAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class IceAdapter(
private val onIceAdapterStopped: () -> Unit,
initialCoturnServers: List<CoturnServer>,
) {
companion object {
val version by lazy {
IceAdapter::class.java.getPackage().implementationVersion ?: "SNAPSHOT"
}
}

private val coturnServers: MutableList<CoturnServer> = ArrayList(initialCoturnServers)
private val objectLock = Object()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.faforever.ice.telemetry

import com.faforever.ice.IceAdapter
import com.faforever.ice.IceOptions
import com.fasterxml.jackson.databind.ObjectMapper
import io.github.oshai.kotlinlogging.KotlinLogging
Expand All @@ -14,12 +15,10 @@ import java.util.concurrent.CompletableFuture
private val logger = KotlinLogging.logger {}

class TelemetryClient(
iceOptions: IceOptions,
private val iceOptions: IceOptions,
private val objectMapper: ObjectMapper,
) {
private val serverBaseUrl = iceOptions.telemetryServer
private val gameId = iceOptions.gameId
private val userId = iceOptions.userId

private val websocketClient: WebSocketClient
private var connectingFuture: CompletableFuture<Void>
Expand Down Expand Up @@ -56,13 +55,14 @@ class TelemetryClient(
"ws",
"http",
)
}/app.html?gameId=$gameId&playerId=$userId"
}/app.html?gameId=${iceOptions.gameId}&playerId=${iceOptions.userId}"
}

val uri: URI = URI.create("$serverBaseUrl/adapter/v1/game/$gameId/player/$userId")
val uri: URI = URI.create("$serverBaseUrl/adapter/v1/game/${iceOptions.gameId}/player/${iceOptions.userId}")
websocketClient = TelemetryWebsocketClient(uri)

connectingFuture = connectAsync()
registerAsPeer()
}

private fun connectAsync() = CompletableFuture.runAsync(websocketClient::connect)
Expand All @@ -82,6 +82,14 @@ class TelemetryClient(
}
}

private fun registerAsPeer() {
val message = RegisterAsPeer(
"kotlin-ice-adapter/${IceAdapter.version}",
iceOptions.userName,
)
sendMessage(message)
}

fun updateCoturnList(servers: Collection<com.faforever.ice.peering.CoturnServer>) {
val telemetryCoturnServers = servers.map { server ->
CoturnServer(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.faforever.ice.telemetry

import com.faforever.ice.IceAdapter
import com.faforever.ice.IceOptions
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import io.mockk.mockkConstructor
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.verify
Expand All @@ -28,11 +30,16 @@ class TelemetryClientTest {
@BeforeEach
fun beforeEach() {
every { mockIceOptions.telemetryServer } returns "wss://mock-telemetryserver.xyz:"
every { mockIceOptions.userName } returns "Player1"
every { mockIceOptions.userId } returns 5000
every { mockIceOptions.gameId } returns 12345

mockkStatic(UUID::class)
every { UUID.randomUUID() } returns messageUuid

mockkObject(IceAdapter)
every { IceAdapter.version } returns "9.9.9-SNAPSHOT"

mockkConstructor(TelemetryClient.TelemetryWebsocketClient::class)
every { anyConstructed<TelemetryClient.TelemetryWebsocketClient>().connect() } returns Unit
sut = TelemetryClient(mockIceOptions, jacksonObjectMapper())
Expand All @@ -43,6 +50,24 @@ class TelemetryClientTest {
unmockkStatic(UUID::class)
}

@Test
fun `test init connects and registers as peer`() {
verify {
anyConstructed<TelemetryClient.TelemetryWebsocketClient>().connect()

val expected =
"""
{
"messageType":"RegisterAsPeer",
"adapterVersion":"kotlin-ice-adapter/9.9.9-SNAPSHOT",
"userName":"Player1",
"messageId":"$messageUuid"
}
""".trimIndent().replace("\n", "")
anyConstructed<TelemetryClient.TelemetryWebsocketClient>().send(expected)
}
}

@Test
fun `test update coturn servers`() {
val coturnServers: List<com.faforever.ice.peering.CoturnServer> = listOf(
Expand Down

0 comments on commit 28e4b49

Please sign in to comment.