-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbackend.go
285 lines (239 loc) · 8.69 KB
/
backend.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2014 The gomqtt Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package broker
import (
"sync"
"github.com/gomqtt/packet"
"github.com/gomqtt/tools"
)
// A Backend provides effective queuing functionality to a Broker and its Clients.
type Backend interface {
// Authenticate should authenticate the client using the user and password
// values and return true if the client is eligible to continue or false
// when the broker should terminate the connection.
Authenticate(client Client, user, password string) (bool, error)
// Setup is called when a new client comes online and is successfully
// authenticated. Setup should return the already stored session for the
// supplied id or create and return a new one. If clean is set to true it
// should additionally reset the session. If the supplied id has a zero
// length, a new session is returned that is not stored further.
//
// Optional: The backend may close any existing clients that use the same
// client id. It may also start a background process that forwards any missed
// messages that match the clients offline subscriptions.
//
// Note: In this call the Backend may also allocate other resources and
// setup the client for further usage as the broker will acknowledge the
// connection when the call returns.
Setup(client Client, id string, clean bool) (Session, bool, error)
// Subscribe should subscribe the passed client to the specified topic and
// call Publish with any incoming messages. It should also return the stored
// retained messages that match the specified topic.
//
// Optional: Subscribe may also return a concatenated list of retained messages
// and missed offline messages, if the later has not been handled already in
// the Setup call.
Subscribe(client Client, topic string) ([]*packet.Message, error)
// Unsubscribe should unsubscribe the passed client from the specified topic.
Unsubscribe(client Client, topic string) error
// Publish should forward the passed message to all other clients that hold
// a subscription that matches the messages topic. It should also store the
// message if Retain is set to true and the payload does not have a zero
// length. If the payload has a zero length and Retain is set to true the
// currently retained message for that topic should be removed.
Publish(client Client, msg *packet.Message) error
// Terminate is called when the client goes offline. Terminate should
// unsubscribe the passed client from all previously subscribed topics.
//
// Optional: The backend may convert a clients subscriptions into offline
// subscriptions, which allows missed messages to be forwarded on the next
// connect.
//
// Note: The Backend may also cleanup previously allocated resources for
// that client as the broker will close the connection when the call
// returns.
Terminate(client Client) error
}
// A MemoryBackend stores everything in memory.
type MemoryBackend struct {
Logins map[string]string
queue *tools.Tree
retained *tools.Tree
offlineQueue *tools.Tree
sessions map[string]*MemorySession
sessionsMutex sync.Mutex
}
// NewMemoryBackend returns a new MemoryBackend.
func NewMemoryBackend() *MemoryBackend {
return &MemoryBackend{
queue: tools.NewTree(),
retained: tools.NewTree(),
offlineQueue: tools.NewTree(),
sessions: make(map[string]*MemorySession),
}
}
// Authenticate authenticates a clients credentials by matching them to the
// saved Logins map.
func (m *MemoryBackend) Authenticate(client Client, user, password string) (bool, error) {
// allow all if there are no logins
if m.Logins == nil {
return true, nil
}
// check login
if pw, ok := m.Logins[user]; ok && pw == password {
return true, nil
}
return false, nil
}
// Setup returns the already stored session for the supplied id or creates
// and returns a new one. If clean is set to true it will additionally reset
// the session. If the supplied id has a zero length, a new session is returned
// that is not stored further. If an existing session has been found it will
// retrieve all stored messages from offline subscriptions and begin with
// forwarding them in a separate goroutine. Furthermore, it will disconnect
// any client connected with the same client id.
func (m *MemoryBackend) Setup(client Client, id string, clean bool) (Session, bool, error) {
m.sessionsMutex.Lock()
defer m.sessionsMutex.Unlock()
// save clean flag
client.Context().Set("clean", clean)
// return a new temporary session if id is zero
if len(id) == 0 {
sess := NewMemorySession()
client.Context().Set("session", sess)
return sess, false, nil
}
// retrieve stored session
sess, ok := m.sessions[id]
// when found
if ok {
// check if session already has a client
if sess.currentClient != nil {
sess.currentClient.Close(true)
}
// set current client
sess.currentClient = client
// reset session if clean is true
if clean {
sess.Reset()
}
// remove all session from the offline queue
m.offlineQueue.Clear(sess)
// send all missed messages in another goroutine
go func() {
for _, msg := range sess.missed() {
client.Publish(msg)
}
}()
// returned stored session
client.Context().Set("session", sess)
return sess, true, nil
}
// create fresh session
sess = NewMemorySession()
sess.currentClient = client
// save session
m.sessions[id] = sess
// return new stored session
client.Context().Set("session", sess)
return sess, false, nil
}
// Subscribe will subscribe the passed client to the specified topic and
// begin to forward messages by calling the clients Publish method.
// It will also return the stored retained messages matching the supplied
// topic.
func (m *MemoryBackend) Subscribe(client Client, topic string) ([]*packet.Message, error) {
// add client to queue
m.queue.Add(topic, client)
// get retained messages
values := m.retained.Search(topic)
var msgs []*packet.Message
// convert types
for _, value := range values {
if msg, ok := value.(*packet.Message); ok {
msgs = append(msgs, msg)
}
}
return msgs, nil
}
// Unsubscribe will unsubscribe the passed client from the specified topic.
func (m *MemoryBackend) Unsubscribe(client Client, topic string) error {
// remove client from queue
m.queue.Remove(topic, client)
return nil
}
// Publish will forward the passed message to all other subscribed clients.
// It will also store the message if Retain is set to true. If the supplied
// message has additionally a zero length payload, the backend removes the
// currently retained message. Finally, it will also add the message to all
// sessions that have an offline subscription.
func (m *MemoryBackend) Publish(client Client, msg *packet.Message) error {
// check retain flag
if msg.Retain {
if len(msg.Payload) > 0 {
m.retained.Set(msg.Topic, msg)
} else {
m.retained.Empty(msg.Topic)
}
}
// publish directly to clients
for _, v := range m.queue.Match(msg.Topic) {
if client, ok := v.(Client); ok {
client.Publish(msg)
}
}
// queue for offline clients
for _, v := range m.offlineQueue.Match(msg.Topic) {
if session, ok := v.(*MemorySession); ok {
session.queue(msg)
}
}
return nil
}
// Terminate will unsubscribe the passed client from all previously subscribed
// topics. If the client connect with clean=true it will also clean the session.
// Otherwise it will create offline subscriptions for all QOS 1 and QOS 2
// subscriptions.
func (m *MemoryBackend) Terminate(client Client) error {
m.sessionsMutex.Lock()
defer m.sessionsMutex.Unlock()
// remove client from queue
m.queue.Clear(client)
// get session
session, ok := client.Context().Get("session").(*MemorySession)
if ok {
// reset stored client
session.currentClient = nil
// check if the client connected with clean=true
clean, ok := client.Context().Get("clean").(bool)
if ok && clean {
// reset session
session.Reset()
return nil
}
// otherwise get stored subscriptions
subscriptions, err := session.AllSubscriptions()
if err != nil {
return err
}
// iterate through stored subscriptions
for _, sub := range subscriptions {
if sub.QOS >= 1 {
// session to offline queue
m.offlineQueue.Add(sub.Topic, session)
}
}
}
return nil
}