netty之pipeline模型

最近在学习netty,netty原生代码看起来挺复杂,为了让自己理解起来容易一点,对netty中的部件进行了简化,这里介绍的是netty的pipeline。

首先看一下简化后的代码结构
在这里插入图片描述
pipeline就像一个双向链表,放着head和tail两个链表节点,提供addLast和addFirst等方法,向链表中添加数据

context是链表的一个个的node节点,包含prev和next指针,以及存放的实际的数据,即handler

pipeline的fireChannelRead等方法像是遍历链表节点的作用

invoker是netty中使用到的,在这里的作用其实没有体现的很明显

context内容

package com.mylovin.netty.nettycode.pipe.context;

import com.mylovin.netty.nettycode.pipe.handler.Handler;
import com.mylovin.netty.nettycode.pipe.invoker.DefaultInvoker;

public class Context {
    public Context next;
    public Context prev;
    public Handler handler;
    public DefaultInvoker invoker;

    DefaultInvoker invoker() {
        return this.invoker == null ? new DefaultInvoker() : this.invoker;
    }

    public void fireChannelRead() {
        Context next = this.next;
        if (null != next) {
            next.invoker().invokeChannelRead(next);
        }
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }
}

handler

package com.mylovin.netty.nettycode.pipe.handler;

import com.mylovin.netty.nettycode.pipe.context.Context;

public interface Handler {
    void fireChannelRead(Context context);
}

DefaultInvoker

package com.mylovin.netty.nettycode.pipe.invoker;

import com.mylovin.netty.nettycode.pipe.context.Context;

public class DefaultInvoker {
    public void invokeChannelRead(Context context) {
        InvokerUtil.invokeChannelReadNow(context);
    }
}

InvokerUtil

package com.mylovin.netty.nettycode.pipe.invoker;

import com.mylovin.netty.nettycode.pipe.context.Context;

public class InvokerUtil {
    public static void invokeChannelReadNow(Context context) {
        context.handler.fireChannelRead(context);
    }
}

pipeline

package com.mylovin.netty.nettycode.pipe.pipeline;

import com.mylovin.netty.nettycode.pipe.context.Context;
import com.mylovin.netty.nettycode.pipe.handler.Handler;

public class DefaultPipeline {
    private Context headContext;
    private Context tailContext;

    public DefaultPipeline() {
        this.headContext = new HeadContext();
        this.tailContext = new TailContext();
        this.headContext.next = this.tailContext;
        this.tailContext.prev = this.headContext;
    }

    public void addLast(Handler handler) {
        Context ctx = new Context();
        ctx.setHandler(handler);

        Context tmp = this.tailContext.prev;
        tmp.next = ctx;
        ctx.prev = tmp;
        ctx.next = this.tailContext;
        this.tailContext.prev = ctx;
    }

    public void fireChannelRead() {
        this.headContext.fireChannelRead();
    }

    private class HeadContext extends Context {
        public HeadContext() {
            super();
            this.handler = new Handler() {
                @Override
                public void fireChannelRead(Context context) {
                    context.fireChannelRead();
                }
            };
        }
    }

    private class TailContext extends Context {
        public TailContext() {
            super();
            this.handler = new Handler() {
                @Override
                public void fireChannelRead(Context context) {
                    context.fireChannelRead();
                }
            };
        }
    }
}

main中调用

package com.mylovin.netty.nettycode.pipe;

import com.mylovin.netty.nettycode.pipe.handler.CustomHandlerOne;
import com.mylovin.netty.nettycode.pipe.handler.CustomHandlerTwo;
import com.mylovin.netty.nettycode.pipe.pipeline.DefaultPipeline;

/**
 * 解析:
 * 1、pipeline就像一个双向链表,放着head和tail两个链表节点,提供addLast和addFirst等方法,向链表中添加数据
 * 2、context是链表的一个个的node节点,包含prev和next指针,以及存放的实际的数据,即handler
 * 3、pipeline的fireChannelRead等方法像是遍历链表节点的作用
 * 4、invoker是netty中使用到的,在这里的作用其实没有体现的很明显
 */
public class Main {
    public static void main(String[] args) {
        DefaultPipeline pipeline = new DefaultPipeline();
        pipeline.addLast(new CustomHandlerOne());
        pipeline.addLast(new CustomHandlerTwo());
        pipeline.fireChannelRead();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值