-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
executable file
·207 lines (171 loc) · 3.79 KB
/
hub.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
package go_remote
import (
"fmt"
)
type Message struct {
Channel string `json:"name"`
Content interface{} `json:"value"`
Clients []ConnectionID
}
type subscription struct {
Client *Client
Channel string
Mode bool
}
type UserChange struct {
ID int `json:"id"`
Connection int `json:"-"`
Status bool `json:"status"`
}
type HubStatus struct {
Users map[int]int
Channels map[string]ChannelStatus
}
type ChannelStatus struct {
Subscribed []int
}
type UserHandler func(u *UserChange)
type ChannelGuard func(*Message, *Client) bool
type channel struct {
clients map[*Client]bool
}
type Hub struct {
UserHandler UserHandler
ConnHandler UserHandler
users map[int]int
channels map[string]channel
filters map[string]ChannelGuard
publish chan Message
subscribe chan subscription
register chan UserChange
}
func newHub() *Hub {
return &Hub{
UserHandler: func(u *UserChange) {},
ConnHandler: func(u *UserChange) {},
publish: make(chan Message),
subscribe: make(chan subscription),
register: make(chan UserChange),
filters: make(map[string]ChannelGuard),
channels: make(map[string]channel),
users: make(map[int]int),
}
}
func (h *Hub) Status() *HubStatus {
info := make(map[string]ChannelStatus)
for name, ch := range h.channels {
subs := make([]int, 0, len(ch.clients))
for cl := range ch.clients {
subs = append(subs, cl.User)
}
info[name] = ChannelStatus{Subscribed: subs}
}
return &HubStatus{
Users: h.users,
Channels: info,
}
}
func (h *Hub) Run() {
for {
select {
case sub := <-h.subscribe:
h.onSubscribe(&sub)
case m := <-h.publish:
h.onPublish(&m)
case u := <-h.register:
h.onRegister(&u)
}
}
}
func (h *Hub) AddGuard(name string, filter func(*Message, *Client) bool) {
h.filters[name] = filter
}
func (h *Hub) Subscribe(channel string, c *Client) {
h.subscribe <- subscription{c, channel, true}
}
func (h *Hub) UnSubscribe(channel string, c *Client) {
h.subscribe <- subscription{c, channel, false}
}
func (h *Hub) Publish(name string, data interface{}, clients ...ConnectionID) {
h.publish <- Message{Channel: name, Content: data, Clients: clients}
}
func (h *Hub) UserIn(id, device int) {
h.register <- UserChange{ID: id, Connection: device, Status: true}
}
func (h *Hub) UserOut(id, conn int) {
h.register <- UserChange{ID: id, Connection: conn, Status: false}
}
func (h *Hub) onSubscribe(sub *subscription) {
if !sub.Mode {
if sub.Channel == "" {
//unsubscribe from all
for name := range h.channels {
h.onUnSubscribe(name, sub.Client)
}
} else {
h.onUnSubscribe(sub.Channel, sub.Client)
}
return
}
ch, ok := h.channels[sub.Channel]
if !ok {
ch = channel{clients: make(map[*Client]bool)}
h.channels[sub.Channel] = ch
}
ch.clients[sub.Client] = true
}
func (h *Hub) onUnSubscribe(channel string, client *Client) {
ch, ok := h.channels[channel]
if !ok {
return
}
delete(ch.clients, client)
if len(ch.clients) == 0 {
delete(h.channels, channel)
}
}
func (h *Hub) onPublish(m *Message) {
ch, ok := h.channels[m.Channel]
filter, hasFilter := h.filters[m.Channel]
if ok {
for c := range ch.clients {
if !hasFilter || filter(m, c) {
if len(m.Clients) != 0 {
for _, x := range m.Clients {
if x == ConnectionID(c.ConnID) {
c.SendMessage("event", m)
}
}
} else {
c.SendMessage("event", m)
}
}
}
}
}
func (h *Hub) onRegister(u *UserChange) {
c := h.users[u.ID]
if u.Status {
if c == 0 {
h.UserHandler(u)
}
c += 1
} else {
if c <= 1 {
h.UserHandler(u)
delete(h.users, u.ID)
return
} else {
c -= 1
}
}
h.users[u.ID] = c
h.ConnHandler(u)
}
func (h *Hub) LogState() string {
out := ""
for name, ch := range h.channels {
out += fmt.Sprintf("%s [%d]\n", name, len(ch.clients))
}
return out
}