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

com.neko233.virtualRouter.core.RouteMessage.kt Maven / Gradle / Ivy

There is a newer version: 0.0.4
Show newest version
package com.neko233.virtualRouter.core

import com.alibaba.fastjson2.JSON
import com.neko233.virtualRouter.core.constant.RouteMessageTypeEnum
import com.neko233.virtualRouter.core.utils.JsonUtils
import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator
import org.slf4j.LoggerFactory


data class RouteMessage(
    val fromRouteId: String,
    val toRouteId: String,
    val messageType: RouteMessageTypeEnum?,
    // json data
    var data: String? = null,
) {

    fun encode(): ByteBuf {
        val jsonBytes = JSON.toJSONBytes(this)
        val buffer = ByteBufAllocator.DEFAULT.buffer(jsonBytes.size + Integer.BYTES)
        buffer.writeInt(jsonBytes.size)
        buffer.writeBytes(jsonBytes)
        return buffer
    }

    companion object {

        private val LOGGER = LoggerFactory.getLogger(RouteMessage::class.java)

        @JvmStatic
        fun decode(jsonMsg: String): RouteMessage {
            try {
                return JsonUtils.deserialize(jsonMsg, RouteMessage::class.java)!!
            } catch (e: Exception) {
                LOGGER.error("err request json = {}", jsonMsg)
                throw e
            }
        }


    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy