1420c9859SShuo Chenpackage muduo.rpc;
2420c9859SShuo Chen
371f65b4dSShuo Chenimport java.io.IOException;
471f65b4dSShuo Chen
5420c9859SShuo Chenimport org.jboss.netty.channel.ChannelHandlerContext;
6420c9859SShuo Chenimport org.jboss.netty.channel.ChannelStateEvent;
771f65b4dSShuo Chenimport org.jboss.netty.channel.ExceptionEvent;
8420c9859SShuo Chenimport org.jboss.netty.channel.MessageEvent;
9420c9859SShuo Chenimport org.jboss.netty.channel.SimpleChannelUpstreamHandler;
10420c9859SShuo Chen
11420c9859SShuo Chenpublic class RpcMessageHandler extends SimpleChannelUpstreamHandler {
12420c9859SShuo Chen
13420c9859SShuo Chen    private volatile RpcChannel channel;
14420c9859SShuo Chen    private RpcPeer rpcPeer;
15420c9859SShuo Chen
16420c9859SShuo Chen    public RpcMessageHandler(RpcPeer peer) {
17420c9859SShuo Chen        this.rpcPeer = peer;
18420c9859SShuo Chen    }
19420c9859SShuo Chen
20420c9859SShuo Chen    public RpcChannel getChannel() {
21420c9859SShuo Chen        return channel;
22420c9859SShuo Chen    }
23420c9859SShuo Chen
24420c9859SShuo Chen    public void setChannel(RpcChannel ch) {
25420c9859SShuo Chen        channel = ch;
26420c9859SShuo Chen    }
27420c9859SShuo Chen
28420c9859SShuo Chen    @Override
29420c9859SShuo Chen    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
30420c9859SShuo Chen        System.err.println("channelConnected");
31420c9859SShuo Chen        rpcPeer.channelConnected(e.getChannel());
32420c9859SShuo Chen    }
33420c9859SShuo Chen
34420c9859SShuo Chen    @Override
35420c9859SShuo Chen    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
36420c9859SShuo Chen        assert e.getChannel() == channel.getChannel();
37420c9859SShuo Chen        channel.messageReceived(ctx, e);
38420c9859SShuo Chen    }
3971f65b4dSShuo Chen
4071f65b4dSShuo Chen    @Override
4171f65b4dSShuo Chen    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
4271f65b4dSShuo Chen        if (!(e.getCause() instanceof IOException)) {
4371f65b4dSShuo Chen            super.exceptionCaught(ctx, e);
4471f65b4dSShuo Chen        }
4571f65b4dSShuo Chen    }
46420c9859SShuo Chen}
47