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

import com.eactive.eai.adapter.socket2.common.Env;
import com.eactive.eai.adapter.socket2.common.SimpleFuture;
import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.protocol.TcpAdapterClient;
import com.eactive.eai.adapter.socket2.protocol.inisafe2020.codec.InisafeCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.inisafe2020.handshake.InisafeClientFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;


public class Inisafe2020Client extends TcpAdapterClient {
	boolean isBooting = true;

	public Inisafe2020Client(SocketAdapterContext context) throws Exception {
		super(context);

		this.handler = new InisafeClientHandler(this);

		config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new InisafeCodecFactory(this)));
		config.getFilterChain().addAfter("codec", "inisafe.codec", new InisafeClientFilter(this));
	}
	
	public SimpleFuture start() throws Exception {
		if (isBooting && !Env.isTest()) {
			if (context.getTraceLevel() >= context.TRACE_LEVEL_INFO) {
				logger.info(context.getAdapterName() + " | inisafe client protocol - skip start process when booting ...");
			}
			isBooting = false;
			
			SimpleFuture future = new SimpleFuture();
			future.set(Boolean.TRUE);
			return future;
		} else {
			return super.start();
		}
	}
}
