Skip to content

Commit

Permalink
Merge branch 'refs/heads/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ming Yue committed Apr 15, 2024
2 parents df1b4bd + bc22e56 commit 8aa54c5
Show file tree
Hide file tree
Showing 35 changed files with 584 additions and 673 deletions.
82 changes: 41 additions & 41 deletions src/main/java/com/github/cao/awa/kalmia/client/KalmiaClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,47 @@ import java.util.function.BiConsumer
import java.util.function.Consumer

class KalmiaClient(config: ClientBootstrapConfig) {
companion object {
private val LOGGER: Logger = LogManager.getLogger("KalmiaClient")
lateinit var clientBootstrapConfig: ClientBootstrapConfig

@JvmStatic
fun setupBootstrapConfig() {
prepareConfig()

clientBootstrapConfig = ClientBootstrapConfig.read(
JSONObject.parse(IOUtil.read(FileReader(KalmiaConstant.CLIENT_CONFIG_PATH))),
KalmiaEnv.DEFAULT_CLIENT_BOOTSTRAP_CONFIG
)

rewriteConfig(clientBootstrapConfig)
}

fun rewriteConfig(bootstrapConfig: ClientBootstrapConfig) {
LOGGER.info("Rewriting client config")

IOUtil.write(
FileWriter(KalmiaConstant.CLIENT_CONFIG_PATH),
bootstrapConfig.toJSON().toString(JSONWriter.Feature.PrettyFormat)
)
}

fun prepareConfig() {
LOGGER.info("Preparing client config")

val configFile = File(KalmiaConstant.CLIENT_CONFIG_PATH)

configFile.parentFile.mkdirs()

if (!configFile.isFile) {
IOUtil.write(
FileWriter(configFile),
IOUtil.read(InputStreamReader(ResourceLoader.stream(KalmiaConstant.CLIENT_DEFAULT_CONFIG_PATH)))
)
}
}
}

private val bootstrapConfig: ClientBootstrapConfig
val messageManager: MessageManager
val userManager: UserManager
Expand Down Expand Up @@ -189,45 +230,4 @@ class KalmiaClient(config: ClientBootstrapConfig) {
null
}
}

companion object {
private val LOGGER: Logger = LogManager.getLogger("KalmiaClient")
lateinit var clientBootstrapConfig: ClientBootstrapConfig

@JvmStatic
fun setupBootstrapConfig() {
prepareConfig()

clientBootstrapConfig = ClientBootstrapConfig.read(
JSONObject.parse(IOUtil.read(FileReader(KalmiaConstant.CLIENT_CONFIG_PATH))),
KalmiaEnv.DEFAULT_CLIENT_BOOTSTRAP_CONFIG
)

rewriteConfig(clientBootstrapConfig)
}

fun rewriteConfig(bootstrapConfig: ClientBootstrapConfig) {
LOGGER.info("Rewriting client config")

IOUtil.write(
FileWriter(KalmiaConstant.CLIENT_CONFIG_PATH),
bootstrapConfig.toJSON().toString(JSONWriter.Feature.PrettyFormat)
)
}

fun prepareConfig() {
LOGGER.info("Preparing client config")

val configFile = File(KalmiaConstant.CLIENT_CONFIG_PATH)

configFile.parentFile.mkdirs()

if (!configFile.isFile) {
IOUtil.write(
FileWriter(configFile),
IOUtil.read(InputStreamReader(ResourceLoader.stream(KalmiaConstant.CLIENT_DEFAULT_CONFIG_PATH)))
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import com.github.cao.awa.kalmia.identity.PureExtraIdentity
import java.util.Queue
import java.util.concurrent.ConcurrentLinkedQueue

class PollingClient(
private val delegate: KalmiaClient
) {
class PollingClient(private val delegate: KalmiaClient) {
companion object {
lateinit var CLIENT: PollingClient
}

private var sessionListenersIdentity: ByteArray = BytesRandomIdentifier.create(24)
private val stackingNotices: Queue<Event> = ConcurrentLinkedQueue()

Expand All @@ -36,8 +38,4 @@ class PollingClient(
fun stackingNotice(notice: Event) {
stackingNotices.add(notice)
}

companion object {
lateinit var CLIENT: PollingClient
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import com.github.zhuaidadaya.rikaishinikui.handler.universal.entrust.EntrustEnv
import java.util.function.Supplier

abstract class ConfigElement {
abstract fun toJSON(): JSONObject

companion object {
@JvmStatic
fun <T> compute(json: JSONObject, key: String, compute: Supplier<T>): T {
Expand All @@ -20,4 +18,6 @@ abstract class ConfigElement {
return EntrustEnvironment.result(json) { jsonObject -> jsonObject.getJSONObject(key) }
}
}

abstract fun toJSON(): JSONObject
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import com.github.cao.awa.kalmia.config.ConfigElement
import com.github.cao.awa.kalmia.config.kalmiagram.client.bootstrap.network.ClientNetworkConfig
import com.github.cao.awa.kalmia.config.kalmiagram.meta.ConfigMeta

class ClientBootstrapConfig(
val meta: ConfigMeta, val clientNetwork: ClientNetworkConfig
) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject()
json["config-meta"] = this.meta.toJSON()
json["client-network"] = this.clientNetwork.toJSON()
return json
}

class ClientBootstrapConfig(val meta: ConfigMeta, val clientNetwork: ClientNetworkConfig) : ConfigElement() {
companion object {
@JvmStatic
fun read(json: JSONObject?, compute: ClientBootstrapConfig?): ClientBootstrapConfig {
Expand All @@ -34,4 +25,11 @@ class ClientBootstrapConfig(
return ClientBootstrapConfig(meta, serverNetwork)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()
json["config-meta"] = this.meta.toJSON()
json["client-network"] = this.clientNetwork.toJSON()
return json
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ package com.github.cao.awa.kalmia.config.kalmiagram.client.bootstrap.network
import com.alibaba.fastjson2.JSONObject
import com.github.cao.awa.kalmia.config.ConfigElement

class ClientNetworkConfig(
val connectHost: String, val connectPort: Int, val useEpoll: Boolean
) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject()
json["connect-host"] = this.connectHost
json["connect-port"] = this.connectPort
json["use-epoll"] = this.useEpoll
return json
}

class ClientNetworkConfig(val connectHost: String, val connectPort: Int, val useEpoll: Boolean) : ConfigElement() {
companion object {
@JvmStatic
fun read(json: JSONObject?, compute: ClientNetworkConfig?): ClientNetworkConfig {
Expand All @@ -32,4 +22,12 @@ class ClientNetworkConfig(
return ClientNetworkConfig(bindHost, bindPort, useEpoll)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()
json["connect-host"] = this.connectHost
json["connect-port"] = this.connectPort
json["use-epoll"] = this.useEpoll
return json
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import com.alibaba.fastjson2.JSONObject
import com.github.cao.awa.kalmia.config.ConfigElement

class ConfigMeta(val version: Int) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject()
json["version"] = this.version
return json
}

companion object {
@JvmStatic
fun read(json: JSONObject?, compute: ConfigMeta?): ConfigMeta {
Expand All @@ -21,9 +15,15 @@ class ConfigMeta(val version: Int) : ConfigElement() {
return compute
}

val version: Int = Math.max(compute(json, "version", compute::version), compute.version)
val version: Int = compute(json, "version", compute::version).coerceAtLeast(compute.version)

return ConfigMeta(version)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()
json["version"] = this.version
return json
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@ import com.alibaba.fastjson2.JSONObject
import com.github.cao.awa.kalmia.config.ConfigElement
import com.github.cao.awa.kalmia.config.kalmiagram.meta.ConfigMeta

class RouterNetworkConfig(
val meta: ConfigMeta, val compressThreshold: Int
) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject()
json["config-meta"] = this.meta.toJSON()
json["compress-threshold"] = this.compressThreshold
return json
}

class RouterNetworkConfig(val meta: ConfigMeta, val compressThreshold: Int) : ConfigElement() {
companion object {
@JvmStatic
fun read(json: JSONObject?, compute: RouterNetworkConfig?): RouterNetworkConfig {
Expand All @@ -32,4 +23,11 @@ class RouterNetworkConfig(
return RouterNetworkConfig(meta, compressThreshold)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()
json["config-meta"] = this.meta.toJSON()
json["compress-threshold"] = this.compressThreshold
return json
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ class ServerBootstrapConfig(
val translation: BootstrapTranslationConfig,
val serverName: String
) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject()
json["config-meta"] = this.meta.toJSON()
json["server-network"] = this.serverNetwork.toJSON()
json["translation"] = this.translation.toJSON()
json["server-name"] = this.serverName
return json
}

companion object {
@JvmStatic
fun read(json: JSONObject?, compute: ServerBootstrapConfig?): ServerBootstrapConfig {
Expand Down Expand Up @@ -48,4 +39,13 @@ class ServerBootstrapConfig(
return ServerBootstrapConfig(meta, serverNetwork, translation, serverName)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()
json["config-meta"] = this.meta.toJSON()
json["server-network"] = this.serverNetwork.toJSON()
json["translation"] = this.translation.toJSON()
json["server-name"] = this.serverName
return json
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ package com.github.cao.awa.kalmia.config.kalmiagram.server.bootstrap.network
import com.alibaba.fastjson2.JSONObject
import com.github.cao.awa.kalmia.config.ConfigElement

class ServerNetworkConfig(
val bindHost: String, val bindPort: Int, val useEpoll: Boolean
) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject();
json["bind-host"] = this.bindHost
json["bind-port"] = this.bindPort
json["use-epoll"] = this.useEpoll
return json
}

class ServerNetworkConfig(val bindHost: String, val bindPort: Int, val useEpoll: Boolean) : ConfigElement() {
companion object {
@JvmStatic
fun read(json: JSONObject?, compute: ServerNetworkConfig?): ServerNetworkConfig {
Expand All @@ -32,4 +22,12 @@ class ServerNetworkConfig(
return ServerNetworkConfig(bindHost, bindPort, useEpoll)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject();
json["bind-host"] = this.bindHost
json["bind-port"] = this.bindPort
json["use-epoll"] = this.useEpoll
return json
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import com.alibaba.fastjson2.JSONObject
import com.github.cao.awa.kalmia.config.ConfigElement

class BootstrapTranslationConfig(val enable: Boolean) : ConfigElement() {
override fun toJSON(): JSONObject {
val json = JSONObject()
json["enable"] = this.enable
return json
}

companion object {
fun read(json: JSONObject?, compute: BootstrapTranslationConfig?): BootstrapTranslationConfig {
if (compute == null) {
Expand All @@ -25,4 +19,10 @@ class BootstrapTranslationConfig(val enable: Boolean) : ConfigElement() {
return BootstrapTranslationConfig(enable)
}
}

override fun toJSON(): JSONObject {
val json = JSONObject()
json["enable"] = this.enable
return json
}
}

This file was deleted.

Loading

0 comments on commit 8aa54c5

Please sign in to comment.