package org.snmp4j.transport;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import org.snmp4j.SNMP4JSettings;
import org.snmp4j.TransportStateReference;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.util.WorkerTask;

/* JADX INFO: loaded from: classes.dex */
public class DefaultUdpTransportMapping extends UdpTransportMapping {
    private static final LogAdapter logger = LogFactory.getLogger(DefaultUdpTransportMapping.class);
    protected WorkerTask listener;
    protected ListenThread listenerThread;
    private int receiveBufferSize;
    protected DatagramSocket socket;
    private int socketTimeout;

    class ListenThread implements WorkerTask {
        private byte[] buf;
        private volatile boolean stop = false;

        public ListenThread() throws SocketException {
            this.buf = new byte[DefaultUdpTransportMapping.this.getMaxInboundMessageSize()];
        }

        public void close() {
            this.stop = true;
        }

        @Override // org.snmp4j.util.WorkerTask
        public void interrupt() {
            if (DefaultUdpTransportMapping.logger.isDebugEnabled()) {
                DefaultUdpTransportMapping.logger.debug("Interrupting worker task: " + ListenThread.class.getName());
            }
            close();
        }

        @Override // org.snmp4j.util.WorkerTask
        public void join() throws InterruptedException {
            if (DefaultUdpTransportMapping.logger.isDebugEnabled()) {
                DefaultUdpTransportMapping.logger.debug("Joining worker task: " + ListenThread.class.getName());
            }
        }

        /* JADX WARN: Removed duplicated region for block: B:27:0x00a4 A[Catch: IOException -> 0x013a, SocketException -> 0x015d, PortUnreachableException -> 0x01dc, SocketTimeoutException -> 0x0209, TryCatch #7 {SocketTimeoutException -> 0x0209, blocks: (B:16:0x0087, B:25:0x009a, B:27:0x00a4, B:28:0x00f1, B:30:0x00f9, B:32:0x0113, B:31:0x010b, B:22:0x0095), top: B:104:0x0087 }] */
        /* JADX WARN: Removed duplicated region for block: B:30:0x00f9 A[Catch: IOException -> 0x013a, SocketException -> 0x015d, PortUnreachableException -> 0x01dc, SocketTimeoutException -> 0x0209, TryCatch #7 {SocketTimeoutException -> 0x0209, blocks: (B:16:0x0087, B:25:0x009a, B:27:0x00a4, B:28:0x00f1, B:30:0x00f9, B:32:0x0113, B:31:0x010b, B:22:0x0095), top: B:104:0x0087 }] */
        /* JADX WARN: Removed duplicated region for block: B:31:0x010b A[Catch: IOException -> 0x013a, SocketException -> 0x015d, PortUnreachableException -> 0x01dc, SocketTimeoutException -> 0x0209, TryCatch #7 {SocketTimeoutException -> 0x0209, blocks: (B:16:0x0087, B:25:0x009a, B:27:0x00a4, B:28:0x00f1, B:30:0x00f9, B:32:0x0113, B:31:0x010b, B:22:0x0095), top: B:104:0x0087 }] */
        @Override // java.lang.Runnable
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void run() {
            /*
                Method dump skipped, instruction units count: 599
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: org.snmp4j.transport.DefaultUdpTransportMapping.ListenThread.run():void");
        }

        @Override // org.snmp4j.util.WorkerTask
        public void terminate() {
            close();
            if (DefaultUdpTransportMapping.logger.isDebugEnabled()) {
                DefaultUdpTransportMapping.logger.debug("Terminated worker task: " + ListenThread.class.getName());
            }
        }
    }

    public DefaultUdpTransportMapping() throws IOException {
        super(new UdpAddress("0.0.0.0/0"));
        this.socket = null;
        this.socketTimeout = 0;
        this.receiveBufferSize = 0;
        this.socket = new DatagramSocket(this.udpAddress.getPort());
    }

    private synchronized DatagramSocket ensureSocket() throws SocketException {
        DatagramSocket datagramSocket;
        datagramSocket = this.socket;
        if (datagramSocket == null) {
            datagramSocket = new DatagramSocket(this.udpAddress.getPort());
            datagramSocket.setSoTimeout(this.socketTimeout);
            this.socket = datagramSocket;
        }
        return datagramSocket;
    }

    @Override // org.snmp4j.transport.UdpTransportMapping, org.snmp4j.transport.AbstractTransportMapping, org.snmp4j.TransportMapping
    public void close() throws IOException {
        WorkerTask workerTask = this.listener;
        boolean z = true;
        boolean z2 = false;
        if (workerTask != null) {
            workerTask.terminate();
            workerTask.interrupt();
            if (this.socketTimeout > 0) {
                try {
                    workerTask.join();
                } catch (InterruptedException e2) {
                    logger.warn(e2);
                    z2 = true;
                }
            }
            this.listener = null;
        }
        DatagramSocket datagramSocket = this.socket;
        if (datagramSocket != null && !datagramSocket.isClosed()) {
            datagramSocket.close();
        }
        this.socket = null;
        if (workerTask == null || this.socketTimeout > 0) {
            z = z2;
        } else {
            try {
                workerTask.join();
                z = z2;
            } catch (InterruptedException e3) {
                logger.warn(e3);
            }
        }
        if (z) {
            Thread.currentThread().interrupt();
        }
    }

    public int getPriority() {
        Object obj = this.listener;
        if (obj instanceof Thread) {
            return ((Thread) obj).getPriority();
        }
        return 5;
    }

    public int getReceiveBufferSize() {
        return this.receiveBufferSize;
    }

    public int getSocketTimeout() {
        return this.socketTimeout;
    }

    public String getThreadName() {
        Object obj = this.listener;
        if (obj instanceof Thread) {
            return ((Thread) obj).getName();
        }
        return null;
    }

    @Override // org.snmp4j.TransportMapping
    public boolean isListening() {
        return this.listener != null;
    }

    @Override // org.snmp4j.transport.UdpTransportMapping, org.snmp4j.transport.AbstractTransportMapping, org.snmp4j.TransportMapping
    public synchronized void listen() throws IOException {
        if (this.listener != null) {
            throw new SocketException("Port already listening");
        }
        ensureSocket();
        this.listenerThread = new ListenThread();
        WorkerTask workerTaskCreateWorkerThread = SNMP4JSettings.getThreadFactory().createWorkerThread("DefaultUDPTransportMapping_" + getAddress(), this.listenerThread, true);
        this.listener = workerTaskCreateWorkerThread;
        workerTaskCreateWorkerThread.run();
    }

    protected DatagramSocket renewSocketAfterException(SocketException socketException, DatagramSocket datagramSocket) throws SocketException {
        if (datagramSocket != null && !datagramSocket.isClosed()) {
            datagramSocket.close();
        }
        DatagramSocket datagramSocket2 = new DatagramSocket(this.udpAddress.getPort(), this.udpAddress.getInetAddress());
        datagramSocket2.setSoTimeout(this.socketTimeout);
        return datagramSocket2;
    }

    public void setMaxInboundMessageSize(int i2) {
        this.maxInboundMessageSize = i2;
    }

    public void setPriority(int i2) {
        Object obj = this.listener;
        if (obj instanceof Thread) {
            ((Thread) obj).setPriority(i2);
        }
    }

    public void setReceiveBufferSize(int i2) {
        if (i2 <= 0) {
            throw new IllegalArgumentException("Receive buffer size must be > 0");
        }
        this.receiveBufferSize = i2;
    }

    public void setSocketTimeout(int i2) {
        this.socketTimeout = i2;
        DatagramSocket datagramSocket = this.socket;
        if (datagramSocket != null) {
            try {
                datagramSocket.setSoTimeout(i2);
            } catch (SocketException e2) {
                throw new RuntimeException(e2);
            }
        }
    }

    public void setThreadName(String str) {
        Object obj = this.listener;
        if (obj instanceof Thread) {
            ((Thread) obj).setName(str);
        }
    }

    @Override // org.snmp4j.transport.UdpTransportMapping, org.snmp4j.TransportMapping
    public UdpAddress getListenAddress() {
        DatagramSocket datagramSocket = this.socket;
        if (datagramSocket != null) {
            return new UdpAddress(datagramSocket.getInetAddress(), datagramSocket.getLocalPort());
        }
        return null;
    }

    @Override // org.snmp4j.transport.UdpTransportMapping, org.snmp4j.transport.AbstractTransportMapping, org.snmp4j.TransportMapping
    public void sendMessage(UdpAddress udpAddress, byte[] bArr, TransportStateReference transportStateReference) throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress(udpAddress.getInetAddress(), udpAddress.getPort());
        if (logger.isDebugEnabled()) {
            logger.debug("Sending message to " + udpAddress + " with length " + bArr.length + ": " + new OctetString(bArr).toHexString());
        }
        ensureSocket().send(new DatagramPacket(bArr, bArr.length, inetSocketAddress));
    }

    public DefaultUdpTransportMapping(UdpAddress udpAddress, boolean z) throws IOException {
        super(udpAddress);
        this.socket = null;
        this.socketTimeout = 0;
        this.receiveBufferSize = 0;
        DatagramSocket datagramSocket = new DatagramSocket((SocketAddress) null);
        this.socket = datagramSocket;
        datagramSocket.setReuseAddress(z);
        this.socket.bind(new InetSocketAddress(udpAddress.getInetAddress(), udpAddress.getPort()));
    }

    public DefaultUdpTransportMapping(UdpAddress udpAddress) throws IOException {
        super(udpAddress);
        this.socket = null;
        this.socketTimeout = 0;
        this.receiveBufferSize = 0;
        this.socket = new DatagramSocket(udpAddress.getPort(), udpAddress.getInetAddress());
    }
}
