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

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

public class ProxyServer {
	static public void startup(String proxyHostname, int proxyPort, String remoteHostname, int remotePort) throws Exception {	

		/**
		 * Proxy Server
		 */
		SocketAdapterContext context = new SocketAdapterContext();
		context.setProtocol("PROXY");
		context.setAdapterGroupName("AdminAdapterGroup");
		context.setAdapterName("ProxyAdapter");
		context.setBoundUsage("IOBOUND");
		context.setSocketType("SERVER");
		context.setHostName(proxyHostname);
		context.setPortNumber(proxyPort);
		context.setLocalHostName(remoteHostname);
		context.setLocalPortNumber(remotePort);
		context.setAckSupport("N");
		context.setPermanent("Y");
		context.setResponseType("SYNC");
		context.setTimeout(60);
		context.setScript("proxy.groovy");
		context.setTraceLevel(context.TRACE_LEVEL_TRACE);
		
		//SocketAdapter adapter = new AdminServer(context);
		ISocketAdapter adapter = SocketAdapterFactory.createSocketAdapter(context);
		adapter.start().join();
	}	

	static public void main(String[] args) throws Exception {	
        if (args.length != 3) {
            // System.out.println("java " + ProxyServer.class.getName() + " <proxy-port> <remote-hostname> <remote-port>");
            return;
        }

    	int proxyPort = Integer.parseInt(args[0]);
    	String remoteHostname = args[1];
    	int remotePort = Integer.parseInt(args[2]);
    	
		startup("*", proxyPort, remoteHostname, remotePort);
	}
}
