snmptrapsession.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 578 行 · 第 1/2 页

JAVA
578
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///// Tab Size = 8//// SnmpTrapSession.java,v 1.1.1.1 2001/11/11 17:27:22 ben Exp////// 7/16/00 - Weave//	Fixed error in send(SnmpPeer peer, SnmpTrapPdu trap) where//	the trap was not actually encoded and stored in the buffer//	that was transmitted. Bug found by Naz Irizarry.//package org.opennms.protocols.snmp;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.SocketException;import org.opennms.protocols.snmp.asn1.ASN1;import org.opennms.protocols.snmp.asn1.AsnEncoder;import org.opennms.protocols.snmp.asn1.AsnEncodingException;/** * <P> * The trap session is used to send and receives SNMPv1 & v2 trap messages. The * messages are received on the configured port, or the default(162) port and * then decoded using the set ASN.1 codec. When messages are sent they are * encoded using the passed SnmpParameters object that is part of the SnmpPeer * object. * </P> *  * <P> * A trap message handler must be bound to the session in order to send or * receive messages. * </P> *  * @author <a href="http://www.opennms.org">OpenNMS </a> * @author <a href="mailto:weave@oculan.com">Brian Weaver </a> * @author Sowmya * @version 1.1.1.1 2001/11/11 17:27:22 *  * @see SnmpTrapHandler */public final class SnmpTrapSession extends Object {    /**     * <P>     * Defines a error due to a thown exception. When the snmpTrapSessionError     * method is invoked in the trap handler, the exception object is passed as     * the ref parameter.     * </P>     *      * @see SnmpTrapHandler#snmpTrapSessionError     *      */    public final static int ERROR_EXCEPTION = -1;    /**     * <P>     * Defines an error condition with an invalid PDU. For the moment this is     * not actually used, but reserved for future use. When the session trap     * handler error method is invoke the pdu in error should be passed as the     * ref parameters     *      * @see SnmpTrapHandler#snmpTrapSessionError     */    public final static int ERROR_INVALID_PDU = -2;    /**     * This is the default port where traps should be sent and received as     * defined by the RFC.     *      */    public final static int DEFAULT_PORT = 162;    /**     * The default port were traps are sent and received by this session.     */    private int m_port;    /**     * The default SNMP trap callback handler. If this is not set and it is     * needed then an SnmpHandlerNotDefinedException is thrown.     *      */    private SnmpPortal m_portal;    /**     * ASN.1 codec used to encode/decode snmp traps that are sent and received     * by this session.     */    private AsnEncoder m_encoder;    /**     * The public trap handler that process received traps.     *      */    private SnmpTrapHandler m_handler;    /**     * If this boolean value is set then the receiver thread is terminated due     * to an exception that was generated in either a handler or a socket error.     * This is considered a fatal exception.     */    private boolean m_threadException;    /**     * This is the saved fatal exception that can be rethrown by the application     */    private Throwable m_why;    /**     * <P>     * The internal trap handler class is designed to receive information from     * the enclosed SnmpPortal class. The information is the processed and     * forwarded when appropiate to the SnmpTrapHandler registered with the     * session.     * </P>     *      */    private class TrapHandler implements SnmpPacketHandler {        /**         * Who to pass as the session parameter         */        private SnmpTrapSession m_forWhom;        /**         * <P>         * Creates a in internal trap handler to be the intermediary for the         * interface between the SnmpPortal and the TrapSession.         * </P>         *          * @param sess         *            The trap session reference.         *          */        public TrapHandler(SnmpTrapSession sess) {            m_forWhom = sess;        }        /**         * <P>         * Processes the default V1 & V2 messages.         * </P>         *          * @param agent         *            The sending agent         * @param port         *            The remote port.         * @param version         *            The SNMP Version of the message.         * @param community         *            The community string from the message.         * @param pduType         *            The type of pdu         * @param pdu         *            The actual pdu         *          * @exception SnmpPduEncodingException         *                Thrown if the pdu fails to decode.         */        public void processSnmpMessage(InetAddress agent, int port, SnmpInt32 version, SnmpOctetString community, int pduType, SnmpPduPacket pdu) throws SnmpPduEncodingException {            if (version.getValue() != SnmpSMI.SNMPV2 && pduType != SnmpPduPacket.V2TRAP)                return;            try {                m_handler.snmpReceivedTrap(m_forWhom, agent, port, community, pdu);            } catch (Exception e) {                // discard            }        }        /**         * <P>         * Processes V1 trap messages.         * </P>         *          * @param agent         *            The sending agent         * @param port         *            The remote port.         * @param community         *            The community string from the message.         * @param pdu         *            The actual pdu         *          * @exception SnmpPduEncodingException         *                Thrown if the pdu fails to decode.         */        public void processSnmpTrap(InetAddress agent, int port, SnmpOctetString community, SnmpPduTrap pdu) throws SnmpPduEncodingException {            try {                m_handler.snmpReceivedTrap(m_forWhom, agent, port, community, pdu);            } catch (Exception e) {                // discard            }        }        /**         * <P>         * Invoked when bad datagrams are received.         * </P>         *          * @param p         *            The datagram packet in question.         *          */        public void processBadDatagram(DatagramPacket p) {            // do nothing - discard?        }        /**         * <P>         * Invoked when an exception occurs in the session.         * </P>         *          * @param e         *            The exception.         */        public void processException(Exception e) {            try {                m_handler.snmpTrapSessionError(m_forWhom, ERROR_EXCEPTION, e);            } catch (Exception e1) {                // discard            }        }    }    /**     * Used to disallow the default constructor.     *      * @exception java.lang.UnsupportedOperationException     *                Thrown if the constructor is called.     *      */    private SnmpTrapSession() throws java.lang.UnsupportedOperationException {        throw new java.lang.UnsupportedOperationException("Illegal constructor call");    }    /**     * The default SnmpTrapSession constructor.     *      * @param handler     *            The handler associated for message processing.     *      * @exception java.net.SocketException     *                If thrown it is from the creation of a DatagramSocket.     * @exception java.lang.SecurityException     *                Thrown if the security manager disallows the creation of     *                the handler.     */    public SnmpTrapSession(SnmpTrapHandler handler) throws SocketException {        m_port = DEFAULT_PORT;        m_encoder = (new SnmpParameters()).getEncoder();        m_threadException = false;        m_why = null;        m_handler = handler;        m_portal = new SnmpPortal(new TrapHandler(this), m_encoder, m_port);    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?