3e3419ef创建于 2024年11月4日历史提交

mqtt4cj 库

介绍

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),用极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务

1 工具类

前置条件:NA

场景:

  1. 提供日志等使用工具。 提供7个级别的输出方法,可输出 className、methodName、message、exception 等信息 OFF ERROR WARN INFO DEBUG TRACE ALL

约束:NA

可靠性:NA

1.1 客户端全流程的日志工具

提供一个客户端全流程的日志工具

1.1.1 主要接口
public interface Logger {
    /* mqtt4cj 日志级别 */
    static prop OFF: Int64
    static prop ERROR: Int64
    static prop WARN: Int64
    static prop INFO: Int64
    static prop DEBUG: Int64
    static prop TRACE: Int64
    static prop ALL: Int64

    /* 日志级别属性 */
    mut prop level: LogLevel

    /*
     * 设置日志输出流
     * 参数 output - 输出流
     */
    func setOutput(output: OutputStream): Unit

    /*
     * 对象初始化
     * 参数 messageCatalog - logger 资源
     * 参数 loggerID - 设置生成的 Logger 对象的 ID 名
     * 参数 resourceName - 设置生成的 Logger 对象的资源名
     */
    func initialise(messageCatalog: HashMap<String, String>, loggerID: String, resourceName: String): Unit

    /*
     * 设置资源名
     * 参数 logContext - logger 资源名
     */
    func setResourceName(logContext: String): Unit

    /*
     * 判断该日志级别是否可打印
     * 参数 level - mqtt4cj 日志级别
     * 返回值 Bool - 是否可打印
     */
    func isLoggable(level: Int64): Bool

    /*
     * 打印 off 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func off(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 off 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func off(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 off 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func off(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 error 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func error(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 error 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func error(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 error 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func error(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit


    /*
     * 打印 warn 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func warn(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 warn 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func warn(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 warn 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func warn(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 info 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func info(sourceClass: String, sourceMethod: String, msg: String): Unit


    /*
     * 打印 info 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func info(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 info 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func info(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 debug 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func debug(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 debug 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func debug(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 debug 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func debug(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 trace 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func trace(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 trace 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func trace(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 trace 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func trace(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 all 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    func all(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 all 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    func all(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 all 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    func all(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit
    
    /*
     * 根据传入的 msg,从 logger 资源里获取对应的信息
     * 参数 msg - 传入要查询的 String
     * 参数 inserts - 额外参数
     * 返回值 String - 返回获取到了信息
     */
    func formatMessage(msg: String, inserts: ?Array<ToString>): String
}

public class LoggerFactory {
    /*
     * 生成 Logger 对象
     * 参数 messageCatalogName - logger 资源
     * 参数 loggerID - 设置生成的 Logger 对象的 ID 名
     * 返回值 Logger - 返回 Logger 对象
     */
    public static func getLogger(messageCatalogName: HashMap<String, String>, loggerID: String): Logger
}

public class MqttLogger <: Logger {
    /* 日志级别属性 */
    public mut prop level: LogLevel

    /*
     * 日志等级映射
     * 参数 level - 日志等级数字
     * 返回值 LogLevel - 对应的 LogLevel 日志等级
     */
    public static func mapJULLevel(level: Int64): LogLevel {

    /*
     * 设置日志输出流
     * 参数 output - 输出流
     */
    public func setOutput(output: OutputStream): Unit

    /*
     * 对象初始化
     * 参数 messageCatalog - logger 资源
     * 参数 loggerID - 设置生成的 Logger 对象的 ID 名
     * 参数 resourceName - 设置生成的 Logger 对象的资源名
     */
    public func initialise(messageCatalog: HashMap<String, String>, loggerID: String, resourceName: String): Unit

    /*
     * 设置资源名
     * 参数 logContext - logger 资源名
     */
    public func setResourceName(logContext: String): Unit

    /*
     * 判断该日志级别是否可打印
     * 参数 level - mqtt4cj 日志级别
     * 返回值 Bool - 是否可打印
     */
    public func isLoggable(level: Int64): Bool

    /*
     * 打印 off 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func off(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 off 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func off(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 off 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func off(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 error 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func error(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 error 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func error(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 error 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func error(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit


    /*
     * 打印 warn 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func warn(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 warn 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func warn(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 warn 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func warn(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 info 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func info(sourceClass: String, sourceMethod: String, msg: String): Unit


    /*
     * 打印 info 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func info(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 info 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func info(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 debug 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func debug(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 debug 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func debug(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 debug 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func debug(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 trace 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func trace(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 trace 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func trace(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 trace 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func trace(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印 all 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     */
    public func all(sourceClass: String, sourceMethod: String, msg: String): Unit

    /*
     * 打印 all 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     */
    public func all(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>): Unit

    /*
     * 打印 all 级别日志
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func all(sourceClass: String, sourceMethod: String, msg: String, inserts: ?Array<ToString>, thrown: Exception): Unit

    /*
     * 打印指定级别的日志
     * 参数 level - mqtt4cj 日志级别
     * 参数 sourceClass - 类名
     * 参数 sourceMethod - 方法名
     * 参数 msg - 日志消息的键
     * 参数 inserts - 消息值数组
     * 参数 thrown - 异常
     */
    public func log(level: Int64,sourceClass: String,sourceMethod: String,msg: String,inserts: ?Array<ToString>,thrown: ?Exception): Unit

    /*
     * 根据传入的 msg,从 logger 资源里获取对应的信息
     * 参数 msg - 传入要查询的 String
     * 参数 inserts - 额外参数
     * 返回值 String - 返回获取到了信息
     */
    public func formatMessage(msg: String, inserts: ?Array<ToString>): String
}
1.1.2 示例
import mqtt4cj.mqttv3.*
import std.log.*
import std.unittest.*
import std.unittest.testmacro.*
import std.io.*

main() {
    let ccc = Test_FeatureApi01()
    ccc.execute()
    ccc.printResult()
    0
}
@Test
public class Test_FeatureApi01 {
    @TestCase
    public func testFeatureApi01(): Unit {
        let log = LoggerFactory.getLogger(MQTT_CLIENT_MSG_CAT, "logTest")
        let out: ByteBuffer = ByteBuffer()
        log.setOutput(out)
        log.info("classname", "methodName", "118", None, Exception("ERROR"))
        let res: String = String.fromUtf8(readToEnd(out))
        @Assert(res.contains("classname methodName: <200=internalSend key={0} message={1} token={2}"), true)
        @Assert(res.contains("Exception: ERROR"), true)
    }
}

执行结果如下:

[ PASSED ] CASE: testFeatureApi01

1.2 HighResolutionTimer 高精度时间戳

1.2.1 主要接口
public interface HighResolutionTimer {
    /*
     * 获取高精度时间戳
     * 参数 Duration - 高精度时间戳
     */
    func duration(): Duration
}

1.3 MultiByteInteger

1.3.1 主要接口
// MqttWireMessage 的 readMBI方法返回值
public class MultiByteInteger {
    /*
     * 初始化
     * 参数 value - 消息长度
     * 参数 length - 计数count
     */
    public init(value: Int64, length: Int64)

    /*
     * 获取length
     * 返回值 Int64 - 获取length
     */
    public func getEncodedLength(): Int64

    /*
     * 获取value
     * 返回值 Int64 - 获取value
     */
    public func getValue(): Int64
}

1.4 SyncList

1.4.1 主要接口
// 线程安全的Collection
public class SyncList<T> <: Collection<T> {
    /*
     * 初始化
     * 参数 capacity - 数组初始大小
     */
    public init(capacity: Int64) 

    /*
     * 初始化
     * 参数 elements - 数组初始数据
     */
    public init(elements: Array<T>)

    /*
     * 是否为空
     * 返回值 Bool - 是否为空
     */
    public func isEmpty(): Bool

    /*
     * 获取数据
     * 参数 index - index
     * 返回值 Option<T> - 数据
     */
    public func get(index: Int64): Option<T>

    /*
     * 插入数据
     * 参数 index - index
     * 参数 element - 数据
     */
    public func insert(index: Int64, element: T): Unit

    /*
     * 尾部添加数据
     * 参数 element - 数据
     */
    public func append(element: T): Unit

    /*
     * 删除数据
     * 参数 index - index
     * 返回值 T - 删除的对象
     */
    public func remove(index: Int64): T

    /*
     * 如果存在 删除数据
     * 参数 predicate - 条件
     */
    public func removeIf(predicate: (T) -> Bool): Unit

    /*
     * 清空数据
     */
    public func clear(): Unit

    /*
     * 返回数组的迭代器
     * 返回值 Iterator<T> - 迭代器
     */
    public func iterator(): Iterator<T>

}

2 持久化

前置条件:NA

场景:

  1. 提供数据持久化功能

约束:NA

可靠性:NA

2.1 persistable 可持久化的数据对象

2.1.1 主要接口
public interface MqttPersistable {
    /**
     * 返回数组中的头字节。字节以getHeaderOffset()开始,长度为getHeaderLength()。
     * @return 头字节
     * @throws MqttPersistenceException 如果获取失败
     */
    func getHeaderBytes(): Array<UInt8>

    /**
     * 返回header长度
     * @return header长度
     * @throws MqttPersistenceException 如果获取失败
     */
    func getHeaderLength(): Int64

    /**
     * 返回 getHeaderBytes()返回的字节数组中header起始偏移量
     * @return header的偏移量
     * @throws MqttPersistenceException 如果获取失败
     *
     */
    func getHeaderOffset(): Int64

    /**
     * 返回数组中的有效负载字节。字节以getPayloadOffset()开始,长度为getPayloadLlength()。
     * @return the payload bytes.
     * @throws MqttPersistenceException 如果获取失败
     */
    func getPayloadBytes(): Array<UInt8>

    /**
     * 返回payload长度
     * @return payload长度
     * @throws MqttPersistenceException 如果获取失败
     */
    func getPayloadLength(): Int64

    /**
     * 返回 getPayloadBytes()返回的字节数组中payload起始偏移量
     * @return payload起始offset.
     * @throws MqttPersistenceException 如果获取失败
     */
    func getPayloadOffset(): Int64
}
public class MqttPersistentData <: MqttPersistable {
    /**
     * 构造函数
     * @param key     key
     * @param header  header
     * @param hOffset header中header字节的起始偏移量
     * @param hLength header中header字节长度
     * @param payload payload 若None则空数组
     * @param pOffset payload中payload字节的起始偏移量 若None则0
     * @param pLength payload中payload字节长度 若None则0
     */
    public init(
        key: String,
        header: Array<Byte>,
        hOffset: Int,
        hLength: Int,
        payload: ?Array<Byte>,
        pOffset: ?Int,
        pLength: ?Int
    )
    /**
     * key
     * @return String
     * @throws MqttPersistenceException 如果获取失败
     */
    public func getKey(): String
    /**
     * 返回数组中的头字节。字节以getHeaderOffset()开始,长度为getHeaderLength()。
     * @return 头字节
     * @throws MqttPersistenceException 如果获取失败
     */
    public func getHeaderBytes(): Array<Byte>
    /**
     * 返回header长度
     * @return header长度
     * @throws MqttPersistenceException 如果获取失败
     */
    public func getHeaderLength():Int
    /**
     * 返回 getHeaderBytes()返回的字节数组中header起始偏移量
     * @return header的偏移量
     * @throws MqttPersistenceException 如果获取失败
     *
     */
    public func getHeaderOffset():Int
    /**
     * 返回数组中的有效负载字节。字节以getPayloadOffset()开始,长度为getPayloadLlength()。
     * @return the payload bytes.
     * @throws MqttPersistenceException 如果获取失败
     */
    public func getPayloadBytes(): Array<Byte>
    /**
     * 返回payload长度
     * @return payload长度
     * @throws MqttPersistenceException 如果获取失败
     */
    public func getPayloadLength():Int
    /**
     * 返回 getPayloadBytes()返回的字节数组中payload起始偏移量
     * @return payload起始offset.
     * @throws MqttPersistenceException 如果获取失败
     */
    public func getPayloadOffset():Int
}

2.2 persistence 持久化策略(内存/文件)

2.2.1 主要接口
/**
   持久化策略
 */
public interface MqttClientPersistence <: Resource {
    /**
     * 初始化持久存储
     * @param clientId clientId
     * @param serverURI serverURI
     * @throws MqttPersistenceException 若open失败
     */
    func open(clientId: String, serverURI: String): Unit

    /**
     * 关闭
     * @throws MqttPersistenceException 若close失败
     */
    func close(): Unit

    /**
     * 将指定的数据放入持久存储区
     * @param key 数据的键,稍后将使用它来检索它
     * @param persistable 要保存的数据
     * @throws MqttPersistenceException 若保存失败
     */
    func put(key: String, persistable: MqttPersistable): Unit

    /**
     * 获取数据
     * @param key 数据的键,这是在最初保存数据时使用的
     * @return 数据
     * @throws MqttPersistenceException 若获取失败
     */
    func get(key: String): MqttPersistable

    /**
     * 删除数据
     * @param key 数据的键
     * @throws MqttPersistenceException 若删除失败
     */
    func remove(key: String): Unit

    /**
     * 返回数据键迭代器
     * @return 数据键迭代器
     * @throws MqttPersistenceException 若获取失败
     */
    func keys(): Iterator<String>

    /**
     * 清除所有数据
     * @throws MqttPersistenceException 若清除失败
     */
    func clear(): Unit

    /**
     * 检查是否存在数据键
     * @param key 数据键
     * @return True 若key存在
     * @throws MqttPersistenceException 若检查失败
     */
    func containsKey(key: String): Bool
}

/**
 * 使用内存持久化
 * 在不需要跨客户端或设备重新启动内存的情况下,可以使用这种内存持久性。在需要可靠性的情况下,比如将 clean 会话设置为 false,那么应该使用非易失性形式的持久性
 * API参考MqttClientPersistence
 */
public class MemoryPersistence <: MqttClientPersistence {
    public func close(): Unit
    /**
     * Resource是否关闭
     * @return true:已关闭 false:未关闭
     */
    public func isClosed(): Bool
    public func keys(): Iterator<String>
    public func get(key: String): MqttPersistable
    public func open(clientId: String, serverURI: String): Unit
    public func put(key: String, persistable: MqttPersistable): Unit
    public func remove(key: String): Unit
    public func clear(): Unit
    public func containsKey(key: String): Bool
}

/**
 * 使用文件持久化
 * 创建持久性对象时指定一个目录。
 * 然后打开持久性(参见 open (String,String))时,将在此客户端 ID 和连接键的基底下创建一个子目录。这允许多个客户端共享一个持久性基目录。
 * API参考MqttClientPersistence
 */
public class MqttDefaultFilePersistence <: MqttClientPersistence {
    public init()
    /*
     * 在指定的目录中创建基于文件的持久性数据存储区。
     * @param directory 持久化目录
     */
    public init(directory: String)
    public func open(clientId: String, theConnection: String): Unit
    public func close(): Unit
    /**
     * Resource是否关闭
     * @return true:已关闭 false:未关闭
     */
    public func isClosed(): Bool
    /**
     * 将指定的持久性数据写入之前指定的持久性目录。此方法使用安全的覆盖策略来确保 IO 错误不会丢失消息。
     * @param message 数据
     * @throws MqttPersistenceException 若持久化失败
     */
    public func put(key: String, message: MqttPersistable): Unit
    public func get(key: String): MqttPersistable
    /**
     * 从先前指定的持久性目录中删除具有指定键的数据
     * @param key 数据键
     * @throws MqttPersistenceException 若操作失败
     */
    public func remove(key: String): Unit
    /**
     * 返回以前指定的持久性目录中的所有持久性数据
     * @return 所有数据键
     * @throws MqttPersistenceException 若操作失败
     */
    public func keys(): Iterator<String>
    public func containsKey(key: String): Bool
    public func clear(): Unit
}

3 实体类 描述用于传输的报文信息

前置条件:NA

场景:

  1. 提供消息,报文等实体类 继承关系 消息 -> 报文 -> 字节 -> socket

约束:NA

可靠性:NA

3.1 MqttWireMessage

3.1.1 主要接口
public abstract class MqttWireMessage {
    /*
     * 消息基类 对象初始化
     * 参数 typeT - 消息类别
     */
    public init(typeT: UInt8)

    /*
     * 获取负载body
     * 返回值 Array<UInt8> - 负载body
     */
    public open func getPayload(): Array<UInt8>

    /*
     * 获取消息类别
     * 返回值 UInt8 - 消息类别
     */
    public func getType(): UInt8

    /*
     * 获取mqtt message ID
     * 返回值 Int64 - message ID
     */
    public func getMessageId(): Int64 

    /*
     * 设置 mqtt message ID
     * 参数 msgId - message ID
     */
    public func setMessageId(msgId: Int64): Unit

    /*
     * 获取message key
     * 返回值 String - key
     */
    public open func getKey(): String

    /*
     * 获取消息头 信息
     * 返回值 Array<UInt8>  - 头信息
     */
    public func getHeader(): Array<UInt8>

    /*
     * 设置信息id是否必须, 对于该类,此接口始终返回true
     * 返回值 Bool - 是否必须, 始终返回true
     */
    public open func isMessageIdRequired(): Bool

    /*
     * 创建MqttWireMessage具体子类的静态方法
     * 参数 data - MqttPersistable对象
     * 返回值 MqttWireMessage - 具体子类
     */
    public static func createWireMessage(data: MqttPersistable): MqttWireMessage

    /*
     * 创建MqttWireMessage具体子类的静态方法
     * 参数 bytes - byte数组
     * 返回值 MqttWireMessage - 具体子类
     */
    public static func createWireMessage(bytes: Array<UInt8>): MqttWireMessage

    /*
     * 对number进行加密成数组
     * 参数 number - 待加密的Int64值
     * 返回值 Array<UInt8> - 加密后的UInt8数组
     */
    public static func encodeMBI(number: Int64): Array<UInt8> 

    /*
     * 对流中的信息进行解密
     * 参数 inputStream - 待解密的流
     * 返回值 MultiByteInteger - 解密后存入的MultiByteInteger对象
     */
    public static func readMBI(inputStream: InputStream): MultiByteInteger

    /*
     * 设置是否重复
     * 参数 duplicate - 是否重复
     */
    public func setDuplicate(duplicate: Bool): Unit

    /*
     * 对stringToEncode进行加密成数组
     * 参数 outStream - 加密后的String写入到输出流中
     * 参数 stringToEncode - 待加密的String
     */
    public static func encodeUTF8(outStream: ByteBuffer, stringToEncode: String)

    /*
     * 对流中的信息进行解密
     * 参数 input - 待解密的流
     * 返回值 String - 解密后存入的String对象
     */
    public static func decodeUTF8(input: ByteBuffer): String

    /*
     * 校验Int64是否在 0-268435455之间
     * 参数 value - 待校验的值
     */
    public static func validateVariableByteInt(value: Int64): Unit

    /*
     * 获取token
     * 返回值 MqttToken - 获取token
     */
    public func getToken(): MqttToken

    /*
     * 设置token
     * 参数 token - token
     */
    public func setToken(token: MqttToken): Unit 

    /*
     * toString方法
     * 返回值 String - 返回MqttWireMessage的子类名称
     */
    public open func toString(): String
}

public class MqttConnect <: MqttWireMessage {
    /*
     * 对象初始化 客户端连接服务端
     * 参数 data - MqttConnect 报文初始化
     */
    public init(data: Array<UInt8>)

    /*
     * 对象初始化
     * 参数 clientId - 客户端id
     * 参数 mqttVersion - 版本
     * 参数 cleanSession - 是否清空会话
     * 参数 keepAliveInterval - 保存时长, 范围应在0-65535之间,超过范围可能会有未知错误
     * 参数 userName - MqttConnect 用户名
     * 参数 password - MqttConnect 密码
     * 参数 willMessage - MqttConnect 消息
     * 参数 willDestination - 目的地
     */
    public init(
        clientId: String,
        mqttVersion: Int64,
        cleanSession: Bool,
        keepAliveInterval: Int64,
        userName: String,
        password: ?Array<UInt8>,
        willMessage: MqttMessage,
        willDestination: String
    )

    /*
     * toString
     * 返回值 String - toString
     */
    public func toString(): String

    /*
     * 是否清空会话
     * 返回值 Bool - 是否清空会话
     */
    public func isCleanSession(): Bool

    /*
     * 获取载荷信息
     * 返回值 Array<UInt8> - 载荷信息
     */
    public func getPayload(): Array<UInt8>

    /*
     * 信息id是否必须, 对于该类,此接口始终返回false
     * 返回值 Bool - 信息id是否必须, 始终返回false
     */
    public func isMessageIdRequired(): Bool

    /*
     * 获取key - "Con"
     * 返回值 String - key
     */
    public func getKey(): String
}

public class MqttUnsubscribe <: MqttWireMessage {
    /*
     * 对象初始化 客户端取消订阅请求
     * 参数 names - String数组
     */
    public init(names: Array<String>)

    /*
     * 对象初始化 客户端取消订阅请求
     * 参数 data - UInt8数组
     */
    public init(data: Array<UInt8>)

    /*
     * toString
     * 返回值 String - toString
     */
    public func toString(): String

    /*
     * 获取载荷信息
     * 返回值 Array<UInt8> - 载荷信息
     */
    public func getPayload(): Array<UInt8>
}

public class MqttPingReq <: MqttWireMessage {
    /*
     * 对象初始化 心跳请求
     */
    public init() 

    /*
     * 设置信息id是否必须, 对于该类,此接口始终返回false
     * 返回值 Bool - 是否必须, 始终返回false
     */
    public func isMessageIdRequired(): Bool 

    /*
     * 获取key - "Ping"
     * 返回值 String - key
     */
    public func getKey(): String
}

public class MqttSubscribe <: MqttWireMessage {
    /*
     * 对象初始化 客户端订阅请求
     * 参数 data - UInt8数组
     */
    public init(data: Array<UInt8>)

    /*
     * 对象初始化 客户端订阅请求
     * 参数 names - String数组
     * 参数 qos - Int64数组
     */
    public init(names: Array<String>, qos: Array<Int64>)

    /*
     * toString
     * 返回值 String - toString
     */
    public func toString(): String

    /*
     * 获取载荷信息
     * 返回值 Array<UInt8> - 载荷信息
     */
    public func getPayload(): Array<UInt8>
}

public class MqttDisconnect <: MqttWireMessage {
    /*
     * 对象初始化 客户端断开连接
     */
    public init() 

    /*
     * 设置信息id是否必须, 对于该类,此接口始终返回false
     * 返回值 Bool - 是否必须, 始终返回false
     */
    public func isMessageIdRequired(): Bool 

    /*
     * 获取key - "Disc"
     * 返回值 String - key
     */
    public func getKey(): String
}

3.2 MqttPersistableWireMessage

3.2.1 主要接口

public abstract class MqttPersistableWireMessage <: MqttWireMessage & MqttPersistable {
    /*
     * 对象初始化
     * 参数 typeT message类型
     */
    public init(typeT: UInt8)

    /*
     * 获取头信息
     * 返回值 Array<UInt8> - 头信息
     */
    public func getHeaderBytes(): Array<UInt8>

    /*
     * 获取头信息的长度
     * 返回值 Int64 - 头信息的长度
     */
    public func getHeaderLength(): Int64

    /*
     * 获取头信息的偏移值
     * 返回值 Int64 - 头信息的偏移值
     */
    public func getHeaderOffset(): Int64

    /*
     * 获取负载信息
     * 返回值 Array<UInt8> - 负载信息
     */
    public func getPayloadBytes(): Array<UInt8>

    /*
     * 获取负载信息的长度
     * 返回值 Int64 - 负载信息的长度
     */
    public func getPayloadLength(): Int64

    /*
     * 获取负载信息的偏移值
     * 返回值 Int64 - 负载信息的偏移值
     */
    public func getPayloadOffset(): Int64
}

public class MqttPublish <: MqttPersistableWireMessage {
    /*
     * 对象初始化 发布消息
     * 参数 name 消息名称
     * 参数 message 消息内容
     */
    public init(name: String, message: MqttMessage)

    /*
     * 对象初始化 发布消息
     * 参数 info 消息名称-UInt8
     * 参数 data  Array<UInt8>-消息内容
     */
    public init(info: UInt8, data: Array<UInt8>)

    /*
     * toString
     * 返回值 String - toString
     */
    public open func toString(): String

    /*
     * 获取话题名称
     * 返回值 String - 话题名称
     */
    public func getTopicName(): String

    /*
     * 获取消息正文
     * 返回值 MqttMessage - 消息正文
     */
    public func getMessage(): MqttMessage

    /*
     * 获取负载body
     * 返回值 Array<UInt8> - 负载body
     */
    public func getPayload(): Array<UInt8>

    /*
     * 获取负载信息的长度
     * 返回值 Int64 - 负载信息的长度
     */
    public func getPayloadLength(): Int64

    /*
     * 设置 mqtt message ID
     * 参数 msgId - message ID
     */
    public func setMessageId(msgId: Int64): Unit

    /*
     * 设置信息id是否必须, 对于该类,此接口始终返回true
     * 返回值 Bool - 是否必须, 始终返回true
     */
    public func isMessageIdRequired(): Bool
}

public class MqttPubRel <: MqttPersistableWireMessage {
    /*
     * 对象初始化 发布收到
     * 参数 pubRec MqttPubRec对象
     */
    public init(pubRec: MqttPubRec)

    /*
     * 对象初始化 发布收到
     * 参数 data  Array<UInt8>-消息内容
     */
    public init(data: Array<UInt8>)

    /*
     * toString
     * 返回值 String - toString
     */
    public open func toString(): String 

}

3.3 MqttAck

3.3.1 主要接口
public abstract class MqttAck <: MqttWireMessage {
    /*
     * 对象初始化
     * 参数 ackType - 确认字符
     */
    public init(ackType: UInt8)

    /*
     * toString
     * 返回值 String - toString
     */
    public open func toString(): String 
}

public class MqttConnack <: MqttAck {

    /*
     * 对象初始化
     * 参数 variableHeader - CONNACK报文初始化
     */
    public init(variableHeader: Array<UInt8>)

    /*
     * 获取返回值
     * 返回值 Int64 - 返回值
     */
    public func getReturnCode(): Int64

    /*
     * 设置信息id是否必须, 对于该类,此接口始终返回false
     * 返回值 Bool - 是否必须, 始终返回false
     */
    public func isMessageIdRequired(): Bool

    /*
     * 获取key
     * 返回值 String - key
     */
    public func getKey(): String

    /*
     * toString
     * 返回值 String - toString
     */
    public open func toString(): String 

    /*
     * 获取会话
     * 返回值 Bool - 会话
     */
    public func getSessionPresent(): Bool 
}

public class MqttPingResp <: MqttAck {
    /*
     * 对象初始化
     */
    public init()

    /*
     * 设置信息id是否必须, 对于该类,此接口始终返回false
     * 返回值 Bool - 是否必须, 始终返回false
     */
    public func isMessageIdRequired(): Bool 

    /*
     * 获取key
     * 返回值 String - key
     */
    public func getKey(): String
}

public class MqttPubAck <: MqttAck {
    /*
     * 对象初始化
     * 参数 data - MqttPubAck 报文初始化
     */
    public init(data: Array<UInt8>)

    /*
     * 对象初始化
     * 参数 publish - MqttPubAck 报文初始化
     */
    public init(publish: MqttPublish)

    /*
     * 对象初始化
     * 参数 messageId - MqttPubAck 报文初始化
     */
    public init(messageId: Int64)
}

public class MqttPubComp <: MqttAck {
    /*
     * 对象初始化
     * 参数 data - MqttPubComp 报文初始化
     */
    public init(data: Array<UInt8>)

    /*
     * 对象初始化
     * 参数 publish - MqttPubComp 报文初始化
     */
    public init(publish: MqttPublish)

    /*
     * 对象初始化
     * 参数 messageId - MqttPubComp 报文初始化
     */
    public init(messageId: Int64)
}

public class MqttPubRec <: MqttAck {
    /*
     * 对象初始化
     * 参数 data - MqttPubRec 报文初始化
     */
    public init(data: Array<UInt8>)

    /*
     * 对象初始化
     * 参数 publish - MqttPubRec 报文初始化
     */
    public init(publish: MqttPublish)

}

public class MqttSuback <: MqttAck {
    /*
     * 对象初始化
     * 参数 data - MqttSuback 报文初始化
     */
    public init(data: Array<UInt8>)

    /*
     * toString
     * 返回值 String - toString
     */
    public open func toString(): String 

    /*
     * 获取Qos等级
     * 返回值 Array<Int64> - Qos等级
     */
    public func getGrantedQos(): Array<Int64> 
}

public class MqttUnsubAck <: MqttAck {
    /*
     * 对象初始化
     * 参数 data - MqttUnsubAck 报文初始化
     */
    public init(data: Array<UInt8>)
}

3.4 MqttMessage 消息

3.4.1 主要接口
/**
 * MQTT 消息,包含负载和指定如何传递消息的选项。负载为字节数组
 */
public open class MqttMessage <: ToString {
    /**
     * 构造函数
     */
    public init()

    /**
     * 构造函数
     * @param   payload - 消息负载
     */
    public init(payload: Array<UInt8>)

    /**
     * 验证qos值
     * @param qos  
     * @throw IllegalArgumentException 若qos非0/1/2
     */
    public static func validateQos(qos: Int64): Unit

    /**
     * 获取负载
     * @return 负载
     */
    public func getPayload(): Array<UInt8>

    /**
     * 清空负载
     * @throw IllegalStateException 若不可更改
     */
    public func clearPayload(): Unit

    /**
     * 设置负载
     * @param payload 负载
     * @throw IllegalStateException 若不可更改
     */
    public func setPayload(payload: Array<UInt8>):Unit

    /**
     * 此消息是否应由服务器保留
     * @return true:应由服务器保留 false:否
     */
    public func isRetained(): Bool

    /**
     * 设置此消息是否应由服务器保留
     * @param retained true:应由服务器保留 false:否
     * @throw IllegalStateException 若不可更改
     */
    public func setRetained(retained: Bool):Unit

    /**
     * 返回此消息的服务质量
     * @return 0/1/2
     */
    public func getQos(): Int64

    /**
     * 设置此消息的服务质量
     * @param  qos 
        0-消息最多只能传递一次(零次或一次)
        1-消息应至少传递一次(一次或多次) 默认
        2-消息应该传递一次。
     * @throw IllegalStateException 若不可更改
     */
    public func setQos(qos: Int64): Unit

    /**
     * toString
     * @return toString
     */
    public func toString(): String

    /**
     * 返回此消息是否可能是已接收消息的副本。这将只在从服务器接收到的消息上设置。
     * @return true若此消息可能为副本
     */
    public func isDuplicate(): Bool

    /**
     * 设置消息id
     * @param messageId
     */
    public func setId(messageId: Int64): Unit

    /**
     * 获取消息id
     * @return messageid
     */
    public func getId(): Int64
}
/**
 * 从服务端接收的消息
 */
public class MqttReceivedMessage <: MqttMessage {
    /**
     * 设置消息id
     * 参数 msgId - 消息id
     */
    public func setMessageId(msgId: Int64): Unit

    /**
     * 获取消息id
     * 返回值 Int64 - 消息id
     */
    public func getMessageId(): Int64

    /**
     * 设置是否重复的标志位
     * 参数 value - 是否重复的标志位
     */
    public func setDuplicate(value: Bool): Unit
}

3.5 MqttTopic 主题

3.4.1 主要接口
public class MqttTopic {
    /**
     * 构造MqttTopic主题对象
     * 参数 name - 主题名称
     * 参数 comms - ClientComms对象
     */
    public init(name: String, comms: ClientComms)

    /**
     * 报文发布
     * 参数 payload - 报文信息数组
     * 参数 qos - 要发布消息的服务质量(0、1或2)
     * 参数 retained - 消息是否应该被保留
     * 返回值 MqttDeliveryToken - 跟踪消息传递状态的MqttDeliveryToken类
     */
    public func publish(payload: Array<UInt8>, qos: Int64, retained: Bool): MqttDeliveryToken

    /**
     * 报文发布
     * 参数 message - 报文信息对象MqttMessage
     * 返回值 MqttDeliveryToken - 跟踪消息传递状态的MqttDeliveryToken类
     */
    public func publish(message: MqttMessage): MqttDeliveryToken

    /**
     * 获取主题名称
     * 返回值 String - 主题名称
     */
    public func getName(): String

    /**
     * toString方法
     * 返回值 String - 主题名称
     */
    public func toString(): String

    /**
     * 校验主题名称
     * 参数 topicString - 待校验的主题名称
     * 参数 wildcardAllowed - 是否允许通配符
     */
    public static func validate(topicString: String, wildcardAllowed: Bool): Unit

    /**
     * 是否匹配
     * 参数 topicFilter - 匹配规则
     * 参数 topicName - 待校验的主题名称
     * 返回值 Bool - 是否匹配成功
     */
    public static func isMatched(topicFilter: String, topicName: String): Bool
}

4 连接选项

前置条件:NA

场景:

  1. 连接选项(身份验证/服务器/协议版本/连接选项/SocketFactory/TLS/LWT)

约束:NA

可靠性:NA

4.1 连接选项 ConnectOption

4.1.1 主要接口
/**
 * MQTT 消息,包含负载和指定如何传递消息的选项。负载为字节数组
 */
public class MqttConnectOptions <: ToString {
    /*
     * 默认构造
     */
    public init()

	/*
     * 获取连接密码
     * 返回值 ?Array<Rune> - Option Rune 数组,初始为空
     */
    public func getPassword(): ?Array<Rune>

	/*
     * 设置连接密码
     * 参数 ?Array<Rune> - 要设置的密码
     */
    public func setPassword(userpass: ?Array<Rune>): Unit

	/*
     * 获取连接用户名
     * 返回值 ?String - Option String 字符串,初始为空
     */
    public func getUserName(): ?String

    /*
     * 设置连接用户名
     * 参数 ?String - 要设置的用户名
     */
    public func setUserName(userName: ?String): Unit

    /*
     * 获取重连等待时间
     * 返回值 Duration - 等待时间
     */
    public func getMaxReconnectDelay(): Duration

    /*
     * 设置重连等待时间
     * 参数 maxReconnectDelay - 要设置的 Duration
     */
    public func setMaxReconnectDelay(maxReconnectDelay: Duration): Unit

    /*
     * 设置 LWT(Last Will and Testament) 遗嘱消息
     * 参数 topic - 要发布消息的主题
     * 参数 payload - 消息的字节载荷
     * 参数 qos - 要发布消息的服务质量(0、1或2)
     * 参数 retained - 消息是否应该被保留
     */
    public func setWill(topic: String, payload: Array<Byte>, qos: Int64, retained: Bool): Unit

    /*
     * 设置 LWT(Last Will and Testament) 遗嘱消息
     * 参数 topic - 要发布消息的主题 MqttTopic 对象
     * 参数 payload - 消息的字节载荷
     * 参数 qos - 要发布消息的服务质量(0、1或2)
     * 参数 retained - 消息是否应该被保留
     */
    public func setWill(topic: MqttTopic, payload: Array<Byte>, qos: Int64, retained: Bool): Unit

    /*
     * 获取 kreepalive 时间
     * 返回值 Duration - 时间
     */
    public func getKeepAliveInterval(): Duration

    /*
     * 设置 kreepalive 时间
     * 参数 keepAliveInterval - 要设置的时间
     */
    public func setKeepAliveInterval(keepAliveInterval: Duration): Unit

    /*
     * 获取 mqtt 版本
     * 返回值 Int64 - mqtt 版本号
     */
    public func getMqttVersion(): Int64

    /*
     * 设置 mqtt 版本,支持三个版本号参数值,为类静态常量值,分别为:MQTT_VERSION_DEFAULT,MQTT_VERSION_3_1,MQTT_VERSION_3_1_1
     * 不设置为上述常量值则抛IllegalArgumentException
     * 参数 mqttVersion - 要设置的 mqtt 版本号
     */
    public func setMqttVersion(mqttVersion: Int64): Unit

    /*
     * 获取最大传输中数,表示允许同时在传输中的最大消息数量
     * 返回值 Int64 - 数量
     */
    public func getMaxInflight(): Int64

    /*
     * 设置最大传输中数
     * 参数 maxInflight - 数量,小于0则抛 IllegalArgumentException 异常
     */
    public func setMaxInflight(maxInflight: Int64): Unit

    /*
     * 获取连接超时时间
     * 返回值 Duration - 时间
     */
    public func getConnectionTimeout(): Duration

    /*
     * 设置连接超时时间
     * 参数 connectionTimeout - 时间
     */
    public func setConnectionTimeout(connectionTimeout: Duration): Unit

    /*
     * 获取返回在连接时将被使用的 SocketFactory ,如果未设置则返回 None
     * 返回值 ?SocketFactory - ?SocketFactory
     */
    public func getSocketFactory(): ?SocketFactory

    /*
     * 设置返回在连接时将被使用的 SocketFactory
     * 参数 ?SocketFactory - ?SocketFactory
     */
    public func setSocketFactory(socketFactory: ?SocketFactory): Unit

    /*
     * 获取 TlsClientConfig 配置 ,如果未设置则返回 None
     * 返回值 ?TlsClientConfig - ?TlsClientConfig
     */
    public func getTlsClientConfig(): ?TlsClientConfig

    /*
     * 设置 TlsClientConfig 配置
     * 参数 ?TlsClientConfig - ?TlsClientConfig
     */
    public func setTlsClientConfig(tlsClientConfig: ?TlsClientConfig): Unit

    /*
     * 获取用于遗嘱(Last Will and Testament,LWT)的主题
     * 返回值 ?String - 字符串,如果未设置 LWT,则返回 None。
     */
    public func getWillDestination(): ?String

    /*
     * 获取作为遗嘱(Last Will and Testament,LWT)发送的消息
     * 返回值 ?MqttMessage - MqttMessage,如果未设置 LWT,则返回 None。
     */
    public func getWillMessage(): ?MqttMessage

    /*
     * 返回 CleanSession 标志状态
     * 返回值 Bool - 状态,默认为 true
     */
    public func isCleanSession(): Bool

    /*
     * 设置 CleanSession 标志状态
     * 参数 cleanSession - 状态
     */
    public func setCleanSession(cleanSession: Bool): Unit

    /*
     * 返回客户端可以连接的服务器 URI 列表
     * 返回值 ?Array<String> - 服务器 URI 列表,如果未设置,则返回 None
     */
    public func getServerURIs(): ?Array<String>

    /*
     * 设置客户端可以连接的服务器 URI 列表
     * 参数 Array<String> - 服务器 URI 列表
     */
    public func setServerURIs(serverURIs: Array<String>): Unit

    /*
     * 返回 AutomaticReconnect 标志状态
     * 返回值 Bool - 状态
     */
    public func isAutomaticReconnect(): Bool

    /*
     * 设置 AutomaticReconnect 标志状态
     * 参数 automaticReconnect - 状态
     */
    public func setAutomaticReconnect(automaticReconnect: Bool): Unit

    /*
     * 返回 SkipPortDuringHandshake 标志状态
     * 返回值 Bool - 状态
     */
    public func isSkipPortDuringHandshake(): Bool

    /*
     * 设置 SkipPortDuringHandshake 标志状态
     * 参数 skip - 状态
     */
    public func setSkipPortDuringHandshake(skip: Bool): Unit

    /*
     * 返回 debug 信息 map
     * 参数 HashMap<String, ToString> - debug 信息
     */
    public func getDebug(): HashMap<String, ToString>

    /*
     * 设置 WebSocket 连接的自定义 WebSocket 头
     * 返回值 ?HashMap<String, String> - 自定义信息,如果未设置,则返回 None
     */
    public func getCustomWebSocketHeaders(): ?HashMap<String, String>

    /*
     * 设置 WebSocket 连接的自定义 WebSocket 头
     * 参数 props - 自定义信息
     */
    public func setCustomWebSocketHeaders(props: ?HashMap<String, String>): Unit

    /*
     * 返回字符串“Connection options”
     * 返回值 String - 字符串“Connection options”
     */
    public func toString(): String
}

5 网络模块

前置条件:NA

场景:

  1. 提供 TCP/WebSocket/TCP-TLS/WebSocket-TLS 的连接能力

一般通过 NetworkModuleService 的静态方法创建 NetWorkModule 实例

约束:NA

可靠性:NA

5.1 网络模块 NetWork

5.1.1 主要接口
public interface NetworkModuleFactory {
    /*
     * 返回由此工厂创建的NetworkModules支持的所有URI方案
     * 返回值 HashSet<String> - String 集合
     */
    func getSupportedUriSchemes(): HashSet<String>

    /*
     * 验证此工厂支持的所有URI方案
     * 参数 brokerUri - URL 连接
     */
    func validateURI(brokerUri: URL): Unit

    /*
     * 创建一个NetworkModule实例
     * 参数 brokerUri - 用于连接到代理的URI
     * 参数 options - 用于连接的选项
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 返回值 NetworkModule - NetworkModule对象
     */
    func createNetworkModule(brokerUri: URL, options: MqttConnectOptions, clientId: String): NetworkModule
}

public class TCPNetworkModuleFactory <: NetworkModuleFactory {
    /*
     * 返回由此工厂创建的NetworkModules支持的所有URI方案
     * 返回值 HashSet<String> - String 集合
     */
    public func getSupportedUriSchemes(): HashSet<String>

    /*
     * 验证此工厂支持的所有URI方案,如果参数URL的path长度不为0则抛IllegalArgumentException
     * 参数 brokerUri - URL 连接
     */
    public func validateURI(brokerUri: URL): Unit

    /*
     * 创建一个NetworkModule实例,如果参数URL的path长度不为0则抛IllegalArgumentException
     * 参数 brokerUri - 用于连接到代理的URI
     * 参数 options - 用于连接的选项
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 返回值 NetworkModule - NetworkModule对象
     */
    public func createNetworkModule(brokerUri: URL, options: MqttConnectOptions, clientId: String): NetworkModule
}

public class TLSNetworkModuleFactory <: NetworkModuleFactory {
    /*
     * 返回由此工厂创建的NetworkModules支持的所有URI方案
     * 返回值 HashSet<String> - String 集合
     */
    public func getSupportedUriSchemes(): HashSet<String>

    /*
     * 验证此工厂支持的所有URI方案,如果参数URL的path长度不为0则抛IllegalArgumentException
     * 参数 brokerUri - URL 连接
     */
    public func validateURI(brokerUri: URL): Unit

    /*
     * 创建一个NetworkModule实例,如果参数URL的path长度不为0则抛IllegalArgumentException
     * 参数 brokerUri - 用于连接到代理的URI
     * 参数 options - 用于连接的选项
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 返回值 NetworkModule - NetworkModule对象
     */
    public func createNetworkModule(brokerUri: URL, options: MqttConnectOptions, clientId: String): NetworkModule
}

public class WebSocketNetworkModuleFactory <: NetworkModuleFactory {
    /*
     * 返回由此工厂创建的NetworkModules支持的所有URI方案
     * 返回值 HashSet<String> - String 集合
     */
    public func getSupportedUriSchemes(): HashSet<String>

    /*
     * 验证此工厂支持的所有URI方案
     * 参数 brokerUri - URL 连接
     */
    public func validateURI(brokerUri: URL): Unit

    /*
     * 创建一个NetworkModule实例
     * 参数 brokerUri - 用于连接到代理的URI
     * 参数 options - 用于连接的选项
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 返回值 NetworkModule - NetworkModule对象
     */
    public func createNetworkModule(brokerUri: URL, options: MqttConnectOptions, clientId: String): NetworkModule
}

public class WebSocketSecureNetworkModuleFactory <: NetworkModuleFactory {
        /*
     * 返回由此工厂创建的NetworkModules支持的所有URI方案
     * 返回值 HashSet<String> - String 集合
     */
    public func getSupportedUriSchemes(): HashSet<String>

    /*
     * 验证此工厂支持的所有URI方案
     * 参数 brokerUri - URL 连接
     */
    public func validateURI(brokerUri: URL): Unit

    /*
     * 创建一个NetworkModule实例
     * 参数 brokerUri - 用于连接到代理的URI
     * 参数 options - 用于连接的选项
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 返回值 NetworkModule - NetworkModule对象
     */
    public func createNetworkModule(brokerUri: URL, options: MqttConnectOptions, clientId: String): NetworkModule
}

public class NetworkModuleService {
    /*
     * 验证提供的URI是否有效,并且是否安装了用于服务它的NetworkModule
     * 地址解析URL.parse失败,或协议不为tcp,tls,ws,wss则抛IllegalArgumentException
     * 参数 brokerUri - URL 连接
     */
    public static func validateURI(brokerUri: String): Unit

    /*
     * 为提供的地址创建一个NetworkModule实例,使用给定的选项进行初始化
     * 参数 address - 地址
     * 参数 options - 用于连接的选项
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 返回值 NetworkModule - NetworkModule对象
     */
    public static func createInstance(address: String, options: MqttConnectOptions, clientId: String): NetworkModule
}

/* SocketFactory 类型定义 */
public type SocketFactory = (String) -> TcpSocket

public interface NetworkModule {
    /*
     * 开始传输
     */
    func start(): Unit

    /*
     * 返回用于接收消息的输入流
     * 返回值 InputStream - 输入流
     */
    func getInputStream(): InputStream

    /*
     * 返回用于发送消息的输出
     * 返回值 OutputStream - 输出流
     */
    func getOutputStream(): OutputStream

    /*
     * 停止传输
     */
    func stop(): Unit

    /*
     * 返回用于创建此 NetworkModule 的URI
     */
    func getServerURI(): String
}

public class TCPNetworkModule <: NetworkModule {
    /*
     * 构造函数
     * 参数 brokerUrl - URL 地址
     * 参数 resourceContext - Context字符串
     * 参数 factory - SocketFactory对象
     * 参数 connTimeout - 连接超时时间
     */
    public init(brokerUrl: URL,resourceContext: String,factory!: SocketFactory,connTimeout!: ?Duration = None)

    /*
     * 开始传输,如果socket连接失败,如地址错误,连接超时,服务器不存等,则抛MqttException
     */
    public func start(): Unit

    /*
     * 返回用于接收消息的输入流
     * 返回值 InputStream - 输入流
     */
    public func getInputStream(): InputStream

    /*
     * 返回用于发送消息的输出
     * 返回值 OutputStream - 输出流
     */
    public func getOutputStream(): OutputStream

    /*
     * 停止传输
     */
    public func stop(): Unit

    /*
     * 返回用于创建此 NetworkModule 的URI
     */
    public func getServerURI(): String
}

public class TLSNetworkModule <: NetworkModule {
    /*
     * 构造函数
     * 参数 brokerUrl - URL 地址
     * 参数 resourceContext - Context字符串
     * 参数 factory - SocketFactory对象
     * 参数 cfg - TlsClientConfig 配置
     * 参数 connTimeout - 连接超时时间
     */
    public init(brokerUrl: URL,resourceContext: String,factory!: SocketFactory,cfg!: TlsClientConfig,connTimeout!: ?Duration = None)

    /*
     * 开始传输
     */
    public func start(): Unit

    /*
     * 返回用于接收消息的输入流
     * 返回值 InputStream - 输入流
     */
    public func getInputStream(): InputStream

    /*
     * 返回用于发送消息的输出
     * 返回值 OutputStream - 输出流
     */
    public func getOutputStream(): OutputStream

    /*
     * 停止传输
     */
    public func stop(): Unit

    /*
     * 返回用于创建此 NetworkModule 的URI
     */
    public func getServerURI(): String
}

public open class WebSocketNetworkModule <: NetworkModule & IOStream {
    /*
     * 构造函数
     * 参数 brokerUrl - URL 地址
     * 参数 resourceContext - Context字符串
     * 参数 factory - SocketFactory对象
     * 参数 connTimeout - 连接超时时间
     * 参数 customWebsocketHeaders - 自定义Websocket头信息
     */
    public init(brokerUrl: URL,resourceContext: String,factory!: ?SocketFactory = None,connTimeout!: ?Duration = None,customWebsocketHeaders!: ?HashMap<String, String> = None)

    /*
     * 开始传输
     */
    public func start(): Unit

    /*
     * 返回用于接收消息的输入流
     * 返回值 InputStream - 输入流
     */
    public func getInputStream(): InputStream

    /*
     * 返回用于发送消息的输出
     * 返回值 OutputStream - 输出流
     */
    public func getOutputStream(): OutputStream

    /*
     * 读取数据
     * 参数 buf - 读取到 Array<Byte>中
     */
    public func read(buf: Array<Byte>): Int64

    /*
     * 写数据
     * 参数 buf - 把Array<Byte>的数据写入流中
     */
    public func write(buf: Array<Byte>): Unit

    /*
     * 停止传输
     */
    public func stop(): Unit

    /*
     * 返回用于创建此 NetworkModule 的URI
     */
    public func getServerURI(): String
}

public class WebSocketSecureNetworkModule <: WebSocketNetworkModule {
    /*
     * 构造函数
     * 参数 brokerUrl - URL 地址
     * 参数 clientId - 在连接的服务器上是唯一的客户端标识符
     * 参数 cfg - TlsClientConfig 配置
     * 参数 connTimeout - 连接超时时间
     * 参数 customWebsocketHeaders - 自定义Websocket头信息
     */
     public init(
        brokerUrl: URL,
        clientId: String,
        factory!: ?SocketFactory = None,
        cfg!: TlsClientConfig = DEFAULT_TLS_CLIENT_CONFIG,
        connTimeout!: ?Duration = None,
        customWebsocketHeaders!: ?HashMap<String, String> = None
    )

    /*
     * 开始传输
     */
    public func start(): Unit

    /*
     * 返回用于创建此 NetworkModule 的URI
     */
    public func getServerURI(): String
}

6 操作令牌

前置条件:NA

场景:

  1. 提供跟踪异步任务完成情况的机制。当使用异步 MQTT 接口时,用于跟踪操作的状态。应用程序可以使用令牌等待操作完成。一旦操作完成,令牌将被传递给回调,并提供将其链接到原始请求的上下文。令牌与单个操作关联。

TokenStore 管理多个 MqttToken 每个 MqttToken 关联一个 Token,提供简化的 setActionCallback/waitForCompletion 接口 Token是对 操作 的包装,在 操作 前可以设置回调,监听者阻塞在 waitForCompletion 操作完成后监听者收到信号 waitForCompletion 期待 notifyComplete 接口通知 token 已完成

约束:NA

可靠性:NA

6.1 操作令牌 token

6.1.1 主要接口
public open class Token <: ToString {
    /*
     * 构造函数
     * 参数 logContext - 字符串
     */
    public init(logContext: String)

    /*
     * 获取消息id
     * 返回值 logContext - Int64 id
     */
    public func getMessageID(): Int64

    /*
     * 设置消息id
     * 返回值 messageID - Int64 id
     */
    public func setMessageID(messageID: Int64): Unit

    /*
     * 检查是否有异常, 如果存在异常,则抛出改异常
     * 返回值 Bool - 是否有异常
     */
    public func checkResult(): Bool

    /*
     * 获取当前异常
     * 返回值 ?MqttException - 异常
     */
    public func getException(): ?MqttException

    /*
     * 设置当前异常
     * 参数 exception - MqttException异常
     */
    public func setException(exception: ?MqttException): Unit

    /*
     * 检查是否完成
     * 返回值 Bool - 是否完成
     */
    public func isComplete(): Bool

    /*
     * 获取 IMqttActionListener 对象
     * 返回值 IMqttActionListener - IMqttActionListener对象
     */
    public func getActionCallback(): ?IMqttActionListener

    /*
     * 设置 IMqttActionListener 对象
     * 参数 ?IMqttActionListener - IMqttActionListener对象
     */
    public func setActionCallback(listener: ?IMqttActionListener): Unit

    /*
     * 等待完成
     */
    public func waitForCompletion(): Unit

    /*
     * 等待完成
     * 参数 timeout - 超时时间 Duration
     */
    public func waitForCompletion(timeout: ?Duration): Unit

    /*
     * 等待发送
     */
    public func waitUntilSent(): Unit

    /*
     * 获取客户端
     * 返回值 ?IMqttAsyncClient - 返回一个 IMqttAsyncClient
     */
    public func getClient(): ?IMqttAsyncClient

    /*
     * 重置 Token ,将内部completed,sent标志设为false,client,response,exception,userContext属性值设为None
     * 如果client 空且isComplete为false,则不允许重置,将抛 MqttException
     */
    public func reset(): Unit

    /*
     * 获取MqttMessage
     * 返回值 ?MqttMessage - 返回一个 MqttMessage
     */
    public func getMessage(): ?MqttMessage

    /*
     * 设置MqttMessage
     * 参数 MqttMessage - 一个 MqttMessage
     */
    public func setMessage(msg: MqttMessage): Unit

    /*
     * 获取MqttWireMessag
     * 返回值 ?MqttWireMessag - 返回一个 MqttWireMessag
     */
    public func getWireMessage(): ?MqttWireMessag

    /*
     * 获取消息主题
     * 返回值 ?Array<String> - 返回一个 Array<String>
     */
    public func getTopics(): ?Array<String>

    /*
     * 设置消息主题
     * 参数 topics - 设置一个 Array<String>
     */
    public func setTopics(topics: Array<String>): Unit

    /*
     * 获取用户自定义值
     * 返回值 Any - Any值
     */
    public func getUserContext(): ?Any

    /*
     * 设置用户自定义值
     * 参数 userContext - Any值
     */
    public func setUserContext(userContext: ?Any): Unit

    /*
     * 获取关键字
     * 返回值 ?String - String值
     */
    public func getKey(): ?String

    /*
     * 设置关键字
     * 参数 key - String值
     */
    public func setKey(key: String): Unit

    /*
     * 获取Notified标记值
     * 返回值 Bool - Bool值
     */
    public func isNotified(): Bool

    /*
     * 设置Notified标记值
     * 参数 Bool - Bool值
     */
    public func setNotified(notified: Bool): Unit

    /*
     * 获取字符串
     * 返回值 String - String
     */
    public func toString(): String

    /*
     * 获取获取订阅请求的订阅确认
     * 返回值 Array<Int64>  - Array<Int64> 值
     */
    public func getGrantedQos(): Array<Int64> 

    /*
     * 获取SessionPresent
     * 返回值 Bool - Bool值
     */
    public func getSessionPresent(): Bool

    /*
     * 获取MqttWireMessage
     * 返回值 MqttWireMessage - MqttWireMessage
     */
    public func getResponse(): ?MqttWireMessage
}

public interface IMqttToken <: ToString {
    /*
     * 阻塞当前线程,直到与此令牌关联的操作完成
     */
    func waitForCompletion(): Unit

    /*
     * 阻塞当前线程,直到与此令牌关联的操作完成,或达到超时时间
     * 参数 timeout - 超时时间 Duration
     */
    func waitForCompletion(timeout: ?Duration): Unit

    /*
     * 返回操作是否已完成
     * 返回值 Bool - 是否完成
     */
    func isComplete(): Bool

    /*
     * 获取当前异常
     * 返回值 ?MqttException - 异常
     */
    func getException(): ?MqttException

    /*
     * 获取 IMqttActionListener 对象
     * 返回值 IMqttActionListener - IMqttActionListener对象
     */
    func getActionCallback(): ?IMqttActionListener

    /*
     * 设置 IMqttActionListener 对象
     * 参数 ?IMqttActionListener - IMqttActionListener对象
     */
    func setActionCallback(listener: ?IMqttActionListener): Unit

    /*
     * 获取客户端
     * 返回值 ?IMqttAsyncClient - 返回一个 IMqttAsyncClient
     */
    func getClient(): ?IMqttAsyncClient

    /*
     * 获取消息主题
     * 返回值 ?Array<String> - 返回一个 Array<String>
     */
    func getTopics(): ?Array<String>

    /*
     * 获取用户自定义值
     * 返回值 Any - Any值
     */
    func getUserContext(): ?Any

    /*
     * 设置用户自定义值
     * 参数 userContext - Any值
     */
    func setUserContext(userContext: ?Any): Unit

    /*
     * 获取消息id
     * 返回值 logContext - Int64 id
     */
    func getMessageId(): Int64

    /*
     * 获取获取订阅请求的订阅确认
     * 返回值 Array<Int64>  - Array<Int64> 值
     */
    func getGrantedQos(): Array<Int64>

    /*
     * 获取SessionPresent
     * 返回值 Bool - Bool值
     */
    func getSessionPresent(): Bool

    /*
     * 获取MqttWireMessage
     * 返回值 MqttWireMessage - MqttWireMessage
     */
    func getResponse(): ?MqttWireMessage
}

public open class MqttToken <: IMqttToken {
    /*
     * 构造函数
     * 参数 logContext - 字符串
     */
    public init(logContext: String)

    /*
     * 获取当前异常
     * 返回值 ?MqttException - 异常
     */
    public func getException(): ?MqttException

    /*
     * 返回操作是否已完成
     * 返回值 Bool - 是否完成
     */
    public func isComplete(): Bool

    /*
     * 获取 IMqttActionListener 对象
     * 返回值 IMqttActionListener - IMqttActionListener对象
     */
    public func getActionCallback(): ?IMqttActionListener

    /*
     * 设置 IMqttActionListener 对象
     * 参数 ?IMqttActionListener - IMqttActionListener对象
     */
    public func setActionCallback(listener: ?IMqttActionListener): Unit

    /*
     * 阻塞当前线程,直到与此令牌关联的操作完成
     */
    public func waitForCompletion(): Unit

    /*
     * 阻塞当前线程,直到与此令牌关联的操作完成,或达到超时时间
     * 参数 timeout - 超时时间 Duration
     */
    public func waitForCompletion(timeout: ?Duration): Unit

    /*
     * 获取客户端
     * 返回值 ?IMqttAsyncClient - 返回一个 IMqttAsyncClient
     */
    public func getClient(): ?IMqttAsyncClient

    /*
     * 获取消息主题
     * 返回值 ?Array<String> - 返回一个 Array<String>
     */
    public func getTopics(): ?Array<String>

    /*
     * 获取用户自定义值
     * 返回值 Any - Any值
     */
    public func getUserContext(): ?Any

    /*
     * 设置用户自定义值
     * 参数 userContext - Any值
     */
    public func setUserContext(userContext: ?Any): Unit

    /*
     * 获取消息id
     * 返回值 logContext - Int64 id
     */
    public func getMessageId(): Int64

    /*
     * 获取获取订阅请求的订阅确认
     * 返回值 Array<Int64>  - Array<Int64> 值
     */
    public func getGrantedQos(): Array<Int64>

    /*
     * 获取SessionPresent
     * 返回值 Bool - Bool值
     */
    public func getSessionPresent(): Bool

    /*
     * 获取MqttWireMessage
     * 返回值 MqttWireMessage - MqttWireMessage
     */
    public func getResponse(): ?MqttWireMessage


    /*
     * 获取字符串,固定返回字符串 "MqttToken"
     * 返回值 String - String
     */
    public func toString(): String
}

public interface IMqttDeliveryToken <: IMqttToken {
    /*
     * 获取MqttWireMessag
     * 返回值 ?MqttWireMessag - 返回一个 MqttWireMessag
     */
    func getMessage(): ?MqttMessage
}

public open class MqttDeliveryToken <: MqttToken & IMqttDeliveryToken {
    /*
     * 构造函数
     * 参数 logContext - 字符串
     */
    public init(logContext: String)

    /*
     * 获取MqttWireMessag
     * 返回值 ?MqttWireMessag - 返回一个 MqttWireMessag
     */
    public func getMessage(): ?MqttMessage
}

public open class CommsTokenStore <: ToString {
    /*
     * 构造函数
     * 参数 logContext - 字符串
     */
    public init(logContext: String)

    /*
     * 获取MqttWireMessage对应的MqttToken
     * 参数 message - MqttWireMessage
     * 返回值 ?MqttToken - MqttToken
     */
    public func getToken(message: MqttWireMessage): ?MqttToken

    /*
     * 获取String对应的MqttToken
     * 参数 message - String
     * 返回值 ?MqttToken - MqttToken
     */
    public func getToken(key: String): ?MqttToken

    /*
     * 移除MqttWireMessage对应的MqttToken
     * 参数 message - ?MqttWireMessage
     * 返回值 ?MqttToken - MqttToken
     */
    public func removeToken(message: ?MqttWireMessage): ?MqttToken

    /*
     * 移除String对应的MqttToken
     * 参数 key - ?String
     * 返回值 ?MqttToken - MqttToken
     */
    public func removeToken(key: ?String): ?MqttToken

    /*
     * 打开令牌存储
     */
    public func open(): Unit
    
    /*
     * 获取未完成的、尚未通知的 DeliveryToken
     * 返回值 Array<MqttDeliveryToken> - MqttDeliveryToken列表
     */
    public func getOutstandingDelTokens(): Array<MqttDeliveryToken>

    /*
     * 获取未完成的 DeliveryToken
     * 返回值 SyncList<MqttToken> - MqttToken SyncList列表
     */
    public func getOutstandingTokens(): SyncList<MqttToken>

    /*
     * 清空所有保存的token
     */
    public func clear(): Unit

    /*
     * 返回当前存储的token数量
     * 返回值 Int64 - 数量
     */
    public func count(): Int64

    /*
     * 获取字符串, 按行拼接所有token的internalTok属性
     * 返回值 String - String
     */
    public func toString(): String
}

7 事件监听

前置条件:NA

场景: 提供事件(连接/断开/发布/订阅/取消订阅)监听(成功/失败)

约束:NA

可靠性:NA

7.1 事件监听

7.1.1 主要接口
public interface IMqttActionListener {
    /*
     * 监听成功动作
     * 参数 asyncActionToken - 成功返回的token
     */
    func onSuccess(asyncActionToken: IMqttToken): Unit

    /*
     * 监听失败动作
     * 参数 asyncActionToken - 失败返回的token
     * 参数 exception - 失败捕获的异常
     */
    func onFailure(asyncActionToken: IMqttToken, exception: Exception): Unit
}

8 客户端对象

前置条件:NA

场景: 提供客户端对象

约束:NA

可靠性:NA

8.1 客户端对象

8.1.1 主要接口
public interface IMqttAsyncClient <: Resource {
    /*
     * 连接
     * 返回值 IMqttToken - IMqttToken接口对象
     * @throws MqttSecurityException - 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connect(): IMqttToken

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 返回值 IMqttToken - IMqttToken接口对象
     * @throws MqttSecurityException - 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connect(options: MqttConnectOptions): IMqttToken

    /*
     * 连接
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
     * @throws MqttSecurityException - 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connect(userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
     * @throws MqttSecurityException - 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connect(options: MqttConnectOptions, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 断开连接
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    func disconnect(): IMqttToken

    /*
     * 断开连接
     * 参数 quiesceTimeout - 超时
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    func disconnect(quiesceTimeout: Duration): IMqttToken

    /*
     * 断开连接
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    func disconnect(userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 断开连接
     * 参数 quiesceTimeout - 超时
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    func disconnect(quiesceTimeout: Duration, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 强制断开连接
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(): Unit

    /*
     * 强制断开连接
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
     * 参数 sendDisconnectPacket - 是否发送断开数据包
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration, sendDisconnectPacket: Bool): Unit

    /*
     * 是否已连接
     * 返回值 Bool - 是否已连接
     */
    func isConnected(): Bool

    /*
     * 获取客户端id
     * 返回值 String - 客户端id
     */
    func getClientId(): String

    /*
     * 获取服务端uri
     * 返回值 String - 服务端uri
     */
    func getServerURI(): String

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 payload - 发布正文数组
     * 参数 qos - 要发布消息的服务质量(0、1或2)
     * 参数 retained - 消息是否应该被保留
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    func publish(topic: String, payload: Array<Byte>, qos: Int64, retained: Bool): IMqttDeliveryToken

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 payload - 发布正文数组
     * 参数 qos - 要发布消息的服务质量(0、1或2
     * 参数 retained - 消息是否应该被保留
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    func publish(
        topic: String,
        payload: Array<Byte>,
        qos: Int64,
        retained: Bool,
        userContext: ?Any,
        callback: ?IMqttActionListener
    ): IMqttDeliveryToken

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 message - MqttMessage对象
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    func publish(topic: String, message: MqttMessage): IMqttDeliveryToken

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 message - MqttMessage对象
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听    
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    func publish(topic: String, message: MqttMessage, userContext: ?Any, callback: ?IMqttActionListener): IMqttDeliveryToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilter: String, qos: Int64): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听  
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilter: String, qos: Int64, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听  
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilters: Array<String>, qos: Array<Int64>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听  
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilters: Array<String>, qos: Array<Int64>, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(
        topicFilter: String,
        qos: Int64,
        userContext: ?Any,
        callback: ?IMqttActionListener,
        messageListener: ?IMqttMessageListener
    ): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilter: String, qos: Int64, messageListener: ?IMqttMessageListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilters: Array<String>, qos: Array<Int64>, messageListeners: ?Array<?IMqttMessageListener>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(
        topicFilters: Array<String>,
        qos: Array<Int64>,
        userContext: ?Any,
        callback: ?IMqttActionListener,
        messageListeners: ?Array<?IMqttMessageListener>
    ): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilter - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func unsubscribe(topicFilter: String): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilters - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func unsubscribe(topicFilters: Array<String>): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilter - 主题
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func unsubscribe(topicFilter: String, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilters - 主题
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func unsubscribe(topicFilters: Array<String>, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 删除信息
     * 参数 token - IMqttDeliveryToken对象
     * 返回值 Bool - 删除是否成功
	 * @throws MqttException - 客户端状态异常
     */
    func removeMessage(token: IMqttDeliveryToken): Bool

    /*
     * 设置回调
     * 参数 callback - MqttCallback对象
     */
    func setCallback(callback: MqttCallback): Unit

    /*
     * 获取当前未交付的消息Token
     * 返回值 callback - IMqttDeliveryToken对象数组
     */
    func getPendingDeliveryTokens(): Array<IMqttDeliveryToken>

    /*
     * 设置是否使用手动确认消息的接收
     * 参数 manualAcks - 否使用手动确认
     */
    func setManualAcks(manualAcks: Bool): Unit

    /*
     * 重新连接
	 * @throws MqttException - 客户端状态异常
     */
    func reconnect(): Unit

    /*
     * 消息到达完成
     * 参数 messageId - 消息id
     * 参数 qos - 级别
	 * @throws MqttException - 客户端状态异常
     */
    func messageArrivedComplete(messageId: Int64, qos: Int64): Unit

    /*
     * 设置缓冲对象
     * 参数 bufferOpts - 设置
     */
    func setBufferOpts(bufferOpts: DisconnectedBufferOptions): Unit

    /*
     * 获取缓冲信息数量
     * 返回值 Int64 - 数量
     */
    func getBufferedMessageCount(): Int64

    /*
     * 获取缓冲信息
     * 参数 bufferIndex - id
     * 返回值 MqttMessage - MqttMessage对象
     */
    func getBufferedMessage(bufferIndex: Int64): MqttMessage

    /*
     * 删除缓冲信息
     * 参数 bufferIndex - id
     */
    func deleteBufferedMessage(bufferIndex: Int64): Unit

    /*
     * 获取待确认接收的信息数量
     * 返回值 Int64 - 数量
     */
    func getInFlightMessageCount(): Int64

    /*
     * 关闭
	 * @throws MqttException - 客户端状态异常
     */
    func close(): Unit
}
/**
 * 异步客户端
 */
public class MqttAsyncClient <: IMqttAsyncClient {
    /*
     * 构建MqttAsyncClient对象
     * 参数 serverURI - 服务端URI地址
     * 参数 clientId - 客户端id
     * 参数 ?persistence - 持久化对象 --默认值 ?MqttDefaultFilePersistence()
     * 参数 pingSender - pingSender对象
     * 参数 highResolutionTimer - 高精时间戳 -- 默认值 SystemHighResolutionTimer()
     * 返回值 MqttAsyncClient - 生成的客户端对象
     * @throws IllegalArgumentException URI不符合规范 比如 "tcp://", "ssl://" or "local://"
     * @throws MqttException - 客户端状态异常
     */
    public static func build(
        serverURI: String,
        clientId: String,
        persistence!: ?MqttClientPersistence = MqttDefaultFilePersistence(),
        pingSender!: MqttPingSender = TimerPingSender(),
        highResolutionTimer!: HighResolutionTimer = SystemHighResolutionTimer()
    ): MqttAsyncClient

    /*
     * 连接
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 连接
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(): IMqttToken

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(options: MqttConnectOptions): IMqttToken

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(options: MqttConnectOptions, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 断开连接
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 断开连接
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(): IMqttToken

    /*
     * 断开连接
     * 参数 quiesceTimeout - 超时
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(quiesceTimeout: Duration): IMqttToken

    /*
     * 断开连接
     * 参数 quiesceTimeout - 超时
     * 参数 userContext - 用户上下文
     * 参数 callback - 监听回调
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(quiesceTimeout: Duration, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 强制断开连接
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(): Unit

    /*
     * 强制断开连接
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
     * 参数 sendDisconnectPacket - 是否发送断开数据包
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration, sendDisconnectPacket: Bool): Unit

    /*
     * 是否已连接
     * 返回值 Bool - 是否已连接
     */
    public func isConnected(): Bool

    /*
     * 是否正在已连接
     * 返回值 Bool - 是否正在已连接
     */
    public func isConnecting(): Bool

    /*
     * 获取客户端id
     * 返回值 String - 客户端id
     */
    public func getClientId(): String

    /*
     * 获取服务端uri
     * 返回值 String - 服务端uri
     */
    public func getServerURI(): String

    /*
     * 获取当前服务端uri
     * 返回值 String - 当前服务端uri
     */
    public func getCurrentServerURI(): String

    /*
     * 判断Ping并且需要的话发送Ping
     * 参数 userContext - Any对象
     * 参数 callback - 回调监听
     * 返回值 String - 服务端返回的IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func checkPing(userContext: ?Any, callback: IMqttActionListener): ?IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听  
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */  
    public func subscribe(topicFilter: String, qos: Int64, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilter: String, qos: Int64): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听  
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilters: Array<String>, qos: Array<Int64>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听  
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(
        topicFilters: Array<String>,
        qos: Array<Int64>,
        userContext: ?Any,
        callback: ?IMqttActionListener
    ): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(
        topicFilter: String,
        qos: Int64,
        userContext: ?Any,
        callback: ?IMqttActionListener,
        messageListener: ?IMqttMessageListener
    ): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilter: String, qos: Int64, messageListener: ?IMqttMessageListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilters: Array<String>, qos: Array<Int64>, messageListeners: ?Array<?IMqttMessageListener>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(
        topicFilters: Array<String>,
        qos: Array<Int64>,
        userContext: ?Any,
        callback: ?IMqttActionListener,
        messageListeners: ?Array<?IMqttMessageListener>
    ): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilter - 主题
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func unsubscribe(topicFilter: String, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilter - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func unsubscribe(topicFilter: String): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilters - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func unsubscribe(topicFilters: Array<String>): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilters - 主题
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func unsubscribe(topicFilters: Array<String>, userContext: ?Any, callback: ?IMqttActionListener): IMqttToken

    /*
     * 删除信息
     * 参数 token - IMqttDeliveryToken对象
     * 返回值 Bool - 删除是否成功
	 * @throws MqttException - 客户端状态异常
     */
    public func removeMessage(token: IMqttDeliveryToken): Bool

    /*
     * 设置回调
     * 参数 callback - MqttCallback对象
     */
    public func setCallback(callback: MqttCallback): Unit

    /*
     * 设置是否使用手动确认消息的接收
     * 参数 manualAcks - 否使用手动确认
     */
    public func setManualAcks(manualAcks: Bool): Unit

    /*
     * 消息到达完成
     * 参数 messageId - 消息id
     * 参数 qos - 级别
	 * @throws MqttException - 客户端状态异常
     */
    public func messageArrivedComplete(messageId: Int64, qos: Int64): Unit

    /*
     * 生成客户端id
     * 返回值 String - 客户端id
     */
    public static func generateClientId(): String

    /*
     * 获取当前未交付的消息Token
     * 返回值 callback - IMqttDeliveryToken对象数组
     */
    public func getPendingDeliveryTokens(): Array<IMqttDeliveryToken>

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 payload - 发布正文数组
     * 参数 qos - 要发布消息的服务质量(0、1或2
     * 参数 retained - 消息是否应该被保留
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    public func publish(
        topic: String,
        payload: Array<Byte>,
        qos: Int64,
        retained: Bool,
        userContext: ?Any,
        callback: ?IMqttActionListener
    ): IMqttDeliveryToken

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 payload - 发布正文数组
     * 参数 qos - 要发布消息的服务质量(0、1或2
     * 参数 retained - 消息是否应该被保留
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    public func publish(topic: String, payload: Array<Byte>, qos: Int64, retained: Bool): IMqttDeliveryToken

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 message - MqttMessage对象
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    public func publish(topic: String, message: MqttMessage): IMqttDeliveryToken

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 message - MqttMessage对象
     * 参数 userContext - 用户上下文
     * 参数 callback - 回调监听    
     * 返回值 IMqttDeliveryToken - IMqttDeliveryToken对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    public func publish(topic: String, message: MqttMessage, userContext: ?Any, callback: ?IMqttActionListener): IMqttDeliveryToken

    /*
     * 重新连接
	 * @throws MqttException - 客户端状态异常
     */
    public func reconnect(): Unit

    /*
     * 设置缓冲对象
     * 参数 bufferOpts - 设置对象
     */
    public func setBufferOpts(bufferOpts: DisconnectedBufferOptions): Unit

    /*
     * 获取缓冲信息数量
     * 返回值 Int64 - 数量
     */
    public func getBufferedMessageCount(): Int64

    /*
     * 获取缓冲信息
     * 参数 bufferIndex - id
     * 返回值 MqttMessage - MqttMessage对象
     */
    public func getBufferedMessage(bufferIndex: Int64): MqttMessage

    /*
     * 删除缓冲信息
     * 参数 bufferIndex - id
     */
    public func deleteBufferedMessage(bufferIndex: Int64): Unit

    /*
     * 获取待确认接收的信息数量
     * 返回值 Int64 - 数量
     */
    public func getInFlightMessageCount(): Int64

    /*
     * 关闭
     */
    public func close(): Unit

    /*
     * 关闭
     * 参数 force - 是否强制
     */
    public func close(force: Bool): Unit

    /*
     * 是否关闭
     * 返回值 Bool - 是否关闭
     */
    public func isClosed(): Bool
}

public interface IMqttClient <: Resource {
    /*
     * 连接
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connect(): Unit

    /*
     * 连接
     * 参数 options - 连接配置对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connect(options: MqttConnectOptions): Unit

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    func connectWithResult(options: MqttConnectOptions): IMqttToken

    /*
     * 断开连接
	 * @throws MqttException - 客户端状态异常
     */
    func disconnect(): Unit

    /*
     * 断开连接
     * 参数 quiesceTimeout - 超时
	 * @throws MqttException - 客户端状态异常
     */
    func disconnect(quiesceTimeout: Duration): Unit

    /*
     * 强制断开连接
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(): Unit

    /*
     * 强制断开连接
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
	 * @throws MqttException - 客户端状态异常
	 * @throws MqttSecurityException 身份/权限失败
     */
    func subscribe(topicFilter: String): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilters: Array<String>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilter: String, qos: Int64): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    func subscribe(topicFilters: Array<String>, qos: Array<Int64>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 messageListener - 消息监听
	 * @throws MqttException - 客户端状态异常
	 * @throws MqttSecurityException 身份/权限失败
     */
    func subscribe(topicFilter: String, messageListener: ?IMqttMessageListener): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 messageListeners - 消息监听
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilters: Array<String>, messageListeners: ?Array<?IMqttMessageListener>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListener - 消息监听
	 * @throws MqttException - 客户端状态异常
     */
    func subscribe(topicFilter: String, qos: Int64, messageListener: ?IMqttMessageListener): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListeners - 消息监听
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    func subscribe(topicFilters: Array<String>, qos: Array<Int64>, messageListeners: ?Array<?IMqttMessageListener>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribeWithResponse(topicFilter: String): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribeWithResponse(topicFilter: String, messageListener: ?IMqttMessageListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribeWithResponse(topicFilter: String, qos: Int64): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribeWithResponse(topicFilter: String, qos: Int64, messageListener: ?IMqttMessageListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribeWithResponse(topicFilters: Array<String>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    func subscribeWithResponse(topicFilters: Array<String>, messageListeners: Array<?IMqttMessageListener>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    func subscribeWithResponse(topicFilters: Array<String>, qos: Array<Int64>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    func subscribeWithResponse(
        topicFilters: Array<String>,
        qos: Array<Int64>,
        messageListeners: Array<?IMqttMessageListener>
    ): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilter - 主题
	 * @throws MqttException - 客户端状态异常
     */
    func unsubscribe(topicFilter: String): Unit

    /*
     * 取消订阅
     * 参数 topicFilters - 主题
	 * @throws MqttException - 客户端状态异常
     */
    func unsubscribe(topicFilters: Array<String>): Unit

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 payload - 发布正文数组
     * 参数 qos - 要发布消息的服务质量(0、1或2
     * 参数 retained - 消息是否应该被保留
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException qos不是(0、1或2)
	 * @throws MqttException - 客户端状态异常
     */
    func publish(topic: String, payload: Array<Byte>, qos: Int64, retained: Bool): Unit

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 message - MqttMessage对象
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws MqttException - 客户端状态异常
     */
    func publish(topic: String, message: MqttMessage): Unit

    /*
     * 设置回调
     * 参数 callback - MqttCallback对象
     */
    func setCallback(callback: MqttCallback): Unit

    /*
     * 获取消息主题
     * 参数 topic - 主题名称
     * 返回值 MqttTopic - MqttTopic对象
	 * @throws IllegalArgumentException topic 包含一个 '+' or '#'
     */
    func getTopic(topic: String): MqttTopic

    /*
     * 是否已连接
     * 返回值 Bool - 是否已连接
     */
    func isConnected(): Bool

    /*
     * 获取客户端id
     * 返回值 String - 客户端id
     */
    func getClientId(): String

    /*
     * 获取服务端uri
     * 返回值 String - 服务端uri
     */
    func getServerURI(): String

    /*
     * 获取当前未交付的消息Token
     * 返回值 callback - IMqttDeliveryToken对象数组
     */
    func getPendingDeliveryTokens(): Array<IMqttDeliveryToken>

    /*
     * 设置是否使用手动确认消息的接收
     * 参数 manualAcks - 否使用手动确认
     */
    func setManualAcks(manualAcks: Bool): Unit

    /*
     * 重新连接
	 * @throws MqttException - 客户端状态异常
     */
    func reconnect(): Unit

    /*
     * 消息到达完成
     * 参数 messageId - 消息id
     * 参数 qos - 级别
	 * @throws MqttException - 客户端状态异常
     */
    func messageArrivedComplete(messageId: Int64, qos: Int64): Unit

    /*
     * 关闭
	 * @throws MqttException - 客户端状态异常
     */
    func close(): Unit
}
/**
 * 同步客户端
 */
public class MqttClient <: IMqttClient {
    /*
     * 构建MqttClient对象
     * 参数 serverURI - 服务端URI地址
     * 参数 clientId - 客户端id
	 * @throws IllegalArgumentException URI不符合规范 比如 "tcp://", "ssl://" or "local://"
	 * @throws MqttException - 客户端状态异常
     */
    public init(serverURI: String, clientId: String)

    /*
     * 构建MqttClient对象
     * 参数 serverURI - 服务端URI地址
     * 参数 clientId - 客户端id
     * 参数 persistence - 持久化对象MqttClientPersistence
	 * @throws IllegalArgumentException URI不符合规范 比如 "tcp://", "ssl://" or "local://"
	 * @throws MqttException - 客户端状态异常
     */
    public init(serverURI: String, clientId: String, persistence: MqttClientPersistence)

    /*
     * 生成客户端id
     * 返回值 String - 客户端id
     */
    public static func generateClientId(): String

    /*
     * 连接
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(): Unit

    /*
     * 连接
     * 参数 options - 连接配置对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(options: MqttConnectOptions): Unit

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 返回值 IMqttToken - IMqttToken接口对象
	 * @throws MqttSecurityException 身份/权限失败
	 * @throws MqttException - 客户端状态异常
     */
    public func connectWithResult(options: MqttConnectOptions): IMqttToken

    /*
     * 断开连接
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(): Unit

    /*
     * 断开连接
     * 参数 quiesceTimeout - 超时
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(quiesceTimeout: Duration): Unit

    /*
     * 强制断开连接
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(): Unit

    /*
     * 强制断开连接
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
     * 参数 sendDisconnectPacket - 是否发送断开数据包
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration, sendDisconnectPacket: Bool): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilter: String): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilters: Array<String>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilter: String, qos: Int64): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    public func subscribe(topicFilters: Array<String>, qos: Array<Int64>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 messageListener - 消息监听
	 * @throws MqttException - 客户端状态异常
	 * @throws MqttSecurityException 身份/权限失败
     */
    public func subscribe(topicFilter: String, messageListener: ?IMqttMessageListener): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 messageListeners - 消息监听
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilters: Array<String>, messageListeners: ?Array<?IMqttMessageListener>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListener - 消息监听
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribe(topicFilter: String, qos: Int64, messageListener: ?IMqttMessageListener): Unit

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListeners - 消息监听
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    public func subscribe(topicFilters: Array<String>, qos: Array<Int64>, messageListeners: ?Array<?IMqttMessageListener>): Unit

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribeWithResponse(topicFilter: String): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribeWithResponse(topicFilter: String, messageListener: ?IMqttMessageListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribeWithResponse(topicFilter: String, qos: Int64): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilter - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListener - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribeWithResponse(topicFilter: String, qos: Int64, messageListener: ?IMqttMessageListener): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribeWithResponse(topicFilters: Array<String>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func subscribeWithResponse(topicFilters: Array<String>, messageListeners: Array<?IMqttMessageListener>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    public func subscribeWithResponse(topicFilters: Array<String>, qos: Array<Int64>): IMqttToken

    /*
     * 订阅主题
     * 参数 topicFilters - 主题
     * 参数 qos - 订阅级别
     * 参数 messageListeners - 消息监听
     * 返回值 IMqttToken - IMqttToken对象
	 * @throws MqttException - 客户端状态异常
	 * @throws IllegalArgumentException topicFilters的长度和qos的长度不一样
     */
    public func subscribeWithResponse(
        topicFilters: Array<String>,
        qos: Array<Int64>,
        messageListeners: Array<?IMqttMessageListener>
    ): IMqttToken

    /*
     * 取消订阅
     * 参数 topicFilter - 主题
	 * @throws MqttException - 客户端状态异常
     */
    public func unsubscribe(topicFilter: String): Unit

    /*
     * 取消订阅
     * 参数 topicFilters - 主题
	 * @throws MqttException - 客户端状态异常
     */
    public func unsubscribe(topicFilters: Array<String>): Unit

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 payload - 发布正文数组
     * 参数 qos - 要发布消息的服务质量(0、1或2
     * 参数 retained - 消息是否应该被保留
	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws IllegalArgumentException - if value of QoS is not 0, 1 or 2.
	 * @throws MqttException - 客户端状态异常
     */
    public func publish(topic: String, payload: Array<Byte>, qos: Int64, retained: Bool): Unit

    /*
     * 发布信息
     * 参数 topic - 发布主题
     * 参数 message - MqttMessage对象
 	 * @throws MqttPersistenceException 生成MqttPublish对象出错
	 * @throws MqttException - 客户端状态异常
     */
    public func publish(topic: String, message: MqttMessage): Unit

    /*
     * 获取等待时间
     * 返回值 Duration - 时间间隔
     */
    public func getTimeToWait(): ?Duration

    /*
     * 设置等待时间
     * 参数 timeToWaitInMillis - 时间间隔
	 * @throws IllegalArgumentException timeToWaitInMillis是非法的
     */
    public func setTimeToWait(timeToWaitInMillis: Duration): Unit

    /*
     * 关闭
	 * @throws MqttException - 客户端状态异常
     */
    public func close(): Unit

    /*
     * 关闭
     * 参数 force - 是否强制
	 * @throws MqttException - 客户端状态异常
     */
    public func close(force: Bool): Unit

    /*
     * 是否关闭
     * 返回值 Bool - 是否关闭
     */
    public func isClosed(): Bool

    /*
     * 获取客户端id
     * 返回值 String - 客户端id
     */
    public func getClientId(): String

    /*
     * 获取当前未交付的消息Token
     * 返回值 callback - IMqttDeliveryToken对象数组
     */
    public func getPendingDeliveryTokens(): Array<IMqttDeliveryToken>

    /*
     * 获取服务端uri
     * 返回值 String - 服务端uri
     */
    public func getServerURI(): String

    /*
     * 获取当前服务端uri
     * 返回值 String - 当前服务端uri
     */
    public func getCurrentServerURI(): String

    /*
     * 获取消息主题
     * 参数 topic - 主题名称
     * 返回值 MqttTopic - MqttTopic对象
	 * @throws IllegalArgumentException topic 包含一个 '+' or '#'
     */
    public func getTopic(topic: String): MqttTopic

    /*
     * 是否已连接
     * 返回值 Bool - 是否已连接
     */
    public func isConnected(): Bool

    /*
     * 是否正在已连接
     * 返回值 Bool - 是否正在已连接
     */
    public func isConnecting(): Bool

    /*
     * 设置回调
     * 参数 callback - MqttCallback对象
     */
    public func setCallback(callback: MqttCallback): Unit

    /*
     * 设置是否使用手动确认消息的接收
     * 参数 manualAcks - 否使用手动确认
     */
    public func setManualAcks(manualAcks: Bool): Unit

    /*
     * 消息到达完成
     * 参数 messageId - 消息id
     * 参数 qos - 级别
	 * @throws MqttException - 客户端状态异常
     */
    public func messageArrivedComplete(messageId: Int64, qos: Int64): Unit

    /*
     * 重新连接
	 * @throws MqttException - 客户端状态异常
     */
    public func reconnect(): Unit
}

9 操作回调

前置条件:NA

场景: 提供 连接丢失/消息送达/发送完成/连接完成 事件的回调操作

约束:NA

可靠性:NA

9.1 操作回调

9.1.1 主要接口
public interface MqttCallback {
    /*
     * 与服务端失去联系
     * 参数 cause - 失去联系捕获的异常
     */
    func connectionLost(cause: Exception): Unit

    /*
     * 消息到达
     * 参数 topic - 连接的主题名称  
     * 参数 message - 实际到达的负载正文
	 * @throws Exception - 若发生异常 客户端应自动关闭
     */
    func messageArrived(topic: String, message: MqttMessage): Unit

    /*
     * 发送完成
     * 参数 token - 与当前message绑定的IMqttDeliveryToken对象
     */
    func deliveryComplete(token: IMqttDeliveryToken): Unit
}

public interface MqttCallbackExtended <: MqttCallback {
    /*
     * 连接完成
     * 参数 reconnect - 是否重连
     * 参数 serverURI - 服务端地址
     */
    func connectComplete(reconnect: Bool, serverURI: String): Unit
}

10 封装异常

前置条件:NA

场景: 提供各种异常

约束:NA

可靠性:NA

10.1 封装异常

10.1.1 主要接口
public open class MqttException <: Exception {
    /*
     * MqttException初始化
     * 参数 reasonCode - 错误编号
     */
    public init(reasonCode: Int64)

    /*
     * MqttException初始化
     * 参数 cause - 内部异常
     */
    public init(cause: Exception)

    /*
     * MqttException初始化
     * 参数 reason - 错误编号
     * 参数 cause - 内部异常
     */
    public init(reason: Int64, cause: Exception)

    /*
     * 获取错误编号
     * 返回值 Int64 - 错误编号
     */
    public func getReasonCode(): Int64

    /*
     * 获取内部异常
     * 返回值 ?Exception - 内部异常
     */
    public func getCause(): ?Exception
    /*
     * 获取详细错误信息
     * 返回值 String - 详细错误信息
     */
    public func getMessage(): String

    /*
     * toString
     * 返回值 String - toString
     */
    public func toString(): String

}

public class MqttPersistenceException <: MqttException {
    /*
     * 初始化
     */
    public init()

    /*
     * 初始化
     * 参数 reasonCode - 错误编号
     */
    public init(reasonCode: Int64)

    /*
     * 初始化
     * 参数 cause - 内部异常
     */
    public init(cause: Exception)

    /*
     * 初始化
     * 参数 reason - 错误编号
     * 参数 cause - 内部异常
     */
    public init(reason: Int64, cause: Exception)
}

/*
 * 客户端无权限操作 / 客户端安全配置异常
 * 参数 reasonCode - 错误编号
 */
public class MqttSecurityException <: MqttException {
    /*
     * 初始化
     * 参数 reasonCode - 错误编号
     */
    public init(reasonCode: Int64) 

    /*
     * 初始化
     * 参数 cause - 内部异常
     */
    public init(cause: Exception)

    /*
     * 初始化
     * 参数 reasonCode - 错误编号
     * 参数 cause - 内部异常
     */
    public init(reasonCode: Int64, cause: Exception)
}

public class HandshakeFailedException <: Exception {
    /*
     * toString
     * 返回值 String - toString
     */
    public func toString(): String
}

public class ExceptionHelper {
    /*
     * 构造MqttException
     * 参数 reasonCode - 错误编号
     * 返回值 MqttException - 生成的MqttException
     */
    public static func createMqttException(reasonCode: Int64): MqttException

    /*
     * 构造MqttException
     * 参数 cause - 内部异常
     * 返回值 MqttException - 生成的MqttException
     */
    public static func createMqttException(cause: Exception): MqttException
}

11 消息监听

前置条件:NA

场景: 提供消息监听

约束:NA

可靠性:NA

11.1 消息监听

11.1.1 主要接口
public interface IMqttMessageListener {
    /*
     * 消息到达
     * 参数 topic - 主题名称
     * 参数 message - 负载消息正文
	 * @throws Exception - 若发生异常 客户端应自动关闭
     */
    func messageArrived(topic: String, message: MqttMessage): Unit
}

12 MqttPingSender

前置条件:NA

场景: 用于客户端测试连接的有效性

约束:NA

可靠性:NA

12.1 MqttPingSender

12.1.1 主要接口
public interface MqttPingSender {
    /*
     * 初始化
     * 参数 comms - ClientComms对象
     */
    func initialize(comms: ClientComms): Unit

    /*
     * 启动 ping sender
     */
    func start(): Unit

    /*
     * 停止 ping sender - 当发生错误或连接断开
     */
    func stop(): Unit

    /*
     * 延迟发送
     * 参数 delay - 延迟时间间隔
     */
    func schedule(delay: Duration): Unit
}

public class TimerPingSender <: MqttPingSender {
    /*
     * 初始化
     * 参数 comms - ClientComms对象
     */
    public func initialize(comms: ClientComms): Unit

    /*
     * 启动 ping sender
     */
    public func start(): Unit

    /*
     * 停止 ping sender - 当发生错误或连接断开
     */
    public func stop(): Unit

    /*
     * 延迟发送
     * 参数 delay - 延迟时间间隔
     */
    public func schedule(delay: Duration): Unit
}

13 客户端通讯模块 ClientComms

前置条件:NA

场景: 处理客户端与 MQTT 代理服务器之间的通信。它包含了客户端与服务器之间的所有通信相关的逻辑,包括消息的接收、处理和发送。各个功能组件的集成者。 依赖 网络模块 / 接收模块 / 发送模块 / 连接检测 / 连接选项 / 状态模块 / 持久化策略

约束:NA

可靠性:NA

13.1 ClientComms

13.1.1 主要接口
public class ClientComms {
    /*
     * 构建对象
     * 参数 client - 客户端对象
     * 参数 persistence - 持久化对象
     * 参数 pingSender - MqttPingSender对象
     * 参数 highResolutionTimer - 高精度时间
     * 返回值 ClientComms - ClientComms对象
	 * @throws MqttException - 客户端状态异常
     */
    public static func build(
        client: IMqttAsyncClient,
        persistence: MqttClientPersistence,
        pingSender: MqttPingSender,
        highResolutionTimer: HighResolutionTimer
    ): ClientComms

    /*
     * 发送
     * 参数 message - 负载信息对象
     * 参数 token - 用于异步的MqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func sendNoWait(message: MqttWireMessage, token: MqttToken): Unit

    /*
     * 删除信息
     * 参数 token - IMqttDeliveryToken对象
     * 返回值 Bool - 删除是否成功
	 * @throws MqttException - 客户端状态异常
     */
    public func removeMessage(token: IMqttDeliveryToken): Bool

    /*
     * 关闭
     * 参数 force - 是否强制
	 * @throws MqttException - 客户端状态异常
     */
    public func close(force: Bool): Unit

    /*
     * 连接
     * 参数 options - 连接配置对象
     * 参数 token - 用于异步的MqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func connect(options: MqttConnectOptions, token: MqttToken): Unit

    /*
     * 连接完成
     * 参数 cack - 连接报文MqttConnack
     * 参数 mex - 异常
	 * @throws MqttException - 客户端状态异常
     */
    public func connectComplete(cack: MqttConnack, mex: ?MqttException): Unit

    /*
     * 关闭连接
     * 参数 token - 用于异步的MqttToken对象
     * 参数 reason - 异常
	 * @throws MqttException - 客户端状态异常
     */
    public func shutdownConnection(token: ?MqttToken, reason: ?MqttException): Unit

    /*
     * 关闭连接
     * 参数 disconnect - MqttDisconnect对象
     * 参数 quiesceTimeout - 静载超时
     * 参数 token - 用于异步的MqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnect(disconnect: MqttDisconnect, quiesceTimeout: Duration, token: MqttToken): Unit

    /*
     * 强制关闭连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 关闭超时
	 * @throws MqttException - 客户端状态异常
     */
    public func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration): Unit

    /*
     * 强制断开连接
     * 参数 quiesceTimeout - 静载超时
     * 参数 disconnectTimeout - 断开超时
     * 参数 sendDisconnectPacket - 是否发送断开数据包
     */
    public func disconnectForcibly(quiesceTimeout: Duration, disconnectTimeout: Duration, sendDisconnectPacket: Bool): Unit

    /*
     * 是否已连接
     * 返回值 Bool - 是否已连接
     */
    public func isConnected(): Bool

    /*
     * 是否正在连接
     * 返回值 Bool - 是否正在连接
     */
    public func isConnecting(): Bool

    /*
     * 是否已经断开连接
     * 返回值 Bool - 是否已经断开连接
     */
    public func isDisconnected(): Bool

    /*
     * 是否正在断开连接
     * 返回值 Bool - 是否正在断开连接
     */
    public func isDisconnecting(): Bool

    /*
     * 是否关闭
     * 返回值 Bool - 是否关闭
     */
    public func isClosed(): Bool

    /*
     * 是否在休眠
     * 返回值 Bool - 是否在休眠
     */
    public func isResting(): Bool

    /*
     * 设置回调
     * 参数 callback - MqttCallback对象
     */
    public func setCallback(mqttCallback: MqttCallback): Unit

    /*
     * 设置回调
     * 参数 callback - MqttCallbackExtended对象
     */
    public func setReconnectCallback(callback: MqttCallbackExtended): Unit

    /*
     * 设置是否使用手动确认消息的接收
     * 参数 manualAcks - 否使用手动确认
     */
    public func setManualAcks(manualAcks: Bool): Unit

    /*
     * 消息到达完成
     * 参数 messageId - 消息id
     * 参数 qos - 级别
     */
    public func messageArrivedComplete(messageId: Int64, qos: Int64): Unit

    /*
     * 设置回调
     * 参数 topicFilter - 主题
     * 参数 messageListener - 负载信息监听回调
     */
    public func setMessageListener(topicFilter: String, messageListener: IMqttMessageListener): Unit

    /*
     * 删除回调
     * 参数 topicFilter - 主题
     */
    public func removeMessageListener(topicFilter: String): Unit

    /*
     * 获取网络模块index
     * 返回值 Int64 - 网络模块index
     */
    public func getNetworkModuleIndex(): Int64

    /*
     * 设置网络模块index
     * 参数 index - 网络模块index
     */
    public func setNetworkModuleIndex(index: Int64): Unit

    /*
     * 获取网络模块
     * 返回值 Array<NetworkModule> - 网络模块
     */
    public func getNetworkModules(): Array<NetworkModule>

    /*
     * 设置网络模块
     * 参数 networkModules - 网络模块
     */
    public func setNetworkModules(networkModules: Array<NetworkModule>): Unit

    /*
     * 获取PendingDeliveryTokens
     * 返回值 Array<MqttDeliveryToken> - PendingDeliveryTokens
     */
    public func getPendingDeliveryTokens(): Array<MqttDeliveryToken>

    /*
     * 获取客户端
     * 返回值 IMqttAsyncClient - 返回一个 IMqttAsyncClient
     */
    public func getClient(): IMqttAsyncClient

    /*
     * 获取存活时间
     * 返回值 Duration - 存活时间
     */
    public func getKeepAlive(): Duration

    /*
     * 获取设置对象
     * 返回值 MqttConnectOptions - 设置对象
     */
    public func getConOptions(): MqttConnectOptions

    /*
     * 获取bug信息
     * 返回值 HashMap<String, ToString> - bug 信息
     */
    public func getDebug(): HashMap<String, ToString>

    /*
     * 校验
     * 返回值 ?MqttToken - MqttToken
     */
    public func checkForActivity(): ?MqttToken

    /*
     * 校验
     * 参数 pingCallback - 动作回调
     */
    public func checkForActivity(pingCallback: ?IMqttActionListener): ?MqttToken

    /*
     * 设置休眠状态
     * 参数 resting - 休眠状态
     */
    public func setRestingState(resting: Bool): Unit

    /*
     * 设置断开后的缓冲信息
     * 参数 disconnectedMessageBuffer - 缓冲信息
     */
    public func setDisconnectedMessageBuffer(disconnectedMessageBuffer: DisconnectedMessageBuffer): Unit

    /*
     * 获取缓冲信息数量
     * 返回值 Int64 - 数量
     */
    public func getBufferedMessageCount(): Int64

    /*
     * 获取缓冲信息
     * 参数 bufferIndex - id
     * 返回值 MqttMessage - MqttMessage对象
     */
    public func getBufferedMessage(bufferIndex: Int64): MqttMessage

    /*
     * 删除缓冲信息
     * 参数 bufferIndex - id
     */
    public func deleteBufferedMessage(bufferIndex: Int64): Unit

    /*
     * 连接后先发送缓冲信息
     */
    public func notifyConnect(): Unit

    /*
     * 获取当前未发送完成的信息个数
     * 返回值 Int64 - 信息个数
     */
    public func getActualInFlight(): Int64
}

14 缓冲模块

前置条件:NA

场景: 缓冲模块

约束:NA

可靠性:NA

14.1 缓冲模块

14.1.1 主要接口
public interface IDiscardedBufferMessageCallback {
    /*
     * 设置信息舍弃回调
     * 参数 callback - 回调
     */
    func messageDiscarded(message: MqttWireMessage): Unit
}

public interface IDisconnectedBufferCallback {
    /*
     * 发布缓冲的消息
     * 参数 bufferedMessage - 缓冲的消息
     */
    func publishBufferedMessage(bufferedMessage: BufferedMessage): Unit
}

public class DisconnectedMessageBuffer {
    /*
     * 初始化
     * 参数 options - 配置对象
     */
    public init(options: DisconnectedBufferOptions)

    /*
     * 添加信息
     * 参数 message - 信息
     * 参数 token - MqttToken对象
	 * @throws MqttException - 客户端状态异常
     */
    public func putMessage(message: MqttWireMessage, token: ?MqttToken): Unit

    /*
     * 获取信息
     * 参数 messageIndex - 信息index
     * 返回值 BufferedMessage - 信息
     */
    public func getMessage(messageIndex: Int64): BufferedMessage

    /*
     * 删除信息
     * 参数 messageIndex - 信息index
	 * @throws MqttException - 客户端状态异常
     */
    public func deleteMessage(messageIndex: Int64): Unit

    /*
     * 获取信息个数
     * 返回值 Int64 - 信息个数
     */
    public func getMessageCount(): Int64

    /*
     * 将缓冲信息更新到连接流中
     */
    public func run(): Unit

    /*
     * 设置发布后的回调
     * 参数 callback - 回调
     */
    public func setPublishCallback(callback: IDisconnectedBufferCallback): Unit

    /*
     * 是否坚持缓冲
     * 返回值 Bool - 是否坚持缓冲
     */
    public func isPersistBuffer(): Bool

    /*
     * 设置信息舍弃回调
     * 参数 callback - 回调
     */
    public func setMessageDiscardedCallBack(callback: IDiscardedBufferMessageCallback): Unit
}

public class DisconnectedBufferOptions {
    /*
     * 初始化
     */
    public init()

    /*
     * 获取缓冲的大小
     * 返回值 Int64 - 缓冲的大小
     */
    public func getBufferSize(): Int64

    /*
     * 设置缓冲的大小
     * 参数 bufferSize - 缓冲的大小
     */
    public func setBufferSize(bufferSize: Int64): Unit

    /*
     * 是否支持缓冲
     * 返回值 Bool - 是否支持缓冲
     */
    public func isBufferEnabled(): Bool

    /*
     * 设置支持缓冲
     * 参数 bufferEnabled - 设置支持缓冲
     */
    public func setBufferEnabled(bufferEnabled: Bool): Unit

    /*
     * 是否坚持缓冲
     * 返回值 Bool - 是否坚持缓冲
     */
    public func isPersistBuffer(): Bool

    /*
     * 设置坚持缓冲
     * 参数 persistBuffer - 设置坚持缓冲
     */
    public func setPersistBuffer(persistBuffer: Bool): Unit

    /*
     * 是否删除老的信息
     * 返回值 Bool - 是否删除老的信息
     */
    public func isDeleteOldestMessages(): Bool

    /*
     * 设置是否删除老的信息
     * 参数 deleteOldestMessages - 设置是否删除老的信息
     */
    public func setDeleteOldestMessages(deleteOldestMessages: Bool): Unit
}

public class BufferedMessage {
    /*
     * 初始化
     * 参数 message - 信息
     * 参数 token - MqttToken
     */
    public init(message: MqttWireMessage, token: MqttToken)

    /*
     * 获取信息
     * 返回值 MqttWireMessage - MqttWireMessage
     */
    public func getMessage(): MqttWireMessage

    /*
     * 获取token
     * 返回值 MqttToken - MqttToken
     */
    public func getToken(): MqttToken
}