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

import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.adapter.AdapterVO;
import com.eactive.eai.adapter.socket.config.ConfigurationContext;
import com.eactive.eai.adapter.socket.config.SocketAdapterManager;

import java.util.Vector;


/**
 * Adapter Name별관리 대상테이블
 */
public class AdminTable {

    private String                  adapterGroupName;
    private String                  adapterName;
    private ConfigurationContext    ctx;
    private Vector<SocketService>   connections;
    private int                     activeCount;

	public AdminTable(String adapterGroupName, String adapterName, ConfigurationContext context ) {
        this.adapterGroupName   = adapterGroupName;
        this.adapterName        = adapterName;
        this.ctx                = context;
        this.connections        = new Vector<SocketService>();
        this.activeCount        = 0;
    }

    public String getAdapterGroupName() {
        return adapterGroupName;
    }

    public void initialize() {
        this.connections.clear();
        this.activeCount = 0;
    }

    public void setContext( ConfigurationContext context ) {
        this.ctx    = context;
    }

    public ConfigurationContext getContext() {
        return ctx;
    }

    public synchronized Object[] getConnections() {
        return connections.toArray();
    }

    public synchronized void checkErrorStatus() {
        synchronized ( connections ) {
            updateStatus( SocketAdapterManager.getInstance().checkActiveStatus( adapterGroupName, adapterName ) );
        }
    }

    public synchronized void checkRecoverStatus() {
        synchronized ( connections ) {
            updateStatus( SocketAdapterManager.getInstance().checkActiveStatus( adapterGroupName, adapterName ) );
        }
    }

    // Adapter Group Session을 한번에 등록한다.
    public synchronized void addConnection(SocketService connection) {
        connections.add( connection );

    }

    public synchronized void removeConnection(Object connection) {
        synchronized ( connections ) {
            int size = connections.size();
            for ( int i=0; i < size; i++ ) {
                Object x = connections.elementAt( i );
                if ( x == connection ) {
                    connections.remove( i );
                    break;
                }
            }
        }

        if ( connections.size() == 0 ) {
            updateStatus( false );
        }
    }

    private void updateStatus( boolean on ) {
        AdapterVO  config  = AdapterManager.getInstance().getAdapterVO(adapterGroupName, adapterName);
        if ( config != null ) {
            if ( config.isStatus() != on ) {
                config.setStatus( on );
            }
        }
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("\n        ▷ AdminTable [");
        sb.append("adapterGroupName=" + adapterGroupName + ", ");
        sb.append("adapterName=" + adapterName + ", ");
        sb.append("connections=" + connections.size() + ", ");
        sb.append("activeCount=" + activeCount);
        sb.append("]");
        return sb.toString();
    }
}
