package com.mercury.chat.client.protocol;

import java.net.SocketException;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

import javax.net.ssl.SSLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.mercury.chat.client.protocol.impl.ConnectionImpl;

public final class SecureChatClient{
    
	static final Logger logger = LogManager.getLogger(SecureChatClient.class);
	
	private EventLoopGroup group = new NioEventLoopGroup();
	
	//default value for client side.
	static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "8992"));

	public static void main(String[] args) throws Exception {
        connect(HOST, PORT);
    }

    public static Connection connect(String host, int port) {
		return new ConnectionImpl().channel(new SecureChatClient().connectInner(host, port));
	}

	private Channel connectInner(String host, int port) {
		// Configure SSL.
        SslContext sslCtx;
		try {
			sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
			Bootstrap b = new Bootstrap();
	        b.group(group)
	         .option(ChannelOption.TCP_NODELAY, true)
	         .channel(NioSocketChannel.class)
	         .handler(new SecureChatClientInitializer(sslCtx));
	         return b.connect(host, port).sync().channel();
		} catch (Exception e) {
			group.shutdownGracefully();
			throw new RuntimeException(e);
		}
	}
    
}

@@ -40,42 +40,8 @@
 l;%0A%0A
-import java.net.SocketException;%0A%0A
 impo
@@ -110,47 +110,8 @@
 el;%0A
-import io.netty.channel.ChannelFuture;%0A
 impo
@@ -392,44 +392,8 @@
 y;%0A%0A
-import javax.net.ssl.SSLException;%0A%0A
 impo
@@ -533,16 +533,72 @@
 ionImpl;
+%0Aimport com.mercury.chat.common.exception.ChatException;
 %0A%0Apublic
@@ -1738,15 +1738,12 @@
 new 
-Runtime
+Chat
 Exce

change the runtime exception to chat exception.