-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener.go
157 lines (133 loc) · 3.29 KB
/
listener.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
package spatial
import (
"sync"
"time"
"github.com/dhconnelly/rtreego"
)
// Listener is an object watching for objects in bounding boxes and with specific ids
// and sends updates through a channel
type Listener struct {
lock sync.RWMutex
srv *Server
ch chan []Indexable
boxes []*boundingBox
filter rtreego.Filter
watchIds map[string]bool
updateInterval time.Duration
dirty bool
stopped bool
}
func newListener(srv *Server, chSize int, interval time.Duration) *Listener {
lstr := &Listener{
srv: srv,
ch: make(chan []Indexable, chSize),
boxes: make([]*boundingBox, 0),
filter: nil,
watchIds: make(map[string]bool),
updateInterval: interval,
stopped: false,
dirty: false,
}
go lstr.loop()
return lstr
}
func (l *Listener) disposeBoxes() {
// No locking here as l.boxes is always overwritten and never changed
for _, box := range l.boxes {
l.srv.tree.Delete(box)
}
}
func (l *Listener) unsubscribeAll() {
// Hoping that l.srv.unsubscribeID never calls back to the listener
// or at least not to a method that tries to acquire the lock
l.lock.RLock()
defer l.lock.RUnlock()
for id := range l.watchIds {
l.srv.unsubscribeID(l, id)
}
}
// SetBounds sets bounds to listen to
func (l *Listener) SetBounds(mb MapBounds) {
l.disposeBoxes()
rects := mb.Rects()
boxes := make([]*boundingBox, len(rects))
for i, rect := range rects {
box := newBoundingBox(rect, l)
l.srv.tree.Insert(box)
boxes[i] = box
}
l.boxes = boxes
}
// SetTypes sets a filter to listen for objects of specified types only
// Does not apply for ID subscriptions
func (l *Listener) SetTypes(types []IndexableType) {
if len(types) == 0 {
l.filter = nil
} else {
l.filter = FilterByTypes(types)
}
}
// Stop stops the listener, closes all the channels so it's free to cleanup by GC
func (l *Listener) Stop() {
l.disposeBoxes()
l.unsubscribeAll()
l.stopped = true
}
// SubscribeID adds a specific id to watch
func (l *Listener) SubscribeID(id string) {
l.lock.Lock()
l.watchIds[id] = true
l.lock.Unlock()
l.srv.subscribeID(l, id)
}
// UnsubscribeID unsubscribes from a specific id
func (l *Listener) UnsubscribeID(id string) {
l.lock.Lock()
delete(l.watchIds, id)
l.lock.Unlock()
l.srv.unsubscribeID(l, id)
}
// ForceUpdate forces the dirty flag on
func (l *Listener) ForceUpdate() {
l.setDirty()
}
func (l *Listener) setDirty() {
l.dirty = true
}
// Updates returns the update channel
func (l *Listener) Updates() <-chan []Indexable {
return l.ch
}
func (l *Listener) loop() {
var rmap map[string]Indexable
t := time.NewTicker(l.updateInterval)
defer t.Stop()
for range t.C {
if l.stopped {
break
}
if l.dirty {
objmap := make(map[string]Indexable)
l.lock.RLock()
for key, obj := range l.srv.findObjectsByIDs(l.watchIds) {
objmap[key] = obj
}
l.lock.RUnlock()
if l.filter == nil {
rmap = l.srv.findObjectsByBoundingBoxes(l.boxes)
} else {
rmap = l.srv.findObjectsByBoundingBoxes(l.boxes, l.filter)
}
for key, obj := range rmap {
objmap[key] = obj
}
objects := make([]Indexable, 0)
for _, obj := range objmap {
objects = append(objects, obj)
}
l.ch <- objects
l.dirty = false
}
}
close(l.ch)
}