forked from eclipse/paho.golang
/
router.go
212 lines (181 loc) · 5.86 KB
/
router.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
package paho
import (
"strings"
"sync"
"github.com/voytoo/paho.golang/packets"
)
// MessageHandler is a type for a function that is invoked
// by a Router when it has received a Publish.
type MessageHandler func(*Publish)
// Router is an interface of the functions for a struct that is
// used to handle invoking MessageHandlers depending on the
// the topic the message was published on.
// RegisterHandler() takes a string of the topic, and a MessageHandler
// to be invoked when Publishes are received that match that topic
// UnregisterHandler() takes a string of the topic to remove
// MessageHandlers for
// Route() takes a Publish message and determines which MessageHandlers
// should be invoked
type Router interface {
RegisterHandler(string, MessageHandler)
UnregisterHandler(string)
Route(*packets.Publish)
SetDebugLogger(Logger)
}
// StandardRouter is a library provided implementation of a Router that
// allows for unique and multiple MessageHandlers per topic
type StandardRouter struct {
sync.RWMutex
subscriptions map[string][]MessageHandler
aliases map[uint16]string
debug Logger
}
// NewStandardRouter instantiates and returns an instance of a StandardRouter
func NewStandardRouter() *StandardRouter {
return &StandardRouter{
subscriptions: make(map[string][]MessageHandler),
aliases: make(map[uint16]string),
debug: NOOPLogger{},
}
}
// RegisterHandler is the library provided StandardRouter's
// implementation of the required interface function()
func (r *StandardRouter) RegisterHandler(topic string, h MessageHandler) {
r.debug.Println("registering handler for:", topic)
r.Lock()
defer r.Unlock()
r.subscriptions[topic] = append(r.subscriptions[topic], h)
}
// UnregisterHandler is the library provided StandardRouter's
// implementation of the required interface function()
func (r *StandardRouter) UnregisterHandler(topic string) {
r.debug.Println("unregistering handler for:", topic)
r.Lock()
defer r.Unlock()
delete(r.subscriptions, topic)
}
// Route is the library provided StandardRouter's implementation
// of the required interface function()
func (r *StandardRouter) Route(pb *packets.Publish) {
r.debug.Println("routing message for:", pb.Topic)
r.RLock()
defer r.RUnlock()
m := PublishFromPacketPublish(pb)
var topic string
if pb.Properties.TopicAlias != nil {
r.debug.Println("message is using topic aliasing")
if pb.Topic != "" {
//Register new alias
r.debug.Printf("registering new topic alias '%d' for topic '%s'", *pb.Properties.TopicAlias, m.Topic)
r.aliases[*pb.Properties.TopicAlias] = pb.Topic
}
if t, ok := r.aliases[*pb.Properties.TopicAlias]; ok {
r.debug.Printf("aliased topic '%d' translates to '%s'", *pb.Properties.TopicAlias, m.Topic)
topic = t
}
} else {
topic = m.Topic
}
for route, handlers := range r.subscriptions {
if match(route, topic) {
r.debug.Println("found handler for:", route)
for _, handler := range handlers {
handler(m)
}
}
}
}
// SetDebugLogger sets the logger l to be used for printing debug
// information for the router
func (r *StandardRouter) SetDebugLogger(l Logger) {
r.debug = l
}
func match(route, topic string) bool {
return route == topic || routeIncludesTopic(route, topic)
}
func matchDeep(route []string, topic []string) bool {
if len(route) == 0 {
return len(topic) == 0
}
if len(topic) == 0 {
return route[0] == "#"
}
if route[0] == "#" {
return true
}
if (route[0] == "+") || (route[0] == topic[0]) {
return matchDeep(route[1:], topic[1:])
}
return false
}
func routeIncludesTopic(route, topic string) bool {
return matchDeep(routeSplit(route), topicSplit(topic))
}
func routeSplit(route string) []string {
if len(route) == 0 {
return nil
}
var result []string
if strings.HasPrefix(route, "$share") {
result = strings.Split(route, "/")[2:]
} else {
result = strings.Split(route, "/")
}
return result
}
func topicSplit(topic string) []string {
if len(topic) == 0 {
return nil
}
return strings.Split(topic, "/")
}
// SingleHandlerRouter is a library provided implementation of a Router
// that stores only a single MessageHandler and invokes this MessageHandler
// for all received Publishes
type SingleHandlerRouter struct {
sync.Mutex
aliases map[uint16]string
handler MessageHandler
debug Logger
}
// NewSingleHandlerRouter instantiates and returns an instance of a SingleHandlerRouter
func NewSingleHandlerRouter(h MessageHandler) *SingleHandlerRouter {
return &SingleHandlerRouter{
aliases: make(map[uint16]string),
handler: h,
debug: NOOPLogger{},
}
}
// RegisterHandler is the library provided SingleHandlerRouter's
// implementation of the required interface function()
func (s *SingleHandlerRouter) RegisterHandler(topic string, h MessageHandler) {
s.debug.Println("registering handler for:", topic)
s.handler = h
}
// UnregisterHandler is the library provided SingleHandlerRouter's
// implementation of the required interface function()
func (s *SingleHandlerRouter) UnregisterHandler(topic string) {}
// Route is the library provided SingleHandlerRouter's
// implementation of the required interface function()
func (s *SingleHandlerRouter) Route(pb *packets.Publish) {
m := PublishFromPacketPublish(pb)
s.debug.Println("routing message for:", m.Topic)
if pb.Properties.TopicAlias != nil {
s.debug.Println("message is using topic aliasing")
if pb.Topic != "" {
//Register new alias
s.debug.Printf("registering new topic alias '%d' for topic '%s'", *pb.Properties.TopicAlias, m.Topic)
s.aliases[*pb.Properties.TopicAlias] = pb.Topic
}
if t, ok := s.aliases[*pb.Properties.TopicAlias]; ok {
s.debug.Printf("aliased topic '%d' translates to '%s'", *pb.Properties.TopicAlias, m.Topic)
m.Topic = t
}
}
s.handler(m)
}
// SetDebugLogger sets the logger l to be used for printing debug
// information for the router
func (s *SingleHandlerRouter) SetDebugLogger(l Logger) {
s.debug = l
}