// 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.subagent;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.snmp.*;
import com.ireasoning.protocol.Listener;
import com.ireasoning.protocol.Msg;
import javax.management.*;


/**
 * Agent main class. This class create a subagent and connect to a master agent running at localhost.
 * It then registers two subtrees with the master agent.
 */
public class Agent extends SnmpAgentX implements Listener
{
    
    /**
     * Constructor. Create MBeanServer instance internally. Agent will be listening on port specified in config file, which can be retrieved by using getConfig().getPort().
     * @param configFileName Agent's config file name. If absolute path is not specified, the file will be assumed located under ./config directory or current directory
     */
    public Agent() throws Exception
    {
        super();
    }
    
    /**
     * Constructor. Create MBeanServer instance internally. Agent will be listening on port specified in config file, which can be retrieved by using getConfig().getPort().
     * @param configFileName Agent's config file name. If absolute path is not specified, the file will be assumed located under ./config directory or current directory
     */
    public Agent(String configFileName) throws Exception
    {
        super(configFileName, true);
    }

    public static void main(String[] args)
    {
        try
        {
            int mode = 0;
            // use SnmpAgent.xml on ./config directory
            Agent agent = new Agent();

            //For testing purpose, add subagent event listener to receive subagent state change event
            //'handleMsg(Object msgSender, Msg msg)' method will be called if there're any new events
            agent.addSubagentEventListener(agent);
            
            String host = null;
            if(args.length > 0)
            {
                host = args[0];
            }
            if(host == null) 
            {
                host = "localhost";
            }
            int port = 705;
            if(args.length > 1)
            {
                port = Integer.parseInt(args[1]);
            }
            if(args.length > 2)
            {//three testing subagents
                if(args[2].equals("1")) mode = 1;
                else if(args[2].equals("2")) mode = 2;
            }

            System.out.println( "Connecting master agent @ " + host + ":" + port);
            SubAgentSession session = agent.connect(host, port);
            //New code
            //To register OID subtrees ...
            int ret1 = 0, ret2 = 0;
            if(mode == 0)
            {// mode 0, registers all table subtree
                ret1 = session.register(".1.3.6.1.4.1.15145.1.1.1"); //InfoGroup
                ret2 = session.register(".1.3.6.1.4.1.15145.1.1.2"); //SubagentTable
            }
            else if(mode == 1)
            {// mode 1, only register first row
                System.out.println( "register first row");
                ret2 = session.register(".1.3.6.1.4.1.15145.1.1.2.1.1.1", 12, 5, 30); //SubagentTable first row
            }
            else if(mode == 2)
            {// mode 2, only register second row
                System.out.println( "register second row");
                ret2 = session.register(".1.3.6.1.4.1.15145.1.1.2.1.1.2", 12, 5, 30); //SubagentTable second row
            }

            if(ret1 != -1 && ret2 != -1)
            {
                System.out.println( "Subtrees were registered successfully");
            }
            else
            {
                System.out.println( "At least one of the subtrees were not registered successfully");
            }
            
            // ...
            // Use sendTrapToMasterAgents(SnmpTrap trap) method to forward trap to master agent
            SnmpTrap trap = new SnmpTrap(getSysUpTime(), new SnmpOID( ".1.3.6.1.4.1.15415.1.1.3.1") );
            trap.addVarBind(new SnmpVarBind(".1.3.6.1.4.1.15415.1.1.1.1", new SnmpInt(1)));
            trap.addVarBind(new SnmpVarBind(".1.3.6.1.4.1.15415.1.1.1.2", new SnmpInt(2)));
            sendTrapToMasterAgents(trap);
            // session.close();
        }
        catch(Exception e)
        {
            Logger.error(e);
            System.exit(1);
        }
    }
    
    /**
     * Callback method for receiving subagent state change event
     * @param msgSender event originator object
     * @param msg a SubagentEvent object
     */
    public void handleMsg(Object msgSender, Msg msg)
    {
        SubagentEvent event = (SubagentEvent) msg;
        System.out.println( event );
    }
    
    /**
     * Sets the MIB tree data structure. This method gets called by base class
     */
    protected void setOIDTree()
    {
        _root = OIDTree.getTree();
    }
    
    /**
     * Register necessary MBeans
     */
    protected void registerMBeans() throws SnmpException
    {
        AgentMib.registerMBeans(_mbeanServer, _root);
    }

    /**
     * Unregister necessary MBeans
     */
    protected void unregisterMBeans()
    {
        AgentMib.unregisterMBeans();
    }

}