forked from shirou/pesand
/
broker.go
166 lines (142 loc) · 3.45 KB
/
broker.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
package main
import (
log "github.com/Sirupsen/logrus"
proto "github.com/huin/mqtt"
"net"
"sync"
"time"
)
type Broker struct {
listen net.Listener
Port string
stats Stats
storage Storage
auth Authorize
clientsMu sync.Mutex
clients map[string]*Connection
conf *Config
Done chan struct{}
StatsIntervalSec time.Duration
}
func NewBroker(conf Config, listen net.Listener) *Broker {
broker := &Broker{
listen: listen,
Port: conf.Default.Port,
stats: NewStats(),
storage: conf.ConfigStorage(),
auth: conf.ConfigAuth(),
clients: make(map[string]*Connection),
conf: &conf,
Done: make(chan struct{}),
StatsIntervalSec: time.Second * conf.Default.StatsIntervalSec,
}
return broker
}
// Auth auth
func (b *Broker) Auth(username string, password string) bool {
return b.auth.Auth(username, password)
}
func (b *Broker) AllowAnon() bool {
return b.auth.AllowAnon()
}
//Start
func (b *Broker) Start() {
log.Info("Broker started")
go func() {
for {
conn, err := b.listen.Accept()
if err != nil {
log.Warnf("Accept: %v", err)
break
}
c := NewConnection(b, conn)
c.Start()
b.stats.clientConnect()
}
close(b.Done)
}()
// start the stats reporting goroutine
go func() {
for {
for _, m := range b.stats.GetStatsMessages(b.StatsIntervalSec) {
b.Publish(m)
}
select {
case <-b.Done:
return
default:
// keep going
}
time.Sleep(b.StatsIntervalSec)
}
}()
}
//Publish
func (b *Broker) Publish(m *proto.Publish) {
topic := m.TopicName
topics, _ := ExpandTopics(topic)
for _, t := range topics {
go func(t string) {
list, err := b.storage.GetTopicClientList(t)
if err != nil {
log.WithField("topic", t).
Error(err)
}
for _, clientid := range list {
//sess, err := b.storage.GetClientSession(clientid)
b.clientsMu.Lock()
conn, ok := b.clients[clientid]
for k, _ := range b.clients {
log.Println(k)
}
b.clientsMu.Unlock()
if !ok {
log.WithFields(log.Fields{"topic": t, "clientID": clientid}).
Error("clientsList conn not ok")
continue
}
conn.submit(m)
b.stats.messageSend()
}
}(t)
}
}
//UpdateRetain
func (b *Broker) UpdateRetain(m *proto.Publish) {
topics, _ := ExpandTopics(m.TopicName)
for _, t := range topics {
err := b.storage.UpdateRetain(t, m)
if err != nil {
log.WithField("topic", t).
Errorf("UpdateRetain error %v", err)
}
}
}
//GetRetain
func (b *Broker) GetRetain(topic string) (*proto.Publish, error) {
m, err := b.storage.GetRetain(topic)
return m, err
}
//Subscribe
func (b *Broker) Subscribe(topic string, sess *Session) {
log.WithFields(log.Fields{"topic": topic, "clientID": sess.Clientid}).
Info("Subscribe")
err := b.storage.Subscribe(topic, sess.Clientid)
if err != nil {
log.WithFields(log.Fields{"topic": topic, "clientID": sess.Clientid}).
Errorf("Subscribe error %v", err)
}
}
//Unsubscribe
func (b *Broker) Unsubscribe(topic string, sess *Session) {
log.WithFields(log.Fields{"topic": topic, "clientID": sess.Clientid}).
Info("Unsubscribe")
topics, _ := ExpandTopics(topic)
for _, t := range topics {
err := b.storage.Unsubscribe(t, sess.Clientid)
if err != nil {
log.WithFields(log.Fields{"topic": topic, "clientID": sess.Clientid}).
Errorf("Unsubscribe error %v", err)
}
}
}