package com.eactive.eai.adapter.socket2.protocol.fixedpayload.handshake;

import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.config.SocketLoggerManager;
import com.eactive.eai.adapter.socket2.protocol.ByteMessage;
import com.eactive.eai.adapter.socket2.protocol.ISocketAdapter;
import com.eactive.eai.common.util.Logger;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;


public class IgnoreFilter extends IoFilterAdapter {
	protected final Logger logger;
	protected final Logger errLogger;

    protected SocketAdapterContext context;
	protected ISocketAdapter adapter;
	
	
	public IgnoreFilter(ISocketAdapter adapter) {
		this.adapter = adapter;
		this.context = adapter.getContext();

	    logger    = SocketLoggerManager.getInstance().getLogger(context.getAdapterGroupName());
	    errLogger = SocketLoggerManager.getInstance().getErrLogger();
	}	
	
	@Override
	public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
		//KB Case - CMBS_OU_NET_SyC{KOREA} adapter group의 경우 만 적용
		ByteMessage m = (ByteMessage) message;
		
		if ( (m.size() >= 2) && (m.getByte(0) == 'S') && (m.getByte(1) == 'A') 
				&& "CMBS_OU_NET_SyC{KOREA}".equals(context.getAdapterGroupName()) ) {
			if (context.getTraceLevel() >= SocketAdapterContext.TRACE_LEVEL_TRACE) {
				logger.debug(context.getAdapterName() + " | IgnoreFilter - ignore : " + m.hexDump());
				adapter.addRecentRecvTrace(session, "IgnoreFilter - ignore : " + m.hexDump());
			}
			return;
		}
		
		// 증권투자수탁 HEADER Check, it must start 'N'
		if ( (context.getRecvPayloadLength() < 0) && (m.getByte(0) != 'N') ) {
			if (context.getTraceLevel() >= SocketAdapterContext.TRACE_LEVEL_TRACE) {
				logger.debug(context.getAdapterName() + " | IgnoreFilter - ignore, HEADER must start 'N' : " + m.hexDump());
				adapter.addRecentRecvTrace(session, "IgnoreFilter - ignore, HEADER must start 'N' : " + m.hexDump());
			}
			return;
		}
		
		nextFilter.messageReceived(session, message);
	}		
}
