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

import com.eactive.eai.adapter.EAIException;
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 Muxer {
	static public void startup(String inboundHostname, int inboundPort, String outboundHostname, int outboundPort) throws Exception {
		/**
		 * Outbound Client
		 */
		SocketAdapterContext context = new SocketAdapterContext();
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setAdapterGroupName("MuxerGroup");
		context.setAdapterName("OutboundAdapter");
		context.setBoundUsage("IOBOUND");
		context.setSocketType("CLIENT");
		context.setHostName(outboundHostname);
		context.setPortNumber(outboundPort);
		context.setResponseType("SYNC");
		context.setMaxConnection(1);
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_TRACE);
		
		ISocketAdapter outboundClient = SocketAdapterFactory.createSocketAdapter(context);
		outboundClient.start().join();
		
		/**
		 * Inbound Server
		 */
		context = new SocketAdapterContext();
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setAdapterGroupName("MuxerGroup");
		context.setAdapterName("InboundAdapter");
		context.setBoundUsage("IOBOUND");
		context.setSocketType("SERVER");
		context.setHostName(inboundHostname);
		context.setPortNumber(inboundPort);
		context.setResponseType("SYNC");
		context.setTimeout(60);
		context.setTraceLevel(context.TRACE_LEVEL_TRACE);
		context.setRequestProcessor(new MuxerRequestProcessor(outboundClient));
		
		ISocketAdapter inboundServer = SocketAdapterFactory.createSocketAdapter(context);
		inboundServer.start().join();

	}
	
	public synchronized byte[] handle(byte[] message) {
	    // System.out.println("##############################################################");
	    // System.out.println("CiscoListener | message received : " + new String(message));
	    // System.out.println("##############################################################");
	    return message;
	}	

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

    	int inboundPort = Integer.parseInt(args[0]);
    	String outboundHostname = args[1];
    	int outboundPort = Integer.parseInt(args[2]);
    	
		startup("*", inboundPort, outboundHostname, outboundPort);
	}
}

class MuxerRequestProcessor implements IRequestProcessor {
	ISocketAdapter outboundAdapter;
	
	public MuxerRequestProcessor(ISocketAdapter outboundAdapter) {
		this.outboundAdapter = outboundAdapter;
	}
	
	public synchronized byte[] handle(byte[] message) throws EAIException {
	    // System.out.println("##############################################################");
	    // System.out.println("MuxerRequestProcessor | message received : " + new String(message));
	    // System.out.println("##############################################################");
	    
	    return (byte[]) outboundAdapter.sendMessage(message);
	}
}
