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 EchoSimulator {
	
	final ISocketAdapter httpAdapter;
//	final ISocketAdapter adapter11;	
	final ISocketAdapter adapter21;	
//	final ISocketAdapter adapter31;	
	final ISocketAdapter adapter41;	
	
	public EchoSimulator(int echoPort, int destPort, int maxConn, String script, int adminPort, String llfieldinclude, String lloffsetexclude, String llInclude) 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_DEFAULT);		
//		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.setLlFieldInclude(llfieldinclude);
		context.setLlOffsetExclude(lloffsetexclude);
		context.setLlInclude(llInclude);
		context.setResponseType("ASYN");
		context.setMaxConnection(maxConn);//30);
		context.setMaxThread(maxConn);//30);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	
		adapter41 = SocketAdapterFactory.createSocketAdapter(context);
		adapter41.start();

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


		/**
		 * Outbound SENDER(CLIENT) - 응답 요청[300]
		 */
//		context = new SocketAdapterContext();
//		context.setAdapterGroupName("EchoSimulatorAdapterGroup");
//		context.setAdapterName("RES-SEND_31");
//		context.setProtocol("FIXED_LENGTH_HEADER");
//		context.setSocketType("CLIENT");
//		context.setHostName(rHost);
//		context.setPortNumber(rPort);
//		//context.setLlFieldType("BIN");
//		//context.setLlFieldLength(2);
//		context.setResponseType("ASYN");
//		context.setMaxConnection(30);
//		context.setMaxThread(30);
//		context.setTimeout(60);
////		context.setScript("sync-perm.groovy");
//		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
//		adapter31 = SocketAdapterFactory.createSocketAdapter(context);
//		adapter31.start();

//		context.setRequestProcessor(new IRequestProcessor() {
//			public byte[] handle(byte[] request) throws Exception {
////				adapter41.sendMessage(request);
//				System.out.println(new String(request));
//				request[request.length-1] = (byte) '-';
//				return request;
//			}
//		});


		/**
		 * Outbound RECEIVER(SERVER) - 요청 수신[200]
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("EchoSimulatorAdapterGroup");
		context.setAdapterName("EchoAsS");
		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(llfieldinclude);
		context.setLlOffsetExclude(lloffsetexclude);
		context.setLlInclude(llInclude);
		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_DEFAULT);		
		adapter21 = SocketAdapterFactory.createSocketAdapter(context);
		adapter21.start();	

//		context.setRequestProcessor(new IRequestProcessor() {
//			public byte[] handle(byte[] request) throws Exception {
//				adapter31.sendMessage(request);
//				return request;
//			}
//		});
		
		
		/**
		 * Inbound SENDER(CLIENT) - 요청 송신[100]
		 */
//		context = new SocketAdapterContext();
//		context.setAdapterGroupName("EchoSimulatorAdapterGroup");
//		context.setAdapterName("Inbound-SEND");
//		context.setProtocol("FIXED_LENGTH_HEADER");
//		context.setSocketType("CLIENT");
//		context.setHostName(hostname);
//		context.setPortNumber(61001);
//		context.setLlFieldType("BIN");
//		context.setLlFieldLength(2);
//		context.setResponseType("ASYN");
//		context.setMaxConnection(30);
//		context.setMaxThread(30);
//		context.setTimeout(60);
//		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
//		adapter11 = SocketAdapterFactory.createSocketAdapter(context);
//		adapter11.start().join();
	}
	
	static public void main(String[] args) throws Exception {

		// echoPort, destPort, maxConn, script, adminPort, llfieldinclude, lloffsetexclude, llinclude
//		String eaiHost = args[0];
//		int eaiPort = Integer.parseInt(args[1]);
		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]);
		String llfieldinclude = args[5];
		String lloffsetexclude = args[6];
		String llInclude = args[7];
		new EchoSimulator(echoPort, destPort, maxConn, script, adminPort, llfieldinclude, lloffsetexclude, llInclude);
	}
}
