package com.eactive.eai.adapter.webt;

import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.adapter.listener.AdapterException;
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.tmax.nio.jtmax.JTmaxNServer;
import com.tmax.nio.jtmax.impl.JTmaxNServerImpl;

public class WebTAsyncServerAdapterListener extends AdapterListenerSupport {
	private static ConcurrentHashMap<String, JTmaxNServer> adapters = new ConcurrentHashMap<>();

	private static final String PORT_NUMBER = "port.number";

	@Override
	public void start() throws AdapterException {
		AdapterPropManager pmanager = AdapterPropManager.getInstance();
		Properties prop = pmanager.getProperties(info.getPropGroupName());
		int portNumber = -1;
		int maxConnection = -1;
		int maxThread = -1;
		try {
			if (info == null) {
				throw new Exception("WebTAsyncServerAdapterListener | AdapterVO가 NULL입니다.");
			}

			try {
				portNumber = Integer.parseInt(prop.getProperty(PORT_NUMBER));
			} catch (Exception e) {
				throw new Exception(
						"WebTAsyncServerAdapterListener | port.number 설정 오류 : " + prop.getProperty("port.number"));
			}

			try {
				maxConnection = Integer.parseInt(prop.getProperty("max.connection"));
			} catch (Exception e) {
				maxConnection = 10;
				if (logger.isInfo())
					logger.info("set default maxConnection = " + maxConnection);
			}

			try {
				maxThread = Integer.parseInt(prop.getProperty("max.thread"));
			} catch (Exception e) {
				maxThread = 10;
				if (logger.isInfo())
					logger.info("set default maxThread = " + maxThread);
			}

			String name = info.getAdapterGroupName() + "." + info.getName();
			CustomJTmaxNJGMessageHandler handler = new CustomJTmaxNJGMessageHandler(info, prop);
			JTmaxNServer server = new JTmaxNServerImpl(name, portNumber, maxConnection, maxThread,
					new CustomJTmaxNJGMessageHandler(info, prop));
			handler.setJTmaxNServer(server);
			server.setRegister(true);
//	    	server.setThreadPoolQSzie(10);
			server.startServer();
			adapters.put(name, server);

		} catch (Exception e) {
			String errCode = ExceptionUtil.getErrorCode(e, "RECEAIOIA008");
			logger.error("Start Failed", e);
			throw new AdapterException(errCode);
		} catch (Throwable t) {
			String errCode = ExceptionUtil.getErrorCode(t, "RECEAIOIA008");
			logger.error("Start Failed", t);
			throw new AdapterException(errCode);
		}
	}

	@Override
	public void stop() throws AdapterException {
		try {
			if (info == null)
				throw new Exception("WebTAsyncServerAdapterListener | AdapterVO가 NULL입니다.");
			String name = info.getAdapterGroupName() + "." + info.getName();
			JTmaxNServer server = null;
			if (adapters.containsKey(name)) {
				server = adapters.get(name);
				server.stopServer();
				adapters.remove(name);
			}
		} catch (Exception e) {
			String errCode = ExceptionUtil.getErrorCode(e, "RECEAIOIA009");
			throw new AdapterException(errCode);
		} catch (Throwable t) {
			String errCode = ExceptionUtil.getErrorCode(t, "RECEAIOIA009");
			throw new AdapterException(errCode);
		}
	}
}
