Skip to content

Commit f7ac693

Browse files
author
Al S-M
committed
API updates in preparation for 1.0 release
1 parent 7b31895 commit f7ac693

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+367
-1825
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ Samples are available in the `/samples` directory for reference.
4343
Runtime tracing
4444
---------------
4545

46-
Tracing is enabled by using the `SetTraceLevel` option when creating a ClientOptions struct. See the ClientOptions
47-
documentation for more details.
46+
Tracing is enabled by assigning logs (from the Go log package) to the logging endpoints, ERROR, CRITICAL, WARN and DEBUG
4847

4948

5049
Reporting bugs

client.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Client struct {
7272
workers sync.WaitGroup
7373
}
7474

75-
// NewClient will create an MQTT v3.1 client with all of the options specified
75+
// NewClient will create an MQTT v3.1.1 client with all of the options specified
7676
// in the provided ClientOptions. The client must have the Start method called
7777
// on it before it may be used. This is to make sure resources (such as a net
7878
// connection) are created before the application is actually ready.
@@ -112,6 +112,10 @@ func (c *Client) setConnected(status bool) {
112112
c.connected = status
113113
}
114114

115+
//ErrNotConnected is the error returned from function calls that are
116+
//made when the client is not connected to a broker
117+
var ErrNotConnected = errors.New("Not Connected")
118+
115119
// Connect will create a connection to the message broker
116120
// If clean session is false, then a slice will
117121
// be returned containing Receipts for all messages
@@ -165,17 +169,17 @@ func (c *Client) Connect() Token {
165169
} else {
166170
ERROR.Println(CLI, err.Error())
167171
WARN.Println(CLI, "failed to connect to broker, trying next")
168-
rc = packets.NetworkError
172+
rc = packets.ErrNetworkError
169173
}
170174
}
171175

172176
if c.conn == nil {
173177
ERROR.Println(CLI, "Failed to connect to a broker")
174178
t.returnCode = rc
175-
if rc != packets.NetworkError {
176-
t.err = connErrors[rc]
179+
if rc != packets.ErrNetworkError {
180+
t.err = packets.ConnErrors[rc]
177181
} else {
178-
t.err = errors.New(connErrors[rc].Error() + " : " + err.Error())
182+
t.err = errors.New(packets.ConnErrors[rc].Error() + " : " + err.Error())
179183
}
180184
t.flowComplete()
181185
return
@@ -274,7 +278,7 @@ func (c *Client) reconnect() {
274278
} else {
275279
ERROR.Println(CLI, err.Error())
276280
WARN.Println(CLI, "failed to connect to broker, trying next")
277-
rc = packets.NetworkError
281+
rc = packets.ErrNetworkError
278282
}
279283
}
280284
if rc != 0 {
@@ -318,7 +322,7 @@ func (c *Client) connect() byte {
318322
if err != nil {
319323
ERROR.Println(NET, "connect got error", err)
320324
//c.errors <- err
321-
return packets.NetworkError
325+
return packets.ErrNetworkError
322326
}
323327
msg := ca.(*packets.ConnackPacket)
324328

@@ -474,3 +478,9 @@ func (c *Client) Unsubscribe(topics ...string) Token {
474478
DEBUG.Println(CLI, "exit Unsubscribe")
475479
return token
476480
}
481+
482+
//DefaultConnectionLostHandler is a definition of a function that simply
483+
//reports to the DEBUG log the reason for the client losing a connection.
484+
func DefaultConnectionLostHandler(client *Client, reason error) {
485+
DEBUG.Println("Connection lost:", reason.Error())
486+
}

fvt/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ Launch mosquitto from the fvt directory, specifiying mosquitto.cfg as config fil
6565

6666
``ex: /usr/bin/mosquitto -c ./mosquitto.cfg``
6767

68-
Note: Mosquitto requires SSL 1.1 or better, while Go 1.1.2 (current) supports
69-
only SSL v1.0. However, Go 1.2.* (near release) supports SSL v1.1 and SSL v1.2.
68+
Note: Mosquitto requires SSL 1.1 or better, while Go 1.1.2 supports
69+
only SSL v1.0. However, Go 1.2+ supports SSL v1.1 and SSL v1.2.
7070

7171

7272
Other Notes
7373
-----------
74-
Go 1.1.2 does not support intermediate certificates, however Go 1.2 does.
74+
Go 1.1.2 does not support intermediate certificates, however Go 1.2+ does.

fvt/setup_IMA.sh

100755100644
File mode changed.

messageids.go

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const (
3636
func (mids *messageIds) freeID(id uint16) {
3737
mids.Lock()
3838
defer mids.Unlock()
39-
//trace_v(MID, "freeing message id: %v", id)
4039
delete(mids.index, id)
4140
}
4241

net.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func outgoing(c *Client) {
110110
msg := pub.p.(*packets.PublishPacket)
111111
if msg.Qos != 0 && msg.MessageID == 0 {
112112
msg.MessageID = c.getID(pub.t)
113-
pub.t.(*PublishToken).messageId = msg.MessageID
113+
pub.t.(*PublishToken).messageID = msg.MessageID
114114
}
115115
//persist_obound(c.persist, msg)
116116

@@ -273,7 +273,9 @@ func alllogic(c *Client) {
273273
// Call onConnectionLost or default error handler
274274
if c.IsConnected() {
275275
go c.options.OnConnectionLost(c, err)
276-
go c.reconnect()
276+
if c.options.AutoReconnect {
277+
go c.reconnect()
278+
}
277279
}
278280
return
279281
}

oops.go

-39
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,6 @@
1414

1515
package mqtt
1616

17-
import (
18-
"errors"
19-
20-
"git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git/packets"
21-
)
22-
23-
/*
24-
* Connect Errors
25-
*/
26-
var connErrors = map[byte]error{
27-
packets.Accepted: nil,
28-
packets.RefusedBadProtocolVersion: errors.New("Unnacceptable protocol version"),
29-
packets.RefusedIDRejected: errors.New("Identifier rejected"),
30-
packets.RefusedServerUnavailable: errors.New("Server Unavailable"),
31-
packets.RefusedBadUsernameOrPassword: errors.New("Bad user name or password"),
32-
packets.RefusedNotAuthorised: errors.New("Not Authorized"),
33-
packets.NetworkError: errors.New("Network Error"),
34-
packets.ProtocolViolation: errors.New("Protocol Violation"),
35-
}
36-
37-
var ErrNotConnected = errors.New("Not Connected")
38-
39-
/*
40-
* Topic Errors
41-
*/
42-
var ErrInvalidTopicNameEmptyString = errors.New("Invalid TopicName - may not be empty string")
43-
var ErrInvalidTopicNameWildcard = errors.New("Invalid TopicName - may not contain wild card")
44-
var ErrInvalidTopicFilterEmptyString = errors.New("Invalid TopicFilter - may not be empty string")
45-
var ErrInvalidTopicFilterMultilevel = errors.New("Invalid TopicFilter - multi-level wildcard must be last level")
46-
47-
/*
48-
* QoS Errors
49-
*/
50-
var ErrInvalidQoS = errors.New("Invalid QoS")
51-
52-
func DefaultConnectionLostHandler(client *Client, reason error) {
53-
DEBUG.Println("Connection lost:", reason.Error())
54-
}
55-
5617
func chkerr(e error) {
5718
if e != nil {
5819
panic(e)

options.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type ClientOptions struct {
5454
TLSConfig tls.Config
5555
KeepAlive time.Duration
5656
MaxReconnectInterval time.Duration
57+
AutoReconnect bool
5758
Store Store
5859
DefaultPublishHander MessageHandler
5960
OnConnect OnConnectHandler
@@ -85,6 +86,7 @@ func NewClientOptions() *ClientOptions {
8586
TLSConfig: tls.Config{},
8687
KeepAlive: 30 * time.Second,
8788
MaxReconnectInterval: 10 * time.Minute,
89+
AutoReconnect: true,
8890
Store: nil,
8991
OnConnect: nil,
9092
OnConnectionLost: DefaultConnectionLostHandler,
@@ -234,12 +236,22 @@ func (o *ClientOptions) SetConnectionLostHandler(onLost ConnectionLostHandler) *
234236

235237
// SetWriteTimeout puts a limit on how long a mqtt publish should block until it unblocks with a
236238
// timeout error. A duration of 0 never times out.
237-
func (o *ClientOptions) SetWriteTimeout(t time.Duration) {
239+
func (o *ClientOptions) SetWriteTimeout(t time.Duration) *ClientOptions {
238240
o.WriteTimeout = t
241+
return o
239242
}
240243

241244
// SetMaxReconnectInterval sets the maximum time that will be waited between reconnection attempts
242245
// when connection is lost
243-
func (o *ClientOptions) SetMaxReconnectInterval(t time.Duration) {
246+
func (o *ClientOptions) SetMaxReconnectInterval(t time.Duration) *ClientOptions {
244247
o.MaxReconnectInterval = t
248+
return o
249+
}
250+
251+
// SetAutoReconnect sets whether the automatic reconnection logic should be used
252+
// when the connection is lost, even if disabled the ConnectionLostHandler is still
253+
// called
254+
func (o *ClientOptions) SetAutoReconnect(a bool) *ClientOptions {
255+
o.AutoReconnect = a
256+
return o
245257
}

packets/connack.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"io"
88
)
99

10-
//CONNACK packet
11-
10+
//ConnackPacket is an internal representation of the fields of the
11+
//Connack MQTT packet
1212
type ConnackPacket struct {
1313
FixedHeader
1414
TopicNameCompression byte
@@ -36,15 +36,22 @@ func (ca *ConnackPacket) Write(w io.Writer) error {
3636
return err
3737
}
3838

39+
//Unpack decodes the details of a ControlPacket after the fixed
40+
//header has been read
3941
func (ca *ConnackPacket) Unpack(b io.Reader) {
4042
ca.TopicNameCompression = decodeByte(b)
4143
ca.ReturnCode = decodeByte(b)
4244
}
4345

46+
//Details returns a Details struct containing the Qos and
47+
//MessageID of this ControlPacket
4448
func (ca *ConnackPacket) Details() Details {
4549
return Details{Qos: 0, MessageID: 0}
4650
}
4751

52+
//UUID returns the unique ID assigned to the ControlPacket when
53+
//it was originally received. Note: this is not related to the
54+
//MessageID field for MQTT packets
4855
func (ca *ConnackPacket) UUID() uuid.UUID {
4956
return ca.uuid
5057
}

packets/connect.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"io"
88
)
99

10-
//CONNECT packet
11-
10+
//ConnectPacket is an internal representation of the fields of the
11+
//Connect MQTT packet
1212
type ConnectPacket struct {
1313
FixedHeader
1414
ProtocolName string
@@ -63,6 +63,8 @@ func (c *ConnectPacket) Write(w io.Writer) error {
6363
return err
6464
}
6565

66+
//Unpack decodes the details of a ControlPacket after the fixed
67+
//header has been read
6668
func (c *ConnectPacket) Unpack(b io.Reader) {
6769
c.ProtocolName = decodeString(b)
6870
c.ProtocolVersion = decodeByte(b)
@@ -88,32 +90,39 @@ func (c *ConnectPacket) Unpack(b io.Reader) {
8890
}
8991
}
9092

93+
//Validate performs validation of the fields of a Connect packet
9194
func (c *ConnectPacket) Validate() byte {
9295
if c.PasswordFlag && !c.UsernameFlag {
93-
return RefusedBadUsernameOrPassword
96+
return ErrRefusedBadUsernameOrPassword
9497
}
9598
if c.ReservedBit != 0 {
96-
fmt.Println("Bad reserved bit")
97-
return ProtocolViolation
99+
//Bad reserved bit
100+
return ErrProtocolViolation
98101
}
99102
if (c.ProtocolName == "MQIsdp" && c.ProtocolVersion != 3) || (c.ProtocolName == "MQTT" && c.ProtocolVersion != 4) {
100-
return RefusedBadProtocolVersion
103+
//Mismatched or unsupported protocol version
104+
return ErrRefusedBadProtocolVersion
101105
}
102106
if c.ProtocolName != "MQIsdp" && c.ProtocolName != "MQTT" {
103-
fmt.Println("Bad protocol name")
104-
return ProtocolViolation
107+
//Bad protocol name
108+
return ErrProtocolViolation
105109
}
106110
if len(c.ClientIdentifier) > 65535 || len(c.Username) > 65535 || len(c.Password) > 65535 {
107-
fmt.Println("Bad size field")
108-
return ProtocolViolation
111+
//Bad size field
112+
return ErrProtocolViolation
109113
}
110114
return Accepted
111115
}
112116

117+
//Details returns a Details struct containing the Qos and
118+
//MessageID of this ControlPacket
113119
func (c *ConnectPacket) Details() Details {
114120
return Details{Qos: 0, MessageID: 0}
115121
}
116122

123+
//UUID returns the unique ID assigned to the ControlPacket when
124+
//it was originally received. Note: this is not related to the
125+
//MessageID field for MQTT packets
117126
func (c *ConnectPacket) UUID() uuid.UUID {
118127
return c.uuid
119128
}

packets/disconnect.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"io"
77
)
88

9-
//DISCONNECT packet
10-
9+
//DisconnectPacket is an internal representation of the fields of the
10+
//Disconnect MQTT packet
1111
type DisconnectPacket struct {
1212
FixedHeader
1313
uuid uuid.UUID
@@ -25,13 +25,20 @@ func (d *DisconnectPacket) Write(w io.Writer) error {
2525
return err
2626
}
2727

28+
//Unpack decodes the details of a ControlPacket after the fixed
29+
//header has been read
2830
func (d *DisconnectPacket) Unpack(b io.Reader) {
2931
}
3032

33+
//Details returns a Details struct containing the Qos and
34+
//MessageID of this ControlPacket
3135
func (d *DisconnectPacket) Details() Details {
3236
return Details{Qos: 0, MessageID: 0}
3337
}
3438

39+
//UUID returns the unique ID assigned to the ControlPacket when
40+
//it was originally received. Note: this is not related to the
41+
//MessageID field for MQTT packets
3542
func (d *DisconnectPacket) UUID() uuid.UUID {
3643
return d.uuid
3744
}

0 commit comments

Comments
 (0)