/
subtree.go
64 lines (56 loc) · 1.35 KB
/
subtree.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
package broker
import (
"github.com/eclipse/paho.golang/packets"
"sync"
)
type subTree struct {
mux sync.RWMutex
hashSub map[string]map[string]packets.SubOptions
}
func NewSubTree() *subTree {
tmp := &subTree{hashSub: map[string]map[string]packets.SubOptions{}}
return tmp
}
func (s *subTree) CreateSub(clientID string, topics map[string]packets.SubOptions) {
s.mux.Lock()
defer s.mux.Unlock()
for topic, qos := range topics {
if s.hashSub[topic] == nil {
s.hashSub[topic] = map[string]packets.SubOptions{}
}
s.hashSub[topic][clientID] = qos
}
}
func (s *subTree) DeleteSub(clientID string, topics []string) {
// TODO implement me
s.mux.Lock()
defer s.mux.Unlock()
for _, topic := range topics {
if s.hashSub[topic] != nil {
delete(s.hashSub[topic], clientID)
if len(s.hashSub[topic]) == 0 {
delete(s.hashSub, topic)
}
}
}
}
func (s *subTree) Match(topic string) (clientIDQos map[string]int32) {
s.mux.RLock()
defer s.mux.RUnlock()
clientIDQos = map[string]int32{}
if clients := s.hashSub[topic]; clients != nil {
for client, qos := range clients {
clientIDQos[client] = int32(qos.QoS)
}
}
return
}
func (s *subTree) HasSub(topic string) bool {
s.mux.RLock()
defer s.mux.RUnlock()
return s.hashSub[topic] != nil
}
func (s *subTree) DeleteClient(clientID string) {
// TODO implement me
panic("implement me")
}