-
Notifications
You must be signed in to change notification settings - Fork 2
/
hub.go
164 lines (146 loc) · 3.65 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
package tms
import (
dig "github.com/buckhx/diglet/burrow"
)
type subMsg struct {
conn *dig.Connection
xyz TileXYZ
}
// hub maintains the set of active connections and broadcasts messages to the
// connections.
type TilesetTopic struct {
name string
subscribers map[*dig.Connection]*tileSubscription
events chan TsEvent
subscribe chan *subMsg
unsubscribe chan *subMsg
shut chan struct{}
}
func (t *TilesetTopic) open() {
for {
select {
case m := <-t.subscribe:
if _, ok := t.subscribers[m.conn]; !ok {
t.subscribers[m.conn] = newTileSubscription()
}
t.subscribers[m.conn].add(m.xyz)
case m := <-t.unsubscribe:
if _, ok := t.subscribers[m.conn]; ok {
t.subscribers[m.conn].remove(m.xyz)
if !t.subscribers[m.conn].isEmpty() {
delete(t.subscribers, m.conn)
}
}
case e := <-t.events:
func(e TsEvent) {}(e)
for c, s := range t.subscribers {
go s.notify(c)
//info("ts notifying %s", e)
//info("%s -> %s", e, c)
}
case <-t.shut:
close(t.events)
close(t.subscribe)
close(t.unsubscribe)
}
}
}
func (t *TilesetTopic) close() {
t.shut <- struct{}{}
}
func newTilesetTopic(name string) (topic *TilesetTopic) {
topic = &TilesetTopic{
name: name,
subscribers: make(map[*dig.Connection]*tileSubscription),
events: make(chan TsEvent),
subscribe: make(chan *subMsg),
unsubscribe: make(chan *subMsg),
shut: make(chan struct{}),
}
return
}
type IoHub struct {
tilesets *TilesetIndex
topics map[string]*TilesetTopic
}
func (h *IoHub) listen() {
for _, topic := range h.topics {
go topic.open()
}
go h.publish(h.tilesets.Events)
}
func (h *IoHub) publish(events <-chan TsEvent) {
for event := range events {
//info("Tileset Change - %s", event.String())
//TODO remove/create messages
//if event was REMOVE, shut topic
if topic, ok := h.topics[event.Name]; ok {
topic.events <- event
} else {
info("Topic did not exist in hub for TsEvent: %s", event)
}
}
}
func (h *IoHub) bindTile(ctx *dig.RequestContext, xyz TileXYZ) (err error) {
msg := &subMsg{conn: ctx.Connection, xyz: xyz}
if topic, ok := h.topics[xyz.Tileset]; ok {
topic.subscribe <- msg
} else {
err = errorf("Tileset does not exist %s", xyz.Tileset)
}
return
}
func (h *IoHub) unbindTile(ctx *dig.RequestContext, xyz TileXYZ) (err error) {
msg := &subMsg{conn: ctx.Connection, xyz: xyz}
if topic, ok := h.topics[xyz.Tileset]; ok {
topic.unsubscribe <- msg
} else {
err = errorf("Tileset does not exist %s", xyz.Tileset)
}
return
}
func NewHub(tilesets *TilesetIndex) (h *IoHub) {
h = &IoHub{
tilesets: tilesets,
topics: make(map[string]*TilesetTopic),
}
for slug := range h.tilesets.Tilesets {
h.topics[slug] = newTilesetTopic(slug)
}
return
}
type tileSubscription struct {
tiles map[TileXYZ]struct{}
}
func newTileSubscription() *tileSubscription {
return &tileSubscription{
tiles: make(map[TileXYZ]struct{}),
}
}
func (s *tileSubscription) add(xyz TileXYZ) {
s.tiles[xyz] = struct{}{}
}
func (s *tileSubscription) remove(xyz TileXYZ) {
if _, ok := s.tiles[xyz]; !ok {
delete(s.tiles, xyz)
}
}
func (s *tileSubscription) isEmpty() bool {
return len(s.tiles) == 0
}
func (s *tileSubscription) notify(conn *dig.Connection) {
//TODO ops will have specific tile in the future?
for xyz := range s.tiles {
var msg *dig.ResponseMessage
if tile, err := tilesets.read(xyz); err != nil {
check(err)
msg = dig.Cerrorf(dig.RpcInvalidRequest, err.Error()).ResponseMessage()
} else {
tile.Data = nil
tile.Y = (1<<uint(tile.Z) - 1) - tile.Y
id := sprintf("%d:%d:%d", tile.Z, tile.X, tile.Y)
msg = dig.RespondMsg(id, tile)
}
conn.Respond(msg)
}
}