Search in sources :

Example 1 with ResponseListener

use of org.snmp4j.event.ResponseListener in project opennms by OpenNMS.

the class Snmp4JStrategy method send.

private void send(Snmp4JAgentConfig agentConfig, PDU pdu, boolean expectResponse, CompletableFuture<SnmpValue[]> future) {
    Snmp session;
    try {
        session = agentConfig.createSnmpSession();
    } catch (IOException e) {
        LOG.error("send: Could not create SNMP session for agent {}", agentConfig, e);
        future.completeExceptionally(new Exception("Could not create SNMP session for agent"));
        return;
    }
    if (expectResponse) {
        try {
            session.listen();
        } catch (IOException e) {
            LOG.error("send: error setting up listener for SNMP responses", e);
            future.completeExceptionally(new Exception("error setting up listener for SNMP responses"));
            return;
        }
    }
    try {
        if (expectResponse) {
            session.send(pdu, agentConfig.getTarget(), null, new ResponseListener() {

                @Override
                public void onResponse(ResponseEvent responseEvent) {
                    if (expectResponse) {
                        try {
                            future.complete(processResponse(agentConfig, responseEvent));
                        } catch (IOException e) {
                            future.completeExceptionally(e);
                        } finally {
                            // Close the tracker using a separate thread
                            // This allows the SnmpWalker to clean up properly instead
                            // of interrupting execution as it's executing the callback
                            REAPER_EXECUTOR.submit(new Runnable() {

                                @Override
                                public void run() {
                                    closeQuietly(session);
                                }
                            });
                        }
                    }
                }
            });
        } else {
            session.send(pdu, agentConfig.getTarget());
            future.complete(null);
            closeQuietly(session);
        }
    } catch (final IOException e) {
        LOG.error("send: error during SNMP operation", e);
        future.completeExceptionally(e);
    } catch (final RuntimeException e) {
        LOG.error("send: unexpected error during SNMP operation", e);
        future.completeExceptionally(e);
    }
}
Also used : Snmp(org.snmp4j.Snmp) ResponseEvent(org.snmp4j.event.ResponseEvent) IOException(java.io.IOException) ResponseListener(org.snmp4j.event.ResponseListener) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IOException (java.io.IOException)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Snmp (org.snmp4j.Snmp)1 ResponseEvent (org.snmp4j.event.ResponseEvent)1 ResponseListener (org.snmp4j.event.ResponseListener)1