-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
216 lines (159 loc) · 3.31 KB
/
server.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
package mqtt
import (
"io"
"net"
"strings"
"github.com/j-forster/mqtt/tools"
)
type SubscriptionRequest struct {
subs *Subscription
topic string
}
const (
CREATE = 1
REMOVE = 2
)
type SubscriptionChange struct {
action int
subs *Subscription
topic string
}
type Server struct {
//subsReq chan SubscriptionRequest
//unsubs chan *Subscription
state int
closer io.Closer
sigclose chan (struct{})
subs chan SubscriptionChange
pub chan *Message
topics *Topic
handler Handler
}
func NewServer(closer io.Closer, handler Handler) *Server {
svr := new(Server)
//svr.subsReq = make(chan SubscriptionRequest)
//svr.unsubs = make(chan *Subscription)
svr.closer = closer
svr.handler = handler
svr.sigclose = make(chan struct{})
svr.subs = make(chan SubscriptionChange)
svr.pub = make(chan *Message)
svr.topics = NewTopic(nil, "")
return svr
}
func (svr *Server) Alive() bool {
return svr.state != CLOSING && svr.state != CLOSED
}
func (svr *Server) Publish(ctx *Context, msg *Message) {
if !svr.Alive() {
return
}
var err error = nil
if svr.handler != nil {
err = svr.handler.Publish(ctx, msg)
}
if err == nil {
svr.pub <- msg
}
}
func (svr *Server) Subscribe(ctx *Context, topic string, qos byte) *Subscription {
if !svr.Alive() {
return nil
}
var err error = nil
if svr.handler != nil {
err = svr.handler.Subscribe(ctx, topic, qos)
}
if err == nil {
subs := NewSubscription(ctx, qos)
svr.subs <- SubscriptionChange{CREATE, subs, topic}
return subs
}
return nil
}
func (svr *Server) Unsubscribe(subs *Subscription) {
if !svr.Alive() {
return
}
svr.subs <- SubscriptionChange{REMOVE, subs, ""}
}
func (svr *Server) Run() {
RUN:
for {
select {
case <-svr.sigclose:
close(svr.subs)
close(svr.pub)
SYSALL := []string{"$SYS", "all"}
subs := svr.topics.Find(SYSALL)
for ; subs != nil; subs = subs.next {
subs.ctx.Close()
}
svr.state = CLOSED
break RUN
case evt := <-svr.subs:
switch evt.action {
case CREATE:
svr.topics.Subscribe(strings.Split(evt.topic, "/"), evt.subs)
case REMOVE:
evt.subs.Unsubscribe()
}
// log.Println("Topics:", svr.topics)
case msg := <-svr.pub:
if msg.Topic == "$SYS/close" {
svr.Close()
} else {
// n := len(msg.buf)
// if n > 30 {
// n = 30
// }
// log.Printf("Publish: %s %q", msg.topic, string(msg.buf[:n]))
svr.topics.Publish(strings.Split(msg.Topic, "/"), msg)
}
}
}
}
func (svr *Server) Close() {
if svr.Alive() {
close(svr.sigclose)
svr.state = CLOSING
if svr.closer != nil {
svr.closer.Close()
}
}
}
func (svr *Server) Serve(rwc io.ReadWriteCloser) {
// uconn := tools.Unblock(rwc)
ctx := NewContext(rwc, rwc, svr)
defer ctx.Close()
// ctx.Subscribe("$SYS/all", 0)
for ctx.Alive() {
ctx.Read(rwc)
}
}
func Join(conn net.Conn, server *Server) {
uconn := tools.Unblock(conn)
ctx := NewContext(uconn, uconn, server)
defer ctx.Close()
ctx.Subscribe("$SYS/all", 0)
for ctx.Alive() {
ctx.Read(conn)
}
}
func ListenAndServe(addr string, handler Handler) error {
tcp, err := net.Listen("tcp", addr)
if err != nil {
return err
}
server := NewServer(tcp, handler)
go server.Run()
for {
conn, err := tcp.Accept()
if err == nil {
go server.Serve(conn)
} else {
return err
}
}
return nil
}