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

📄 snmp.java

📁 snmp4j 1.8.2版 The org.snmp4j classes are capable of creating, sending, and receiving SNMPv1/v2c/v3
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    if (tm instanceof ConnectionOrientedTransportMapping) {
      ((ConnectionOrientedTransportMapping)tm).setConnectionTimeout(0);
    }
    tm.addTransportListener(messageDispatcher);
    if (notificationDispatcher == null) {
      notificationDispatcher = new NotificationDispatcher();
      addCommandResponder(notificationDispatcher);
    }
    notificationDispatcher.addNotificationListener(listenAddress, tm, listener);
    try {
      tm.listen();
      if (logger.isInfoEnabled()) {
        logger.info("Added notification listener for address: "+
                    listenAddress);
      }
      return true;
    }
    catch (IOException ex) {
      logger.warn("Failed to initialize notification listener for address '"+
                  listenAddress+"': "+ex.getMessage());
      return false;
    }
  }

  /**
   * Removes (deletes) the notification listener for the specified transport
   * endpoint.
   * @param listenAddress
   *    the listen <code>Address</code> to be removed.
   * @return
   *    <code>true</code> if the notification listener has been removed
   *    successfully.
   */
  public synchronized boolean removeNotificationListener(Address listenAddress)
  {
    if (notificationDispatcher != null) {
      if (logger.isInfoEnabled()) {
        logger.info("Removing notification listener for address: "+
                    listenAddress);
      }
      return notificationDispatcher.removeNotificationListener(listenAddress);
    }
    else {
      return false;
    }
  }

  /**
   * Puts all associated transport mappings into listen mode.
   * @throws IOException
   *    if a transport mapping throws an <code>IOException</code> when its
   *    {@link TransportMapping#listen()} method has been called.
   */
  public void listen() throws IOException {
    for (Iterator it = messageDispatcher.getTransportMappings().iterator();
         it.hasNext(); ) {
      TransportMapping tm = (TransportMapping) it.next();
      if (!tm.isListening()) {
        tm.listen();
      }
    }
  }

  /**
   * Gets the next unique request ID. The returned ID is unique across
   * the last 2^31-1 IDs generated by this message dispatcher.
   * @return
   *    an integer value in the range 1..2^31-1. The returned ID can be used
   *    to map responses to requests send through this message dispatcher.
   * @since 1.1
   * @see MessageDispatcher#getNextRequestID
   */
  public int getNextRequestID() {
    return messageDispatcher.getNextRequestID();
  }

  /**
   * Closes the session and frees any allocated resources, i.e. sockets and
   * the internal thread for processing request timeouts.
   * <p>
   * If there are any pending requests, the {@link ResponseListener} associated
   * with the pending requests, will be called with a <code>null</code>
   * response and a {@link InterruptedException} in the error member of the
   * {@link ResponseEvent} returned.
   * <p>
   * After a <code>Session</code> has been closed it must not be used anymore.
   * @throws IOException
   *    if a transport mapping cannot be closed successfully.
   */
  public void close() throws IOException {
    for (Iterator it = messageDispatcher.getTransportMappings().iterator();
         it.hasNext(); ) {
      ((TransportMapping) it.next()).close();
    }
    timer.cancel();
    for (Iterator it = pendingRequests.values().iterator(); it.hasNext(); ) {
      PendingRequest pending = (PendingRequest) it.next();
      ResponseEvent e =
          new ResponseEvent(this, null, pending.pdu, null, pending.userObject,
                            new InterruptedException(
          "Snmp session has been closed"));
      pending.listener.onResponse(e);
    }
    // close all notification listeners
    if (notificationDispatcher != null) {
      notificationDispatcher.closeAll();
    }
  }

  /**
   * Sends a GET request to a target. This method sets the PDU's type to
   * {@link PDU#GET} and then sends a synchronous request to the supplied
   * target.
   * @param pdu
   *    a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
   *    instance has to be a <code>ScopedPDU</code> instance.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @return
   *    the received response encapsulated in a <code>ResponseEvent</code>
   *    instance. To obtain the received response <code>PDU</code> call
   *    {@link ResponseEvent#getResponse()}. If the request timed out,
   *    that method will return <code>null</code>.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public ResponseEvent get(PDU pdu, Target target) throws IOException {
    pdu.setType(PDU.GET);
    return send(pdu, target);
  }

  /**
   * Asynchronously sends a GET request <code>PDU</code> to the given target.
   * The response is then returned by calling the supplied
   * <code>ResponseListener</code> instance.
   *
   * @param pdu
   *    the PDU instance to send.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @param userHandle
   *    an user defined handle that is returned when the request is returned
   *    via the <code>listener</code> object.
   * @param listener
   *    a <code>ResponseListener</code> instance that is called when
   *    <code>pdu</code> is a confirmed PDU and the request is either answered
   *    or timed out.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public void get(PDU pdu, Target target, Object userHandle,
                  ResponseListener listener) throws IOException {
    pdu.setType(PDU.GET);
    send(pdu, target, userHandle, listener);
  }

  /**
   * Sends a GETNEXT request to a target. This method sets the PDU's type to
   * {@link PDU#GETNEXT} and then sends a synchronous request to the supplied
   * target. This method is a convenience wrapper for the
   * {@link #send(PDU pdu, Target target)} method.
   * @param pdu
   *    a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
   *    instance has to be a <code>ScopedPDU</code> instance.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @return
   *    the received response encapsulated in a <code>ResponseEvent</code>
   *    instance. To obtain the received response <code>PDU</code> call
   *    {@link ResponseEvent#getResponse()}. If the request timed out,
   *    that method will return <code>null</code>.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public ResponseEvent getNext(PDU pdu, Target target) throws IOException {
    pdu.setType(PDU.GETNEXT);
    return send(pdu, target);
  }

  /**
   * Asynchronously sends a GETNEXT request <code>PDU</code> to the given
   * target. The response is then returned by calling the supplied
   * <code>ResponseListener</code> instance.
   *
   * @param pdu
   *    the PDU instance to send.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @param userHandle
   *    an user defined handle that is returned when the request is returned
   *    via the <code>listener</code> object.
   * @param listener
   *    a <code>ResponseListener</code> instance that is called when
   *    <code>pdu</code> is a confirmed PDU and the request is either answered
   *    or timed out.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public void getNext(PDU pdu, Target target, Object userHandle,
                      ResponseListener listener) throws IOException {
    pdu.setType(PDU.GETNEXT);
    send(pdu, target, userHandle, listener);
  }

  /**
   * Sends a GETBULK request to a target. This method sets the PDU's type to
   * {@link PDU#GETBULK} and then sends a synchronous request to the supplied
   * target. This method is a convenience wrapper for the
   * {@link #send(PDU pdu, Target target)} method.
   * @param pdu
   *    a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
   *    instance has to be a <code>ScopedPDU</code> instance.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @return
   *    the received response encapsulated in a <code>ResponseEvent</code>
   *    instance. To obtain the received response <code>PDU</code> call
   *    {@link ResponseEvent#getResponse()}. If the request timed out,
   *    that method will return <code>null</code>.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public ResponseEvent getBulk(PDU pdu, Target target) throws IOException {
    pdu.setType(PDU.GETBULK);
    return send(pdu, target);
  }

  /**
   * Asynchronously sends a GETBULK request <code>PDU</code> to the given
   * target. The response is then returned by calling the supplied
   * <code>ResponseListener</code> instance.
   *
   * @param pdu
   *    the PDU instance to send.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @param userHandle
   *    an user defined handle that is returned when the request is returned
   *    via the <code>listener</code> object.
   * @param listener
   *    a <code>ResponseListener</code> instance that is called when
   *    <code>pdu</code> is a confirmed PDU and the request is either answered
   *    or timed out.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public void getBulk(PDU pdu, Target target, Object userHandle,
                      ResponseListener listener) throws IOException {
    pdu.setType(PDU.GETBULK);
    send(pdu, target, userHandle, listener);
  }

  /**
   * Sends an INFORM request to a target. This method sets the PDU's type to
   * {@link PDU#INFORM} and then sends a synchronous request to the supplied
   * target. This method is a convenience wrapper for the
   * {@link #send(PDU pdu, Target target)} method.
   * @param pdu
   *    a <code>PDU</code> instance. For SNMPv3 messages, the supplied PDU
   *    instance has to be a <code>ScopedPDU</code> instance.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @return
   *    the received response encapsulated in a <code>ResponseEvent</code>
   *    instance. To obtain the received response <code>PDU</code> call
   *    {@link ResponseEvent#getResponse()}. If the request timed out,
   *    that method will return <code>null</code>.
   * @throws IOException
   *    if the inform request could not be send to the specified target.
   * @since 1.1
   */
  public ResponseEvent inform(PDU pdu, Target target) throws IOException {
    pdu.setType(PDU.INFORM);
    return send(pdu, target);
  }

  /**
   * Asynchronously sends an INFORM request <code>PDU</code> to the given
   * target. The response is then returned by calling the supplied
   * <code>ResponseListener</code> instance.
   *
   * @param pdu
   *    the PDU instance to send.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>.
   * @param userHandle
   *    an user defined handle that is returned when the request is returned
   *    via the <code>listener</code> object.
   * @param listener
   *    a <code>ResponseListener</code> instance that is called when
   *    <code>pdu</code> is a confirmed PDU and the request is either answered
   *    or timed out.
   * @throws IOException
   *    if the PDU cannot be sent to the target.
   * @since 1.1
   */
  public void inform(PDU pdu, Target target, Object userHandle,
                     ResponseListener listener) throws IOException {
    pdu.setType(PDU.INFORM);
    send(pdu, target, userHandle, listener);
  }

  /**
   * Sends a SNMPv1 trap to a target. This method sets the PDU's type to
   * {@link PDU#V1TRAP} and then sends it to the supplied target. This method
   * is a convenience wrapper for the  {@link #send(PDU pdu, Target target)}
   * method.
   * @param pdu
   *    a <code>PDUv1</code> instance.
   * @param target
   *    the Target instance representing the target SNMP engine where to send
   *    the <code>pdu</code>. The selected SNMP protocol version for the
   *    target must be {@link SnmpConstants#version1}.
   * @throws IOException
   *    if the trap cannot be sent.
   * @since 1.1
   */

⌨️ 快捷键说明

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