// 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.mib2;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.*;
import com.ireasoning.protocol.snmp.*;
import javax.management.*;
// import agent.DbConfig;

/**
 * Agent main class
 */
public class Agent extends SnmpBaseAgent 
{
    static Agent _agent;
    
    /**
     * Constructor
     * @param port the server port number agent uses
     * @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(MBeanServer server, int port, String configFileName) throws Exception
    {
        super(server, port, configFileName, false);
        _agent = this;
    }
    
    /**
     * Constructor
     * @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(Object configFileName) throws Exception
    {
        super(configFileName);
        _agent = this;
    }
    
    public Agent(DefaultAgentConfig config) throws Exception
    {
        super(config);
        _agent = this;
    }

    public static Agent getAgent()
    {
        return _agent;
    }

    public static boolean isWindows()
    {
        String os = System.getProperty("os.name").toUpperCase();
        return (os.indexOf("WINDOWS") >= 0);
    }

    public static boolean isLinux()
    {
        String os = System.getProperty("os.name").toUpperCase();
        return (os.indexOf("LINUX") >= 0);
    }
    
    public static void main(String[] args)
    {
        try
        {
            String os = System.getProperty("os.name").toUpperCase();
            if(!isWindows()  && !isLinux())
            {
                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 ' \"netstat\" command to figure out interfaces instead of making native calls, so it can only run correctly on Windows or Linux, although snmpagent.jar itself is platform independent.");
                return;
            }
            
            //load from DB
            // DbConfig config = new DbConfig();
            // Agent agent = new Agent(config);
            
            //load from InputStream
            // FileInputStream configFile = new FileInputStream("config/SnmpAgent.xml");
            // Agent agent = new Agent(configFile);
 
            //load from file
            String configFile = "SnmpAgent.xml";
            Agent agent = new Agent(configFile);

            agent.start();
            sendV1ColdStartTrap();

            //listen for AuthenticationFailed event
            agent.addAgentEventListener(new ListenerImpl());
        }
        catch(java.net.BindException be)
        {
            System.out.println( "Cannot bind to the port.");
            System.exit(1);
        }
        catch(Exception e)
        {
            Logger.error(e);
            System.exit(1);
        }
    }
    
    /**
     * 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();
    }

    static class ListenerImpl implements Listener
    {
        public void handleMsg(Object msgSender, Msg msg)
        {
            AgentEvent e = (AgentEvent) msg;
            if(e.getType() == AgentEvent.AUTHENTICATION_FAILURE)
            {
                System.out.println( "AuthenticationFailed.");
                SnmpPdu pdu = e.getPdu();
                if(pdu != null)
                {
                    System.out.println( "IpAddress: " + pdu.getIpAddress());
                }
            }
        }
    }
}