-
Notifications
You must be signed in to change notification settings - Fork 13
/
subscription_manager.go
195 lines (156 loc) · 6.26 KB
/
subscription_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package spine
import (
"errors"
"fmt"
"reflect"
"sync/atomic"
"github.com/ahmetb/go-linq/v3"
"github.com/enbility/eebus-go/spine/model"
)
type SubscriptionManager interface {
AddSubscription(localDevice *DeviceLocalImpl, remoteDevice *DeviceRemoteImpl, data model.SubscriptionManagementRequestCallType) error
RemoveSubscription(data model.SubscriptionManagementDeleteCallType, remoteDevice *DeviceRemoteImpl) error
RemoveSubscriptionsForEntity(remoteEntity *EntityRemoteImpl)
Subscriptions(remoteDevice *DeviceRemoteImpl) []*SubscriptionEntry
SubscriptionsOnFeature(featureAddress model.FeatureAddressType) []*SubscriptionEntry
}
type SubscriptionEntry struct {
id uint64
serverFeature FeatureLocal
clientFeature *FeatureRemoteImpl
}
type SubscriptionManagerImpl struct {
subscriptionNum uint64
subscriptionEntries []*SubscriptionEntry
// TODO: add persistence
}
func NewSubscriptionManager() SubscriptionManager {
c := &SubscriptionManagerImpl{
subscriptionNum: 0,
}
return c
}
// is sent from the client (remote device) to the server (local device)
func (c *SubscriptionManagerImpl) AddSubscription(localDevice *DeviceLocalImpl, remoteDevice *DeviceRemoteImpl, data model.SubscriptionManagementRequestCallType) 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
}
subscriptionEntry := &SubscriptionEntry{
id: c.subscriptionId(),
serverFeature: serverFeature,
clientFeature: clientFeature,
}
for _, item := range c.subscriptionEntries {
if reflect.DeepEqual(item.serverFeature, serverFeature) && reflect.DeepEqual(item.clientFeature, clientFeature) {
return fmt.Errorf("requested subscription is already present")
}
}
c.subscriptionEntries = append(c.subscriptionEntries, subscriptionEntry)
payload := EventPayload{
Ski: remoteDevice.ski,
EventType: EventTypeSubscriptionChange,
ChangeType: ElementChangeAdd,
Data: data,
Feature: clientFeature,
}
Events.Publish(payload)
return nil
}
// Remove a specific subscription that is provided by a delete message from a remote device
func (c *SubscriptionManagerImpl) RemoveSubscription(data model.SubscriptionManagementDeleteCallType, remoteDevice *DeviceRemoteImpl) error {
// TODO: test this!!!
var newSubscriptionEntries []*SubscriptionEntry
// according to the spec 7.4.4
// a. The absence of "subscriptionDelete. clientAddress. device" SHALL be treated as if it was
// present and set to the sender's "device" address part.
// b. The absence of "subscriptionDelete. 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()
}
clientFeature := remoteDevice.FeatureByAddress(data.ClientAddress)
if clientFeature == nil {
return fmt.Errorf("client feature '%s' in remote device '%s' not found", data.ClientAddress, *remoteDevice.Address())
}
for _, item := range c.subscriptionEntries {
if !reflect.DeepEqual(item.clientFeature.Address(), clientAddress) {
newSubscriptionEntries = append(newSubscriptionEntries, item)
}
}
if len(newSubscriptionEntries) == len(c.subscriptionEntries) {
return errors.New("could not find requested SubscriptionId to be removed")
}
c.subscriptionEntries = newSubscriptionEntries
payload := EventPayload{
Ski: remoteDevice.ski,
EventType: EventTypeSubscriptionChange,
ChangeType: ElementChangeRemove,
Data: data,
Device: remoteDevice,
Feature: clientFeature,
}
Events.Publish(payload)
return nil
}
// Remove all existing subscriptions for a given remote device entity
func (c *SubscriptionManagerImpl) RemoveSubscriptionsForEntity(remoteEntity *EntityRemoteImpl) {
if remoteEntity == nil {
return
}
var newSubscriptionEntries []*SubscriptionEntry
for _, item := range c.subscriptionEntries {
if !reflect.DeepEqual(item.clientFeature.Address().Entity, remoteEntity.Address().Entity) {
newSubscriptionEntries = append(newSubscriptionEntries, item)
continue
}
clientFeature := remoteEntity.Feature(item.clientFeature.address.Feature)
payload := EventPayload{
Ski: remoteEntity.Device().ski,
EventType: EventTypeSubscriptionChange,
ChangeType: ElementChangeRemove,
Entity: remoteEntity,
Feature: clientFeature,
}
Events.Publish(payload)
}
c.subscriptionEntries = newSubscriptionEntries
}
func (c *SubscriptionManagerImpl) Subscriptions(remoteDevice *DeviceRemoteImpl) []*SubscriptionEntry {
var result []*SubscriptionEntry
linq.From(c.subscriptionEntries).WhereT(func(s *SubscriptionEntry) bool {
return s.clientFeature.Device().Ski() == remoteDevice.Ski()
}).ToSlice(&result)
return result
}
func (c *SubscriptionManagerImpl) SubscriptionsOnFeature(featureAddress model.FeatureAddressType) []*SubscriptionEntry {
var result []*SubscriptionEntry
linq.From(c.subscriptionEntries).WhereT(func(s *SubscriptionEntry) bool {
return reflect.DeepEqual(*s.serverFeature.Address(), featureAddress)
}).ToSlice(&result)
return result
}
func (c *SubscriptionManagerImpl) subscriptionId() uint64 {
i := atomic.AddUint64(&c.subscriptionNum, 1)
return i
}
func (c *SubscriptionManagerImpl) 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
}