blob: bf5fbb32169222f0a9c5127455b6fd8deb36471a (
plain)
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
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
l4g "code.google.com/p/log4go"
)
type Hub struct {
teamHubs map[string]*TeamHub
register chan *WebConn
unregister chan *WebConn
stop chan string
}
var hub = &Hub{
register: make(chan *WebConn),
unregister: make(chan *WebConn),
teamHubs: make(map[string]*TeamHub),
stop: make(chan string),
}
func (h *Hub) Register(webConn *WebConn) {
h.register <- webConn
}
func (h *Hub) Unregister(webConn *WebConn) {
h.unregister <- webConn
}
func (h *Hub) Stop(teamId string) {
h.stop <- teamId
}
func (h *Hub) Start() {
go func() {
for {
select {
case c := <-h.register:
nh := h.teamHubs[c.TeamId]
if nh == nil {
nh = NewTeamHub(c.TeamId)
h.teamHubs[c.TeamId] = nh
nh.Start()
}
nh.Register(c)
case c := <-h.unregister:
if nh, ok := h.teamHubs[c.TeamId]; ok {
nh.Unregister(c)
}
case s := <-h.stop:
if len(s) == 0 {
l4g.Debug("stopping all connections")
for _, v := range h.teamHubs {
v.Stop()
}
return
} else if nh, ok := h.teamHubs[s]; ok {
delete(h.teamHubs, s)
nh.Stop()
}
}
}
}()
}
|