Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
utfunderscore committed Aug 27, 2024
1 parent 0dfd5df commit dc1191e
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package org.readutf.orchestrator.wrapper

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.jetbrains.annotations.Blocking
import org.readutf.orchestrator.shared.server.Server
import org.readutf.orchestrator.shared.utils.ApiResponse
import org.readutf.orchestrator.shared.utils.Result
import org.readutf.orchestrator.wrapper.services.DockerService
import org.readutf.orchestrator.wrapper.services.ServerService
import org.readutf.orchestrator.wrapper.types.ContainerPort
import org.readutf.orchestrator.wrapper.types.NetworkAddress
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import java.util.Base64
Expand Down Expand Up @@ -41,6 +43,8 @@ class OrchestratorApi(
return objectMapper.readValue(json, object : TypeReference<ApiResponse<List<ContainerPort>>>() {})
}

suspend fun getNetworks(shortId: String): ApiResponse<List<NetworkAddress>> = dockerService.getIp(shortId)

suspend fun getServerByType(gameType: String): Result<List<Server>> =
try {
val response = serverService.getServer(gameType)
Expand Down Expand Up @@ -78,6 +82,6 @@ class OrchestratorApi(
}

companion object {
val objectMapper = ObjectMapper()
val objectMapper = ObjectMapper().registerKotlinModule()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.readutf.orchestrator.wrapper.services

import org.readutf.orchestrator.shared.utils.ApiResponse
import org.readutf.orchestrator.wrapper.types.NetworkAddress
import retrofit2.http.GET
import retrofit2.http.Query

Expand All @@ -8,4 +10,9 @@ interface DockerService {
suspend fun getPort(
@Query("shortId") shortId: String,
): String

@GET("/docker/ip")
suspend fun getIp(
@Query("shortId") shortId: String,
): ApiResponse<List<NetworkAddress>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.readutf.orchestrator.wrapper.types

data class NetworkAddress(
val network: String,
val ipAddress: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,26 @@ class ApiTests {
@Test
fun testGetServers() {
runBlocking {
val response = orchestratorApi.serverService
println(response)

Assertions.assertEquals(true, response.getAllServers().success)
orchestratorApi.getServers()
}
}

@Test
fun testGetPort() {
runBlocking {
println(orchestratorApi.getPort("894475dac0c0"))
println(orchestratorApi.getPort("20a3eae4b5a4"))
}
}

@Test
fun testGetServer() {
runBlocking {
val allServers = orchestratorApi.serverService.getAllServers()
val allServers = orchestratorApi.getServers().get()

Assertions.assertEquals(true, allServers.success)
val serverId = allServers.response!![0].serverId
val serverId = allServers[0].serverId

val getServer = orchestratorApi.serverService.getServer(serverId)
Assertions.assertEquals(true, getServer.success)
val getServer = orchestratorApi.getServerById(serverId)
Assertions.assertEquals(true, getServer.isOk())

println(getServer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.readutf.orchestrator.server

import com.esotericsoftware.kryo.kryo5.Kryo
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.github.oshai.kotlinlogging.KotlinLogging
import org.readutf.hermes.PacketManager
import org.readutf.hermes.platform.netty.nettyServer
Expand Down Expand Up @@ -82,6 +83,6 @@ class Orchestrator(
}.start()

companion object {
val objectMapper = ObjectMapper()
val objectMapper = ObjectMapper().registerKotlinModule()
}
}
13 changes: 13 additions & 0 deletions Server/src/main/kotlin/org/readutf/orchestrator/server/Test.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
package org.readutf.orchestrator.server

import org.readutf.orchestrator.server.docker.DockerManager
import org.readutf.orchestrator.server.settings.DockerSettings
import java.util.*

fun main() {
val dockerManager =
DockerManager(
DockerSettings(
uri = "tcp://localhost:2375",
maxConnections = 100,
responseTimeout = 30,
connectionTimeout = 30,
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,35 @@ class DockerEndpoint(
),
)
}

@Get("ip")
fun getIp(
context: Context,
@Query("shortId") shortId: String,
) {
val containerByShortId = dockerManager.getContainerByShortId(shortId)

if (containerByShortId.isError()) {
context.json(
Base64.getEncoder().encodeToString(
Orchestrator.objectMapper.writeValueAsString(ApiResponse.failure<String>(containerByShortId.getError())).toByteArray(),
),
)
return
}

val get = dockerManager.getContainerByShortId("557e9050b341").get()

val networkSettings = get.networkSettings ?: return

val networks =
networkSettings.networks.map { (name, network) ->
mapOf(
"network" to name,
"ip" to network.ipAddress,
)
}

context.json(networks)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.readutf.orchestrator.server.server.RegisteredServer
import org.readutf.orchestrator.server.server.store.ServerStore
import org.readutf.orchestrator.shared.server.Server
import org.readutf.orchestrator.shared.server.ServerHeartbeat
import org.readutf.orchestrator.shared.utils.TypeWrapper
import java.util.*

class MemoryServerStore : ServerStore {
Expand Down Expand Up @@ -49,7 +50,7 @@ class MemoryServerStore : ServerStore {
) {
val serverById = getServerById(serverId) ?: return

serverById.attributes[attributeName] = typedObject
serverById.attributes[attributeName] = TypeWrapper(typedObject)
}

override fun removeAttribute(
Expand Down
1 change: 1 addition & 0 deletions Shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
api("io.github.oshai:kotlin-logging-jvm:5.1.0")
api("com.esotericsoftware:kryo5:5.6.0")
api("com.fasterxml.jackson.core:jackson-databind:2.17.2")
api("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.+")

val hermesVersion: String by rootProject.extra
compileOnly("org.readutf.hermes:core:$hermesVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package org.readutf.orchestrator.shared.server

import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeInfo.As
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import org.readutf.orchestrator.shared.game.GameFinderType
import org.readutf.orchestrator.shared.utils.TypeWrapper
import java.util.UUID

open class Server(
val serverId: UUID,
val address: ServerAddress,
val gameTypes: List<String>,
val gameFinders: List<GameFinderType>,
var heartbeat: ServerHeartbeat = ServerHeartbeat(serverId, System.currentTimeMillis()),
@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.CLASS) var attributes: MutableMap<String, Any>,
@JsonProperty("serverId") val serverId: UUID,
@JsonProperty("address") val address: ServerAddress,
@JsonProperty("gameTypes") val gameTypes: List<String>,
@JsonProperty("gameFinders") val gameFinders: List<GameFinderType>,
@JsonProperty("heartbeat") var heartbeat: ServerHeartbeat = ServerHeartbeat(serverId, System.currentTimeMillis()),
@JsonProperty("attributes") var attributes: MutableMap<String, TypeWrapper>,
) {
override fun toString(): String =
"Server(serverId=$serverId, address=$address, gameTypes=$gameTypes, gameFinders=$gameFinders, heartbeat=$heartbeat)"

@JsonIgnore
fun getShortId(): String = serverId.toString().substring(0, 8)

override fun toString(): String =
"Server(serverId=$serverId, address=$address, gameTypes=$gameTypes, gameFinders=$gameFinders, heartbeat=$heartbeat, attributes=$attributes)"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.readutf.orchestrator.shared.server

import com.fasterxml.jackson.annotation.JsonProperty

data class ServerAddress(
val host: String,
val port: Int,
@JsonProperty("host") val host: String,
@JsonProperty("port") val port: Int,
) {
override fun toString(): String = "$host:$port"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.readutf.orchestrator.shared.utils

import com.fasterxml.jackson.annotation.JsonIgnore

class ApiResponse<T>(
data class ApiResponse<T>(
val success: Boolean,
var failureReason: String?,
val response: T?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.readutf.orchestrator.shared.utils

import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize

class TypeWrapper(
data: Any,
) {
@JsonSerialize(using = TypeWrapperSerializer::class)
@JsonDeserialize(using = TypeWrapperDeserializer::class)
val data: Any = data

override fun toString(): String = "TypeWrapper(data=$data)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.readutf.orchestrator.shared.utils

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.*

class TypeWrapperSerializer : JsonSerializer<Any>() {
override fun serialize(
data: Any,
generator: JsonGenerator,
provider: SerializerProvider,
) {
generator.writeStartObject()
generator.writeStringField("__type", data::class.java.canonicalName)
generator.writeObjectField("data", data)
generator.writeEndObject()
}
}

class TypeWrapperDeserializer : JsonDeserializer<Any>() {
override fun deserialize(
parser: JsonParser,
context: DeserializationContext,
): Any {
val node: JsonNode = parser.codec.readTree(parser)

val type = Class.forName(node.get("__type").asText())

val rootNode = node.get("data")

val jacksonType: JavaType = context.typeFactory.constructType(type)
val deserializer: JsonDeserializer<*> = context.findRootValueDeserializer(jacksonType)
val nodeParser: JsonParser = rootNode.traverse(context.parser.codec)
nodeParser.nextToken()
return deserializer.deserialize(nodeParser, context)
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "org.readutf.orchestrator"
version = "1.6.0"
version = "1.6.3"

dependencies {
testImplementation(kotlin("test"))
Expand Down

0 comments on commit dc1191e

Please sign in to comment.