-
Notifications
You must be signed in to change notification settings - Fork 13
/
binding_manager.go
149 lines (116 loc) · 4.53 KB
/
binding_manager.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
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
147
148
149
package spine
import (
"errors"
"fmt"
"reflect"
"sync/atomic"
"github.com/ahmetb/go-linq/v3"
"github.com/enbility/eebus-go/spine/model"
)
type BindingManager interface {
AddBinding(localDevice *DeviceLocalImpl, remoteDevice *DeviceRemoteImpl, data model.BindingManagementRequestCallType) error
RemoveBinding(data model.BindingManagementDeleteCallType, remoteDevice *DeviceRemoteImpl) error
Bindings(remoteDevice *DeviceRemoteImpl) []*BindingEntry
BindingsOnFeature(featureAddress model.FeatureAddressType) []*BindingEntry
}
type BindingEntry struct {
id uint64
serverFeature FeatureLocal
clientFeature *FeatureRemoteImpl
}
type BindingManagerImpl struct {
bindingNum uint64
bindingEntries []*BindingEntry
// TODO: add persistence
}
func NewBindingManager() BindingManager {
c := &BindingManagerImpl{
bindingNum: 0,
}
return c
}
// is sent from the client (remote device) to the server (local device)
func (c *BindingManagerImpl) AddBinding(localDevice *DeviceLocalImpl, remoteDevice *DeviceRemoteImpl, data model.BindingManagementRequestCallType) error {
serverFeature := localDevice.FeatureByAddress(data.ServerAddress)
if serverFeature == nil {
return fmt.Errorf("server feature '%s' in local device '%s' not found", data.ServerAddress, *localDevice.Address())
}
if err := c.checkRoleAndType(serverFeature, model.RoleTypeServer, *data.ServerFeatureType); err != nil {
return err
}
clientFeature := remoteDevice.FeatureByAddress(data.ClientAddress)
if clientFeature == nil {
return fmt.Errorf("client feature '%s' in remote device '%s' not found", data.ClientAddress, *remoteDevice.Address())
}
if err := c.checkRoleAndType(clientFeature, model.RoleTypeClient, *data.ServerFeatureType); err != nil {
return err
}
bindingEntry := &BindingEntry{
id: c.bindingId(),
serverFeature: serverFeature,
clientFeature: clientFeature,
}
// TOV-TODO: check if binding already exists
c.bindingEntries = append(c.bindingEntries, bindingEntry)
payload := EventPayload{
Ski: remoteDevice.ski,
EventType: EventTypeBindingChange,
ChangeType: ElementChangeAdd,
Data: data,
Feature: clientFeature,
}
Events.Publish(payload)
// TOV-TODO: Send heartbeat to the feature which subscribed to DeviceDiagnostic
return nil
}
func (c *BindingManagerImpl) RemoveBinding(data model.BindingManagementDeleteCallType, remoteDevice *DeviceRemoteImpl) error {
// TODO: test this!!!
var newBindingEntries []*BindingEntry
// according to the spec 7.4.4
// a. The absence of "bindingDelete. clientAddress. device" SHALL be treated as if it was
// present and set to the sender's "device" address part.
// b. The absence of "bindingDelete. serverAddress. device" SHALL be treated as if it was
// present and set to the recipient's "device" address part.
clientAddress := data.ClientAddress
if data.ClientAddress.Device == nil {
clientAddress.Device = remoteDevice.Address()
}
for _, item := range c.bindingEntries {
if !reflect.DeepEqual(item.clientFeature.Address(), clientAddress) {
newBindingEntries = append(newBindingEntries, item)
}
}
if len(newBindingEntries) == len(c.bindingEntries) {
return errors.New("could not find requested BindingId to be removed")
}
c.bindingEntries = newBindingEntries
// TOV-TODO: stop heartbeat for remote device when it has no binding to DeviceDiagnostic anymore
return nil
}
func (c *BindingManagerImpl) Bindings(remoteDevice *DeviceRemoteImpl) []*BindingEntry {
var result []*BindingEntry
linq.From(c.bindingEntries).WhereT(func(s *BindingEntry) bool {
return s.clientFeature.Device().Ski() == remoteDevice.Ski()
}).ToSlice(&result)
return result
}
func (c *BindingManagerImpl) BindingsOnFeature(featureAddress model.FeatureAddressType) []*BindingEntry {
var result []*BindingEntry
linq.From(c.bindingEntries).WhereT(func(s *BindingEntry) bool {
return reflect.DeepEqual(*s.serverFeature.Address(), featureAddress)
}).ToSlice(&result)
return result
}
func (c *BindingManagerImpl) bindingId() uint64 {
i := atomic.AddUint64(&c.bindingNum, 1)
return i
}
func (c *BindingManagerImpl) checkRoleAndType(feature Feature, role model.RoleType, featureType model.FeatureTypeType) error {
if feature.Role() != model.RoleTypeSpecial && feature.Role() != role {
return fmt.Errorf("found feature %s is not matching required role %s", feature.Type(), role)
}
if feature.Type() != featureType && feature.Type() != model.FeatureTypeTypeGeneric {
return fmt.Errorf("found feature %s is not matching required type %s", feature.Type(), featureType)
}
return nil
}