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

import java.util.HashMap;
import java.util.Iterator;

public class OutboundChannelManager {
	
	private static OutboundChannelManager   instance = new OutboundChannelManager();
	private HashMap<String,OutboundChannel> pool     = new HashMap<String,OutboundChannel>() ;	   
    
	public OutboundChannelManager() {
	}
	
	public static OutboundChannelManager getInstance() {
		return instance;
	}
    
	public void stop() {
        this.removeAll();
    }
    
    public void removeAll() {
        pool.clear();
    }
    
    public void start(String adapterGroupName, OutboundChannel channel) {
        this.addConnectionGroup(adapterGroupName, channel);
    }
    
    // Adapter Group Connection 을 한번에 등록한다.
    public synchronized void addConnectionGroup(String adapterGroupName, OutboundChannel channel) {
    	OutboundChannel oc = pool.get( adapterGroupName );
    	if ( oc == null ) {
    		synchronized ( pool ) {
    			oc =  pool.get( adapterGroupName );
    			if ( oc == null ) {
    				pool.put( adapterGroupName, channel );
    			}
    		}
    	}
    }
 
    public void addConnection( String adapterGroupName, SocketService connection ) {
        OutboundChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return;
        try {
            channel.addConnection( connection );
        } catch( Exception e ) {}
    }
    
    public void stop(String adapterGroupName) {
        // adapterGroupName의 Connection stop & clear
        OutboundChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return;
        try {
            channel.removeAllConnection();
        } catch ( Exception e ) {}
        
        pool.remove( adapterGroupName );
    }
    
    public OutboundChannel getChannel( String adapterGroupName ) {
        return (OutboundChannel) pool.get( adapterGroupName );
    }
        
    public boolean hasChannel( String adapterGroupName ) {
        return pool.containsKey( adapterGroupName );
    }
    
    public SocketService getConnection( String adapterGroupName ) {
        OutboundChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return null;
        
        SocketService  connection = null;
        try {
            connection = (SocketService) channel.getConnection();
        } catch ( Exception e ) { }
        
        return connection;
    }
    
    public void removeConnection ( String adapterGroupName, Object connection ) {
        OutboundChannel  channel = this.getChannel( adapterGroupName );
        if ( channel == null ) return;
        try {
            channel.removeConnection( connection );
        } catch (Exception e) {}
    }
    
    public String toString() {
        StringBuffer sb = new StringBuffer();
        
        sb.append("\n■ OutboundChannelManager : ");
        try {
            for(Iterator<String> iter = pool.keySet().iterator(); iter.hasNext();) {
                String key = iter.next();
                sb.append("\n    ▣ " + key + " : " + pool.get(key).getPoolSize());
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
