diff --git a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PlayerPositionUpdate.kt b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PlayerPositionUpdate.kt index bc9316d..6a4f746 100644 --- a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PlayerPositionUpdate.kt +++ b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PlayerPositionUpdate.kt @@ -16,5 +16,5 @@ import kotlinx.serialization.Serializable @Serializable data class PlayerPositionUpdate( val playerUid: String, - val position: WorldPosition + val position: PointedWorldPosition ) : RabbitSendable diff --git a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PointedWorldPosition.kt b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PointedWorldPosition.kt index 92084d9..687cb43 100644 --- a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PointedWorldPosition.kt +++ b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/PointedWorldPosition.kt @@ -1,13 +1,16 @@ package de.themeparkcraft.audioserver.common.interfaces +import kotlinx.serialization.Serializable + /** * Represents a pointed position in the world, specified by its x, y, z coordinates, the world it belongs to, * and the yaw and pitch rotations. * * @see WorldPosition */ -class PointedWorldPosition( - x: Double, y: Double, z: Double, world: String, +@Serializable +class PointedWorldPosition ( + val _x: Double, val _y: Double, val _z: Double, val _world: String, /** * The yaw rotation of a pointed world position. * @@ -31,4 +34,10 @@ class PointedWorldPosition( * @see WorldPosition */ val pitch: Float -) : WorldPosition(x, y, z, world) \ No newline at end of file +) : WorldPosition(_x, _y, _z, _world) { + + override fun toString(): String { + return "PointedWorldPosition(x=$x, y=$y, z=$z, world='$world', yaw=$yaw, pitch=$pitch)" + } + +} \ No newline at end of file diff --git a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/RabbitSendable.kt b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/RabbitSendable.kt index c79d746..66342e3 100644 --- a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/RabbitSendable.kt +++ b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/RabbitSendable.kt @@ -2,7 +2,6 @@ package de.themeparkcraft.audioserver.common.interfaces import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.Serializable -import kotlinx.serialization.decodeFromByteArray import kotlinx.serialization.encodeToByteArray import kotlinx.serialization.protobuf.ProtoBuf @@ -14,9 +13,4 @@ sealed interface RabbitSendable { return ProtoBuf.encodeToByteArray(this) } - @OptIn(ExperimentalSerializationApi::class) - fun decode(data: ByteArray): RabbitSendable { - return ProtoBuf.decodeFromByteArray(data) - } - } \ No newline at end of file diff --git a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/WorldPosition.kt b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/WorldPosition.kt index 513bd20..95f9360 100644 --- a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/WorldPosition.kt +++ b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/interfaces/WorldPosition.kt @@ -33,4 +33,10 @@ open class WorldPosition( * Represents the name of the world. */ val world: String -): RabbitSendable +): RabbitSendable { + + override fun toString(): String { + return "WorldPosition(x=$x, y=$y, z=$z, world='$world')" + } + +} diff --git a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/rabbit/RabbitClient.kt b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/rabbit/RabbitClient.kt index 6cf5510..fed9f1a 100644 --- a/common/src/main/kotlin/de/themeparkcraft/audioserver/common/rabbit/RabbitClient.kt +++ b/common/src/main/kotlin/de/themeparkcraft/audioserver/common/rabbit/RabbitClient.kt @@ -63,7 +63,6 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) { fun sendMessage(rabbitSendable: RabbitSendable) { CoroutineScope(Dispatchers.Default).launch { val message = rabbitSendable.encode() - val combinedMessage = (rabbitSendable::class.java.simpleName + ":").toByteArray(Charset.forName("UTF-8")) + message channel.basicPublish(EXCHANGE_NAME, ROUTIING_KEY, null, message) } } @@ -77,15 +76,9 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) { @OptIn(ExperimentalSerializationApi::class) fun deserializeRabbitMessage(message: ByteArray): RabbitSendable { val messageString = message.toString(Charset.forName("UTF-8")) - getLogger().info("Received message: $messageString") -// val splitIndex = messageString.indexOf(":") -// val className = messageString.substring(0, splitIndex) -// val messageBody = messageString.substring(splitIndex + 1).toByteArray(Charset.forName("UTF-8")) + getLogger().info("Deserializing message: $messageString") try { -// val customClass = Class.forName("de.themeparkcraft.audioserver.common.interfaces.$className") - val sendable = ProtoBuf.decodeFromByteArray (message) - - return sendable + return ProtoBuf.decodeFromByteArray (message) } catch (e: Exception) { getLogger().error("Failed to deserialize message: $messageString", e) throw e diff --git a/gradle.properties b/gradle.properties index 85e7982..1593b06 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ kotlin.code.style=official -audioServerVersion=0.4-Snapshot +audioServerVersion=0.5-Snapshot diff --git a/standalone/src/main/kotlin/de/themeparkcraft/audioserver/AudioServerStandalone.kt b/standalone/src/main/kotlin/de/themeparkcraft/audioserver/AudioServerStandalone.kt index a31d35f..cbba874 100644 --- a/standalone/src/main/kotlin/de/themeparkcraft/audioserver/AudioServerStandalone.kt +++ b/standalone/src/main/kotlin/de/themeparkcraft/audioserver/AudioServerStandalone.kt @@ -2,6 +2,7 @@ package de.themeparkcraft.audioserver import de.themeparkcraft.audioserver.common.data.RabbitConfiguration import de.themeparkcraft.audioserver.common.extensions.getLogger +import de.themeparkcraft.audioserver.common.interfaces.PlayerPositionUpdate import de.themeparkcraft.audioserver.common.rabbit.RabbitClient import de.themeparkcraft.audioserver.utils.Environment import kotlin.system.exitProcess @@ -25,8 +26,14 @@ class AudioServerStandalone { rabbitClient = RabbitClient(rabbitConfiguration) rabbitClient.withListener { message, delivery -> - val rabbitSendable = rabbitClient.deserializeRabbitMessage(delivery.body) - getLogger().info("Received message: ${rabbitSendable.javaClass.simpleName}") + when (val rabbitSendable = rabbitClient.deserializeRabbitMessage(delivery.body)) { + is PlayerPositionUpdate -> { + getLogger().info("Received PlayerPositionUpdate message: ${rabbitSendable.playerUid} at ${rabbitSendable.position}") + } + else -> { + getLogger().warn("Received unknown RabbitSendable message: $rabbitSendable") + } + } } getLogger().info("AudioServer standalone started.")