Search in sources :

Example 1 with MultiThreadedMessageDispatcher

use of org.snmp4j.util.MultiThreadedMessageDispatcher in project LogHub by fbacchella.

the class SnmpTrap method configure.

@Override
public boolean configure(Properties properties) {
    decoder = Receiver.NULLDECODER;
    if (!reconfigured && properties.containsKey("mibdirs")) {
        reconfigured = true;
        String[] mibdirs = null;
        try {
            mibdirs = Arrays.stream((Object[]) properties.get("mibdirs")).map(i -> i.toString()).toArray(String[]::new);
            formatter = OIDFormatter.register(mibdirs);
        } catch (ClassCastException e) {
            logger.error("mibdirs property is not a string array");
            logger.catching(Level.DEBUG, e.getCause());
            return false;
        }
    } else {
        formatter = OIDFormatter.register();
    }
    threadPool = ThreadPool.create("Trap", 2);
    MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
    dispatcher.addCommandResponder(this);
    dispatcher.addMessageProcessingModel(new MPv1());
    dispatcher.addMessageProcessingModel(new MPv2c());
    Address listenAddress = GenericAddress.parse(protocol + ":" + listen + "/" + port);
    TransportMapping<?> transport;
    try {
        transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
    } catch (IOException e) {
        logger.error("can't bind to {}: {}", listenAddress, e.getMessage());
        return false;
    }
    snmp = new Snmp(dispatcher, transport);
    try {
        snmp.listen();
    } catch (IOException e) {
        logger.error("can't listen: {}", e.getMessage());
    }
    return super.configure(properties);
}
Also used : UdpAddress(org.snmp4j.smi.UdpAddress) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) UdpAddress(org.snmp4j.smi.UdpAddress) InetSocketAddress(java.net.InetSocketAddress) GenericAddress(org.snmp4j.smi.GenericAddress) MPv2c(org.snmp4j.mp.MPv2c) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) IOException(java.io.IOException) Snmp(org.snmp4j.Snmp) MPv1(org.snmp4j.mp.MPv1) MultiThreadedMessageDispatcher(org.snmp4j.util.MultiThreadedMessageDispatcher)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)1 Snmp (org.snmp4j.Snmp)1 MPv1 (org.snmp4j.mp.MPv1)1 MPv2c (org.snmp4j.mp.MPv2c)1 Address (org.snmp4j.smi.Address)1 GenericAddress (org.snmp4j.smi.GenericAddress)1 IpAddress (org.snmp4j.smi.IpAddress)1 OctetString (org.snmp4j.smi.OctetString)1 TransportIpAddress (org.snmp4j.smi.TransportIpAddress)1 UdpAddress (org.snmp4j.smi.UdpAddress)1 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)1 MultiThreadedMessageDispatcher (org.snmp4j.util.MultiThreadedMessageDispatcher)1