-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.go
261 lines (240 loc) · 7.03 KB
/
mqtt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package wbgo
import (
"fmt"
"log"
"log/syslog"
"math/rand"
"os"
"sync"
"time"
MQTT "github.com/contactless/org.eclipse.paho.mqtt.golang"
)
const (
DISCONNECT_WAIT_MS = 100
TOKEN_QUEUE_LEN = 512
)
type MQTTSubscriptionMap map[string][]MQTTMessageHandler
type mqttRetainedMessageMap map[string]MQTTMessage
type PahoMQTTClient struct {
startMtx sync.Mutex
connMtx sync.Mutex
innerClient *MQTT.Client
waitForRetained bool
ready chan struct{}
stopped chan struct{}
tokens chan MQTT.Token
subs MQTTSubscriptionMap
retainedMessages mqttRetainedMessageMap
started bool
connected bool
}
func NewPahoMQTTClient(server, clientID string, waitForRetained bool) (client *PahoMQTTClient) {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
clientID = fmt.Sprintf("%s-%s-%d", clientID, hostname, os.Getpid())
client = &PahoMQTTClient{
waitForRetained: waitForRetained,
ready: make(chan struct{}),
stopped: make(chan struct{}),
tokens: make(chan MQTT.Token, TOKEN_QUEUE_LEN),
subs: make(MQTTSubscriptionMap),
retainedMessages: make(mqttRetainedMessageMap),
started: false,
connected: false,
}
opts := MQTT.NewClientOptions().
AddBroker(server).
SetClientID(clientID).
SetOnConnectHandler(client.onConnect).
SetConnectionLostHandler(client.onConnectionLost)
client.innerClient = MQTT.NewClient(opts)
return
}
func (client *PahoMQTTClient) onConnect(innerClient *MQTT.Client) {
Info.Printf("MQTT connection established")
client.connMtx.Lock()
client.connected = true
for topic, callbacks := range client.subs {
for _, callback := range callbacks {
if DebuggingEnabled() {
Debug.Printf("RESUB: %s", topic)
}
client.tokens <- client.subscribe(callback, topic)
}
}
for topic, msg := range client.retainedMessages {
if DebuggingEnabled() {
Debug.Printf("REPUB: %s", topic)
}
client.Publish(msg)
}
client.connMtx.Unlock()
}
func (client *PahoMQTTClient) onConnectionLost(inner *MQTT.Client, err error) {
Warn.Printf("MQTT connection lost")
client.connMtx.Lock()
client.connected = false
client.connMtx.Unlock()
}
func (client *PahoMQTTClient) WaitForReady() <-chan struct{} {
if !client.waitForRetained {
close(client.ready)
} else {
go func() {
// There's no guarantee that messages with different QoS don't get
// mixed up, thus we check QoS 1 and QoS 2 for retained messages here.
// According to the standard, our message will arrive after the
// old messages.
// On the other hand, there's no guarantee that QoS 0 messages
// will arrive at all, so we don't check for QoS 0 messages here.
r := rand.New(rand.NewSource(time.Now().UnixNano()))
waitTopic := fmt.Sprintf("/wbretainhack/%016x%016x", r.Int63(), r.Int63())
got1, got2 := false, false
// subscribe synchronously to make sure that
// messages are published after the subscription is complete
token := client.subscribe(func(msg MQTTMessage) {
switch {
case got1 && got2:
// avoid closing the channel twice upon QoS1
// message duplication
return
case msg.Payload == "1":
got1 = true
case msg.Payload == "2":
got2 = true
}
if got1 && got2 {
client.Unsubscribe(waitTopic)
close(client.ready)
}
}, waitTopic)
token.Wait()
if token.Error() != nil {
Error.Printf("wbretainhack subscription failed: %s", token.Error())
// don't hang there, anyway
close(client.ready)
}
client.Publish(MQTTMessage{Topic: waitTopic, Payload: "1", QoS: 1})
client.Publish(MQTTMessage{Topic: waitTopic, Payload: "2", QoS: 2})
}()
}
return client.ready
}
func (client *PahoMQTTClient) Start() {
client.startMtx.Lock()
defer client.startMtx.Unlock()
if client.started {
return
}
client.started = true
if token := client.innerClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
go func() {
for {
select {
case <-client.stopped:
return
case token := <-client.tokens:
token.Wait()
if token.Error() != nil {
Error.Printf("MQTT error: %s", token.Error())
}
}
}
}()
}
func (client *PahoMQTTClient) Stop() {
// FIXME: restarting the client may not work properly
client.startMtx.Lock()
defer client.startMtx.Unlock()
if !client.started {
return
}
client.started = false
client.innerClient.Disconnect(DISCONNECT_WAIT_MS)
client.stopped <- struct{}{}
}
func (client *PahoMQTTClient) Publish(message MQTTMessage) {
if DebuggingEnabled() {
Debug.Printf("PUB: %s -> %s", message.Topic, message.Payload)
}
client.tokens <- client.innerClient.Publish(
message.Topic, message.QoS, message.Retained, message.Payload)
if message.Retained {
if message.Payload == "" {
delete(client.retainedMessages, message.Topic)
} else {
client.retainedMessages[message.Topic] = message
}
}
}
func (client *PahoMQTTClient) PublishSync(message MQTTMessage) {
if DebuggingEnabled() {
Debug.Printf("PUB: %s -> %s", message.Topic, message.Payload)
}
token := client.innerClient.Publish(
message.Topic, message.QoS, message.Retained, message.Payload)
token.Wait()
}
func (client *PahoMQTTClient) subscribe(callback MQTTMessageHandler, topics ...string) MQTT.Token {
filters := make(map[string]byte)
for _, topic := range topics {
if DebuggingEnabled() {
Debug.Printf("SUB: %s", topic)
}
filters[topic] = 2
}
wrappedCallback := func(client *MQTT.Client, msg MQTT.Message) {
if DebuggingEnabled() {
Debug.Printf("GOT MESSAGE: %s --- %s", msg.Topic(), string(msg.Payload()))
}
callback(MQTTMessage{msg.Topic(), string(msg.Payload()),
byte(msg.Qos()), msg.Retained()})
}
return client.innerClient.SubscribeMultiple(filters, wrappedCallback)
}
func (client *PahoMQTTClient) Subscribe(callback MQTTMessageHandler, topics ...string) {
client.connMtx.Lock()
for _, topic := range topics {
subList, found := client.subs[topic]
if !found {
client.subs[topic] = []MQTTMessageHandler{callback}
} else {
client.subs[topic] = append(subList, callback)
}
}
if !client.connected {
client.connMtx.Unlock()
return
}
client.connMtx.Unlock()
client.tokens <- client.subscribe(callback, topics...)
}
func (client *PahoMQTTClient) Unsubscribe(topics ...string) {
client.connMtx.Lock()
for _, topic := range topics {
delete(client.subs, topic)
}
if !client.connected {
client.connMtx.Unlock()
return
}
client.connMtx.Unlock()
client.innerClient.Unsubscribe(topics...)
}
func EnableMQTTDebugLog(use_syslog bool) {
if use_syslog {
MQTT.ERROR = makeSyslogger(syslog.LOG_DAEMON|syslog.LOG_ERR, "[MQTT] ERROR: ")
MQTT.CRITICAL = makeSyslogger(syslog.LOG_DAEMON|syslog.LOG_CRIT, "[MQTT] CRITICAL: ")
MQTT.WARN = makeSyslogger(syslog.LOG_DAEMON|syslog.LOG_WARNING, "[MQTT] WARNING: ")
MQTT.DEBUG = makeSyslogger(syslog.LOG_DAEMON|syslog.LOG_DEBUG, "[MQTT] DEBUG: ")
} else {
MQTT.ERROR = log.New(os.Stdout, "", 0)
MQTT.CRITICAL = log.New(os.Stdout, "", 0)
MQTT.WARN = log.New(os.Stdout, "", 0)
MQTT.DEBUG = log.New(os.Stdout, "", 0)
}
}