This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistrar.go
80 lines (66 loc) · 1.89 KB
/
registrar.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
package gateway
import (
"container/list"
"github.com/freeconf/restconf/device"
"github.com/freeconf/yang/nodeutil"
)
type Registration struct {
DeviceId string
Address string
Device device.Device
}
type Registrar interface {
RegistrationCount() int
LookupRegistration(deviceId string) (Registration, bool)
RegisterDevice(deviceId string, address string)
OnRegister(l RegisterListener) nodeutil.Subscription
}
type RegisterListener func(Registration)
type LocalRegistrar struct {
regs map[string]Registration
proto device.ProtocolHandler
listeners *list.List
}
func NewLocalRegistrar(proto device.ProtocolHandler) *LocalRegistrar {
return &LocalRegistrar{
proto: proto,
regs: make(map[string]Registration),
listeners: list.New(),
}
}
func (gw *LocalRegistrar) Device(deviceId string) (device.Device, error) {
return gw.regs[deviceId].Device, nil
}
func InstallRegistrar(reg *LocalRegistrar, dev *device.Local) error {
if err := dev.Add("fc-gateway", RegistrarNode(reg)); err != nil {
return err
}
return dev.Add("fc-call-home-server", CallHomeServer(reg))
}
func (gw *LocalRegistrar) LookupRegistration(deviceId string) (Registration, bool) {
found, reg := gw.regs[deviceId]
return found, reg
}
func (gw *LocalRegistrar) RegisterDevice(deviceId string, address string) error {
dev, err := gw.proto(address)
if err != nil {
return err
}
reg := Registration{Address: address, DeviceId: deviceId, Device: dev}
gw.regs[deviceId] = reg
gw.updateListeners(reg)
return nil
}
func (gw *LocalRegistrar) updateListeners(reg Registration) {
p := gw.listeners.Front()
for p != nil {
p.Value.(RegisterListener)(reg)
p = p.Next()
}
}
func (gw *LocalRegistrar) RegistrationCount() int {
return len(gw.regs)
}
func (gw *LocalRegistrar) OnRegister(l RegisterListener) nodeutil.Subscription {
return nodeutil.NewSubscription(gw.listeners, gw.listeners.PushBack(l))
}