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 AsyncSimulator {
	String hostname;
	String rHostname;
	int rPort;
	int mThread;
	
	final ISocketAdapter httpAdapter;
//	final ISocketAdapter adapter11;	
	
	final ISocketAdapter adapter21;	
	final ISocketAdapter adapter22;	
	final ISocketAdapter adapter23;	
	final ISocketAdapter adapter24;	
	final ISocketAdapter adapter25;	
	final ISocketAdapter adapter26;	
	
	final ISocketAdapter adapter31;	
	final ISocketAdapter adapter32;	
	final ISocketAdapter adapter33;	
	final ISocketAdapter adapter34;	
	final ISocketAdapter adapter35;	
	final ISocketAdapter adapter36;	
	
	final ISocketAdapter adapter41;	
	final ISocketAdapter adapter42;
	final ISocketAdapter adapter43;
	final ISocketAdapter adapter44;	
	final ISocketAdapter adapter45;
	final ISocketAdapter adapter46;
	
	public AsyncSimulator(String hostname, String rHostname, int rPort, int mThread) throws Exception {
		this.hostname = hostname;
		this.rHostname = rHostname;
		this.rPort = rPort;
		this.mThread = mThread;
		
		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(8282);
		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) - 응답 수신
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-RECV_41");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(44041);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	

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

		adapter41 = SocketAdapterFactory.createSocketAdapter(context);
		adapter41.start();

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-RECV_42");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(44042);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	

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

		adapter42 = SocketAdapterFactory.createSocketAdapter(context);
		adapter42.start();

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-RECV_43");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(44043);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	

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

		adapter43 = SocketAdapterFactory.createSocketAdapter(context);
		adapter43.start();

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-RECV_44");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(44044);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	

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

		adapter44 = SocketAdapterFactory.createSocketAdapter(context);
		adapter44.start();

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-RECV_45");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(44045);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	

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

		adapter45 = SocketAdapterFactory.createSocketAdapter(context);
		adapter45.start();

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-RECV_46");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(44046);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);	

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

		adapter46 = SocketAdapterFactory.createSocketAdapter(context);
		adapter46.start();

		/**
		 * Outbound SENDER(CLIENT) - 응답 요청
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-SEND_31");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("CLIENT");
		context.setHostName(rHostname);
		context.setPortNumber(rPort + 1);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(mThread);
		context.setMaxThread(mThread);
		context.setTimeout(60);
		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);
				return request;
			}
		});

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-SEND_32");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("CLIENT");
		context.setHostName(rHostname);
		context.setPortNumber(rPort + 2);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(mThread);
		context.setMaxThread(mThread);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter32 = SocketAdapterFactory.createSocketAdapter(context);
		adapter32.start();

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter42.sendMessage(request);
				return request;
			}
		});

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-SEND_33");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("CLIENT");
		context.setHostName(rHostname);
		context.setPortNumber(rPort + 3);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(mThread);
		context.setMaxThread(mThread);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter33 = SocketAdapterFactory.createSocketAdapter(context);
		adapter33.start();

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter43.sendMessage(request);
				return request;
			}
		});

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-SEND_34");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("CLIENT");
		context.setHostName(rHostname);
		context.setPortNumber(rPort + 4);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(mThread);
		context.setMaxThread(mThread);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter34 = SocketAdapterFactory.createSocketAdapter(context);
		adapter34.start();

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter44.sendMessage(request);
				return request;
			}
		});

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-SEND_35");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("CLIENT");
		context.setHostName(rHostname);
		context.setPortNumber(rPort + 5);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(mThread);
		context.setMaxThread(mThread);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter35 = SocketAdapterFactory.createSocketAdapter(context);
		adapter35.start();

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter45.sendMessage(request);
				return request;
			}
		});

		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("RES-SEND_36");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("CLIENT");
		context.setHostName(rHostname);
		context.setPortNumber(rPort + 6);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(mThread);
		context.setMaxThread(mThread);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter36 = SocketAdapterFactory.createSocketAdapter(context);
		adapter36.start();

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter46.sendMessage(request);
				return request;
			}
		});

		/**
		 * Outbound RECEIVER(SERVER) - 요청 수신
		 */
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("REQ-RECV_21");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(42001);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		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;
			}
		});
		
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("REQ-RECV_22");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(42002);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter22 = SocketAdapterFactory.createSocketAdapter(context);
		adapter22.start();	

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter32.sendMessage(request);
				return request;
			}
		});
		
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("REQ-RECV_23");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(42003);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter23 = SocketAdapterFactory.createSocketAdapter(context);
		adapter23.start();	

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter33.sendMessage(request);
				return request;
			}
		});
		
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("REQ-RECV_24");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(42004);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter24 = SocketAdapterFactory.createSocketAdapter(context);
		adapter24.start();	

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter34.sendMessage(request);
				return request;
			}
		});
		
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("REQ-RECV_25");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(42005);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter25 = SocketAdapterFactory.createSocketAdapter(context);
		adapter25.start();	

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				adapter35.sendMessage(request);
				return request;
			}
		});
		
		context = new SocketAdapterContext();
		context.setAdapterGroupName("AsyncSimulatorAdapterGroup");
		context.setAdapterName("REQ-RECV_26");
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setSocketType("SERVER");
		context.setHostName(hostname);
		context.setPortNumber(42006);
		context.setLlFieldType("CHAR");
		context.setLlFieldLength(4);
		context.setResponseType("ASYN");
		context.setMaxConnection(100);
		context.setMaxThread(100);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_DEFAULT);		
		adapter26 = SocketAdapterFactory.createSocketAdapter(context);
		adapter26.start();	

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

		//String hostname = "localhost";
		String hostname = args[0];
		String rHostname = args[1];
		int rPort = Integer.parseInt(args[2]);
		int mThread = Integer.parseInt(args[3]);
		new AsyncSimulator(hostname, rHostname, rPort, mThread);
	}
}
