使用 SNMP4J 异步 SNMP 获取消息

问题描述 投票:0回答:1

我一直在使用 SNMP4J 库为 java 开发一个简单的 SNMP 管理器。我正在尝试发送简单的异步获取消息。我使用 SNMPmanager 来创建 PDU 并启动侦听器,以及一个名为 SNMPget 的简单类来发出简单的获取请求。

javadoc中所述:

通过在实现 ResponseListener 接口的对象实例上调用回调方法来返回异步响应。

我已按照确切的说明来实现回调,但看起来在发送 get 请求时回调方法 onResponse 从未执行。事实上,我已经使用wireshark来检查数据包是否正在发送、请求是否正确发送以及是否收到正确的响应。关于我做错了什么有任何线索吗?

    public class SNMPManager {
private static SNMPManager instance = new SNMPManager();
private Snmp snmp_ = null;

//Common SNMP variables
private int requestPort = 161;
private int timeout = 10000;
private int retryCount = 1;
private int maxRepetitions = 20;
private int nonRepeaters = 0;

//Singleton pattern
public static SNMPManager getInstance() {
    return instance;
}

//Empty Constructor
private SNMPManager() { }

//Start the manager
public void start() throws IOException {
    //Create a UDP transport on all interfaces
    DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
    //Create a SNMP instance
    snmp_ = new Snmp(transport);
    //Put all the transport mappings into listen mode
    snmp_.listen();
}

//Methods for creating targets

// Create the target for version 2c
public Target createVersion2cTarget(String ipaddress, String communityName) {
    CommunityTarget target = new CommunityTarget();
    target.setAddress(new UdpAddress(String.format("%s/%d", ipaddress, requestPort)));
    target.setCommunity(new OctetString(communityName));
    target.setTimeout(timeout);
    target.setRetries(retryCount);
    target.setVersion(SnmpConstants.version2c);
    return target;
}

//Methods for creating PDUs
public PDU createGetRequestPdu(String requestOID) {
    PDU pdu = new PDU();
    //Set the request type
    pdu.setType(PDU.GET);
    //Set the OID
    OID rOID = new OID(requestOID);
    pdu.add(new VariableBinding(rOID));
    return pdu;
}

//Methods for the request (async mode)
public void getRequest(PDU pdu, Target target, Object userHandle, ResponseListener listener) throws IOException {
    snmp_.get(pdu, target, userHandle, listener);
}

}

public class SNMPget {

protected final SNMPManager snmp_;
private String requestOID;
private String ipAddress;
private String communityName;

public SNMPget(String newRequestOID, String hostIpAddress, String newCommunityName) {
    snmp_ = SNMPManager.getInstance();
    requestOID = newRequestOID;
    ipAddress = hostIpAddress;
    communityName = newCommunityName;
}
public void get(ResponseListener listener) throws IOException {
    Target targetHost = snmp_.createVersion2cTarget(ipAddress, communityName);
    PDU pduSNMPget = snmp_.createGetRequestPdu(requestOID);
    snmp_.getRequest(pduSNMPget, targetHost, null, listener);
}

}

public class mainTest {
public static void main(String[] args) throws IOException {

    SNMPManager snmp = SNMPManager.getInstance();
    snmp.start();
    ResponseListener listener = new ResponseListener() {
        @Override
        public void onResponse(ResponseEvent event) {
            // Always cancel async request when response has been received
            // otherwise a memory leak is created! Not canceling a request
            // immediately can be useful when sending a request to a broadcast
            // address.
            ((Snmp) event.getSource()).cancel(event.getRequest(), this);
            PDU response = event.getResponse();
            System.out.println("Received response "+response);
        }
    };

    SNMPget snmpGetCmd = new SNMPget("1.3.6.1.2.1.1.1.0", "192.168.137.104", "public");

    snmpGetCmd.get(listener);

}

}

java snmp snmp4j
1个回答
0
投票

由于这是一个异步操作,因此主程序在 ResponseListener 收到响应之前退出。如果您添加代码以防止主程序退出,那么您将收到响应。

© www.soinside.com 2019 - 2024. All rights reserved.