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

import java.io.IOException;
import java.net.Socket;

import com.eactive.eai.adapter.socket.common.CommonLib;
import com.eactive.eai.adapter.socket.config.ConfigurationContext;
import com.eactive.eai.adapter.socket.service.SocketLogManager;
import com.eactive.eai.adapter.socket.service.SocketService;
import com.eactive.eai.common.util.Logger;

public class OutboundSocketClient implements SocketService {

	//파일로거
	private static Logger           logger = SocketLogManager.getInstance().getLogger();

    private boolean					active;

    private int                     currentState;

    private long				 	firstActivity;
    private long				 	lastActivity;
    
    private Socket                  socket;
    
    private String                  remoteIPAddress;
    private String                  port;


    public OutboundSocketClient(Socket socket) {
        this.active              = true;

        this.currentState        = CommonLib.PROCESSING_PROTOCOL;

        this.firstActivity		 = System.currentTimeMillis();
        this.lastActivity		 = System.currentTimeMillis();

        this.socket              = socket;

        this.remoteIPAddress     = socket.getInetAddress().getHostAddress();
        this.port                = socket.getPort() + "";

    }

    public String getPort() {
        return this.port;
    }

    public String getRemoteIPAddress() {
        return this.remoteIPAddress;
    }

    public ConfigurationContext getContext() {
        return null;
    }

    public boolean idle(long timeout) {
        long idleTime = System.currentTimeMillis() - this.lastActivity;
        return (idleTime > timeout);
    }

    public boolean isActive() {
        return active;
    }

    public synchronized void shutdown() {
        active          = false;

        if (this.socket == null) {
            logger.info("OutboundSocketClient : socket 이 null 입니다.");
            return;
        }
        if (this.socket.isClosed()) {
            logger.info("OutboundSocketClient : socket 이 이미 close 되었습니다.");
            return;
        }
        
        try {
            socket.setSoLinger(true, 100);
            socket.shutdownInput();
            socket.shutdownOutput();
            socket.close();
            logger.info("OutboundSocketClient : Outbound Socket Closed !!");

        } catch (Exception ex) {
            logger.error("★★★★★ Outbound Socket Close 시 오류 발생 !! ★★★★★", ex);
        }
    }

    public Socket getCurrentSocket() {
    	return this.socket;
    }

    public boolean connect() {
    	return true;
    }

    public void disconnect() {
        shutdown();
    }

    public void setControl(Object control) {
    }

    public void notifyMessage() {
    }

    public int getCurrentState() {
        return this.currentState;
    }

    public boolean isConnected() throws IOException {
        return this.socket.isConnected();
    }

    public void setLastActivity() {
        lastActivity = System.currentTimeMillis();
    }

    public void checkActivity() {
    }

    public long getFirstActivity() {
        return firstActivity;
    }

    public long getLastActivity() {
        return lastActivity;
    }
    
}
