forked from viert/spatial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistener.go
More file actions
146 lines (121 loc) · 2.8 KB
/
listener.go
File metadata and controls
146 lines (121 loc) · 2.8 KB
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
package spatial
import (
"sync"
"time"
)
// 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
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),
watchIds: make(map[string]bool),
updateInterval: interval,
stopped: false,
dirty: false,
}
go lstr.loop()
return lstr
}
func (l *Listener) disposeBoxes() {
for _, box := range l.boxes {
l.srv.tree.Delete(box)
}
}
func (l *Listener) unsubscribeAll() {
for id := range l.watchIds {
l.srv.unsubscribeID(l, id)
}
}
// SetBounds sets bounds to listen to
func (l *Listener) SetBounds(mb MapBounds) {
l.lock.Lock()
l.srv.lock.Lock()
defer l.lock.Unlock()
defer l.srv.lock.Unlock()
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
}
// Stop stops the listener, closes all the channels so it's free to cleanup by GC
func (l *Listener) Stop() {
l.lock.Lock()
l.srv.lock.Lock()
defer l.lock.Unlock()
defer l.srv.lock.Unlock()
l.disposeBoxes()
l.unsubscribeAll()
l.stopped = true
}
// SubscribeID adds a specific id to watch
func (l *Listener) SubscribeID(id string) {
l.lock.Lock()
l.srv.lock.Lock()
defer l.lock.Unlock()
defer l.srv.lock.Unlock()
l.watchIds[id] = true
l.srv.subscribeID(l, id)
}
// UnsubscribeID unsubscribes from a specific id
func (l *Listener) UnsubscribeID(id string) {
l.lock.Lock()
l.srv.lock.Lock()
defer l.lock.Unlock()
defer l.srv.lock.Unlock()
delete(l.watchIds, id)
l.srv.unsubscribeID(l, id)
}
func (l *Listener) setDirty() {
l.lock.Lock()
defer l.lock.Unlock()
l.dirty = true
}
// Updates returns the update channel
func (l *Listener) Updates() <-chan []Indexable {
return l.ch
}
func (l *Listener) loop() {
t := time.NewTicker(l.updateInterval)
defer t.Stop()
for range t.C {
l.lock.RLock()
if l.stopped {
l.lock.RUnlock()
break
}
if l.dirty {
objmap := make(map[string]Indexable)
for key, obj := range l.srv.findObjectsByIDs(l.watchIds) {
objmap[key] = obj
}
for key, obj := range l.srv.findObjectsByBoundingBoxes(l.boxes) {
objmap[key] = obj
}
objects := make([]Indexable, 0)
for _, obj := range objmap {
objects = append(objects, obj)
}
l.ch <- objects
l.dirty = false
}
l.lock.RUnlock()
}
close(l.ch)
}