-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_iterface.go
345 lines (280 loc) · 8.31 KB
/
node_iterface.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package acmelib
import (
"fmt"
"strings"
"golang.org/x/exp/slices"
)
// NodeInterface represents an interface between a [Bus] and a [Node].
type NodeInterface struct {
parentBus *Bus
sentMessages *set[EntityID, *Message]
sentMessageNames *set[string, EntityID]
sentMessageIDs *set[MessageID, EntityID]
sentMessageStaticCANIDs *set[CANID, EntityID]
receivedMessages *set[EntityID, *Message]
number int
node *Node
}
func newNodeInterface(number int, node *Node) *NodeInterface {
return &NodeInterface{
parentBus: nil,
sentMessages: newSet[EntityID, *Message](),
sentMessageNames: newSet[string, EntityID](),
sentMessageIDs: newSet[MessageID, EntityID](),
sentMessageStaticCANIDs: newSet[CANID, EntityID](),
receivedMessages: newSet[EntityID, *Message](),
number: number,
node: node,
}
}
func (ni *NodeInterface) setName(nodeName string) string {
return fmt.Sprintf("%s/int%d", nodeName, ni.number)
}
func (ni *NodeInterface) hasParentBus() bool {
return ni.parentBus != nil
}
func (ni *NodeInterface) errorf(err error) error {
if ni.hasParentBus() {
return ni.parentBus.errorf(err)
}
return err
}
func (ni *NodeInterface) stringify(b *strings.Builder, tabs int) {
tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%snumber: %d\n", tabStr, ni.number))
b.WriteString(fmt.Sprintf("%snode:\n", tabStr))
ni.node.stringify(b, tabs+1)
if ni.sentMessages.size() == 0 {
return
}
b.WriteString(fmt.Sprintf("%ssent_messages:\n", tabStr))
for _, msg := range ni.SentMessages() {
msg.stringify(b, tabs+1)
b.WriteRune('\n')
}
b.WriteString(fmt.Sprintf("%sreceived_messages:\n", tabStr))
for _, msg := range ni.ReceivedMessages() {
msg.stringify(b, tabs+1)
b.WriteRune('\n')
}
}
func (ni *NodeInterface) String() string {
builder := new(strings.Builder)
ni.stringify(builder, 0)
return builder.String()
}
func (ni *NodeInterface) verifyMessageName(name string) error {
err := ni.sentMessageNames.verifyKeyUnique(name)
if err != nil {
return &NameError{
Name: name,
Err: err,
}
}
return nil
}
func (ni *NodeInterface) verifyMessageID(msgID MessageID) error {
err := ni.sentMessageIDs.verifyKeyUnique(msgID)
if err != nil {
return &MessageIDError{
MessageID: msgID,
Err: err,
}
}
return nil
}
func (ni *NodeInterface) verifyStaticCANID(staticCANID CANID) error {
if err := ni.sentMessageStaticCANIDs.verifyKeyUnique(staticCANID); err != nil {
return &CANIDError{
CANID: staticCANID,
Err: err,
}
}
if ni.hasParentBus() {
if err := ni.parentBus.verifyStaticCANID(staticCANID); err != nil {
return &CANIDError{
CANID: staticCANID,
Err: err,
}
}
}
return nil
}
func (ni *NodeInterface) addReceivedMessage(msg *Message) error {
if ni.sentMessages.hasKey(msg.entityID) {
return ErrReceiverIsSender
}
ni.receivedMessages.add(msg.entityID, msg)
msg.receivers.add(ni.node.entityID, ni)
return nil
}
func (ni *NodeInterface) removeReceivedMessage(msg *Message) {
ni.receivedMessages.remove(msg.entityID)
msg.receivers.remove(ni.node.entityID)
}
// AddSentMessage adds a [Message] that the [NodeInterface] can send.
//
// It returns an [ArgumentError] if the given message is nil or
// a [NameError]/[MessageIDError] if the message name/id is already used.
func (ni *NodeInterface) AddSentMessage(message *Message) error {
if message == nil {
return &ArgumentError{
Name: "message",
Err: ErrIsNil,
}
}
addMsgErr := &AddEntityError{
EntityID: message.entityID,
Name: message.name,
}
if err := ni.verifyMessageName(message.name); err != nil {
addMsgErr.Err = err
return ni.errorf(addMsgErr)
}
if message.hasStaticCANID {
if err := ni.verifyStaticCANID(message.staticCANID); err != nil {
addMsgErr.Err = err
return ni.errorf(addMsgErr)
}
if ni.hasParentBus() {
ni.parentBus.messageStaticCANIDs.add(message.staticCANID, message.entityID)
}
ni.sentMessageStaticCANIDs.add(message.staticCANID, message.entityID)
} else {
if err := ni.verifyMessageID(message.id); err != nil {
addMsgErr.Err = err
return ni.errorf(addMsgErr)
}
ni.sentMessageIDs.add(message.id, message.entityID)
}
ni.sentMessages.add(message.entityID, message)
ni.sentMessageNames.add(message.name, message.entityID)
message.senderNodeInt = ni
return nil
}
// RemoveSentMessage removes a [Message] sent by the [NodeInterface].
//
// It returns an [ErrNotFound] if the given entity id does not match
// any message.
func (ni *NodeInterface) RemoveSentMessage(messageEntityID EntityID) error {
msg, err := ni.sentMessages.getValue(messageEntityID)
if err != nil {
return ni.errorf(&RemoveEntityError{
EntityID: messageEntityID,
Err: err,
})
}
msg.senderNodeInt = nil
ni.sentMessages.remove(messageEntityID)
ni.sentMessageNames.remove(msg.name)
if msg.hasStaticCANID {
ni.sentMessageStaticCANIDs.remove(msg.staticCANID)
if ni.hasParentBus() {
ni.parentBus.messageStaticCANIDs.remove(msg.staticCANID)
}
} else {
ni.sentMessageIDs.remove(msg.id)
}
return nil
}
// RemoveAllSentMessages removes all the messages sent by the [NodeInterface].
func (ni *NodeInterface) RemoveAllSentMessages() {
for _, tmpMsg := range ni.sentMessages.entries() {
tmpMsg.senderNodeInt = nil
if ni.hasParentBus() && tmpMsg.hasStaticCANID {
ni.parentBus.messageStaticCANIDs.remove(tmpMsg.staticCANID)
}
}
ni.sentMessages.clear()
ni.sentMessageNames.clear()
ni.sentMessageIDs.clear()
ni.sentMessageStaticCANIDs.clear()
}
// GetSentMessageByName returns the sent [Message] with the given name.
//
// It returns an [ErrNotFound] wrapped by a [NameError]
// if the name does not match any message.
func (ni *NodeInterface) GetSentMessageByName(name string) (*Message, error) {
id, err := ni.sentMessageNames.getValue(name)
if err != nil {
return nil, ni.errorf(&NameError{Name: name, Err: err})
}
msg, err := ni.sentMessages.getValue(id)
if err != nil {
panic(err)
}
return msg, nil
}
// SentMessages returns a slice of messages sent by the [NodeInterface].
func (ni *NodeInterface) SentMessages() []*Message {
msgSlice := ni.sentMessages.getValues()
slices.SortFunc(msgSlice, func(a, b *Message) int {
return int(a.id) - int(b.id)
})
return msgSlice
}
// AddReceivedMessage adds a [Message] that the [NodeInterface] can receive.
//
// It returns an [ArgumentError] if the given message is nil or
// a [ErrReceiverIsSender] wrapped by a [AddEntityError]
// if the message is already sent by the [NodeInterface].
func (ni *NodeInterface) AddReceivedMessage(message *Message) error {
if message == nil {
return ni.errorf(&ArgumentError{
Name: "message",
Err: ErrIsNil,
})
}
if err := ni.addReceivedMessage(message); err != nil {
return ni.errorf(&AddEntityError{
EntityID: message.entityID,
Name: message.name,
Err: err,
})
}
return nil
}
// RemoveReceivedMessage removes a [Message] received by the [NodeInterface].
//
// It returns an [ErrNotFound] wrapped by a [RemoveEntityError]
// if the given entity id does not match any received message.
func (ni *NodeInterface) RemoveReceivedMessage(messageEntityID EntityID) error {
msg, err := ni.receivedMessages.getValue(messageEntityID)
if err != nil {
return ni.errorf(&RemoveEntityError{
EntityID: messageEntityID,
Err: err,
})
}
ni.removeReceivedMessage(msg)
return nil
}
// RemoveAllReceivedMessages removes all the messages received by the [NodeInterface].
func (ni *NodeInterface) RemoveAllReceivedMessages() {
for _, tmpMsg := range ni.receivedMessages.entries() {
tmpMsg.receivers.remove(ni.node.entityID)
}
ni.receivedMessages.clear()
}
// ReceivedMessages returns a slice of messages received by the [NodeInterface].
func (ni *NodeInterface) ReceivedMessages() []*Message {
msgSlice := ni.receivedMessages.getValues()
slices.SortFunc(msgSlice, func(a, b *Message) int {
return int(a.id) - int(b.id)
})
return msgSlice
}
// Node returns the [Node] that owns the [NodeInterface].
func (ni *NodeInterface) Node() *Node {
return ni.node
}
// ParentBus returns the [Bus] attached to the [NodeInterface].
func (ni *NodeInterface) ParentBus() *Bus {
return ni.parentBus
}
// Number returns the number of the [NodeInterface].
// The number is unique among all the interfaces within a [Node]
// and it cannot be manually assigned.
func (ni *NodeInterface) Number() int {
return ni.number
}