-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
212 lines (173 loc) · 5 KB
/
node.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
package acmelib
import (
"fmt"
"strings"
)
// NodeID is a unique identifier for a [Node].
type NodeID uint32
func (nid NodeID) String() string {
return fmt.Sprintf("%d", nid)
}
// Node is the representation of an ECU or an electronic component capable
// to send messages over a [Bus] through one or more [NodeInterface].
// It holds a list of interfaces that can send messages on the bus.
type Node struct {
*entity
*withAttributes
interfaces []*NodeInterface
intErrNum int
id NodeID
interfaceCount int
}
func newNodeFromEntity(ent *entity, id NodeID, intCount int) *Node {
node := &Node{
entity: ent,
withAttributes: newWithAttributes(),
interfaces: []*NodeInterface{},
intErrNum: -1,
id: id,
interfaceCount: intCount,
}
node.interfaces = make([]*NodeInterface, intCount)
for i := 0; i < intCount; i++ {
node.interfaces[i] = newNodeInterface(i, node)
}
return node
}
// NewNode creates a new [Node] with the given name, id and count of interfaces.
// The id must be unique among all nodes within a bus.
func NewNode(name string, id NodeID, interfaceCount int) *Node {
return newNodeFromEntity(newEntity(name, EntityKindNode), id, interfaceCount)
}
func (n *Node) errorf(err error) error {
nodeErr := &EntityError{
Kind: EntityKindNode,
EntityID: n.entityID,
Name: n.name,
Err: err,
}
if len(n.interfaces) > 0 {
if n.intErrNum >= 0 {
nodeInt := n.interfaces[n.intErrNum]
n.intErrNum = -1
return nodeInt.errorf(nodeErr)
}
}
return nodeErr
}
func (n *Node) stringify(b *strings.Builder, tabs int) {
n.entity.stringify(b, tabs)
tabStr := getTabString(tabs)
b.WriteString(fmt.Sprintf("%snode_id: %s\n", tabStr, n.id.String()))
}
func (n *Node) String() string {
builder := new(strings.Builder)
n.stringify(builder, 0)
return builder.String()
}
// UpdateName updates the name of the [Node].
// By updating the name, also the name of the interfaces are updated.
//
// It may return a [NameError] that wraps the cause of the error.
func (n *Node) UpdateName(newName string) error {
if n.name == newName {
return nil
}
buses := []*Bus{}
for _, tmpInt := range n.interfaces {
if !tmpInt.hasParentBus() {
continue
}
tmpBus := tmpInt.parentBus
if err := tmpBus.verifyNodeName(newName); err != nil {
n.intErrNum = tmpInt.number
return n.errorf(&UpdateNameError{Err: err})
}
buses = append(buses, tmpBus)
}
for _, tmpBus := range buses {
tmpBus.nodeNames.modifyKey(n.name, newName, n.entityID)
}
n.name = newName
return nil
}
// UpdateID updates the id of the [Node].
//
// It returns a [NodeError] if the new id is invalid.
func (n *Node) UpdateID(newID NodeID) error {
if newID == n.id {
return nil
}
buses := []*Bus{}
for _, tmpNodeInt := range n.interfaces {
if !tmpNodeInt.hasParentBus() {
continue
}
if err := tmpNodeInt.parentBus.verifyNodeID(newID); err != nil {
return n.errorf(err)
}
buses = append(buses, tmpNodeInt.parentBus)
}
for _, tmpBus := range buses {
tmpBus.nodeIDs.modifyKey(n.id, newID, tmpBus.entityID)
}
n.id = newID
return nil
}
// Interfaces returns a slice with all the interfaces of the [Node].
func (n *Node) Interfaces() []*NodeInterface {
return n.interfaces
}
// GetInterface returns the [NodeInterface] with the given interface number.
//
// It returns an [ArgumentError] if the interface number is negative or out of bounds.
func (n *Node) GetInterface(interfaceNumber int) (*NodeInterface, error) {
if interfaceNumber < 0 {
return nil, &ArgumentError{
Name: "interfaceNumber",
Err: ErrIsNegative,
}
}
if interfaceNumber >= n.interfaceCount {
return nil, &ArgumentError{
Name: "interfaceNumber",
Err: ErrOutOfBounds,
}
}
return n.interfaces[interfaceNumber], nil
}
// ID returns the id of the [Node].
func (n *Node) ID() NodeID {
return n.id
}
// AssignAttribute assigns the given attribute/value pair to the [Node].
//
// It returns an [ArgumentError] if the attribute is nil,
// or an [AttributeValueError] if the value does not conform to the attribute.
func (n *Node) AssignAttribute(attribute Attribute, value any) error {
if err := n.addAttributeAssignment(attribute, n, value); err != nil {
return n.errorf(err)
}
return nil
}
// RemoveAttributeAssignment removes the [AttributeAssignment]
// with the given attribute entity id from the [Node].
//
// It returns an [ErrNotFound] if the provided attribute entity id is not found.
func (n *Node) RemoveAttributeAssignment(attributeEntityID EntityID) error {
if err := n.removeAttributeAssignment(attributeEntityID); err != nil {
return n.errorf(err)
}
return nil
}
// GetAttributeAssignment returns the [AttributeAssignment]
// with the given attribute entity id from the [Node].
//
// It returns an [ErrNotFound] if the provided attribute entity id is not found.
func (n *Node) GetAttributeAssignment(attributeEntityID EntityID) (*AttributeAssignment, error) {
attAss, err := n.getAttributeAssignment(attributeEntityID)
if err != nil {
return nil, n.errorf(err)
}
return attAss, nil
}