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

import java.util.Vector;

/**
 * AdapterGroupName별 SessionChannel를 갖는다.
 */
public class SessionChannel 
{ 
    private     int                    defaultNumOfSessions;
    private     int                    maxNumOfSessions;
    private     Vector<SessionService> activeSessions;
    private     long                   sessionTimeout;
    
    public  SessionChannel(int defaultNumOfSessions, int maxNumOfSessions) {
        this( defaultNumOfSessions, maxNumOfSessions, 0L );
    }
    
    public  SessionChannel(int defaultNumOfSessions, int maxNumOfSessions, long sessionTimeout) {
        this.defaultNumOfSessions   = defaultNumOfSessions;
        this.maxNumOfSessions       = maxNumOfSessions;
        this.sessionTimeout         = sessionTimeout * 1000;
        this.activeSessions         = new Vector<SessionService>();
    }

    public int getDefaultNumOfSessions() {
        return this.defaultNumOfSessions;
    }
    
    public int getMaxNumOfSessions() {
        return this.maxNumOfSessions;
    }
    
    public  void setMaxNumOfSessions( int newNumOfSessions ) {
        if ( newNumOfSessions < this.maxNumOfSessions ) return;
        this.maxNumOfSessions   = newNumOfSessions;
    }
        
    public long getSessionTimeout() {
        return this.sessionTimeout;
    }
    
    public void setSessionTimeout(long timeout) {
        this.sessionTimeout = timeout * 1000;
    }
    
    /**
     * SessionService를 parameter로 갖는다.
     */
    public void addSession( SessionService session ) throws Exception {
        this.addSession( session, true );
    }
    
    public void addSession( SessionService session, boolean isPermanent ) throws Exception {
        synchronized ( this.activeSessions ) {
            this.activeSessions.add( session );
        }
    }
    
    public Object getSession() throws Exception {
        synchronized ( this.activeSessions ) {
            if ( this.activeSessions.size() > 0 ) {
                //Object x = this.activeSessions.get( 0 );
                return this.activeSessions.remove( 0 );
            } else {
                return null;
            }
        }
    }
    
    public void removeSession( Object session ) throws Exception {
        synchronized ( this.activeSessions ) {
            this.activeSessions.remove( session );
        }
    }
    
    public String   toString() {
        StringBuffer sb = new StringBuffer();
        int active = 0, idle = 0;
        active  = (this.maxNumOfSessions - this.activeSessions.size());
        //idle    = this.activeSessions.size();
        idle    =  this.maxNumOfSessions - active;
        sb.append("SessionChannel [DefaultSessions=").append( this.defaultNumOfSessions ).append(",");
        sb.append("MaxSessions=").append( this.maxNumOfSessions ).append(",");
        sb.append("ActiveSessions=").append( active ).append(",");
        sb.append("IdleSessions=").append( idle ).append("]");
        return sb.toString();
    }    
} 
