Skip to content

Commit

Permalink
feat: Implemented mindustry server messenger requests
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Nov 6, 2023
1 parent 623ea07 commit 1f2e0bf
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import com.xpdustry.imperium.common.account.AccountManager
import com.xpdustry.imperium.common.account.MongoAccountManager
import com.xpdustry.imperium.common.account.MongoUserManager
import com.xpdustry.imperium.common.account.UserManager
import com.xpdustry.imperium.common.application.ImperiumMetadata
import com.xpdustry.imperium.common.bridge.PlayerTracker
import com.xpdustry.imperium.common.bridge.RequestingPlayerTracker
import com.xpdustry.imperium.common.config.ImageAnalysisConfig
import com.xpdustry.imperium.common.config.ImperiumConfig
import com.xpdustry.imperium.common.config.ImperiumConfigFactory
Expand Down Expand Up @@ -54,6 +55,9 @@ import com.xpdustry.imperium.common.storage.MinioStorageBucket
import com.xpdustry.imperium.common.storage.StorageBucket
import com.xpdustry.imperium.common.translator.DeeplTranslator
import com.xpdustry.imperium.common.translator.Translator
import com.xpdustry.imperium.common.version.ImperiumVersion
import java.util.function.Supplier
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration
import okhttp3.OkHttpClient
Expand All @@ -70,7 +74,7 @@ fun commonModule() =
}
}

single<Discovery> { SimpleDiscovery(get(), get(), get(), get()) }
single<Discovery> { SimpleDiscovery(get(), get("identifier"), get("discovery"), get()) }

single<VpnDetection> {
when (val config = get<ImperiumConfig>().network.vpnDetection) {
Expand All @@ -81,7 +85,7 @@ fun commonModule() =

single<Messenger> {
when (val config = get<ImperiumConfig>().messenger) {
is MessengerConfig.RabbitMQ -> RabbitmqMessenger(config, get())
is MessengerConfig.RabbitMQ -> RabbitmqMessenger(config, get("identifier"))
}
}

Expand All @@ -91,8 +95,6 @@ fun commonModule() =
}
}

single { ImperiumMetadata() }

single<MongoProvider> { SimpleMongoProvider(get()) }

single<AccountManager> { MongoAccountManager(get()) }
Expand Down Expand Up @@ -122,4 +124,14 @@ fun commonModule() =
}

single<SnowflakeGenerator> { SimpleSnowflakeGenerator(get()) }

single<Supplier<Discovery.Data>>("discovery") { Supplier { Discovery.Data.Unknown } }

single<String>("identifier") {
get<ImperiumConfig>().server.name + "-" + Random.nextInt(0, 1000)
}

single<ImperiumVersion> { ImperiumVersion(1, 1, 1) }

single<PlayerTracker> { RequestingPlayerTracker(get()) }
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Imperium, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.imperium.common.bridge

import com.xpdustry.imperium.common.message.Message
import com.xpdustry.imperium.common.message.Messenger
import com.xpdustry.imperium.common.message.request
import com.xpdustry.imperium.common.security.Identity
import com.xpdustry.imperium.common.serialization.SerializableJInstant
import java.time.Instant
import kotlin.time.Duration.Companion.seconds
import kotlinx.serialization.Serializable

interface PlayerTracker {
suspend fun getPlayerJoins(server: String): List<Entry>?

suspend fun getPlayerQuits(server: String): List<Entry>?

suspend fun getOnlinePlayers(server: String): List<Entry>?

suspend fun getPlayerEntry(tid: Long): Entry?

@Serializable
data class Entry(
val player: Identity.Mindustry,
val tid: Long,
val timestamp: SerializableJInstant = Instant.now()
)
}

open class RequestingPlayerTracker(protected val messenger: Messenger) : PlayerTracker {

override suspend fun getPlayerJoins(server: String): List<PlayerTracker.Entry>? =
requestPlayerList(server, PlayerListRequest.Type.JOIN)

override suspend fun getPlayerQuits(server: String): List<PlayerTracker.Entry>? =
requestPlayerList(server, PlayerListRequest.Type.QUIT)

override suspend fun getOnlinePlayers(server: String): List<PlayerTracker.Entry>? =
requestPlayerList(server, PlayerListRequest.Type.ONLINE)

private suspend fun requestPlayerList(server: String, type: PlayerListRequest.Type) =
messenger
.request<PlayerListResponse>(PlayerListRequest(server, type), timeout = 1.seconds)
?.entries

override suspend fun getPlayerEntry(tid: Long): PlayerTracker.Entry? =
messenger
.request<PlayerLookupResponse>(PlayerLookupRequest(tid), timeout = 1.seconds)
?.entry

@Serializable
protected data class PlayerListRequest(
val server: String,
val type: Type,
) : Message {
enum class Type {
JOIN,
QUIT,
ONLINE
}
}

@Serializable
protected data class PlayerListResponse(val entries: List<PlayerTracker.Entry>) : Message

@Serializable protected data class PlayerLookupRequest(val tid: Long) : Message

@Serializable
protected data class PlayerLookupResponse(val entry: PlayerTracker.Entry) : Message
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface Messenger {
}

fun interface FunctionListener<M : Message, R : Message> {
suspend fun onMessage(message: M): R
suspend fun onMessage(message: M): R?
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import com.rabbitmq.client.Consumer
import com.rabbitmq.client.Envelope
import com.rabbitmq.client.ShutdownSignalException
import com.xpdustry.imperium.common.application.ImperiumApplication
import com.xpdustry.imperium.common.application.ImperiumMetadata
import com.xpdustry.imperium.common.async.ImperiumScope
import com.xpdustry.imperium.common.config.MessengerConfig
import com.xpdustry.imperium.common.misc.LoggerDelegate
Expand Down Expand Up @@ -54,7 +53,7 @@ private typealias MessageOrRequest<T> = Pair<T, RabbitmqMessenger.RequestData?>

class RabbitmqMessenger(
private val config: MessengerConfig.RabbitMQ,
private val metadata: ImperiumMetadata
private val identifier: String
) : Messenger, ImperiumApplication.Listener {
internal val flows = ConcurrentHashMap<KClass<out Message>, FlowWithCTag<out Message>>()
private lateinit var channel: Channel
Expand All @@ -77,7 +76,7 @@ class RabbitmqMessenger(
}
}

connection = factory.newConnection(metadata.identifier.toString())
connection = factory.newConnection(identifier)
channel = connection.createChannel()
channel.exchangeDeclare(IMPERIUM_EXCHANGE, BuiltinExchangeType.DIRECT, false, true, null)
}
Expand Down Expand Up @@ -132,7 +131,7 @@ class RabbitmqMessenger(
val bytes = json.encodeToByteArray()
val headers =
mutableMapOf(
SENDER_HEADER to metadata.identifier.toString(),
SENDER_HEADER to identifier,
JAVA_CLASS_HEADER to message::class.jvmName,
)
if (request != null) {
Expand Down Expand Up @@ -166,7 +165,7 @@ class RabbitmqMessenger(
function: Messenger.FunctionListener<M, R>
): Job {
return handle(type) { (message, request) ->
val response = function.onMessage(message)
val response = function.onMessage(message) ?: return@handle
publish0(response, false, request!!.copy(reply = true))
}
}
Expand Down Expand Up @@ -236,7 +235,7 @@ class RabbitmqMessenger(
"Received ${type.simpleName ?: type.jvmName} message without sender header from $envelope")
return@runBlocking
}
if (sender == metadata.identifier.toString()) {
if (sender == identifier) {
return@runBlocking
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,52 @@
*/
package com.xpdustry.imperium.common.network

import com.xpdustry.imperium.common.serialization.SerializableInetAddress
import com.xpdustry.imperium.common.version.MindustryVersion
import kotlinx.serialization.Serializable

interface Discovery {
val servers: Collection<Server>

fun heartbeat()

val servers: List<ServerInfo>
@Serializable data class Server(val name: String, val identifier: String, val data: Data)

@Serializable
sealed interface Data {
@Serializable data object Unknown : Data

@Serializable data object Discord : Data

@Serializable
data class Mindustry(
val name: String,
val host: SerializableInetAddress,
val port: Int,
val mapName: String,
val description: String,
val wave: Int,
val playerCount: Int,
val playerLimit: Int,
val gameVersion: MindustryVersion,
val gameMode: GameMode,
val gameModeName: String?,
val state: State,
) : Data {

enum class State {
PLAYING,
PAUSED,
STOPPED
}

enum class GameMode {
SURVIVAL,
SANDBOX,
ATTACK,
PVP,
EDITOR,
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.xpdustry.imperium.common.message.Message
import kotlinx.serialization.Serializable

@Serializable
data class DiscoveryMessage(val info: ServerInfo, val type: Type) : Message {
data class DiscoveryMessage(val info: Discovery.Server, val type: Type) : Message {
enum class Type {
DISCOVER,
UN_DISCOVER,
Expand Down

This file was deleted.

Loading

0 comments on commit 1f2e0bf

Please sign in to comment.