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

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 InisafePayloadServerHandler extends InisafePayloadHandler {
	public InisafePayloadServerHandler(ISocketAdapter adapter) {
		super(adapter);
	}
	
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
	}

	@Override
	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;
				}
			}
		}
	}
}
