package org.eclipse.paho.mqttv5.client;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.paho.mqttv5.client.internal.ClientComms;
import org.eclipse.paho.mqttv5.client.internal.ConnectActionListener;
import org.eclipse.paho.mqttv5.client.internal.DisconnectedMessageBuffer;
import org.eclipse.paho.mqttv5.client.internal.MqttConnectionState;
import org.eclipse.paho.mqttv5.client.internal.MqttSessionState;
import org.eclipse.paho.mqttv5.client.internal.NetworkModule;
import org.eclipse.paho.mqttv5.client.internal.NetworkModuleService;
import org.eclipse.paho.mqttv5.client.logging.Logger;
import org.eclipse.paho.mqttv5.client.logging.LoggerFactory;
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
import org.eclipse.paho.mqttv5.client.persist.MqttDefaultFilePersistence;
import org.eclipse.paho.mqttv5.client.util.Debug;
import org.eclipse.paho.mqttv5.common.ExceptionHelper;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.MqttSecurityException;
import org.eclipse.paho.mqttv5.common.MqttSubscription;
import org.eclipse.paho.mqttv5.common.packet.MqttAuth;
import org.eclipse.paho.mqttv5.common.packet.MqttDataTypes;
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.MqttSubscribe;
import org.eclipse.paho.mqttv5.common.packet.MqttUnsubscribe;
import org.eclipse.paho.mqttv5.common.util.MqttTopicValidator;

/* JADX INFO: loaded from: classes2.dex */
public class MqttAsyncClient implements MqttClientInterface, IMqttAsyncClient {
    private static final String CLASS_NAME = "org.eclipse.paho.mqttv5.client.MqttAsyncClient";
    private static final long DISCONNECT_TIMEOUT = 10000;
    private static final char MAX_HIGH_SURROGATE = 56319;
    private static final char MIN_HIGH_SURROGATE = 55296;
    private static final long QUIESCE_TIMEOUT = 30000;
    private static final Object clientLock = new Object();
    private static int reconnectDelay = 1000;
    protected ClientComms comms;
    private MqttConnectionOptions connOpts;
    private ScheduledExecutorService executorService;
    private Logger log;
    private MqttCallback mqttCallback;
    private MqttConnectionState mqttConnection;
    private MqttSessionState mqttSession;
    private MqttClientPersistence persistence;
    private MqttPingSender pingSender;
    private Timer reconnectTimer;
    private boolean reconnecting;
    private String serverURI;
    private Hashtable<String, MqttTopic> topics;
    private Object userContext;

    protected static boolean Character_isHighSurrogate(char c) {
        return c >= 55296 && c <= 56319;
    }

    public MqttAsyncClient(String str, String str2) throws MqttException {
        this(str, str2, new MqttDefaultFilePersistence());
    }

    public MqttAsyncClient(String str, String str2, MqttClientPersistence mqttClientPersistence) throws MqttException {
        this(str, str2, mqttClientPersistence, null, null);
    }

    public MqttAsyncClient(String str, String str2, MqttClientPersistence mqttClientPersistence, MqttPingSender mqttPingSender, ScheduledExecutorService scheduledExecutorService) throws MqttException {
        String str3 = CLASS_NAME;
        this.log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT, str3);
        this.reconnecting = false;
        this.mqttSession = new MqttSessionState();
        this.log.setResourceName(str2);
        if (str2 != null) {
            MqttDataTypes.encodeUTF8(new DataOutputStream(new ByteArrayOutputStream()), str2);
            if (r2.size() - 2 > 65535) {
                throw new IllegalArgumentException("ClientId longer than 65535 characters");
            }
        } else {
            str2 = "";
        }
        this.mqttConnection = new MqttConnectionState(str2);
        NetworkModuleService.validateURI(str);
        this.serverURI = str;
        this.mqttSession.setClientId(str2);
        this.persistence = mqttClientPersistence;
        if (mqttClientPersistence == null) {
            this.persistence = new MemoryPersistence();
        }
        this.executorService = scheduledExecutorService;
        this.pingSender = mqttPingSender;
        if (mqttPingSender == null) {
            this.pingSender = new TimerPingSender(this.executorService);
        }
        this.log.fine(str3, "MqttAsyncClient", "101", new Object[]{str2, str, mqttClientPersistence});
        this.persistence.open(str2);
        this.comms = new ClientComms(this, this.persistence, this.pingSender, this.executorService, this.mqttSession, this.mqttConnection);
        this.persistence.close();
        this.topics = new Hashtable<>();
    }

    protected NetworkModule[] createNetworkModules(String str, MqttConnectionOptions mqttConnectionOptions) throws MqttException {
        this.log.fine(CLASS_NAME, "createNetworkModules", "116", new Object[]{str});
        String[] serverURIs = mqttConnectionOptions.getServerURIs();
        if (serverURIs == null || serverURIs.length == 0) {
            serverURIs = new String[]{str};
        }
        NetworkModule[] networkModuleArr = new NetworkModule[serverURIs.length];
        for (int i = 0; i < serverURIs.length; i++) {
            networkModuleArr[i] = createNetworkModule(serverURIs[i], mqttConnectionOptions);
        }
        this.log.fine(CLASS_NAME, "createNetworkModules", "108");
        return networkModuleArr;
    }

    private NetworkModule createNetworkModule(String str, MqttConnectionOptions mqttConnectionOptions) throws MqttException {
        this.log.fine(CLASS_NAME, "createNetworkModule", "115", new Object[]{str});
        return NetworkModuleService.createInstance(str, mqttConnectionOptions, this.mqttSession.getClientId());
    }

    private String getHostName(String str) {
        int iIndexOf = str.indexOf(58);
        if (iIndexOf == -1) {
            iIndexOf = str.indexOf(47);
        }
        if (iIndexOf == -1) {
            iIndexOf = str.length();
        }
        return str.substring(0, iIndexOf);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken connect(Object obj, MqttActionListener mqttActionListener) throws MqttException {
        return connect(new MqttConnectionOptions(), obj, mqttActionListener);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken connect() throws MqttException {
        return connect(null, null);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken connect(MqttConnectionOptions mqttConnectionOptions) throws MqttException {
        return connect(mqttConnectionOptions, null, null);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken connect(MqttConnectionOptions mqttConnectionOptions, Object obj, MqttActionListener mqttActionListener) throws MqttException {
        if (this.comms.isConnected()) {
            throw ExceptionHelper.createMqttException(32100);
        }
        if (this.comms.isConnecting()) {
            throw new MqttException(32110);
        }
        if (this.comms.isDisconnecting()) {
            throw new MqttException(32102);
        }
        if (this.comms.isClosed()) {
            throw new MqttException(32111);
        }
        MqttConnectionOptions mqttConnectionOptions2 = mqttConnectionOptions == null ? new MqttConnectionOptions() : mqttConnectionOptions;
        this.connOpts = mqttConnectionOptions2;
        this.userContext = obj;
        boolean zIsAutomaticReconnect = mqttConnectionOptions2.isAutomaticReconnect();
        this.log.fine(CLASS_NAME, "connect", "103", new Object[]{Boolean.valueOf(mqttConnectionOptions2.isCleanStart()), Integer.valueOf(mqttConnectionOptions2.getConnectionTimeout()), Integer.valueOf(mqttConnectionOptions2.getKeepAliveInterval()), mqttConnectionOptions2.getUserName(), mqttConnectionOptions2.getPassword() == null ? "[null]" : "[notnull]", mqttConnectionOptions2.getWillMessage() == null ? "[null]" : "[notnull]", obj, mqttActionListener});
        this.comms.setNetworkModules(createNetworkModules(this.serverURI, mqttConnectionOptions2));
        this.comms.setReconnectCallback(new MqttReconnectCallback(zIsAutomaticReconnect));
        MqttToken mqttToken = new MqttToken(getClientId());
        ConnectActionListener connectActionListener = new ConnectActionListener(this, this.persistence, this.comms, mqttConnectionOptions2, mqttToken, obj, mqttActionListener, this.reconnecting, this.mqttSession, this.mqttConnection);
        mqttToken.setActionCallback(connectActionListener);
        mqttToken.setUserContext(this);
        this.mqttConnection.setSendReasonMessages(this.connOpts.isSendReasonMessages());
        MqttCallback mqttCallback = this.mqttCallback;
        if (mqttCallback instanceof MqttCallback) {
            connectActionListener.setMqttCallbackExtended(mqttCallback);
        }
        if (this.connOpts.isCleanStart()) {
            this.mqttSession.clearSessionState();
        }
        this.mqttConnection.clearConnectionState();
        this.mqttConnection.setIncomingTopicAliasMax(this.connOpts.getTopicAliasMaximum());
        this.comms.setNetworkModuleIndex(0);
        connectActionListener.connect();
        return mqttToken;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken disconnect(Object obj, MqttActionListener mqttActionListener) throws MqttException {
        return disconnect(QUIESCE_TIMEOUT, obj, mqttActionListener, 0, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken disconnect() throws MqttException {
        return disconnect(null, null);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken disconnect(long j) throws MqttException {
        return disconnect(j, null, null, 0, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken disconnect(long j, Object obj, MqttActionListener mqttActionListener, int i, MqttProperties mqttProperties) throws MqttException {
        this.log.fine(CLASS_NAME, "disconnect", "104", new Object[]{Long.valueOf(j), obj, mqttActionListener});
        MqttToken mqttToken = new MqttToken(getClientId());
        mqttToken.setActionCallback(mqttActionListener);
        mqttToken.setUserContext(obj);
        try {
            this.comms.disconnect(new MqttDisconnect(i, mqttProperties), j, mqttToken);
            Thread.sleep(100L);
        } catch (MqttException e) {
            this.log.fine(CLASS_NAME, "disconnect", "105", null, e);
            throw e;
        } catch (Exception unused) {
        }
        this.log.fine(CLASS_NAME, "disconnect", "108");
        return mqttToken;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void disconnectForcibly() throws MqttException {
        disconnectForcibly(QUIESCE_TIMEOUT, DISCONNECT_TIMEOUT, 0, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void disconnectForcibly(long j) throws MqttException {
        disconnectForcibly(QUIESCE_TIMEOUT, j, 0, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void disconnectForcibly(long j, long j2, int i, MqttProperties mqttProperties) throws MqttException {
        try {
            this.comms.disconnectForcibly(j, j2, i, mqttProperties);
            Thread.sleep(100L);
        } catch (MqttException e) {
            this.log.fine(CLASS_NAME, "disconnectForcibly", "105", null, e);
            throw e;
        } catch (Exception unused) {
        }
        this.log.fine(CLASS_NAME, "disconnectForcibly", "108");
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void disconnectForcibly(long j, long j2, boolean z) throws MqttException {
        this.comms.disconnectForcibly(j, j2, z, 0, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public boolean isConnected() {
        return this.comms.isConnected();
    }

    @Override // org.eclipse.paho.mqttv5.client.MqttClientInterface, org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public String getClientId() {
        return this.mqttSession.getClientId();
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void setClientId(String str) {
        this.mqttSession.setClientId(str);
    }

    @Override // org.eclipse.paho.mqttv5.client.MqttClientInterface, org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public String getServerURI() {
        return this.serverURI;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public String getCurrentServerURI() {
        return this.comms.getNetworkModules()[this.comms.getNetworkModuleIndex()].getServerURI();
    }

    protected MqttTopic getTopic(String str) {
        MqttTopicValidator.validate(str, false, true);
        MqttTopic mqttTopic = this.topics.get(str);
        if (mqttTopic != null) {
            return mqttTopic;
        }
        MqttTopic mqttTopic2 = new MqttTopic(str, this.comms);
        this.topics.put(str, mqttTopic2);
        return mqttTopic2;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken checkPing(Object obj, MqttActionListener mqttActionListener) throws MqttException {
        Logger logger = this.log;
        String str = CLASS_NAME;
        logger.fine(str, "ping", "117");
        MqttToken mqttTokenCheckForActivity = this.comms.checkForActivity(mqttActionListener);
        this.log.fine(str, "ping", "118");
        return mqttTokenCheckForActivity;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(String str, int i, Object obj, MqttActionListener mqttActionListener) throws MqttException {
        return subscribe(new MqttSubscription[]{new MqttSubscription(str, i)}, obj, mqttActionListener, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(String[] strArr, int[] iArr, Object obj, MqttActionListener mqttActionListener) throws MqttException {
        MqttSubscription[] mqttSubscriptionArr = new MqttSubscription[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            mqttSubscriptionArr[i] = new MqttSubscription(strArr[i], iArr[i]);
        }
        return subscribe(mqttSubscriptionArr, obj, mqttActionListener, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(String str, int i) throws MqttException {
        return subscribe(new MqttSubscription[]{new MqttSubscription(str, i)}, (Object) null, (MqttActionListener) null, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(String[] strArr, int[] iArr) throws MqttException {
        return subscribe(strArr, iArr, (Object) null, (MqttActionListener) null);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription mqttSubscription) throws MqttException {
        return subscribe(new MqttSubscription[]{mqttSubscription}, (Object) null, (MqttActionListener) null, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription[] mqttSubscriptionArr) throws MqttException {
        return subscribe(mqttSubscriptionArr, (Object) null, (MqttActionListener) null, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription[] mqttSubscriptionArr, Object obj, MqttActionListener mqttActionListener, MqttProperties mqttProperties) throws MqttException {
        for (MqttSubscription mqttSubscription : mqttSubscriptionArr) {
            this.comms.removeMessageListener(mqttSubscription.getTopic());
            MqttTopicValidator.validate(mqttSubscription.getTopic(), this.mqttConnection.isWildcardSubscriptionsAvailable().booleanValue(), this.mqttConnection.isSharedSubscriptionsAvailable().booleanValue());
        }
        return subscribeBase(mqttSubscriptionArr, obj, mqttActionListener, mqttProperties);
    }

    private IMqttToken subscribeBase(MqttSubscription[] mqttSubscriptionArr, Object obj, MqttActionListener mqttActionListener, MqttProperties mqttProperties) throws MqttException {
        if (this.log.isLoggable(5)) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < mqttSubscriptionArr.length; i++) {
                if (i > 0) {
                    stringBuffer.append(", ");
                }
                stringBuffer.append(mqttSubscriptionArr[i].toString());
            }
            this.log.fine(CLASS_NAME, "subscribe", "106", new Object[]{stringBuffer.toString(), obj, mqttActionListener});
        }
        MqttToken mqttToken = new MqttToken(getClientId());
        mqttToken.setActionCallback(mqttActionListener);
        mqttToken.setUserContext(obj);
        MqttSubscribe mqttSubscribe = new MqttSubscribe(mqttSubscriptionArr, mqttProperties);
        mqttToken.setRequestMessage(mqttSubscribe);
        this.comms.sendNoWait(mqttSubscribe, mqttToken);
        this.log.fine(CLASS_NAME, "subscribe", "109");
        return mqttToken;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription mqttSubscription, Object obj, MqttActionListener mqttActionListener, IMqttMessageListener iMqttMessageListener, MqttProperties mqttProperties) throws MqttException {
        return subscribe(new MqttSubscription[]{mqttSubscription}, obj, mqttActionListener, iMqttMessageListener, mqttProperties);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription mqttSubscription, IMqttMessageListener iMqttMessageListener) throws MqttException {
        return subscribe(new MqttSubscription[]{mqttSubscription}, (Object) null, (MqttActionListener) null, iMqttMessageListener, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription[] mqttSubscriptionArr, IMqttMessageListener iMqttMessageListener) throws MqttException {
        return subscribe(mqttSubscriptionArr, (Object) null, (MqttActionListener) null, iMqttMessageListener, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription[] mqttSubscriptionArr, Object obj, MqttActionListener mqttActionListener, IMqttMessageListener[] iMqttMessageListenerArr, MqttProperties mqttProperties) throws Exception {
        for (int i = 0; i < mqttSubscriptionArr.length; i++) {
            MqttTopicValidator.validate(mqttSubscriptionArr[i].getTopic(), this.mqttConnection.isWildcardSubscriptionsAvailable().booleanValue(), this.mqttConnection.isSharedSubscriptionsAvailable().booleanValue());
            if (iMqttMessageListenerArr == null || iMqttMessageListenerArr[i] == null) {
                this.comms.removeMessageListener(mqttSubscriptionArr[i].getTopic());
            } else {
                this.comms.setMessageListener(null, mqttSubscriptionArr[i].getTopic(), iMqttMessageListenerArr[i]);
            }
        }
        try {
            return subscribeBase(mqttSubscriptionArr, obj, mqttActionListener, mqttProperties);
        } catch (Exception e) {
            for (MqttSubscription mqttSubscription : mqttSubscriptionArr) {
                this.comms.removeMessageListener(mqttSubscription.getTopic());
            }
            throw e;
        }
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken subscribe(MqttSubscription[] mqttSubscriptionArr, Object obj, MqttActionListener mqttActionListener, IMqttMessageListener iMqttMessageListener, MqttProperties mqttProperties) throws Exception {
        int iIntValue = mqttProperties.getSubscriptionIdentifiers().get(0).intValue();
        if (this.connOpts.useSubscriptionIdentifiers() && this.mqttConnection.isSubscriptionIdentifiersAvailable().booleanValue()) {
            if (iIntValue != 0) {
                if (this.comms.doesSubscriptionIdentifierExist(iIntValue)) {
                    throw new IllegalArgumentException(String.format("The Subscription Identifier %s already exists.", Integer.valueOf(iIntValue)));
                }
            } else {
                iIntValue = this.mqttSession.getNextSubscriptionIdentifier().intValue();
            }
        }
        for (MqttSubscription mqttSubscription : mqttSubscriptionArr) {
            MqttTopicValidator.validate(mqttSubscription.getTopic(), this.mqttConnection.isWildcardSubscriptionsAvailable().booleanValue(), this.mqttConnection.isSharedSubscriptionsAvailable().booleanValue());
            if (iMqttMessageListener == null) {
                this.comms.removeMessageListener(mqttSubscription.getTopic());
            } else {
                this.comms.setMessageListener(Integer.valueOf(iIntValue), mqttSubscription.getTopic(), iMqttMessageListener);
            }
        }
        try {
            return subscribeBase(mqttSubscriptionArr, obj, mqttActionListener, mqttProperties);
        } catch (Exception e) {
            for (MqttSubscription mqttSubscription2 : mqttSubscriptionArr) {
                this.comms.removeMessageListener(mqttSubscription2.getTopic());
            }
            throw e;
        }
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken unsubscribe(String str, Object obj, MqttActionListener mqttActionListener) throws MqttException {
        return unsubscribe(new String[]{str}, obj, mqttActionListener, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken unsubscribe(String str) throws MqttException {
        return unsubscribe(new String[]{str}, null, null, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken unsubscribe(String[] strArr) throws MqttException {
        return unsubscribe(strArr, null, null, new MqttProperties());
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken unsubscribe(String[] strArr, Object obj, MqttActionListener mqttActionListener, MqttProperties mqttProperties) throws MqttException {
        if (this.log.isLoggable(5)) {
            String str = "";
            for (int i = 0; i < strArr.length; i++) {
                if (i > 0) {
                    str = String.valueOf(str) + ", ";
                }
                str = String.valueOf(str) + strArr[i];
            }
            this.log.fine(CLASS_NAME, "unsubscribe", "107", new Object[]{str, obj, mqttActionListener});
        }
        for (String str2 : strArr) {
            MqttTopicValidator.validate(str2, true, this.mqttConnection.isSharedSubscriptionsAvailable().booleanValue());
        }
        for (String str3 : strArr) {
            this.comms.removeMessageListener(str3);
        }
        MqttToken mqttToken = new MqttToken(getClientId());
        mqttToken.setActionCallback(mqttActionListener);
        mqttToken.setUserContext(obj);
        mqttToken.internalTok.setTopics(strArr);
        MqttUnsubscribe mqttUnsubscribe = new MqttUnsubscribe(strArr, mqttProperties);
        mqttToken.setRequestMessage(mqttUnsubscribe);
        this.comms.sendNoWait(mqttUnsubscribe, mqttToken);
        this.log.fine(CLASS_NAME, "unsubscribe", "110");
        return mqttToken;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void setCallback(MqttCallback mqttCallback) {
        this.mqttCallback = mqttCallback;
        this.comms.setCallback(mqttCallback);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void setManualAcks(boolean z) {
        this.comms.setManualAcks(z);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void messageArrivedComplete(int i, int i2) throws MqttException {
        this.comms.messageArrivedComplete(i, i2);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken[] getPendingTokens() {
        return this.comms.getPendingTokens();
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken publish(String str, byte[] bArr, int i, boolean z, Object obj, MqttActionListener mqttActionListener) throws MqttException {
        MqttMessage mqttMessage = new MqttMessage(bArr);
        mqttMessage.setProperties(new MqttProperties());
        mqttMessage.setQos(i);
        mqttMessage.setRetained(z);
        return publish(str, mqttMessage, obj, mqttActionListener);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken publish(String str, byte[] bArr, int i, boolean z) throws MqttException {
        return publish(str, bArr, i, z, null, null);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken publish(String str, MqttMessage mqttMessage) throws MqttException {
        return publish(str, mqttMessage, (Object) null, (MqttActionListener) null);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken publish(String str, MqttMessage mqttMessage, Object obj, MqttActionListener mqttActionListener) throws MqttException {
        Logger logger = this.log;
        String str2 = CLASS_NAME;
        logger.fine(str2, "publish", "111", new Object[]{str, obj, mqttActionListener});
        MqttTopicValidator.validate(str, false, true);
        MqttToken mqttToken = new MqttToken(getClientId());
        mqttToken.internalTok.setDeliveryToken(true);
        mqttToken.setActionCallback(mqttActionListener);
        mqttToken.setUserContext(obj);
        mqttToken.setMessage(mqttMessage);
        mqttToken.internalTok.setTopics(new String[]{str});
        MqttPublish mqttPublish = new MqttPublish(str, mqttMessage, mqttMessage.getProperties());
        mqttToken.setRequestMessage(mqttPublish);
        this.comms.sendNoWait(mqttPublish, mqttToken);
        this.log.fine(str2, "publish", "112");
        return mqttToken;
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void reconnect() throws MqttException {
        this.log.fine(CLASS_NAME, "reconnect", "500", new Object[]{this.mqttSession.getClientId()});
        if (this.comms.isConnected()) {
            throw ExceptionHelper.createMqttException(32100);
        }
        if (this.comms.isConnecting()) {
            throw new MqttException(32110);
        }
        if (this.comms.isDisconnecting()) {
            throw new MqttException(32102);
        }
        if (this.comms.isClosed()) {
            throw new MqttException(32111);
        }
        stopReconnectCycle();
        attemptReconnect();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void attemptReconnect() {
        this.log.fine(CLASS_NAME, "attemptReconnect", "500", new Object[]{this.mqttSession.getClientId()});
        try {
            connect(this.connOpts, this.userContext, new MqttReconnectActionListener("attemptReconnect"));
        } catch (MqttSecurityException e) {
            this.log.fine(CLASS_NAME, "attemptReconnect", "804", null, e);
        } catch (MqttException e2) {
            this.log.fine(CLASS_NAME, "attemptReconnect", "804", null, e2);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void startReconnectCycle() {
        this.log.fine(CLASS_NAME, "startReconnectCycle", "503", new Object[]{this.mqttSession.getClientId(), Long.valueOf(reconnectDelay)});
        Timer timer = new Timer("MQTT Reconnect: " + this.mqttSession.getClientId());
        this.reconnectTimer = timer;
        timer.schedule(new ReconnectTask(this, null), reconnectDelay);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void stopReconnectCycle() {
        this.log.fine(CLASS_NAME, "stopReconnectCycle", "504", new Object[]{this.mqttSession.getClientId()});
        synchronized (clientLock) {
            if (this.connOpts.isAutomaticReconnect()) {
                Timer timer = this.reconnectTimer;
                if (timer != null) {
                    timer.cancel();
                    this.reconnectTimer = null;
                }
                reconnectDelay = 1000;
            }
        }
    }

    private class ReconnectTask extends TimerTask {
        private static final String methodName = "ReconnectTask.run";

        private ReconnectTask() {
        }

        /* synthetic */ ReconnectTask(MqttAsyncClient mqttAsyncClient, ReconnectTask reconnectTask) {
            this();
        }

        @Override // java.util.TimerTask, java.lang.Runnable
        public void run() {
            MqttAsyncClient.this.log.fine(MqttAsyncClient.CLASS_NAME, methodName, "506");
            MqttAsyncClient.this.attemptReconnect();
        }
    }

    class MqttReconnectCallback implements MqttCallback {
        final boolean automaticReconnect;

        @Override // org.eclipse.paho.mqttv5.client.MqttCallback
        public void authPacketArrived(int i, MqttProperties mqttProperties) {
        }

        @Override // org.eclipse.paho.mqttv5.client.MqttCallback
        public void connectComplete(boolean z, String str) {
        }

        @Override // org.eclipse.paho.mqttv5.client.MqttCallback
        public void deliveryComplete(IMqttToken iMqttToken) {
        }

        @Override // org.eclipse.paho.mqttv5.client.MqttCallback
        public void messageArrived(String str, MqttMessage mqttMessage) throws Exception {
        }

        @Override // org.eclipse.paho.mqttv5.client.MqttCallback
        public void mqttErrorOccurred(MqttException mqttException) {
        }

        MqttReconnectCallback(boolean z) {
            this.automaticReconnect = z;
        }

        @Override // org.eclipse.paho.mqttv5.client.MqttCallback
        public void disconnected(MqttDisconnectResponse mqttDisconnectResponse) {
            if (this.automaticReconnect) {
                MqttAsyncClient.this.comms.setRestingState(true);
                MqttAsyncClient.this.reconnecting = true;
                MqttAsyncClient.this.startReconnectCycle();
            }
        }
    }

    class MqttReconnectActionListener implements MqttActionListener {
        final String methodName;

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

        @Override // org.eclipse.paho.mqttv5.client.MqttActionListener
        public void onSuccess(IMqttToken iMqttToken) {
            MqttAsyncClient.this.log.fine(MqttAsyncClient.CLASS_NAME, this.methodName, "501", new Object[]{iMqttToken.getClient().getClientId()});
            MqttAsyncClient.this.comms.setRestingState(false);
            MqttAsyncClient.this.stopReconnectCycle();
        }

        @Override // org.eclipse.paho.mqttv5.client.MqttActionListener
        public void onFailure(IMqttToken iMqttToken, Throwable th) {
            MqttAsyncClient.this.log.fine(MqttAsyncClient.CLASS_NAME, this.methodName, "502", new Object[]{iMqttToken.getClient().getClientId()});
            if (MqttAsyncClient.reconnectDelay < MqttAsyncClient.this.connOpts.getMaxReconnectDelay()) {
                MqttAsyncClient.reconnectDelay *= 2;
            }
            rescheduleReconnectCycle(MqttAsyncClient.reconnectDelay);
        }

        private void rescheduleReconnectCycle(int i) {
            MqttAsyncClient.this.log.fine(MqttAsyncClient.CLASS_NAME, String.valueOf(this.methodName) + ":rescheduleReconnectCycle", "505", new Object[]{MqttAsyncClient.this.mqttSession.getClientId(), String.valueOf(MqttAsyncClient.reconnectDelay)});
            synchronized (MqttAsyncClient.clientLock) {
                if (MqttAsyncClient.this.connOpts.isAutomaticReconnect()) {
                    if (MqttAsyncClient.this.reconnectTimer != null) {
                        MqttAsyncClient.this.reconnectTimer.schedule(new ReconnectTask(MqttAsyncClient.this, null), i);
                    } else {
                        MqttAsyncClient.reconnectDelay = i;
                        MqttAsyncClient.this.startReconnectCycle();
                    }
                }
            }
        }
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void setBufferOpts(DisconnectedBufferOptions disconnectedBufferOptions) {
        this.comms.setDisconnectedMessageBuffer(new DisconnectedMessageBuffer(disconnectedBufferOptions));
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public int getBufferedMessageCount() {
        return this.comms.getBufferedMessageCount();
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public MqttMessage getBufferedMessage(int i) {
        return this.comms.getBufferedMessage(i);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void deleteBufferedMessage(int i) {
        this.comms.deleteBufferedMessage(i);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public int getInFlightMessageCount() {
        return this.comms.getActualInFlight();
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void close() throws MqttException {
        close(false);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public void close(boolean z) throws MqttException {
        Logger logger = this.log;
        String str = CLASS_NAME;
        logger.fine(str, "close", "113");
        this.comms.close(z);
        this.log.fine(str, "close", "114");
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public Debug getDebug() {
        return new Debug(this.mqttSession.getClientId(), this.comms);
    }

    @Override // org.eclipse.paho.mqttv5.client.IMqttAsyncClient
    public IMqttToken authenticate(int i, Object obj, MqttProperties mqttProperties) throws MqttException {
        MqttToken mqttToken = new MqttToken(getClientId());
        mqttToken.setUserContext(obj);
        this.comms.sendNoWait(new MqttAuth(i, mqttProperties), mqttToken);
        return null;
    }
}
