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

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


public class InisafeNatnPayloadHandler extends ProtocolHandler {
	public InisafeNatnPayloadHandler(ISocketAdapter adapter) {
		super(adapter);
	}
	
	protected void doSessionClosed (IoSession session) throws Exception {
		/**
		 * client-server pair (in same adapter group)
		 */
		
		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()) {
				/**
				 * client - shutdown pool & stop adapter
				 */
				if (adapters[i].getState() == ISocketAdapter.State.STARTED) {
					adapters[i].getSessionPool().shutdown();
					adapters[i].stopForcely();
				}
			} else {
				/**
				 * server - shutdown pool
				 */
				adapters[i].getSessionPool().shutdown();
			}
		}		
	}	
}
