-
Notifications
You must be signed in to change notification settings - Fork 0
/
membackend.go
114 lines (97 loc) · 2.33 KB
/
membackend.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
package main
import (
"bytes"
"expvar"
"net"
"sync"
)
var (
vars = map[string]*expvar.Map{
"networks": expvar.NewMap("networks"),
"devices": expvar.NewMap("devices"),
}
)
type MemBackend struct {
networks []Network
devices map[string][]Device
nlock sync.Mutex
dlock sync.Mutex
}
func NewMemBackend() Backend {
return &MemBackend{
networks: []Network{},
devices: map[string][]Device{},
nlock: sync.Mutex{},
dlock: sync.Mutex{},
}
}
func (s *MemBackend) Open() {
debugf("opened memory store")
}
func (s *MemBackend) Close() {
debugf("closed memory store")
}
func (s *MemBackend) HWAddrExists(hw net.HardwareAddr) bool {
s.dlock.Lock()
defer s.dlock.Unlock()
for _, dd := range s.devices {
for _, d := range dd {
if bytes.Equal(d.HardwareAddr, hw) {
return true
}
}
}
return false
}
func (s *MemBackend) Networks() []Network {
s.nlock.Lock()
defer s.nlock.Unlock()
return s.networks
}
func (s *MemBackend) AddNetwork(network Network) {
s.nlock.Lock()
defer s.nlock.Unlock()
s.networks = append(s.networks, network)
// TODO: this makes invalid expvar json
vars["networks"].Set(network.Name, &network.IPNet)
vars["devices"].Set(network.Name, new(expvar.Map).Init())
debugf("added network %s", network.Name)
}
func (s *MemBackend) RemoveNetwork(network Network) {
s.nlock.Lock()
defer s.nlock.Unlock()
n := []Network{}
for _, net := range s.networks {
if net.Name == network.Name {
n = append(n, network)
}
}
s.networks = n
debugf("removed network %s", network.Name)
}
func (s *MemBackend) AddDevice(networks []string, device Device) {
s.dlock.Lock()
defer s.dlock.Unlock()
for _, network := range networks {
s.devices[network] = append(s.devices[network], device)
dvars := vars["devices"].Get(network).(*expvar.Map)
dval := new(expvar.String)
dval.Set(device.Name)
dvars.Set(device.HardwareAddr.String(), dval)
}
debugf("added device %s to networks %v", device.HardwareAddr.String(), networks)
}
func (s *MemBackend) RemoveDevice(device Device) {
s.dlock.Lock()
defer s.dlock.Unlock()
for _, network := range s.networks {
devices := []Device{}
for _, d := range s.devices[network.Name] {
if d.Name != device.Name {
devices = append(devices, device)
}
}
s.devices[network.Name] = devices
}
debugf("removed device %s", device.HardwareAddr.String())
}