package com.eactive.eai.adapter.socket2.protocol.inisafe2020;

import com.eactive.eai.adapter.socket2.config.SocketAdapterManager;
import com.eactive.eai.adapter.socket2.protocol.ISocketAdapter;
import org.apache.mina.common.IoSession;

import java.net.InetSocketAddress;


public class InisafeServerHandler extends InisafeHandler {
	public InisafeServerHandler(ISocketAdapter adapter) {
		super(adapter);
	}
	
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}

	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
		
		/**
		 * L4 에 오는 polling (check) connection 이면 무시한다.
		 */
		ISocketAdapter[] adapters = SocketAdapterManager.getInstance().getSocketAdapters(adapter.getContext().getAdapterGroupName());
		if(adapters == null) return;
		for (int i=0; i<adapters.length; i++) {
			if (adapters[i].getContext().isClient()) {
				String remoteIp = ((InetSocketAddress) session.getRemoteAddress()).getHostName();				
				if (!remoteIp.equals(adapters[i].getContext().getHostName())) {
					return;
				}
			}
		}
		
		/**
		 * client-server pair 로 작업한다.
		 */
		doSessionClosed(session);
	}
}
