// Home | Go Back //

/*
 * Copyright (c) 2002-2003 iReasoning Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by iReasoning Inc. as part
 * of an iReasoning Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of iReasoning Inc.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD IREASONING SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */

package agent.mib2jni;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.snmp.*;
import javax.management.*;
import com.ireasoning.jni.*;
import EDU.oswego.cs.dl.util.concurrent.*;

public abstract class DynamicTable  extends SnmpTable
{
    String _tableName ;
    Class  _classObj;
    
    public DynamicTable (OIDTreeNode root, String oid, String tableName, Class classObj)
    {
        this(root, oid, tableName, classObj, 5000);
    }

    public DynamicTable (OIDTreeNode root, String oid, String tableName, Class classObj, int updateTime)
    {
        super(root, oid);
        _tableName = tableName;
        _classObj = classObj;
        update();
        setUpdateInterval(updateTime);//set update interval. so the following update() method will get called if content is older than 30 seconds
    }

    public void update()
    {
        //System.out.println( "Update table:" + _tableName);
        super.update();//call super class' update method to update some internal variables in super class
        deleteAllRows();//delete all old rows, and we're going to create all rows again.
        int rows = JNIFunction.getNumberOfRows(_tableName);
        PooledExecutor pool = new PooledExecutor(20);
        for (int i = 0; i < rows ; i++) 
        {
            try
            {
                pool.execute(new AddEntry(this, i, _tableName, _classObj));
            }
            catch(Exception e)
            {
                Logger.error(e);
            }
        }
        pool.shutdownNow ();
        try
        {
            pool.awaitTerminationAfterShutdown();
        }
        catch(Exception e)
        {//unlikely
        }
    }

    static class AddEntry implements Runnable
    {
        int _index;
        String _tableName;
        Class _classObj;
        SnmpTable _table;
        public AddEntry(SnmpTable t, int i, String tableName, Class classObj)
        {
            _table = t;
            _index = i + 1;
            _tableName = tableName;
            _classObj = classObj;
        }
        public void run()
        {
            String [] columns = JNIFunction.getRow(_tableName, _index);
            /* may result in empty iftable on fedora
            for (int i = 0; i < columns.length ; i++) 
            {
                if(columns [i] == null) return;//incomplete row. don't add it.
            }
            */
            SnmpTableEntry entry =  JNIFunction.createEntry(_table, _classObj, columns);
            if(entry != null) entry.addRow();
        }
    }
}// end of class DynamicTable