All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.neko233.virtualRouter.server.RouterServer.kt Maven / Gradle / Ivy

package com.neko233.virtualRouter.server

import com.neko233.virtualRouter.core.anno.BlockingMainThreadWaitShutdown
import com.neko233.virtualRouter.core.utils.JsonUtils
import com.neko233.virtualRouter.core.utils.NettyUtils
import com.neko233.virtualRouter.server.config.RouterServerConfig
import com.neko233.virtualRouter.server.pipeline.RouterServerChannelInitializer
import io.netty.channel.EventLoopGroup
import org.slf4j.LoggerFactory

class RouterServer(
    val configName: String = RouterServerConstant.CONFIG_NAME
) {

    val config: RouterServerConfig

    companion object {
        @JvmStatic
        private val LOGGER = LoggerFactory.getLogger(RouterServer::class.java)!!
    }

    init {
        val config = RouterServerConfig.readFromConfigFile(configName)
        this.config = config

        LOGGER.info(
            "\n" + """
            ====================================
                    Router Server 启动!
            ====================================
            Server config = ${JsonUtils.serialize(config)}
        """.trimIndent()
        )
    }


    @BlockingMainThreadWaitShutdown
    fun startUp() {
        // handler
        val pipeline = RouterServerChannelInitializer()

        // server
        val server = NettyUtils.createNettyServer(pipeline)
        // server options
        val bossGroup: EventLoopGroup = server.bossGroup
        val workerGroup: EventLoopGroup = server.workerGroup
        val serverBootstrap = server.serverBootstrap

        try {

            val future = serverBootstrap.bind(
                config.routerServerPort
            ).sync()
            future.channel().closeFuture().sync()
        } finally {
            bossGroup.shutdownGracefully()
            workerGroup.shutdownGracefully()
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy