package com.signal.sig.ws;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

 
public class SigWebSocketServer implements Runnable {
	private static final int port  = 7902;

	String name;
     
    public SigWebSocketServer(String name) {
    	System.out.println("========== Start Sig Web Thread " + name + "==========");
        this.name = name;
    }

    public static void start() throws Exception {}

	@Override
	public void run() {
		System.out.println("[ DBUG ] SigWebSocketServer run() START");
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ChannelFuture channelFuture = null;

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                //.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SigHttpServerInit());

            Channel ch = b.bind(port).sync().channel();	/* 통신포트 환경설정파일에서 읽어와서 설정 할 수 있도록 수정(7902,8902) */
             
            channelFuture = ch.closeFuture();
            channelFuture.sync();

        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
	}
}

