-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
66 lines (57 loc) · 1.42 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
package topic_sockets
import (
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
)
type manager interface {
Topic() Topic
HandleEvent(TopicEvent, chan<- TopicEvent)
}
type Hub struct {
managers map[Topic]manager
connections map[*connection]bool
register, unregister chan *connection
handleReceive chan clientTopicEvent
}
func NewHub() *Hub {
return &Hub{
managers: make(map[Topic]manager),
connections: make(map[*connection]bool),
register: make(chan *connection),
unregister: make(chan *connection),
handleReceive: make(chan clientTopicEvent),
}
}
func (h *Hub) AddManager(m manager) {
h.managers[m.Topic()] = m
}
func (h *Hub) Listen(addr string) {
go h.startBroker()
r := mux.NewRouter()
r.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) {
h.register <- NewClient(w, r, h.handleReceive, h.unregister)
})
err := http.ListenAndServe(addr, handlers.CombinedLoggingHandler(os.Stdout, r))
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func (h *Hub) startBroker() {
for {
select {
case conn := <-h.register:
h.connections[conn] = true
case conn := <-h.unregister:
delete(h.connections, conn)
case event := <-h.handleReceive:
h.dispatchEvent(event)
}
}
}
func (h *Hub) dispatchEvent(event clientTopicEvent) {
m := h.managers[event.Topic]
m.HandleEvent(event.TopicEvent, event.Client.Sink)
}