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

import com.seewo.vcommons.constants.SeewoConstants;
import org.eclipse.paho.mqttv5.client.MqttActionListener;
import org.eclipse.paho.mqttv5.client.MqttClientInterface;
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.packet.MqttAck;
import org.eclipse.paho.mqttv5.common.packet.MqttConnAck;
import org.eclipse.paho.mqttv5.common.packet.MqttPubAck;
import org.eclipse.paho.mqttv5.common.packet.MqttPubComp;
import org.eclipse.paho.mqttv5.common.packet.MqttPubRec;
import org.eclipse.paho.mqttv5.common.packet.MqttPubRel;
import org.eclipse.paho.mqttv5.common.packet.MqttSubAck;
import org.eclipse.paho.mqttv5.common.packet.MqttUnsubAck;
import org.eclipse.paho.mqttv5.common.packet.MqttWireMessage;

/* JADX INFO: loaded from: classes2.dex */
public class Token {
    private static final String CLASS_NAME = "org.eclipse.paho.mqttv5.client.internal.Token";
    private String key;
    private Logger log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT, CLASS_NAME);
    private volatile boolean completed = false;
    private boolean pendingComplete = false;
    private boolean sent = false;
    private final Object responseLock = new Object();
    private final Object sentLock = new Object();
    protected MqttMessage message = null;
    private MqttWireMessage response = null;
    private MqttWireMessage request = null;
    private MqttException exception = null;
    private String[] topics = null;
    private MqttClientInterface client = null;
    private MqttActionListener callback = null;
    private Object userContext = null;
    private int messageID = 0;
    private boolean notified = false;
    private int[] reasonCodes = null;
    private boolean deliveryToken = false;

    public Token(String str) {
        this.log.setResourceName(str);
    }

    public int getMessageID() {
        return this.messageID;
    }

    public void setMessageID(int i) {
        this.messageID = i;
    }

    public void setDeliveryToken(boolean z) {
        this.deliveryToken = z;
    }

    public boolean isDeliveryToken() {
        return this.deliveryToken;
    }

    public boolean checkResult() throws MqttException {
        if (getException() == null) {
            return true;
        }
        throw getException();
    }

    public MqttException getException() {
        return this.exception;
    }

    public boolean isComplete() {
        return this.completed;
    }

    protected boolean isCompletePending() {
        return this.pendingComplete;
    }

    protected boolean isInUse() {
        return (getClient() == null || isComplete()) ? false : true;
    }

    public void setActionCallback(MqttActionListener mqttActionListener) {
        this.callback = mqttActionListener;
    }

    public MqttActionListener getActionCallback() {
        return this.callback;
    }

    public void waitForCompletion() throws MqttException {
        waitForCompletion(-1L);
    }

    public void waitForCompletion(long j) throws MqttException {
        MqttWireMessage mqttWireMessageWaitForResponse;
        this.log.fine(CLASS_NAME, "waitForCompletion", "407", new Object[]{getKey(), Long.valueOf(j), this});
        try {
            mqttWireMessageWaitForResponse = waitForResponse(j);
        } catch (MqttException e) {
            if (e.getReasonCode() != 32102) {
                throw e;
            }
            mqttWireMessageWaitForResponse = null;
        }
        if (mqttWireMessageWaitForResponse == null && !this.completed) {
            this.log.fine(CLASS_NAME, "waitForCompletion", "406", new Object[]{getKey(), this});
            MqttException mqttException = new MqttException(32000);
            this.exception = mqttException;
            throw mqttException;
        }
        checkResult();
    }

    protected MqttWireMessage waitForResponse() throws MqttException {
        return waitForResponse(-1L);
    }

    protected MqttWireMessage waitForResponse(long j) throws MqttException {
        synchronized (this.responseLock) {
            Logger logger = this.log;
            String str = CLASS_NAME;
            String key = getKey();
            Long lValueOf = Long.valueOf(j);
            Boolean boolValueOf = Boolean.valueOf(this.sent);
            Boolean boolValueOf2 = Boolean.valueOf(this.completed);
            MqttException mqttException = this.exception;
            logger.fine(str, "waitForResponse", "400", new Object[]{key, lValueOf, boolValueOf, boolValueOf2, mqttException == null ? "false" : SeewoConstants.VALUE_LOCK_TOUCH_OFF, this.response, this}, mqttException);
            while (!this.completed) {
                if (this.exception == null) {
                    try {
                        this.log.fine(CLASS_NAME, "waitForResponse", "408", new Object[]{getKey(), Long.valueOf(j)});
                        if (j <= 0) {
                            this.responseLock.wait();
                        } else {
                            this.responseLock.wait(j);
                        }
                    } catch (InterruptedException e) {
                        this.exception = new MqttException(e);
                    }
                }
                if (!this.completed) {
                    MqttException mqttException2 = this.exception;
                    if (mqttException2 != null) {
                        this.log.fine(CLASS_NAME, "waitForResponse", "401", null, mqttException2);
                        throw this.exception;
                    }
                    if (j > 0) {
                        break;
                    }
                }
            }
        }
        this.log.fine(CLASS_NAME, "waitForResponse", "402", new Object[]{getKey(), this.response});
        return this.response;
    }

    protected void update(MqttWireMessage mqttWireMessage, MqttException mqttException) {
        this.log.fine(CLASS_NAME, "markComplete", "411", new Object[]{getKey(), mqttWireMessage, mqttException});
        synchronized (this.responseLock) {
            if ((mqttWireMessage instanceof MqttPubRec) && mqttWireMessage.getReasonCodes() != null) {
                updateReasonCodes(mqttWireMessage.getReasonCodes());
            }
        }
    }

    protected void updateReasonCodes(int[] iArr) {
        int[] iArr2 = this.reasonCodes;
        if (iArr2 == null) {
            this.reasonCodes = iArr;
            return;
        }
        int[] iArr3 = new int[iArr2.length + iArr.length];
        System.arraycopy(iArr2, 0, iArr3, 0, iArr2.length);
        System.arraycopy(iArr, 0, iArr3, this.reasonCodes.length, iArr.length);
        this.reasonCodes = iArr3;
    }

    protected void markComplete(MqttWireMessage mqttWireMessage, MqttException mqttException) {
        this.log.fine(CLASS_NAME, "markComplete", "404", new Object[]{getKey(), mqttWireMessage, mqttException});
        synchronized (this.responseLock) {
            if (((mqttWireMessage instanceof MqttPubAck) || (mqttWireMessage instanceof MqttPubComp) || (mqttWireMessage instanceof MqttPubRec) || (mqttWireMessage instanceof MqttPubRel) || (mqttWireMessage instanceof MqttSubAck) || (mqttWireMessage instanceof MqttUnsubAck)) && mqttWireMessage.getReasonCodes() != null) {
                updateReasonCodes(mqttWireMessage.getReasonCodes());
            }
            if (mqttWireMessage instanceof MqttAck) {
                this.message = null;
            }
            this.pendingComplete = true;
            this.response = mqttWireMessage;
            this.exception = mqttException;
        }
    }

    protected void notifyComplete() {
        this.log.fine(CLASS_NAME, "notifyComplete", "404", new Object[]{getKey(), this.response, this.exception});
        synchronized (this.responseLock) {
            if (this.exception == null && this.pendingComplete) {
                this.completed = true;
                this.pendingComplete = false;
            } else {
                this.pendingComplete = false;
            }
            this.responseLock.notifyAll();
        }
        synchronized (this.sentLock) {
            this.sent = true;
            this.sentLock.notifyAll();
        }
    }

    public void waitUntilSent() throws MqttException {
        boolean z;
        synchronized (this.sentLock) {
            synchronized (this.responseLock) {
                MqttException mqttException = this.exception;
                if (mqttException != null) {
                    throw mqttException;
                }
            }
            while (true) {
                z = this.sent;
                if (z) {
                    break;
                }
                try {
                    this.log.fine(CLASS_NAME, "waitUntilSent", "409", new Object[]{getKey()});
                    this.sentLock.wait();
                } catch (InterruptedException unused) {
                }
            }
            if (!z) {
                MqttException mqttException2 = this.exception;
                if (mqttException2 == null) {
                    throw ExceptionHelper.createMqttException(6);
                }
                throw mqttException2;
            }
        }
    }

    protected void notifySent() {
        this.log.fine(CLASS_NAME, "notifySent", "403", new Object[]{getKey()});
        synchronized (this.responseLock) {
            this.response = null;
            this.completed = false;
        }
        synchronized (this.sentLock) {
            this.sent = true;
            this.sentLock.notifyAll();
        }
    }

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

    protected void setClient(MqttClientInterface mqttClientInterface) {
        this.client = mqttClientInterface;
    }

    public void reset() throws MqttException {
        if (isInUse()) {
            throw new MqttException(32201);
        }
        this.log.fine(CLASS_NAME, "reset", "410", new Object[]{getKey()});
        this.client = null;
        this.completed = false;
        this.response = null;
        this.sent = false;
        this.exception = null;
        this.userContext = null;
    }

    public MqttMessage getMessage() {
        return this.message;
    }

    public MqttWireMessage getWireMessage() {
        return this.response;
    }

    public void setMessage(MqttMessage mqttMessage) {
        this.message = mqttMessage;
    }

    public String[] getTopics() {
        return this.topics;
    }

    public void setTopics(String[] strArr) {
        this.topics = strArr;
    }

    public Object getUserContext() {
        return this.userContext;
    }

    public void setUserContext(Object obj) {
        this.userContext = obj;
    }

    public void setKey(String str) {
        this.key = str;
    }

    public String getKey() {
        return this.key;
    }

    public void setException(MqttException mqttException) {
        synchronized (this.responseLock) {
            this.exception = mqttException;
        }
    }

    public boolean isNotified() {
        return this.notified;
    }

    public void setNotified(boolean z) {
        this.notified = z;
    }

    public String toString() {
        StringBuffer stringBuffer = new StringBuffer("key=");
        stringBuffer.append(getKey());
        stringBuffer.append(" ,topics=");
        if (getTopics() != null) {
            for (int i = 0; i < getTopics().length; i++) {
                stringBuffer.append(getTopics()[i]).append(", ");
            }
        }
        stringBuffer.append(" ,usercontext=").append(getUserContext());
        stringBuffer.append(" ,isComplete=").append(isComplete());
        stringBuffer.append(" ,isNotified=").append(isNotified());
        stringBuffer.append(" ,exception=").append(getException());
        stringBuffer.append(" ,actioncallback=").append(getActionCallback());
        return stringBuffer.toString();
    }

    public int[] getGrantedQos() {
        int[] iArr = new int[0];
        MqttWireMessage mqttWireMessage = this.response;
        return (!(mqttWireMessage instanceof MqttSubAck) || mqttWireMessage == null) ? iArr : ((MqttSubAck) mqttWireMessage).getReturnCodes();
    }

    public boolean getSessionPresent() {
        MqttWireMessage mqttWireMessage = this.response;
        if (!(mqttWireMessage instanceof MqttConnAck) || mqttWireMessage == null) {
            return false;
        }
        return ((MqttConnAck) mqttWireMessage).getSessionPresent();
    }

    public MqttWireMessage getResponse() {
        return this.response;
    }

    public int[] getReasonCodes() {
        return this.reasonCodes;
    }

    public void setRequestMessage(MqttWireMessage mqttWireMessage) {
        this.request = mqttWireMessage;
    }

    public MqttWireMessage getRequestMessage() {
        return this.request;
    }
}
