-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_peer.go
74 lines (63 loc) · 1.63 KB
/
get_peer.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
package bifrost_entitygraph
import (
"sync"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/controllerbus/directive"
"github.com/aperturerobotics/entitygraph/entity"
)
// getPeerHandler handles the GetPeer directive results
type getPeerHandler struct {
c *Reporter
mtx sync.Mutex
vals map[directive.Value]entity.Entity
}
// newGetPeerHandler constructs a getPeerHandler
func newGetPeerHandler(c *Reporter) *getPeerHandler {
return &getPeerHandler{
c: c,
vals: make(map[directive.Value]entity.Entity),
}
}
// HandleValueAdded is called when a value is added to the directive.
func (h *getPeerHandler) HandleValueAdded(
inst directive.Instance,
val directive.AttachedValue,
) {
nod, ok := val.GetValue().(peer.Peer)
if !ok {
h.c.le.Warn("GetPeer value was not a Peer")
return
}
nodObj := NewPeerEntity(nod.GetPeerID())
h.mtx.Lock()
_, exists := h.vals[val]
if !exists {
h.vals[val] = nodObj
}
h.mtx.Unlock()
if !exists {
h.c.store.AddEntityObj(nodObj)
}
}
// HandleValueRemoved is called when a value is removed from the directive.
func (h *getPeerHandler) HandleValueRemoved(
inst directive.Instance,
val directive.AttachedValue,
) {
h.mtx.Lock()
ent, exists := h.vals[val]
if exists {
delete(h.vals, val)
}
h.mtx.Unlock()
if exists {
h.c.store.RemoveEntityObj(ent)
}
}
// HandleInstanceDisposed is called when a directive instance is disposed.
// This will occur if Close() is called on the directive instance.
func (h *getPeerHandler) HandleInstanceDisposed(inst directive.Instance) {
// noop
}
// _ is a type assertion
var _ directive.ReferenceHandler = ((*getPeerHandler)(nil))