package com.eactive.eai.adapter.socket.config;

import com.eactive.eai.adapter.RequestDispatcher;
import com.eactive.eai.adapter.socket.common.CommonLib;
import com.eactive.eai.adapter.socket.service.InboundControl;
import com.eactive.eai.adapter.socket.service.SocketLogManager;
import com.eactive.eai.common.util.Logger;

public class RequestDescriptor {

	private InboundControl control;

    private static Logger logger = Logger.getLogger(Logger.LOGGER_ADAPTER);
    private static Logger errLogger = SocketLogManager.getInstance().getErrLogger();

	public RequestDescriptor(InboundControl control) {
		this.control = control;
	}

	public void request() {

		logger.debug("################ RequestDescriptor.request() 호출됨.");

		long srtTime = 0;
		srtTime = System.currentTimeMillis();

		//BatchDoc batchMsgDoc = null;

        try {
            //Properties prop = new Properties();
            ConfigurationContext ctx = SocketAdapterManager.getInstance().getContext( control.getAdapterName() );

            if ( ctx.getDescription().equals("ECHO SESSION") ) {
                //수신 메시지와 동일하게 설정
                control.setResponse( control.getRequest() );

                //SocketAdapter adapter = SocketAdapter.getInstance();
                //prop.put( SocketAdapter.ADAPTER_GORUP_NAME, "SOCKET_Test_AIX_OUT");
                //resp = (byte[]) adapter.execute( prop, control.getRequest() );
                //control.setResponse( resp );

            } else {
                //================================================================================================
                //Client Request 메시지 처리 요청
                RequestDispatcher reqDispacher = RequestDispatcher.getRequestDispatcher( control.getAdapterGroupName() );
                reqDispacher.handle(control);
                //batchMsgDoc = reqDispacher.handle(control);

                //control.setResponse( batchMsgDoc.getEAIBatchMessage().getSendTelegram().getBytes() );
                //control.setBatchMsgDoc(batchMsgDoc);
                //================================================================================================
            }

        } catch ( Throwable e ) {
            control.setLastError( e );
            String errMsg = CommonLib.getMessage("BECEAIASI001", new String[] { e.getMessage() } );
            errLogger.error( errMsg, e );
            errLogger.error( CommonLib.getDumpMessage( control.getRequest() ));

            control.setResponse( errMsg.getBytes() );
        }

		logger.debug("################ RequestDescriptor.request() 종료. (수행 시간: "+ ((System.currentTimeMillis() - srtTime)/1000.0) +" Sec)");
	}
}
