RpcMessageHandler.java revision 420c9859
1420c9859SShuo Chenpackage muduo.rpc;
2420c9859SShuo Chen
3420c9859SShuo Chenimport org.jboss.netty.channel.ChannelHandlerContext;
4420c9859SShuo Chenimport org.jboss.netty.channel.ChannelStateEvent;
5420c9859SShuo Chenimport org.jboss.netty.channel.MessageEvent;
6420c9859SShuo Chenimport org.jboss.netty.channel.SimpleChannelUpstreamHandler;
7420c9859SShuo Chen
8420c9859SShuo Chenpublic class RpcMessageHandler extends SimpleChannelUpstreamHandler {
9420c9859SShuo Chen
10420c9859SShuo Chen    private volatile RpcChannel channel;
11420c9859SShuo Chen    private RpcPeer rpcPeer;
12420c9859SShuo Chen
13420c9859SShuo Chen    public RpcMessageHandler(RpcPeer peer) {
14420c9859SShuo Chen        this.rpcPeer = peer;
15420c9859SShuo Chen    }
16420c9859SShuo Chen
17420c9859SShuo Chen    public RpcChannel getChannel() {
18420c9859SShuo Chen        return channel;
19420c9859SShuo Chen    }
20420c9859SShuo Chen
21420c9859SShuo Chen    public void setChannel(RpcChannel ch) {
22420c9859SShuo Chen        channel = ch;
23420c9859SShuo Chen    }
24420c9859SShuo Chen
25420c9859SShuo Chen    @Override
26420c9859SShuo Chen    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
27420c9859SShuo Chen        System.err.println("channelConnected");
28420c9859SShuo Chen        rpcPeer.channelConnected(e.getChannel());
29420c9859SShuo Chen    }
30420c9859SShuo Chen
31420c9859SShuo Chen    @Override
32420c9859SShuo Chen    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
33420c9859SShuo Chen        assert e.getChannel() == channel.getChannel();
34420c9859SShuo Chen        channel.messageReceived(ctx, e);
35420c9859SShuo Chen    }
36420c9859SShuo Chen}
37