package com.lutao.cloud.iot.mqtt.core.client;

import com.lutao.cloud.iot.mqtt.codec.MqttMessageIdVariableHeader;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * mqtt 客户端的消息 id
 */
public enum MqttClientMessageId {
    /**
     * 实例
     */
    INSTANCE(new AtomicInteger(1));

    private final AtomicInteger value;

    MqttClientMessageId(AtomicInteger value) {
        this.value = value;
    }

    public static int getId() {
        return INSTANCE.getMessageId();
    }

    public static MqttMessageIdVariableHeader getVariableHeader() {
        return MqttMessageIdVariableHeader.from(getId());
    }

    public int getMessageId() {
        this.value.compareAndSet(0xffff, 1);
        return this.value.getAndIncrement();
    }

}
