-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
link.go
62 lines (51 loc) · 1.05 KB
/
link.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
package hub
import "sync"
type linker interface {
Send(pkt *Packet) error
Close()
}
var uplinks = make(map[linker]bool) // keyed by linker
var uplinksMu sync.RWMutex
func uplinksAdd(l linker) {
uplinksMu.Lock()
defer uplinksMu.Unlock()
uplinks[l] = true
}
func uplinksRemove(l linker) {
uplinksMu.Lock()
defer uplinksMu.Unlock()
delete(uplinks, l)
}
func uplinksRoute(pkt *Packet) {
uplinksMu.RLock()
defer uplinksMu.RUnlock()
for ul := range uplinks {
ul.Send(pkt)
}
}
var downlinks = make(map[string]linker) // keyed by device id
var downlinksMu sync.RWMutex
func downlinksAdd(id string, l linker) {
downlinksMu.Lock()
defer downlinksMu.Unlock()
downlinks[id] = l
}
func downlinksRemove(id string) {
downlinksMu.Lock()
defer downlinksMu.Unlock()
delete(downlinks, id)
}
func downlinkRoute(pkt *Packet) {
downlinksMu.RLock()
defer downlinksMu.RUnlock()
if dl, ok := downlinks[pkt.Dst]; ok {
dl.Send(pkt)
}
}
func downlinkClose(id string) {
downlinksMu.RLock()
defer downlinksMu.RUnlock()
if dl, ok := downlinks[id]; ok {
dl.Close()
}
}