@@ -3,9 +3,10 @@ package mosquitto
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "net"
7
+ "regexp"
6
8
"strconv"
7
9
"sync"
8
- "time"
9
10
10
11
// Frameworks
11
12
gopi "github.com/djthorpe/gopi/v2"
@@ -18,20 +19,17 @@ import (
18
19
// TYPES
19
20
20
21
type Mosquitto struct {
21
- ClientId string
22
- User string
23
- Password string
24
- Host string
25
- Port uint
26
- Keepalive time.Duration
27
- Bus gopi.Bus
22
+ ClientId string
23
+ User string
24
+ Password string
25
+ Broker string
26
+ Bus gopi.Bus
28
27
}
29
28
30
29
type mosquitto struct {
31
30
host string
32
31
port uint
33
32
client * mosq.Client
34
- keepalive time.Duration
35
33
connected bool
36
34
bus gopi.Bus
37
35
@@ -40,6 +38,13 @@ type mosquitto struct {
40
38
sync.WaitGroup
41
39
}
42
40
41
+ ////////////////////////////////////////////////////////////////////////////////
42
+ // GLOBALS
43
+
44
+ var (
45
+ reHostPort = regexp .MustCompile ("^([^\\ :]+)\\ :(\\ d+)$" )
46
+ )
47
+
43
48
////////////////////////////////////////////////////////////////////////////////
44
49
// IMPLEMENTATION gopi.Unit
45
50
@@ -60,7 +65,6 @@ func (config Mosquitto) New(log gopi.Logger) (gopi.Unit, error) {
60
65
// IMPLEMENTATION mosquitto.Client
61
66
62
67
func (this * mosquitto ) Init (config Mosquitto ) error {
63
-
64
68
// Bus
65
69
if config .Bus == nil {
66
70
return gopi .ErrBadParameter .WithPrefix ("bus" )
@@ -71,29 +75,26 @@ func (this *mosquitto) Init(config Mosquitto) error {
71
75
// Initialize
72
76
if err := mosq .Init (); err != nil {
73
77
return err
74
- } else if client , err := mosq .New (config .ClientId , true , uintptr ( 0 ) ); err != nil {
78
+ } else if client , err := mosq .New (config .ClientId , true , 0 ); err != nil {
75
79
return fmt .Errorf ("New: %w" , err )
76
80
} else {
77
81
this .client = client
78
82
}
79
83
80
- // Check host and port
81
- if config .Host == "" {
82
- return gopi .ErrBadParameter .WithPrefix ("host" )
83
- } else {
84
- this .host = config .Host
85
- }
86
- if config .Port == 0 {
87
- this .port = mosq .MOSQ_DEFAULT_PORT
88
- } else {
89
- this .port = config .Port
84
+ // Add the port on the end if not added
85
+ if config .Broker == "" {
86
+ return gopi .ErrBadParameter .WithPrefix ("-mqtt.broker" )
87
+ } else if reHostPort .MatchString (config .Broker ) == false {
88
+ config .Broker = fmt .Sprintf ("%v:%v" , config .Broker , mosq .MOSQ_DEFAULT_PORT )
90
89
}
91
-
92
- // Set keep alive
93
- if config .Keepalive == 0 {
94
- return gopi .ErrBadParameter .WithPrefix ("keepalive" )
90
+ // Check host and port
91
+ if host , port , err := net .SplitHostPort (config .Broker ); err != nil {
92
+ return gopi .ErrBadParameter .WithPrefix ("-mqtt.broker" )
93
+ } else if port_ , err := strconv .ParseUint (port , 10 , 32 ); err != nil {
94
+ return gopi .ErrBadParameter .WithPrefix ("-mqtt.broker" )
95
95
} else {
96
- this .keepalive = config .Keepalive
96
+ this .host = host
97
+ this .port = uint (port_ )
97
98
}
98
99
99
100
// Set credentials
@@ -129,10 +130,24 @@ func (this *mosquitto) Close() error {
129
130
////////////////////////////////////////////////////////////////////////////////
130
131
// CONNECT AND DISCONNECT
131
132
132
- func (this * mosquitto ) Connect (flags iface.Flags ) error {
133
+ func (this * mosquitto ) Connect (opts ... iface.Opt ) error {
133
134
this .Mutex .Lock ()
134
135
defer this .Mutex .Unlock ()
135
136
137
+ // Process options
138
+ flags := iface .MOSQ_FLAG_EVENT_ALL
139
+ keepalive_secs := int (60 )
140
+ for _ , opt := range opts {
141
+ switch opt .Type {
142
+ case iface .MOSQ_OPTION_FLAGS :
143
+ flags = opt .Flags
144
+ case iface .MOSQ_OPTION_KEEPALIVE :
145
+ keepalive_secs = opt .Int
146
+ default :
147
+ return gopi .ErrBadParameter .WithPrefix (fmt .Sprint (opt .Type ))
148
+ }
149
+ }
150
+
136
151
// Set flags
137
152
if flags & iface .MOSQ_FLAG_EVENT_CONNECT == iface .MOSQ_FLAG_EVENT_CONNECT {
138
153
this .client .SetConnectCallback (func (userInfo uintptr , rc int ) {
@@ -196,9 +211,7 @@ func (this *mosquitto) Connect(flags iface.Flags) error {
196
211
}
197
212
198
213
// Perform connection, start loop
199
- if keepalive_secs := int (this .keepalive .Seconds ()); keepalive_secs < 1 {
200
- return gopi .ErrBadParameter .WithPrefix ("keepalive" )
201
- } else if err := this .client .LoopStart (); err != nil {
214
+ if err := this .client .LoopStart (); err != nil {
202
215
return err
203
216
} else if err := this .client .Connect (this .host , int (this .port ), keepalive_secs , false ); err != nil {
204
217
this .client .LoopStop (true )
@@ -244,7 +257,7 @@ func (this *mosquitto) Version() string {
244
257
func (this * mosquitto ) String () string {
245
258
str := "<mosq.Client"
246
259
str += " version=" + strconv .Quote (this .Version ())
247
- str += " host =" + fmt .Sprintf ("%v:%v" , this .host , this .port )
260
+ str += " broker =" + fmt .Sprintf ("%v:%v" , this .host , this .port )
248
261
str += " connected=" + fmt .Sprint (this .connected )
249
262
return str + ">"
250
263
}
0 commit comments