⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpadaptorserver.java

📁 Mobile 应用程序使用 Java Micro Edition (Java ME) 平台
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**     * Return the actual port to which the adaptor is bound.      * Can be different from the port given at construction time if     * that port number was 0.     * @return the actual port to which the adaptor is bound.     **/    public int getPort() {	synchronized (this) {	    if (socket != null) return socket.getLocalPort();	}	return super.getPort();    }    /**     * Closes the datagram socket.     */    protected void doUnbind() 	throws CommunicationException, InterruptedException {        if (isTraceOn()) {            trace("doUnbind","Finally close the socket");        }	synchronized (this) {	    if (socket != null) {		socket.close() ;		socket = null ; 		// Important to inform finalize() that the socket is closed...	    }	}        closeTrapSocketIfNeeded() ;        closeInformSocketIfNeeded() ;    }        void createSnmpRequestHandler(SnmpAdaptorServer server, int id, 				  DatagramSocket s, DatagramPacket p,				  SnmpMibTree tree, Vector m, Object a, 				  SnmpPduFactory factory, 				  SnmpUserDataFactory dataFactory,				  MBeanServer f, ObjectName n) {	final SnmpRequestHandler handler = 	    new SnmpRequestHandler(this, id, s, p, tree, m, a, factory,				   dataFactory, f, n);	threadService.submitTask(handler);    }    /**     * Reads a packet from the datagram socket and creates a request      * handler which decodes and processes the request.     */    protected void doReceive() 	throws CommunicationException, InterruptedException {        // Let's wait for something to be received.        //        try {            packet = new DatagramPacket(new byte[bufferSize], bufferSize) ;            socket.receive(packet);	    int state = getState();	    	    if(state != ONLINE) {		if (isTraceOn()) {		    trace("doReceive", 		       "received a message but state not online, returning.");		}		return;	    }	    	    createSnmpRequestHandler(this, servedClientCount, socket, 				     packet, root, mibs, ipacl, pduFactory,				     userDataFactory, topMBS, objectName);        } catch (SocketException e) {            // Let's check if we have been interrupted by stop().            //            if (e.getMessage().equals(InterruptSysCallMsg))                throw new InterruptedException(e.toString()) ;            else                throw new CommunicationException(e) ;        } catch (InterruptedIOException e) {            throw new InterruptedException(e.toString()) ;        } catch (CommunicationException e) {            throw e ;        } catch (Exception e) {            throw new CommunicationException(e) ;        }        if (isTraceOn()) {            trace("doReceive", "received a message");        }    }      protected void doError(Exception e) throws CommunicationException {        return;    }        /**     * Not used in this context.     */    protected void doProcess() 	throws CommunicationException, InterruptedException {    }        /**     * The number of times the communicator server will attempt     * to bind before giving up.      * We attempt only once...     * @return 1     **/    protected int getBindTries() {	return 1;    }    /**     * Stops this SNMP protocol adaptor.     * Closes the datagram socket.     * <p>      * Has no effect if this SNMP protocol adaptor is <CODE>OFFLINE</CODE> or      * <CODE>STOPPING</CODE>.     */    public void stop(){        	final int port = getPort();        if (isTraceOn()) {            trace("stop", "Stopping: using port " + port);        }        if ((state == ONLINE) || (state == STARTING)){            super.stop();            try {		DatagramSocket sn = new DatagramSocket(0);		try {		    byte[] ob = new byte[1];		    		    DatagramPacket pk;		    if (address != null)			pk = new DatagramPacket(ob , 1, address, port);		    else			pk = new DatagramPacket(ob , 1, 				 java.net.InetAddress.getLocalHost(), port);                                		    if (isTraceOn()) {			trace("stop", "Sending: using port " + port);		    }		    sn.send(pk);		} finally {		    sn.close();		}            } catch (Throwable e){                if (isDebugOn()) {                    debug("stop", e);                }            }         }    }        // SENDING SNMP TRAPS STUFF    //-------------------------        /**     * Sends a trap using SNMP V1 trap format.     * <BR>The trap is sent to each destination defined in the ACL file      * (if available).     * If no ACL file or no destinations are available, the trap is sent      * to the local host.     *      * @param generic The generic number of the trap.     * @param specific The specific number of the trap.     * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null.     *     * @exception IOException An I/O error occurred while sending the trap.     * @exception SnmpStatusException If the trap exceeds the limit defined      *            by <CODE>bufferSize</CODE>.     */    public void snmpV1Trap(int generic, int specific, 			   SnmpVarBindList varBindList)         throws IOException, SnmpStatusException {        if (isTraceOn()) {            trace("snmpV1Trap", "generic=" + generic + 		  ", specific=" + specific);        }                // First, make an SNMP V1 trap pdu        //        SnmpPduTrap pdu = new SnmpPduTrap() ;        pdu.address = null ;        pdu.port = trapPort ;        pdu.type = pduV1TrapPdu ;        pdu.version = snmpVersionOne ;        pdu.community = null ;        pdu.enterprise = enterpriseOid ;        pdu.genericTrap = generic ;        pdu.specificTrap = specific ;        pdu.timeStamp = getSysUpTime();            if (varBindList != null) {            pdu.varBindList = new SnmpVarBind[varBindList.size()] ;            varBindList.copyInto(pdu.varBindList);        }        else            pdu.varBindList = null ;              // If the local host cannot be determined, we put 0.0.0.0 in agentAddr        try {            if (address != null)                pdu.agentAddr = handleMultipleIpVersion(address.getAddress());            else pdu.agentAddr =               handleMultipleIpVersion(InetAddress.getLocalHost().getAddress());        } catch (UnknownHostException e) {	    byte[] zeroedAddr = new byte[4];            pdu.agentAddr = handleMultipleIpVersion(zeroedAddr) ;        }            // Next, send the pdu to all destinations defined in ACL        //        sendTrapPdu(pdu) ;    }        private SnmpIpAddress handleMultipleIpVersion(byte[] address) {	if(address.length == 4)	  return new SnmpIpAddress(address);	else {	    if(isDebugOn())		debug("handleMultipleIPVersion", 		      "Not an IPv4 address, return null");	    return null;	}    }    /**     * Sends a trap using SNMP V1 trap format.     * <BR>The trap is sent to the specified <CODE>InetAddress</CODE>      * destination using the specified community string (and the ACL file      * is not used).     *      * @param addr The <CODE>InetAddress</CODE> destination of the trap.     * @param cs The community string to be used for the trap.     * @param generic The generic number of the trap.     * @param specific The specific number of the trap.     * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null.     *     * @exception IOException An I/O error occurred while sending the trap.     * @exception SnmpStatusException If the trap exceeds the limit defined      *            by <CODE>bufferSize</CODE>.     */    public void snmpV1Trap(InetAddress addr, String cs, int generic, 			   int specific, SnmpVarBindList varBindList)         throws IOException, SnmpStatusException {	        if (isTraceOn()) {            trace("snmpV1Trap", "generic=" + generic + ", specific=" + 		  specific);        }                // First, make an SNMP V1 trap pdu        //        SnmpPduTrap pdu = new SnmpPduTrap() ;        pdu.address = null ;        pdu.port = trapPort ;        pdu.type = pduV1TrapPdu ;        pdu.version = snmpVersionOne ;		if(cs != null)	    pdu.community = cs.getBytes();	else	    pdu.community = null ;	        pdu.enterprise = enterpriseOid ;        pdu.genericTrap = generic ;        pdu.specificTrap = specific ;        pdu.timeStamp = getSysUpTime();            if (varBindList != null) {            pdu.varBindList = new SnmpVarBind[varBindList.size()] ;            varBindList.copyInto(pdu.varBindList);        }        else            pdu.varBindList = null ;              // If the local host cannot be determined, we put 0.0.0.0 in agentAddr        try {            if (address != null)                pdu.agentAddr = handleMultipleIpVersion(address.getAddress());            else pdu.agentAddr = 	      handleMultipleIpVersion(InetAddress.getLocalHost().getAddress());        } catch (UnknownHostException e) {	    byte[] zeroedAddr = new byte[4];            pdu.agentAddr = handleMultipleIpVersion(zeroedAddr) ;        }	        // Next, send the pdu to the specified destination        //	if(addr != null)	    sendTrapPdu(addr, pdu) ;	else	    sendTrapPdu(pdu);    }        /**     * Sends a trap using SNMP V1 trap format.     * <BR>The trap is sent to the specified <CODE>InetAddress</CODE>      * destination using the specified parameters (and the ACL file is not     * used).     * Note that if the specified <CODE>InetAddress</CODE> destination is null,     * then the ACL file mechanism is used.     *      * @param addr The <CODE>InetAddress</CODE> destination of the trap.     * @param agentAddr The agent address to be used for the trap.     * @param cs The community string to be used for the trap.     * @param enterpOid The enterprise OID to be used for the trap.     * @param generic The generic number of the trap.     * @param specific The specific number of the trap.     * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null.     * @param time The time stamp (overwrite the current time).     *     * @exception IOException An I/O error occurred while sending the trap.     * @exception SnmpStatusException If the trap exceeds the limit defined      *            by <CODE>bufferSize</CODE>.     *     * @since 1.5     */    public void snmpV1Trap(InetAddress addr,                           SnmpIpAddress agentAddr,                           String cs,                           SnmpOid enterpOid,                           int generic,                           int specific,                           SnmpVarBindList varBindList,                           SnmpTimeticks time)        throws IOException, SnmpStatusException {	snmpV1Trap(addr, 		   trapPort,		   agentAddr,		   cs,		   enterpOid,		   generic,		   specific,		   varBindList,		   time);    }    /**     * Sends a trap using SNMP V1 trap format.     * <BR>The trap is sent to the specified <CODE>SnmpPeer</CODE> destination.     * The community string used is the one located in the      * <CODE>SnmpPeer</CODE> parameters      * (<CODE>SnmpParameters.getRdCommunity() </CODE>).     *      * @param peer The <CODE>SnmpPeer</CODE> destination of the trap.     * @param agentAddr The agent address to be used for the trap.     * @param enterpOid The enterprise OID to be used for the trap.     * @param generic The generic number of the trap.     * @param specific The specific number of the trap.     * @param varBindList A list of <CODE>SnmpVarBind</CODE> instances or null.     * @param time The time stamp (overwrite the current time).     *     * @exception IOException An I/O error occurred while sending the trap.     * @exception SnmpStatusException If the trap exceeds the limit     * defined by <CODE>bufferSize</CODE>.     *     * @since 1.5     */    public void snmpV1Trap(SnmpPeer peer,			   SnmpIpAddress agentAddr,			   SnmpOid enterpOid,			   int generic,			   int specific,			   SnmpVarBindList varBindList,			   SnmpTimeticks time) 	throws IOException, SnmpStatusException {	SnmpParameters p = (SnmpParameters) peer.getParams();	snmpV1Trap(peer.getDestAddr(), 		   peer.getDestPort(),		   agentAddr,		   p.getRdCommunity(),		   enterpOid,		   generic,		   specific,		   varBindList,		   time);    }        private void snmpV1Trap(InetAddress addr,			    int port,			    SnmpIpAddress agentAddr,			    String cs,			    SnmpOid enterpOid,			    int generic,			    int specific,			    SnmpVarBindList varBindList,			    SnmpTimeticks time) 	throws IOException, SnmpStatusException {		if (isTraceOn()) {            trace("snmpV1Trap", "generic=" + generic + ", specific=" + 		  specific);        }                // First, make an SNMP V1 trap pdu        //        SnmpPduTrap pdu = new SnmpPduTrap() ;        pdu.address = null ;        pdu.port = port ;        pdu.type = pduV1TrapPdu ;        pdu.version = snmpVersionOne ;	        //Diff start        if(cs != null)             pdu.community = cs.getBytes();        else            pdu.community = null ;        //Diff end

⌨️ 快捷键说明

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