package org.eclipse.paho.mqttv5.client.internal;

import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import org.eclipse.paho.mqttv5.client.BufferedMessage;
import org.eclipse.paho.mqttv5.client.IMqttMessageListener;
import org.eclipse.paho.mqttv5.client.MqttActionListener;
import org.eclipse.paho.mqttv5.client.MqttCallback;
import org.eclipse.paho.mqttv5.client.MqttClientInterface;
import org.eclipse.paho.mqttv5.client.MqttClientPersistence;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.eclipse.paho.mqttv5.client.MqttPingSender;
import org.eclipse.paho.mqttv5.client.MqttToken;
import org.eclipse.paho.mqttv5.client.MqttTopic;
import org.eclipse.paho.mqttv5.client.logging.Logger;
import org.eclipse.paho.mqttv5.client.logging.LoggerFactory;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.MqttPersistenceException;
import org.eclipse.paho.mqttv5.common.packet.MqttConnAck;
import org.eclipse.paho.mqttv5.common.packet.MqttConnect;
import org.eclipse.paho.mqttv5.common.packet.MqttDisconnect;
import org.eclipse.paho.mqttv5.common.packet.MqttProperties;
import org.eclipse.paho.mqttv5.common.packet.MqttPublish;
import org.eclipse.paho.mqttv5.common.packet.MqttWireMessage;

/* JADX INFO: loaded from: classes2.dex */
public class ClientComms {
    public static String BUILD_LEVEL = "L${build.level}";
    private static final String CLASS_NAME = "org.eclipse.paho.mqttv5.client.internal.ClientComms";
    private static final byte CLOSED = 4;
    private static final byte CONNECTED = 0;
    private static final byte CONNECTING = 1;
    private static final byte DISCONNECTED = 3;
    private static final byte DISCONNECTING = 2;
    public static String VERSION = "${project.version}";
    private CommsCallback callback;
    private MqttClientInterface client;
    private ClientState clientState;
    private MqttConnectionOptions conOptions;
    private byte conState;
    private DisconnectedMessageBuffer disconnectedMessageBuffer;
    private ExecutorService executorService;
    private MqttConnectionState mqttConnection;
    private int networkModuleIndex;
    private NetworkModule[] networkModules;
    private MqttClientPersistence persistence;
    private MqttPingSender pingSender;
    private CommsReceiver receiver;
    private CommsSender sender;
    private CommsTokenStore tokenStore;
    private Logger log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT, CLASS_NAME);
    private boolean stoppingComms = false;
    private final Object conLock = new Object();
    private boolean closePending = false;
    private boolean resting = false;

    public ClientComms(MqttClientInterface mqttClientInterface, MqttClientPersistence mqttClientPersistence, MqttPingSender mqttPingSender, ExecutorService executorService, MqttSessionState mqttSessionState, MqttConnectionState mqttConnectionState) throws MqttException {
        this.conState = (byte) 3;
        this.conState = (byte) 3;
        this.client = mqttClientInterface;
        this.persistence = mqttClientPersistence;
        this.pingSender = mqttPingSender;
        mqttPingSender.init(this);
        this.executorService = executorService;
        this.mqttConnection = mqttConnectionState;
        this.tokenStore = new CommsTokenStore(getClient().getClientId());
        this.callback = new CommsCallback(this);
        ClientState clientState = new ClientState(mqttClientPersistence, this.tokenStore, this.callback, this, mqttPingSender, this.mqttConnection);
        this.clientState = clientState;
        this.callback.setClientState(clientState);
        this.log.setResourceName(getClient().getClientId());
    }

    CommsReceiver getReceiver() {
        return this.receiver;
    }

    void internalSend(MqttWireMessage mqttWireMessage, MqttToken mqttToken) throws MqttException {
        Logger logger = this.log;
        String str = CLASS_NAME;
        logger.fine(str, "internalSend", "200", new Object[]{mqttWireMessage.getKey(), mqttWireMessage, mqttToken});
        if (mqttToken.getClient() == null) {
            mqttToken.internalTok.setClient(getClient());
            try {
                this.clientState.send(mqttWireMessage, mqttToken);
                return;
            } catch (MqttException e) {
                mqttToken.internalTok.setClient(null);
                if (mqttWireMessage instanceof MqttPublish) {
                    this.clientState.undo((MqttPublish) mqttWireMessage);
                }
                throw e;
            }
        }
        this.log.fine(str, "internalSend", "213", new Object[]{mqttWireMessage.getKey(), mqttWireMessage, mqttToken});
        throw new MqttException(32201);
    }

    public void sendNoWait(MqttWireMessage mqttWireMessage, MqttToken mqttToken) throws MqttException {
        if (isConnected() || ((!isConnected() && (mqttWireMessage instanceof MqttConnect)) || (isDisconnecting() && (mqttWireMessage instanceof MqttDisconnect)))) {
            DisconnectedMessageBuffer disconnectedMessageBuffer = this.disconnectedMessageBuffer;
            if (disconnectedMessageBuffer != null && disconnectedMessageBuffer.getMessageCount() != 0) {
                this.log.fine(CLASS_NAME, "sendNoWait", "507", new Object[]{mqttWireMessage.getKey()});
                if ((mqttWireMessage instanceof MqttPublish) && mqttWireMessage.getProperties().getTopicAlias() != null) {
                    MqttProperties properties = mqttWireMessage.getProperties();
                    properties.setTopicAlias(null);
                    mqttWireMessage.setProperties(properties);
                }
                if (this.disconnectedMessageBuffer.isPersistBuffer()) {
                    this.clientState.persistBufferedMessage(mqttWireMessage);
                }
                this.disconnectedMessageBuffer.putMessage(mqttWireMessage, mqttToken);
                return;
            }
            if (mqttWireMessage instanceof MqttPublish) {
                if (this.mqttConnection.getMaximumQoS() != null) {
                    MqttPublish mqttPublish = (MqttPublish) mqttWireMessage;
                    if (mqttPublish.getMessage().getQos() > this.mqttConnection.getMaximumQoS().intValue()) {
                        MqttMessage message = mqttPublish.getMessage();
                        message.setQos(this.mqttConnection.getMaximumQoS().intValue());
                        mqttPublish.setMessage(message);
                    }
                }
                if (this.mqttConnection.isRetainAvailable() != null) {
                    MqttPublish mqttPublish2 = (MqttPublish) mqttWireMessage;
                    if (mqttPublish2.getMessage().isRetained() && !this.mqttConnection.isRetainAvailable().booleanValue()) {
                        MqttMessage message2 = mqttPublish2.getMessage();
                        message2.setRetained(false);
                        mqttPublish2.setMessage(message2);
                    }
                }
            }
            internalSend(mqttWireMessage, mqttToken);
            return;
        }
        if (this.disconnectedMessageBuffer != null && isResting()) {
            this.log.fine(CLASS_NAME, "sendNoWait", "508", new Object[]{mqttWireMessage.getKey()});
            if (this.disconnectedMessageBuffer.isPersistBuffer()) {
                this.clientState.persistBufferedMessage(mqttWireMessage);
            }
            this.disconnectedMessageBuffer.putMessage(mqttWireMessage, mqttToken);
            return;
        }
        this.log.fine(CLASS_NAME, "sendNoWait", "208");
        throw ExceptionHelper.createMqttException(32104);
    }

    public void close(boolean z) throws MqttException {
        synchronized (this.conLock) {
            if (!isClosed()) {
                if (!isDisconnected() || z) {
                    this.log.fine(CLASS_NAME, "close", "224");
                    if (isConnecting()) {
                        throw new MqttException(32110);
                    }
                    if (isConnected()) {
                        throw ExceptionHelper.createMqttException(32100);
                    }
                    if (isDisconnecting()) {
                        this.closePending = true;
                        return;
                    }
                }
                this.conState = (byte) 4;
                this.clientState.close();
                this.clientState = null;
                this.callback = null;
                this.persistence = null;
                this.sender = null;
                this.pingSender = null;
                this.receiver = null;
                this.networkModules = null;
                this.conOptions = null;
                this.tokenStore = null;
            }
        }
    }

    public void connect(MqttConnectionOptions mqttConnectionOptions, MqttToken mqttToken) throws MqttException {
        synchronized (this.conLock) {
            if (isDisconnected() && !this.closePending) {
                this.log.fine(CLASS_NAME, "connect", "214");
                this.conState = (byte) 1;
                this.conOptions = mqttConnectionOptions;
                MqttConnect mqttConnect = new MqttConnect(this.client.getClientId(), this.conOptions.getMqttVersion(), this.conOptions.isCleanStart(), this.conOptions.getKeepAliveInterval(), this.conOptions.getConnectionProperties(), this.conOptions.getWillMessageProperties());
                if (this.conOptions.getWillDestination() != null) {
                    mqttConnect.setWillDestination(this.conOptions.getWillDestination());
                }
                if (this.conOptions.getWillMessage() != null) {
                    mqttConnect.setWillMessage(this.conOptions.getWillMessage());
                }
                if (this.conOptions.getUserName() != null) {
                    mqttConnect.setUserName(this.conOptions.getUserName());
                }
                if (this.conOptions.getPassword() != null) {
                    mqttConnect.setPassword(this.conOptions.getPassword());
                }
                this.mqttConnection.setKeepAliveSeconds(this.conOptions.getKeepAliveInterval());
                this.clientState.setCleanStart(this.conOptions.isCleanStart());
                this.tokenStore.open();
                new ConnectBG(this, mqttToken, mqttConnect, this.executorService).start();
            } else {
                this.log.fine(CLASS_NAME, "connect", "207", new Object[]{Byte.valueOf(this.conState)});
                if (isClosed() || this.closePending) {
                    throw new MqttException(32111);
                }
                if (isConnecting()) {
                    throw new MqttException(32110);
                }
                if (isDisconnecting()) {
                    throw new MqttException(32102);
                }
                throw ExceptionHelper.createMqttException(32100);
            }
        }
    }

    public void connectComplete(MqttConnAck mqttConnAck, MqttException mqttException) throws MqttException {
        int returnCode = mqttConnAck.getReturnCode();
        synchronized (this.conLock) {
            if (returnCode == 0) {
                this.log.fine(CLASS_NAME, "connectComplete", "215");
                this.conState = (byte) 0;
            } else {
                this.log.fine(CLASS_NAME, "connectComplete", "204", new Object[]{Integer.valueOf(returnCode)});
                throw mqttException;
            }
        }
    }

    public void shutdownConnection(MqttToken mqttToken, MqttException mqttException, MqttDisconnect mqttDisconnect) {
        CommsCallback commsCallback;
        CommsCallback commsCallback2;
        MqttClientPersistence mqttClientPersistence;
        NetworkModule networkModule;
        synchronized (this.conLock) {
            if (!this.stoppingComms && !this.closePending && !isClosed()) {
                this.stoppingComms = true;
                this.log.fine(CLASS_NAME, "shutdownConnection", "216");
                boolean z = isConnected() || isDisconnecting();
                this.conState = (byte) 2;
                if (mqttToken != null && !mqttToken.isComplete()) {
                    mqttToken.internalTok.setException(mqttException);
                }
                CommsCallback commsCallback3 = this.callback;
                if (commsCallback3 != null) {
                    commsCallback3.stop();
                }
                CommsReceiver commsReceiver = this.receiver;
                if (commsReceiver != null) {
                    commsReceiver.stop();
                }
                try {
                    NetworkModule[] networkModuleArr = this.networkModules;
                    if (networkModuleArr != null && (networkModule = networkModuleArr[this.networkModuleIndex]) != null) {
                        networkModule.stop();
                    }
                } catch (Exception unused) {
                }
                this.tokenStore.quiesce(new MqttException(32102));
                MqttToken mqttTokenHandleOldTokens = handleOldTokens(mqttToken, mqttException);
                try {
                    this.clientState.disconnected(mqttException);
                    if (this.clientState.getCleanStart()) {
                        this.callback.removeMessageListeners();
                    }
                } catch (Exception unused2) {
                }
                CommsSender commsSender = this.sender;
                if (commsSender != null) {
                    commsSender.stop();
                }
                MqttPingSender mqttPingSender = this.pingSender;
                if (mqttPingSender != null) {
                    mqttPingSender.stop();
                }
                try {
                    if (this.disconnectedMessageBuffer == null && (mqttClientPersistence = this.persistence) != null) {
                        mqttClientPersistence.close();
                    }
                } catch (Exception unused3) {
                }
                synchronized (this.conLock) {
                    this.log.fine(CLASS_NAME, "shutdownConnection", "217");
                    this.conState = (byte) 3;
                    this.stoppingComms = false;
                }
                if (mqttTokenHandleOldTokens != null && (commsCallback2 = this.callback) != null) {
                    commsCallback2.asyncOperationComplete(mqttTokenHandleOldTokens);
                }
                if (z && (commsCallback = this.callback) != null) {
                    commsCallback.connectionLost(mqttException, mqttDisconnect);
                }
                synchronized (this.conLock) {
                    if (this.closePending) {
                        try {
                            close(true);
                        } catch (Exception unused4) {
                        }
                    }
                }
            }
        }
    }

    private MqttToken handleOldTokens(MqttToken mqttToken, MqttException mqttException) {
        this.log.fine(CLASS_NAME, "handleOldTokens", "222");
        MqttToken mqttToken2 = null;
        if (mqttToken != null) {
            try {
                if (this.tokenStore.getToken(mqttToken.internalTok.getKey()) == null) {
                    this.tokenStore.saveToken(mqttToken, mqttToken.internalTok.getKey());
                }
            } catch (Exception unused) {
            }
        }
        Enumeration<MqttToken> enumerationElements = this.clientState.resolveOldTokens(mqttException).elements();
        while (enumerationElements.hasMoreElements()) {
            MqttToken mqttTokenNextElement = enumerationElements.nextElement();
            if (mqttTokenNextElement.internalTok.getKey().equals(MqttDisconnect.KEY) || mqttTokenNextElement.internalTok.getKey().equals("Con")) {
                mqttToken2 = mqttTokenNextElement;
            } else {
                this.callback.asyncOperationComplete(mqttTokenNextElement);
            }
        }
        return mqttToken2;
    }

    public void disconnect(MqttDisconnect mqttDisconnect, long j, MqttToken mqttToken) throws MqttException {
        synchronized (this.conLock) {
            if (isClosed()) {
                this.log.fine(CLASS_NAME, "disconnect", "223");
                throw ExceptionHelper.createMqttException(32111);
            }
            if (isDisconnected()) {
                this.log.fine(CLASS_NAME, "disconnect", "211");
                throw ExceptionHelper.createMqttException(32101);
            }
            if (isDisconnecting()) {
                this.log.fine(CLASS_NAME, "disconnect", "219");
                throw ExceptionHelper.createMqttException(32102);
            }
            if (Thread.currentThread() == this.callback.getThread()) {
                this.log.fine(CLASS_NAME, "disconnect", "210");
                throw ExceptionHelper.createMqttException(32107);
            }
            this.log.fine(CLASS_NAME, "disconnect", "218");
            this.conState = (byte) 2;
            new DisconnectBG(mqttDisconnect, j, mqttToken, this.executorService).start();
        }
    }

    public void disconnectForcibly(long j, long j2, int i, MqttProperties mqttProperties) throws MqttException {
        disconnectForcibly(j, j2, true, i, mqttProperties);
    }

    public void disconnectForcibly(long j, long j2, boolean z, int i, MqttProperties mqttProperties) throws MqttException {
        this.conState = (byte) 2;
        ClientState clientState = this.clientState;
        if (clientState != null) {
            clientState.quiesce(j);
        }
        MqttToken mqttToken = new MqttToken(this.client.getClientId());
        if (z) {
            try {
                internalSend(new MqttDisconnect(i, mqttProperties), mqttToken);
                mqttToken.waitForCompletion(j2);
            } catch (Exception unused) {
            } catch (Throwable th) {
                mqttToken.internalTok.markComplete(null, null);
                shutdownConnection(mqttToken, null, null);
                throw th;
            }
        }
        mqttToken.internalTok.markComplete(null, null);
        shutdownConnection(mqttToken, null, null);
    }

    public boolean isConnected() {
        boolean z;
        synchronized (this.conLock) {
            z = this.conState == 0;
        }
        return z;
    }

    public boolean isConnecting() {
        boolean z;
        synchronized (this.conLock) {
            z = true;
            if (this.conState != 1) {
                z = false;
            }
        }
        return z;
    }

    public boolean isDisconnected() {
        boolean z;
        synchronized (this.conLock) {
            z = this.conState == 3;
        }
        return z;
    }

    public boolean isDisconnecting() {
        boolean z;
        synchronized (this.conLock) {
            z = this.conState == 2;
        }
        return z;
    }

    public boolean isClosed() {
        boolean z;
        synchronized (this.conLock) {
            z = this.conState == 4;
        }
        return z;
    }

    public boolean isResting() {
        boolean z;
        synchronized (this.conLock) {
            z = this.resting;
        }
        return z;
    }

    public void setCallback(MqttCallback mqttCallback) {
        this.callback.setCallback(mqttCallback);
    }

    public void setReconnectCallback(MqttCallback mqttCallback) {
        this.callback.setReconnectCallback(mqttCallback);
    }

    public void setManualAcks(boolean z) {
        this.callback.setManualAcks(z);
    }

    public void messageArrivedComplete(int i, int i2) throws MqttException {
        this.callback.messageArrivedComplete(i, i2);
    }

    public void setMessageListener(Integer num, String str, IMqttMessageListener iMqttMessageListener) {
        this.callback.setMessageListener(num, str, iMqttMessageListener);
    }

    public void removeMessageListener(String str) {
        this.callback.removeMessageListener(str);
    }

    protected MqttTopic getTopic(String str) {
        return new MqttTopic(str, this);
    }

    public void setNetworkModuleIndex(int i) {
        this.networkModuleIndex = i;
    }

    public int getNetworkModuleIndex() {
        return this.networkModuleIndex;
    }

    public NetworkModule[] getNetworkModules() {
        return this.networkModules;
    }

    public void setNetworkModules(NetworkModule[] networkModuleArr) {
        this.networkModules = networkModuleArr;
    }

    public MqttToken[] getPendingTokens() {
        return this.tokenStore.getOutstandingDelTokens();
    }

    protected void deliveryComplete(MqttPublish mqttPublish) throws MqttPersistenceException {
        this.clientState.deliveryComplete(mqttPublish);
    }

    protected void deliveryComplete(int i) throws MqttPersistenceException {
        this.clientState.deliveryComplete(i);
    }

    public MqttClientInterface getClient() {
        return this.client;
    }

    public long getKeepAlive() {
        return this.mqttConnection.getKeepAlive();
    }

    public MqttState getClientState() {
        return this.clientState;
    }

    public MqttConnectionOptions getConOptions() {
        return this.conOptions;
    }

    public Properties getDebug() {
        Properties properties = new Properties();
        properties.put("conState", Integer.valueOf(this.conState));
        properties.put("serverURI", getClient().getServerURI());
        properties.put("callback", this.callback);
        properties.put("stoppingComms", Boolean.valueOf(this.stoppingComms));
        return properties;
    }

    private class ConnectBG implements Runnable {
        ClientComms clientComms;
        MqttConnect conPacket;
        MqttToken conToken;
        private String threadName;

        ConnectBG(ClientComms clientComms, MqttToken mqttToken, MqttConnect mqttConnect, ExecutorService executorService) {
            this.clientComms = clientComms;
            this.conToken = mqttToken;
            this.conPacket = mqttConnect;
            this.threadName = "MQTT Con: " + ClientComms.this.getClient().getClientId();
        }

        void start() {
            if (ClientComms.this.executorService != null) {
                ClientComms.this.executorService.execute(this);
            } else {
                new Thread(this).start();
            }
        }

        @Override // java.lang.Runnable
        public void run() {
            MqttException e;
            Thread.currentThread().setName(this.threadName);
            ClientComms.this.log.fine(ClientComms.CLASS_NAME, "connectBG:run", "220");
            try {
                for (MqttToken mqttToken : ClientComms.this.tokenStore.getOutstandingDelTokens()) {
                    mqttToken.internalTok.setException(null);
                }
                ClientComms.this.tokenStore.saveToken(this.conToken, this.conPacket);
                NetworkModule networkModule = ClientComms.this.networkModules[ClientComms.this.networkModuleIndex];
                networkModule.start();
                ClientComms.this.receiver = new CommsReceiver(this.clientComms, ClientComms.this.clientState, ClientComms.this.tokenStore, networkModule.getInputStream());
                ClientComms.this.receiver.start("MQTT Rec: " + ClientComms.this.getClient().getClientId(), ClientComms.this.executorService);
                ClientComms.this.sender = new CommsSender(this.clientComms, ClientComms.this.clientState, ClientComms.this.tokenStore, networkModule.getOutputStream());
                ClientComms.this.sender.start("MQTT Snd: " + ClientComms.this.getClient().getClientId(), ClientComms.this.executorService);
                ClientComms.this.callback.start("MQTT Call: " + ClientComms.this.getClient().getClientId(), ClientComms.this.executorService);
                ClientComms.this.internalSend(this.conPacket, this.conToken);
                e = null;
            } catch (MqttException e2) {
                e = e2;
                ClientComms.this.log.fine(ClientComms.CLASS_NAME, "connectBG:run", "212", null, e);
            } catch (Exception e3) {
                ClientComms.this.log.fine(ClientComms.CLASS_NAME, "connectBG:run", "209", null, e3);
                e = ExceptionHelper.createMqttException(e3);
            }
            if (e != null) {
                ClientComms.this.shutdownConnection(this.conToken, e, null);
            }
        }
    }

    private class DisconnectBG implements Runnable {
        MqttDisconnect disconnect;
        long quiesceTimeout;
        private String threadName;
        MqttToken token;

        DisconnectBG(MqttDisconnect mqttDisconnect, long j, MqttToken mqttToken, ExecutorService executorService) {
            this.disconnect = mqttDisconnect;
            this.quiesceTimeout = j;
            this.token = mqttToken;
        }

        void start() {
            this.threadName = "MQTT Disc: " + ClientComms.this.getClient().getClientId();
            if (ClientComms.this.executorService != null) {
                ClientComms.this.executorService.execute(this);
            } else {
                new Thread(this).start();
            }
        }

        /* JADX WARN: Removed duplicated region for block: B:25:0x00ac  */
        @Override // java.lang.Runnable
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void run() {
            /*
                r4 = this;
                java.lang.Thread r0 = java.lang.Thread.currentThread()
                java.lang.String r1 = r4.threadName
                r0.setName(r1)
                org.eclipse.paho.mqttv5.client.internal.ClientComms r0 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.logging.Logger r0 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$1(r0)
                java.lang.String r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$2()
                java.lang.String r2 = "disconnectBG:run"
                java.lang.String r3 = "221"
                r0.fine(r1, r2, r3)
                org.eclipse.paho.mqttv5.client.internal.ClientComms r0 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.ClientState r0 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$6(r0)
                long r1 = r4.quiesceTimeout
                r0.quiesce(r1)
                r0 = 0
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                org.eclipse.paho.mqttv5.common.packet.MqttDisconnect r2 = r4.disconnect     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                org.eclipse.paho.mqttv5.client.MqttToken r3 = r4.token     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                r1.internalSend(r2, r3)     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                org.eclipse.paho.mqttv5.client.internal.CommsSender r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r1)     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                if (r1 == 0) goto L4a
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                org.eclipse.paho.mqttv5.client.internal.CommsSender r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r1)     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                boolean r1 = r1.isRunning()     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                if (r1 == 0) goto L4a
                org.eclipse.paho.mqttv5.client.MqttToken r1 = r4.token     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                org.eclipse.paho.mqttv5.client.internal.Token r1 = r1.internalTok     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
                r1.waitUntilSent()     // Catch: java.lang.Throwable -> L66 org.eclipse.paho.mqttv5.common.MqttException -> L91
            L4a:
                org.eclipse.paho.mqttv5.client.MqttToken r1 = r4.token
                org.eclipse.paho.mqttv5.client.internal.Token r1 = r1.internalTok
                r1.markComplete(r0, r0)
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.CommsSender r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r1)
                if (r1 == 0) goto Lac
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.CommsSender r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r1)
                boolean r1 = r1.isRunning()
                if (r1 != 0) goto Lb3
                goto Lac
            L66:
                r1 = move-exception
                org.eclipse.paho.mqttv5.client.MqttToken r2 = r4.token
                org.eclipse.paho.mqttv5.client.internal.Token r2 = r2.internalTok
                r2.markComplete(r0, r0)
                org.eclipse.paho.mqttv5.client.internal.ClientComms r2 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.CommsSender r2 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r2)
                if (r2 == 0) goto L82
                org.eclipse.paho.mqttv5.client.internal.ClientComms r2 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.CommsSender r2 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r2)
                boolean r2 = r2.isRunning()
                if (r2 != 0) goto L89
            L82:
                org.eclipse.paho.mqttv5.client.MqttToken r2 = r4.token
                org.eclipse.paho.mqttv5.client.internal.Token r2 = r2.internalTok
                r2.notifyComplete()
            L89:
                org.eclipse.paho.mqttv5.client.internal.ClientComms r2 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.MqttToken r4 = r4.token
                r2.shutdownConnection(r4, r0, r0)
                throw r1
            L91:
                org.eclipse.paho.mqttv5.client.MqttToken r1 = r4.token
                org.eclipse.paho.mqttv5.client.internal.Token r1 = r1.internalTok
                r1.markComplete(r0, r0)
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.CommsSender r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r1)
                if (r1 == 0) goto Lac
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.internal.CommsSender r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.access$10(r1)
                boolean r1 = r1.isRunning()
                if (r1 != 0) goto Lb3
            Lac:
                org.eclipse.paho.mqttv5.client.MqttToken r1 = r4.token
                org.eclipse.paho.mqttv5.client.internal.Token r1 = r1.internalTok
                r1.notifyComplete()
            Lb3:
                org.eclipse.paho.mqttv5.client.internal.ClientComms r1 = org.eclipse.paho.mqttv5.client.internal.ClientComms.this
                org.eclipse.paho.mqttv5.client.MqttToken r4 = r4.token
                r1.shutdownConnection(r4, r0, r0)
                return
            */
            throw new UnsupportedOperationException("Method not decompiled: org.eclipse.paho.mqttv5.client.internal.ClientComms.DisconnectBG.run():void");
        }
    }

    public MqttToken checkForActivity() {
        return checkForActivity(null);
    }

    public MqttToken checkForActivity(MqttActionListener mqttActionListener) {
        try {
            return this.clientState.checkForActivity(mqttActionListener);
        } catch (MqttException e) {
            this.handleRunException(e);
            return null;
        } catch (Exception e2) {
            this.handleRunException(e2);
            return null;
        }
    }

    private void handleRunException(Exception exc) {
        MqttException mqttException;
        this.log.fine(CLASS_NAME, "handleRunException", "804", null, exc);
        if (!(exc instanceof MqttException)) {
            mqttException = new MqttException(32109, exc);
        } else {
            mqttException = (MqttException) exc;
        }
        shutdownConnection(null, mqttException, null);
    }

    public void setRestingState(boolean z) {
        this.resting = z;
    }

    public void setDisconnectedMessageBuffer(DisconnectedMessageBuffer disconnectedMessageBuffer) {
        this.disconnectedMessageBuffer = disconnectedMessageBuffer;
    }

    public int getBufferedMessageCount() {
        return this.disconnectedMessageBuffer.getMessageCount();
    }

    public MqttMessage getBufferedMessage(int i) {
        return ((MqttPublish) this.disconnectedMessageBuffer.getMessage(i).getMessage()).getMessage();
    }

    public void deleteBufferedMessage(int i) {
        this.disconnectedMessageBuffer.deleteMessage(i);
    }

    public void notifyReconnect() {
        if (this.disconnectedMessageBuffer != null) {
            this.log.fine(CLASS_NAME, "notifyReconnect", "509");
            this.disconnectedMessageBuffer.setPublishCallback(new ReconnectDisconnectedBufferCallback("notifyReconnect"));
            ExecutorService executorService = this.executorService;
            if (executorService == null) {
                new Thread(this.disconnectedMessageBuffer).start();
            } else {
                executorService.execute(this.disconnectedMessageBuffer);
            }
        }
    }

    class ReconnectDisconnectedBufferCallback implements IDisconnectedBufferCallback {
        final String methodName;

        ReconnectDisconnectedBufferCallback(String str) {
            this.methodName = str;
        }

        @Override // org.eclipse.paho.mqttv5.client.internal.IDisconnectedBufferCallback
        public void publishBufferedMessage(BufferedMessage bufferedMessage) throws MqttException {
            if (ClientComms.this.isConnected()) {
                ClientComms.this.log.fine(ClientComms.CLASS_NAME, this.methodName, "510", new Object[]{bufferedMessage.getMessage().getKey()});
                ClientComms.this.internalSend(bufferedMessage.getMessage(), bufferedMessage.getToken());
                ClientComms.this.clientState.unPersistBufferedMessage(bufferedMessage.getMessage());
                return;
            }
            ClientComms.this.log.fine(ClientComms.CLASS_NAME, this.methodName, "208");
            throw ExceptionHelper.createMqttException(32104);
        }
    }

    public int getActualInFlight() {
        return this.clientState.getActualInFlight();
    }

    public boolean doesSubscriptionIdentifierExist(int i) {
        return this.callback.doesSubscriptionIdentifierExist(i);
    }
}
