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

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


public class InisafeNatnPayloadClient extends TcpAdapterClient {
	boolean isBooting = true;

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

		this.handler = new InisafeNatnPayloadClientHandler(this);

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