netty pipeline仿写例子

package netty.handler.context

abstract class AbstractChannelHandlerContext(private val inBound : Boolean, val outBound : Boolean){

    lateinit var next : AbstractChannelHandlerContext
    lateinit var prev : AbstractChannelHandlerContext


    abstract fun handler() : ChannelHandler

    fun fireChannelRegistered() {
        invokeChannelRegistered(findContextInbound())
    }

    /**
     * inbound是从head开始向下找,找到下一个inBound
     * HeadContext的inBound属性为false
     */
    private fun findContextInbound(): AbstractChannelHandlerContext {
        var ctx : AbstractChannelHandlerContext = this
        do {
            ctx = ctx.next
        }while (!ctx.inBound)
        return ctx
    }

    /**
     * 拿掉下一个节点获取handler并调用
     */
    private fun invokeChannelRegistered(next : AbstractChannelHandlerContext) {
        (next.handler() as ChannelInBoundHandler).channelRegistered()
    }

    companion object {
        // ctx 当前节点已经可以进行调用了,HeadContext的inBound属性为false所以需要找到下一个inBound节点
        //AbstractChannelHandlerContext 和 pipeline都有fireChannelRegistered方法,区别是pipeline会走遍整个节点的inBound,如果直接调用ctx.fireChannelRegistered则表示将节点向下传递
        fun invokeChannelRegistered(ctx: AbstractChannelHandlerContext) {
            ctx.fireChannelRegistered()
        }
    }

}
class HeadContext : AbstractChannelHandlerContext(false, true), ChannelOutBoundHandler {

    override fun handler(): ChannelHandler = this
}
class TailContext : AbstractChannelHandlerContext(true, false), ChannelOutBoundHandler {

    override fun handler(): ChannelHandler = this
}
class DefaultChannelHandlerContext(var handler : ChannelHandler) : AbstractChannelHandlerContext(isInBound(handler), isOutBound(handler)) {


    override fun handler() = handler

    companion object {
        fun isOutBound(handler : ChannelHandler) = handler is ChannelOutBoundHandler

        fun isInBound(handler : ChannelHandler) = handler is ChannelInBoundHandler
    }


}
interface ChannelInBoundHandler : ChannelHandler{

    fun channelRegistered()

}
interface ChannelHandler {
}
class Pipeline(
        private val head: AbstractChannelHandlerContext = HeadContext(),
        private val tail : AbstractChannelHandlerContext = TailContext()
) {

    init {
        head.next = tail
        tail.prev = head
    }

    fun addLast(handler : ChannelHandler){
        val context = newContext(handler)
        addLast(context)
    }

    private fun addLast(ctx: AbstractChannelHandlerContext) {
        val prev = tail.prev
        ctx.next = tail
        ctx.prev = prev
        prev.next = ctx
        tail.prev = ctx
    }

    private fun newContext(handler : ChannelHandler) = DefaultChannelHandlerContext(handler)

    // pipeline管理AbstractChannelHandlerContext组成的链表, fire方法用来处理in 事件
    // 以下方法作为触发pipeline流程的方法
    fun fireChannelRegistered(){
        //利用静态方法作为触发点
        AbstractChannelHandlerContext.invokeChannelRegistered(head)
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值