1package muduo.rpc; 2 3import java.io.IOException; 4 5import org.jboss.netty.channel.ChannelHandlerContext; 6import org.jboss.netty.channel.ChannelStateEvent; 7import org.jboss.netty.channel.ExceptionEvent; 8import org.jboss.netty.channel.MessageEvent; 9import org.jboss.netty.channel.SimpleChannelUpstreamHandler; 10 11public class RpcMessageHandler extends SimpleChannelUpstreamHandler { 12 13 private volatile RpcChannel channel; 14 private RpcPeer rpcPeer; 15 16 public RpcMessageHandler(RpcPeer peer) { 17 this.rpcPeer = peer; 18 } 19 20 public RpcChannel getChannel() { 21 return channel; 22 } 23 24 public void setChannel(RpcChannel ch) { 25 channel = ch; 26 } 27 28 @Override 29 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { 30 System.err.println("channelConnected"); 31 rpcPeer.channelConnected(e.getChannel()); 32 } 33 34 @Override 35 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { 36 assert e.getChannel() == channel.getChannel(); 37 channel.messageReceived(ctx, e); 38 } 39 40 @Override 41 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { 42 if (!(e.getCause() instanceof IOException)) { 43 super.exceptionCaught(ctx, e); 44 } 45 } 46} 47