Skip to content

Commit 19bdb73

Browse files
authored
Documentation (#61)
* Fix reading wrong property from CONNACK for pingreq interval * Add build documentation scripts * Add comments for properties * Add comments to MQTTReason * Comments for connection return values * More comments
1 parent cc4beb0 commit 19bdb73

File tree

9 files changed

+178
-8
lines changed

9 files changed

+178
-8
lines changed

Sources/MQTTNIO/MQTTClient.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import NIOSSL
1111
import NIOTransportServices
1212

1313
/// Swift NIO MQTT Client
14+
///
15+
/// Main public interface providing methods for connecting to a server, publishing
16+
/// and subscribing to MQTT topics
17+
/// ```
18+
/// let client = MQTTClient(
19+
/// host: "mqtt.eclipse.org",
20+
/// port: 1883,
21+
/// identifier: "My Client",
22+
/// eventLoopGroupProvider: .createNew
23+
/// )
24+
/// try client.connect().wait()
25+
/// ```
1426
public final class MQTTClient {
1527
/// EventLoopGroup used by MQTTCllent
1628
public let eventLoopGroup: EventLoopGroup
@@ -232,7 +244,7 @@ public final class MQTTClient {
232244
return self.disconnect(packet: MQTTDisconnectPacket())
233245
}
234246

235-
/// Return is client has an active connection to broker
247+
/// Return if client has an active connection to broker
236248
public func isActive() -> Bool {
237249
return self.connection?.channel.isActive ?? false
238250
}
@@ -383,8 +395,8 @@ extension MQTTClient {
383395
for property in connack.properties.properties {
384396
// alter pingreq interval based on session expiry returned from server
385397
if let connection = self.connection {
386-
if case .sessionExpiryInterval(let sessionExpiryInterval) = property {
387-
let pingTimeout = TimeAmount.seconds(max(Int64(sessionExpiryInterval - 5), 5))
398+
if case .serverKeepAlive(let keepAliveInterval) = property {
399+
let pingTimeout = TimeAmount.seconds(max(Int64(keepAliveInterval - 5), 5))
388400
connection.updatePingreqTimeout(pingTimeout)
389401
}
390402
}

Sources/MQTTNIO/MQTTClientV5.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import NIO
22

33
extension MQTTClient {
4+
/// Provides implementations of functions that expose MQTT Version 5.0 features
45
public struct V5 {
56
let client: MQTTClient
67

Sources/MQTTNIO/MQTTConfiguration.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import NIOSSL
44
#endif
55

66
extension MQTTClient {
7+
/// Version of MQTT server to connect to
78
public enum Version {
89
case v3_1_1
910
case v5_0
@@ -25,6 +26,7 @@ extension MQTTClient {
2526

2627
/// Configuration for MQTTClient
2728
public struct Configuration {
29+
/// Initialize MQTTClient configuration struct
2830
public init(
2931
version: Version = .v3_1_1,
3032
disablePing: Bool = false,
@@ -59,7 +61,7 @@ extension MQTTClient {
5961
self.webSocketMaxFrameSize = webSocketMaxFrameSize
6062
}
6163

62-
/// disable the sending of pingreq messages
64+
/// Version of MQTT server client is connecting to
6365
public let version: Version
6466
/// disable the sending of pingreq messages
6567
public let disablePing: Bool

Sources/MQTTNIO/MQTTCoreTypesV5.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct MQTTConnackV5 {
1212

1313
/// MQTT V5 ACK information. Returned with PUBACK, PUBREL
1414
public struct MQTTAckV5 {
15-
/// MQTT v5 disconnection reason
15+
/// MQTT v5 reason code
1616
public let reason: MQTTReasonCode
1717
/// MQTT v5 properties
1818
public let properties: MQTTProperties
@@ -25,9 +25,13 @@ public struct MQTTAckV5 {
2525

2626
/// MQTT SUBSCRIBE packet parameters.
2727
public struct MQTTSubscribeInfoV5 {
28+
/// Retain handling options
2829
public enum RetainHandling: UInt8 {
30+
/// always send retain message
2931
case sendAlways = 0
32+
/// send retain if new
3033
case sendIfNew = 1
34+
/// do not send retain message
3135
case doNotSend = 2
3236
}
3337

@@ -65,7 +69,7 @@ public struct MQTTSubscribeInfoV5 {
6569
///
6670
/// Contains data returned in subscribe/unsubscribe ack packets
6771
public struct MQTTSubackV5 {
68-
/// MQTT v5 disconnection reason
72+
/// MQTT v5 subscription reason code
6973
public let reasons: [MQTTReasonCode]
7074
/// MQTT v5 properties
7175
public let properties: MQTTProperties
@@ -80,7 +84,7 @@ public struct MQTTSubackV5 {
8084
///
8185
/// Contains data returned in subscribe/unsubscribe ack packets
8286
public struct MQTTAuthV5 {
83-
/// MQTT v5 disconnection reason
87+
/// MQTT v5 authentication reason code
8488
public let reason: MQTTReasonCode
8589
/// MQTT v5 properties
8690
public let properties: MQTTProperties

Sources/MQTTNIO/MQTTError.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
/// MQTTClient errors
22
public enum MQTTError: Error {
3+
/// Value returned in connection error
34
public enum ConnectionReturnValue: UInt8 {
5+
/// connection was accepted
46
case accepted = 0
7+
/// The Server does not support the version of the MQTT protocol requested by the Client.
58
case unacceptableProtocolVersion = 1
9+
/// The Client Identifier is a valid string but is not allowed by the Server.
610
case identifierRejected = 2
11+
/// The MQTT Server is not available.
712
case serverUnavailable = 3
13+
/// The Server does not accept the User Name or Password specified by the Client
814
case badUserNameOrPassword = 4
15+
/// The client is not authorized to connect
916
case notAuthorized = 5
17+
/// Return code was unrecognised
1018
case unrecognizedReturnValue = 0xFF
1119
}
1220

Sources/MQTTNIO/MQTTProperties.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,65 @@ import NIO
22

33
/// MQTT v5.0 properties. A property consists of a identifier and a value
44
public struct MQTTProperties {
5+
/// MQTT Property
56
public enum Property: Equatable {
7+
/// Payload format: 0 = bytes, 1 = UTF8 string (available for PUBLISH)
68
case payloadFormat(UInt8)
9+
/// Message expiry indicates the lifetime of the message (available for PUBLISH)
710
case messageExpiry(UInt32)
11+
/// String describing the content of the message eg "application/json" (available for PUBLISH)
812
case contentType(String)
13+
/// Response topic used in request/response interactions (available for PUBLISH)
914
case responseTopic(String)
15+
/// Correlation data used to id a request/response in request/response interactions (available for PUBLISH)
1016
case correlationData(ByteBuffer)
17+
/// Subscription identifier set in SUBSCRIBE packet and included in related PUBLISH packet
18+
/// (available for PUBLISH, SUBSCRIBE)
1119
case subscriptionIdentifier(UInt)
20+
/// Interval before session expires (available for CONNECT, CONNACK, DISCONNECT)
1221
case sessionExpiryInterval(UInt32)
22+
/// Client identifier assigned to client if they didn't provide one (available for CONNACK)
1323
case assignedClientIdentifier(String)
24+
/// Indication to client on how long server will keep connection without activity (available for CONNACK)
1425
case serverKeepAlive(UInt16)
26+
/// String indicating the authentication method to use (available for CONNECT, CONNACK, AUTH)
1527
case authenticationMethod(String)
28+
/// Data used in authentication (available for CONNECT, CONNACK, AUTH)
1629
case authenticationData(ByteBuffer)
30+
/// Request that server sends a reason string in its CONNACK or DISCONNECT packets (available for CONNECT)
1731
case requestProblemInformation(UInt8)
32+
/// Interval to wait before publishing connect will message (available for CONNECT will)
1833
case willDelayInterval(UInt32)
34+
/// Request response information from server (available for CONNECT)
1935
case requestResponseInformation(UInt8)
36+
/// Response information from server. Commonly used to pass a globally unique portion
37+
/// of the topic tree for this client (available for CONNACK)
2038
case responseInformation(String)
39+
/// Server uses serverReference in CONNACK to indicate to either use another server or that the server has moved
40+
/// (available for CONNACK)
2141
case serverReference(String)
42+
/// String representing the reason associated with this response (available for CONNACK,
43+
/// PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, DISCONNECT, AUTH)
2244
case reasonString(String)
45+
/// Maximum number of PUBLISH, PUBREL messages that can be sent without receiving a response (available for CONNECT, CONNACK)
2346
case receiveMaximum(UInt16)
47+
/// Maximum number for topic alias (available for CONNECT, CONNACK)
2448
case topicAliasMaximum(UInt16)
49+
/// Topic alias. Use instead of full topic name to reduce packet size (available for PUBLISH)
2550
case topicAlias(UInt16)
51+
/// Maximum QoS supported by server (available for CONNACK)
2652
case maximumQoS(MQTTQoS)
53+
/// Does server support retained publish packets (available for CONNACK)
2754
case retainAvailable(UInt8)
55+
/// User property, key and value (available for all packets)
2856
case userProperty(String, String)
57+
/// Maximum packet size supported (available for CONNECT, CONNACK)
2958
case maximumPacketSize(UInt32)
59+
/// Does server support wildcard subscription (available for CONNACK)
3060
case wildcardSubscriptionAvailable(UInt8)
61+
/// Does server support subscription identifiers (available for CONNACK)
3162
case subscriptionIdentifierAvailable(UInt8)
63+
/// Does server support shared subscriptions (available for CONNACK)
3264
case sharedSubscriptionAvailable(UInt8)
3365
}
3466

Sources/MQTTNIO/MQTTReason.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,97 @@
11
/// MQTT v5.0 reason codes. A reason code is a one byte unsigned value that indicates the result of an operation.
22
/// Reason codes less than 128 are considered successful. Codes greater than or equal to 128 are considered
3-
/// a failure
3+
/// a failure. These are returned by CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, DISCONNECT and
4+
/// AUTH packets
45
public enum MQTTReasonCode: UInt8 {
6+
/// Success (available for all). For SUBACK mean QoS0 is available
57
case success = 0
8+
/// The subscription is accepted and the maximum QoS sent will be QoS 1. This might be a lower QoS than was requested.
69
case grantedQoS1 = 1
10+
/// The subscription is accepted and any received QoS will be sent to this subscription.
711
case grantedQoS2 = 2
12+
/// The Client wishes to disconnect but requires that the Server also publishes its Will Message.
813
case disconnectWithWill = 4
14+
/// The PUBLISH message is accepted but there are no subscribers. This is sent only by the Server. If the Server knows that
15+
/// there are no matching subscribers, it MAY use this Reason Code instead of 0x00 (Success).
916
case noMatchingSubscriber = 16
17+
/// No matching Topic Filter is being used by the Client.
1018
case noSubscriptionExisted = 17
19+
/// Continue the authentication with another step
1120
case continueAuthentication = 24
21+
/// Initiate a re-authentication
1222
case reAuthenticate = 25
23+
/// Unaccpeted and the Server either does not wish to reveal the reason or none of the other Reason Codes apply.
1324
case unspecifiedError = 128
25+
/// Data within the packet could not be correctly parsed.
1426
case malformedPacket = 129
27+
/// Data in the packet does not conform to this specification.
1528
case protocolError = 130
29+
/// Packet is valid but the server does not accept it
1630
case implementationSpecificError = 131
31+
/// The Server does not support the version of the MQTT protocol requested by the Client.
1732
case unsupportedProtocolVersion = 132
33+
/// The Client Identifier is a valid string but is not allowed by the Server.
1834
case clientIdentifierNotValid = 133
35+
/// The Server does not accept the User Name or Password specified by the Client
1936
case badUsernameOrPassword = 134
37+
/// The client is not authorized to do this
2038
case notAuthorized = 135
39+
/// The MQTT Server is not available.
2140
case serverUnavailable = 136
41+
/// The Server is busy. Try again later.
2242
case serverBusy = 137
43+
/// This Client has been banned by administrative action. Contact the server administrator.
2344
case banned = 138
45+
/// The Server is shutting down.
2446
case serverShuttingDown = 139
47+
/// The authentication method is not supported or does not match the authentication method currently in use.
2548
case badAuthenticationMethod = 140
49+
/// The Connection is closed because no packet has been received for 1.5 times the Keepalive time.
2650
case keepAliveTimeout = 141
51+
/// Another Connection using the same ClientID has connected causing this Connection to be closed.
2752
case sessionTakenOver = 142
53+
/// The Topic Filter is correctly formed but is not allowed for this Client.
2854
case topicFilterInvalid = 143
55+
/// The Topic Name is not malformed, but is not accepted by this Client or Server.
2956
case topicNameInvalid = 144
57+
/// The specified Packet Identifier is already in use. This might indicate a mismatch in the Session State between the Client and Server.
3058
case packetIdentifierInUse = 145
59+
/// The Packet Identifier is not known. This is not an error during recovery, but at other times indicates a mismatch between
60+
/// the Session State on the Client and Server.
3161
case packetIdentifierNotFound = 146
62+
/// The Client or Server has received more than Receive Maximum publication for which it has not sent PUBACK or PUBCOMP.
3263
case receiveMaximumExceeded = 147
64+
/// The Client or Server has received a PUBLISH packet containing a Topic Alias which is greater than the Maximum Topic Alias it
65+
/// sent in the CONNECT or CONNACK packet.
3366
case topicAliasInvalid = 148
67+
/// The packet exceeded the maximum permissible size.
3468
case packetTooLarge = 149
69+
/// The received data rate is too high.
3570
case messageRateTooHigh = 150
71+
/// An implementation or administrative imposed limit has been exceeded.
3672
case quotaExceeeded = 151
73+
/// The Connection is closed due to an administrative action.
3774
case administrativeAction = 152
75+
/// The PUBLISH payload format does not match the one specified in the Payload Format Indicator.
3876
case payloadFormatInvalid = 153
77+
/// The Server does not support retained messages, and retain was set to 1.
3978
case retainNotSupported = 154
79+
/// The Server does not support the QoS set
4080
case qosNotSupported = 155
81+
/// The Client should temporarily use another server.
4182
case useAnotherServer = 156
83+
/// The Client should permanently use another server.
4284
case serverMoved = 157
85+
/// The Server does not support Shared Subscriptions for this Client.
4386
case sharedSubscriptionsNotSupported = 158
87+
/// The connection rate limit has been exceeded.
4488
case connectionRateExceeeded = 159
89+
/// The maximum connection time authorized for this connection has been exceeded.
4590
case maximumConnectTime = 160
91+
/// The Server does not support Subscription Identifiers; the subscription is not accepted.
4692
case subscriptionIdentifiersNotSupported = 161
93+
/// The Server does not support Wildcard Subscriptions; the subscription is not accepted.
4794
case wildcardSubscriptionsNotSupported = 162
95+
/// Reason code was unrecognised
4896
case unrecognisedReason = 255
4997
}

scripts/build-docs.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
set -eux
4+
5+
PROJECT=${1:-}
6+
CWD=$(pwd)
7+
TEMP_DIR=$(mktemp -d)
8+
9+
get_latest_version() {
10+
RELEASE_REVISION=$(git rev-list --tags --max-count=1)
11+
echo $(git describe --tags "$RELEASE_REVISION")
12+
}
13+
14+
build_docs() {
15+
VERSION=$(get_latest_version)
16+
jazzy \
17+
--clean \
18+
--author "Adam Fowler" \
19+
--author_url https://github.com/adam-fowler \
20+
--github_url https://github.com/adam-fowler/mqtt-nio \
21+
--module-version "$VERSION" \
22+
--output "$CWD"/docs/
23+
}
24+
25+
build_docs

scripts/commit-docs.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
set -eux
4+
5+
FOLDER=current
6+
SUBFOLDER=${1:-}
7+
8+
# stash everything that isn't in docs, store result in STASH_RESULT
9+
STASH_RESULT=$(git stash push -- ":(exclude)docs")
10+
# get branch name
11+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
12+
REVISION_HASH=$(git rev-parse HEAD)
13+
14+
git checkout gh-pages
15+
if [[ -z "$SUBFOLDER" ]]; then
16+
# copy contents of docs to docs/current replacing the ones that are already there
17+
rm -rf "$FOLDER"
18+
mv docs/ "$FOLDER"/
19+
# commit
20+
git add --all "$FOLDER"
21+
else
22+
# copy contents of subfolder of docs to docs/current replacing the ones that are already there
23+
rm -rf "$FOLDER"/"$SUBFOLDER"
24+
mv docs/"$SUBFOLDER"/ "$FOLDER"/"$SUBFOLDER"
25+
# commit
26+
git add --all "$FOLDER"/"$SUBFOLDER"
27+
fi
28+
29+
git status
30+
git commit -m "Documentation for https://github.com/adam-fowler/mqtt-nio/tree/$REVISION_HASH"
31+
git push
32+
# return to branch
33+
git checkout $CURRENT_BRANCH
34+
35+
if [ "$STASH_RESULT" != "No local changes to save" ]; then
36+
git stash pop
37+
fi
38+

0 commit comments

Comments
 (0)