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

import com.eactive.eai.adapter.EAIException;
import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.filter.polling.KFTCPollingFilter;
import com.eactive.eai.adapter.socket2.protocol.TcpAdapterClient;
import com.eactive.eai.adapter.socket2.protocol.dynamicoffset.codec.DynamicOffsetCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.fixedheader.ack.AckClientFilter;
import com.eactive.eai.adapter.socket2.protocol.fixedheader.codec.ChunkCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;


public class DynamicOffsetClient extends TcpAdapterClient {
	public DynamicOffsetClient(SocketAdapterContext context) throws Exception {
		super(context);		
		this.handler = new DynamicOffsetHandler(this);
		
		if ("NONE".equals(context.getLlFieldType())) {
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new ChunkCodecFactory(this)));
		} else {
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new DynamicOffsetCodecFactory(this)));
		}
		
		if ("Y".equals(context.getAckSupport())) {
			config.getFilterChain().addAfter("codec", "ack", new AckClientFilter(this));
		}
		
		if (context.isPollingUse()) {
			config.getFilterChain().addAfter("polling", "kftcPolling", new KFTCPollingFilter(this));
			config.getFilterChain().remove("polling");
		}
		
	}
	
	@Override
	public Object sendMessage(Object message) throws EAIException {
		return super.sendMessage(message);
	}		
}
