package org.msgpack.rpc.client.netty;

import java.net.ConnectException;

import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.msgpack.rpc.client.TCPSocket;

@ChannelPipelineCoverage("all")
public class RPCClientHandler extends SimpleChannelHandler {
	protected TCPSocket sock;
	
	public RPCClientHandler(TCPSocket sock) {
    	super();
    	this.sock = sock;
    }

    @Override
    public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent ev) throws Exception {
         super.handleUpstream(ctx, ev);
    }

    @Override
    public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent ev) throws Exception {
    	ctx.sendUpstream(ev);
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent ev) {
    	try {
			sock.onConnected();
		} catch (Exception e) {
			e.printStackTrace();
			sock.onConnectFailed();
		}
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent ev) {
        try {
			sock.onMessageReceived(ev.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			sock.onFailed();
		}
	}

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent ev) {
    	Throwable e = ev.getCause();
    	if (e instanceof ConnectException)
    		sock.onConnectFailed();
    	else
    		sock.onFailed();
        ev.getChannel().close();
        ctx.sendUpstream(ev);
    }
}

@@ -672,307 +672,8 @@
  %7D%0A%0A
-    @Override%0A    public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent ev) throws Exception %7B%0A         super.handleUpstream(ctx, ev);%0A    %7D%0A%0A    @Override%0A    public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent ev) throws Exception %7B%0A    %09ctx.sendUpstream(ev);%0A    %7D%0A%0A
     

remove unused handler functions from RPCClientHandler