Skip to content

Commit

Permalink
feature: Attribute modifications and corresponding packet listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
utfunderscore committed Aug 24, 2024
1 parent dac65b3 commit 35a2f60
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import org.readutf.orchestrator.server.game.GameManager
import org.readutf.orchestrator.server.game.store.impl.InMemoryGameStore
import org.readutf.orchestrator.server.network.exception.SocketExceptionHandler
import org.readutf.orchestrator.server.network.listeners.ChannelCloseListener
import org.readutf.orchestrator.server.network.listeners.GamesUpdateListener
import org.readutf.orchestrator.server.network.listeners.HeartbeatListener
import org.readutf.orchestrator.server.network.listeners.ServerRegisterListener
import org.readutf.orchestrator.server.network.listeners.ServerUnregisterListener
import org.readutf.orchestrator.server.network.listeners.game.GamesUpdateListener
import org.readutf.orchestrator.server.network.listeners.server.AttributeUpdateListener
import org.readutf.orchestrator.server.network.listeners.server.HeartbeatListener
import org.readutf.orchestrator.server.network.listeners.server.ServerRegisterListener
import org.readutf.orchestrator.server.network.listeners.server.ServerUnregisterListener
import org.readutf.orchestrator.server.server.ServerCommand
import org.readutf.orchestrator.server.server.ServerManager
import org.readutf.orchestrator.server.server.store.impl.MemoryServerStore
Expand Down Expand Up @@ -73,6 +74,7 @@ class Orchestrator(
listeners.registerListener(ServerRegisterListener(serverManager))
listeners.registerListener(ServerUnregisterListener(serverManager))
listeners.registerListener(GamesUpdateListener(gameManager))
listeners.registerListener(AttributeUpdateListener(serverManager))
}.exception(SocketException::class.java, SocketExceptionHandler())
.exception {
logger.error(it) { "Netty Exception" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.readutf.orchestrator.server.network.listeners.server

import org.readutf.hermes.channel.HermesChannel
import org.readutf.orchestrator.server.network.listeners.NoopListener
import org.readutf.orchestrator.server.server.RegisteredServer
import org.readutf.orchestrator.server.server.ServerManager
import org.readutf.orchestrator.shared.packets.ServerRegisterPacket

class ServerRegisterListener(
private val serverManager: ServerManager,
) : NoopListener<ServerRegisterPacket> {
override fun handle(
packet: ServerRegisterPacket,
channel: HermesChannel,
) {
serverManager.registerServer(RegisteredServer.create(packet.server, channel))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.readutf.orchestrator.server.network.listeners.server

import org.readutf.hermes.channel.HermesChannel
import org.readutf.orchestrator.server.network.listeners.NoopListener
import org.readutf.orchestrator.server.server.ServerManager
import org.readutf.orchestrator.shared.packets.ServerUnregisterPacket

class ServerUnregisterListener(
private val serverManager: ServerManager,
) : NoopListener<ServerUnregisterPacket> {
override fun handle(
packet: ServerUnregisterPacket,
channel: HermesChannel,
) {
serverManager.unregisterServer(packet.serverId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RegisteredServer(
heartbeat: ServerHeartbeat = ServerHeartbeat(serverId, System.currentTimeMillis()),
channel: HermesChannel,
private val registeredAt: Long = System.currentTimeMillis(),
) : Server(serverId, address, gameTypes, gameFinders, heartbeat, mapOf()) {
) : Server(serverId, address, gameTypes, gameFinders, heartbeat, mutableMapOf()) {
@JSONField(serialize = false)
fun getUptime() = System.currentTimeMillis() - registeredAt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ class ServerManager(
serverStore.updateHeartbeat(serverHeartbeat.serverId, serverHeartbeat)
}

fun setAttribute(
serverId: UUID,
attributeName: String,
any: Any,
) {
serverStore.setAttribute(serverId, attributeName, any)
}

fun removeAttribute(
serverId: UUID,
attributeName: String,
) {
serverStore.removeAttribute(serverId, attributeName)
}

fun getServerByShortId(shortId: String): Server? = serverStore.getServerByShortId(shortId)

fun getServerById(serverId: UUID): Server? = serverStore.getServerById(serverId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ interface ServerStore {
fun getAllServers(): List<RegisteredServer>

fun getServerByShortId(shortId: String): Server?

fun setAttribute(
serverId: UUID,
attributeName: String,
any: Any,
)

fun removeAttribute(
serverId: UUID,
attributeName: String,
)
}
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.TypedObject
import java.util.*

class MemoryServerStore : ServerStore {
Expand Down Expand Up @@ -40,7 +41,26 @@ class MemoryServerStore : ServerStore {

override fun getAllServers(): List<RegisteredServer> = servers.values.toList()

override fun getServerByShortId(shortId: String): Server? = servers.values.first { it.serverId.toString().startsWith(shortId) }
override fun getServerByShortId(shortId: String): Server? = servers.values.firstOrNull { it.serverId.toString().startsWith(shortId) }

override fun setAttribute(
serverId: UUID,
attributeName: String,
data: TypedObject,
) {
val serverById = getServerById(serverId) ?: return

serverById.attributes[attributeName] = data
}

override fun removeAttribute(
serverId: UUID,
attributeName: String,
) {
val serverById = getServerById(serverId) ?: return

serverById.attributes.remove(attributeName)
}

override fun getTimedOutServers(): List<RegisteredServer> {
val now = System.currentTimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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.TypedObject
import java.util.*

class H2ServerStore : ServerStore {
Expand Down Expand Up @@ -41,4 +42,19 @@ class H2ServerStore : ServerStore {
override fun getServerByShortId(shortId: String): Server? {
TODO("Not yet implemented")
}

override fun setAttribute(
serverId: UUID,
attributeName: String,
data: TypedObject,
) {
TODO("Not yet implemented")
}

override fun removeAttribute(
serverId: UUID,
attributeName: String,
) {
TODO("Not yet implemented")
}
}

0 comments on commit 35a2f60

Please sign in to comment.