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

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

public class DumpServer {
	static public void main(String[] args) throws Exception {
        if (args.length != 1) {
            // System.out.println("Usage : dump <port>");
            return;
        }
    	int port = Integer.parseInt(args[0]);

		SocketAdapterContext context = new SocketAdapterContext();
		context.setAdapterGroupName("DumpServerAdapterGroup");
		context.setAdapterName("DumpServerAdapter");
		context.setBoundUsage("INBOUND");
		context.setSocketType("SERVER");
		context.setHostName("localhost");
		context.setPortNumber(port);
		context.setResponseType("ASYN");
		context.setTimeout(10);
		context.setMaxConnection(10);
		context.setTraceLevel(context.TRACE_LEVEL_DEBUG);
		context.setPermanent("N");
		
		context.setProtocol("FIXED_LENGTH_HEADER");
		context.setLlFieldType("NONE");

		context.setRequestProcessor(new IRequestProcessor() {
			public byte[] handle(byte[] request) throws Exception {
				// do nothing
				return request;
			}
		});	
		
		ISocketAdapter adapter = SocketAdapterFactory.createSocketAdapter(context);
		adapter.start().join();

		
		/**
		 * process command
		 */
		processCommand();
	}
	
    private static void processCommand() {
        while(true) {
            try {
            	System.out.print("> ");
            	String cmd = (new java.io.BufferedReader(new java.io.InputStreamReader(System.in))).readLine();
            	if ("".equals(cmd)) {
            		// System.out.println(SocketAdapterManager.getInstance());
            	} else if ("exit".equals(cmd)) {
            		System.exit(1);
            	}
            } catch(Exception e) {
            	// System.out.println(e.toString());
            }
        }
	}	
}
