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

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.inisafe.codec.InisafeCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.inisafe.handshake.InisafeClientFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;


public class InisafeClient extends TcpAdapterClient {
	boolean isBooting = true;

	public InisafeClient(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));
	}
	
	@Override
	public SimpleFuture start() throws Exception {
		if (isBooting && !Env.isTest()) {
			if (context.getTraceLevel() >= SocketAdapterContext.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();
		}
	}
}
