// 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.master;
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
 */
public class Agent extends SnmpAgentX implements Listener
{
    public Agent(String configFileName) throws Exception
    {
        super(configFileName, false);
    }
    
    public static void main(String[] args)
    {
        try
        {
            //make sure only runs on Windows
            String os = System.getProperty("os.name").toUpperCase();
            if(os.indexOf("WINDOWS") < 0)
            {
                System.out.println( "The purpose of this demo is to demononstrate how to create a simple agent. To make it as simple as possible, it makes use of windows' \"ipconfig\" command to figure out interfaces instead of making native calls, so it can only run correctly on Windows, although snmpagent.jar itself is platform independent. ");
                return;
            }
            // use SnmpAgent.xml on ./config directory
            String configFile = "SnmpAgent.xml";
            Agent agent = new Agent(configFile); //new Agent(port, configFile);
            agent.startMasterAgent();//use port number specified in config file
            agent.sendV1ColdStartTrap();
        
            //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);
        }
        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)
    {
        if(msg instanceof SubagentEvent)
        {
            SubagentEvent event = (SubagentEvent) msg;
            System.out.println( event );
        }
        else if(msg instanceof AgentEvent)
        {
            AgentEvent event = (AgentEvent) 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);
    }
}