package com.eactive.eai.adapter.socket2.app.tester;

import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.config.SocketAdapterFactory;
import com.eactive.eai.adapter.socket2.protocol.IRequestProcessor;
import com.eactive.eai.adapter.socket2.protocol.ISocketAdapter;

public class Xecure2Simulator {
	
	final ISocketAdapter httpAdapter;
	final ISocketAdapter adapter21;	
	final ISocketAdapter adapter41;	
	
	public Xecure2Simulator(int echoPort, int destPort, int maxConn, String script, int adminPort) throws Exception {
		
		SocketAdapterContext context = null;
		
		/**
		 * Http Server
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AdminAdapterGroup");
		context.setAdapterName("HttpAdapter");
		context.setProtocol("HTTP");
		context.setBoundUsage("IOBOUND");
		context.setSocketType("SERVER");
		context.setHostName("*");
		context.setPortNumber(adminPort);
		context.setAckSupport("N");
		context.setPermanent("N");
		context.setResponseType("SYNC");
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_TRACE);		
		context.setAclList("172.26.128.*,127.0.0.1");
		httpAdapter = SocketAdapterFactory.createSocketAdapter(context);
		httpAdapter.start();		
		
		/**
		 * Inbound RECEIVER(SERVER) - 응답 수신[400]
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("EchoSimulatorAdapterGroup");
		context.setAdapterName("DestinationAsS");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		//context.setBoundUsage("INBOUND");
		//context.setHostName(hostname);
		context.setPortNumber(destPort);//64001);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(maxConn);//30);
		context.setMaxThread(maxConn);//30);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_TRACE);	
		adapter41 = SocketAdapterFactory.createSocketAdapter(context);
		adapter41.start();

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
//				System.out.print(".");
				return request;
			}
		});

		/**
		 * Outbound RECEIVER(SERVER) - 요청 수신[200]
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("EchoSimulatorAdapterGroup");
		context.setAdapterName("EchoAsS");
//		context.setProtocol("XECURE2");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setBoundUsage("IOBOUND");
		//context.setHostName(hostname);
		context.setPortNumber(echoPort);//62001);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setLlFieldOffset(0);
		context.setLlFieldInclude("Y");
		context.setLlInclude("Y");
		context.setResponseType("ASYN");
		context.setMaxConnection(maxConn);//30);
		context.setMaxThread(maxConn);//30);
		context.setTimeout(60);
		context.setScript(script);//"asyn-perm.groovy");
		context.setTraceLevel(context.TRACE_LEVEL_TRACE);		
		adapter21 = SocketAdapterFactory.createSocketAdapter(context);
		adapter21.start();	

	}
	
	static public void main(String[] args) throws Exception {

		// eaiHost, eaiPort, echoPort, destPort, maxConn, script
		int echoPort = Integer.parseInt(args[0]);
		int destPort = Integer.parseInt(args[1]);
		int maxConn = Integer.parseInt(args[2]);
		String script = args[3];
		int adminPort = Integer.parseInt(args[4]);
		new Xecure2Simulator(echoPort, destPort, maxConn, script, adminPort);
	}
}
