diff --git a/bus.go b/bus.go index 471eaa8..5ea1432 100644 --- a/bus.go +++ b/bus.go @@ -201,7 +201,7 @@ func (b *Bus) AddNodeInterface(nodeInterface *NodeInterface) error { return b.errorf(err) } - messages := nodeInterface.messages.getValues() + messages := nodeInterface.sentMessages.getValues() msgStaticCANIDs := make(map[CANID]EntityID) for _, tmpMsg := range messages { if tmpMsg.hasStaticCANID { @@ -246,7 +246,7 @@ func (b *Bus) RemoveNodeInterface(nodeInterfaceEntityID EntityID) error { b.nodeNames.remove(nodeInt.node.name) b.nodeIDs.remove(nodeInt.node.id) - for _, tmpMsg := range nodeInt.messages.getValues() { + for _, tmpMsg := range nodeInt.sentMessages.getValues() { if tmpMsg.hasStaticCANID { b.messageStaticCANIDs.remove(tmpMsg.staticCANID) } diff --git a/errors.go b/errors.go index 2ebc213..32df7fe 100644 --- a/errors.go +++ b/errors.go @@ -32,6 +32,9 @@ var ErrIntersect = errors.New("is intersecting") // ErrInvalidType is returned when an invalid type is used. var ErrInvalidType = errors.New("invalid type") +// ErrReceiverIsSender is returned when the receiver is the sender. +var ErrReceiverIsSender = errors.New("receiver is sender") + // ErrInvalidOneof is returned when a oneof field does not match // a kind/type field. type ErrInvalidOneof struct { diff --git a/examples/markdown/main.go b/examples/markdown/main.go index a5efd34..1af34dd 100644 --- a/examples/markdown/main.go +++ b/examples/markdown/main.go @@ -163,7 +163,7 @@ func checkErr(err error) { } func modifySignalTypeName(nodeInt *acmelib.NodeInterface, msgName, sigName, newName string) { - tmpMsg, err := nodeInt.GetMessageByName(msgName) + tmpMsg, err := nodeInt.GetSentMessageByName(msgName) checkErr(err) tmpSig, err := tmpMsg.GetSignalByName(sigName) checkErr(err) @@ -173,7 +173,7 @@ func modifySignalTypeName(nodeInt *acmelib.NodeInterface, msgName, sigName, newN } func modifySignalType(nodeInt *acmelib.NodeInterface, msgName, sigName string, newType *acmelib.SignalType) { - tmpMsg, err := nodeInt.GetMessageByName(msgName) + tmpMsg, err := nodeInt.GetSentMessageByName(msgName) checkErr(err) tmpSig, err := tmpMsg.GetSignalByName(sigName) checkErr(err) @@ -195,7 +195,7 @@ func parseNodeIDs(mcb *acmelib.Bus) { func parseMessageIDs(mcb *acmelib.Bus) { for _, tmpNodeInt := range mcb.NodeInterfaces() { - for _, tmpMsg := range tmpNodeInt.Messages() { + for _, tmpMsg := range tmpNodeInt.SentMessages() { if msgID, ok := messageIDs[tmpMsg.Name()]; ok { checkErr(tmpMsg.UpdateID(msgID)) } diff --git a/exporter.go b/exporter.go index cc0fd0e..1a8f2ff 100644 --- a/exporter.go +++ b/exporter.go @@ -291,7 +291,7 @@ func (e *exporter) exportNodeInterfaces(nodeInts []*NodeInterface) { dbcNodes.Names = append(dbcNodes.Names, nodeName) - for _, msg := range nodeInt.Messages() { + for _, msg := range nodeInt.SentMessages() { e.exportMessage(msg) } } diff --git a/importer.go b/importer.go index 47c0060..18a7f90 100644 --- a/importer.go +++ b/importer.go @@ -124,7 +124,7 @@ func (i *importer) importFile(dbcFile *dbc.File) (*Bus, error) { panic(err) } - if len(dummyNode.Messages()) == 0 { + if len(dummyNode.SentMessages()) == 0 { if err := bus.RemoveNodeInterface(dummyNode.node.entityID); err != nil { panic(err) } @@ -470,7 +470,7 @@ func (i *importer) importMessage(dbcMsg *dbc.Message) error { return i.errorf(dbcMsg, err) } - if err := sendNode.AddMessage(msg); err != nil { + if err := sendNode.AddSentMessage(msg); err != nil { return i.errorf(dbcMsg, err) } diff --git a/importer_test.go b/importer_test.go index 257824b..0d22d5a 100644 --- a/importer_test.go +++ b/importer_test.go @@ -43,7 +43,7 @@ func Test_ImportDBCFile(t *testing.T) { // testing signal types aggregation sigTypeIDs := make(map[EntityID]bool) for _, tmpNodeInt := range bus.NodeInterfaces() { - for _, tmpMsg := range tmpNodeInt.Messages() { + for _, tmpMsg := range tmpNodeInt.SentMessages() { for _, sig := range tmpMsg.signals.getValues() { if sig.Kind() != SignalKindStandard { continue diff --git a/loader.go b/loader.go index ad32bc4..6edd6d4 100644 --- a/loader.go +++ b/loader.go @@ -216,7 +216,7 @@ func (l *loader) loadNodeInterface(pNodeInt *acmelibv1.NodeInterface) (*NodeInte return nil, err } - if err := nodeInt.AddMessage(msg); err != nil { + if err := nodeInt.AddSentMessage(msg); err != nil { return nil, err } } diff --git a/loader_test.go b/loader_test.go index b1ff35f..c28bb09 100644 --- a/loader_test.go +++ b/loader_test.go @@ -44,8 +44,8 @@ func compareNodeInterfaces(assert *assert.Assertions, expected, curr *NodeInterf compareNode(assert, expected.Node(), curr.Node()) - expectedMessages := expected.Messages() - currMessages := curr.Messages() + expectedMessages := expected.SentMessages() + currMessages := curr.SentMessages() assert.Len(currMessages, len(expectedMessages)) for i, currMessage := range currMessages { expectedMessage := expectedMessages[i] diff --git a/md_exporter.go b/md_exporter.go index f27f4c6..20f9cd6 100644 --- a/md_exporter.go +++ b/md_exporter.go @@ -82,7 +82,7 @@ func (e *mdExporter) exportTOC(net *Network) { e.w.BulletList(e.getHeaderLink(bus.name)) for _, nodeInt := range bus.NodeInterfaces() { e.w.PlainTextf("\t- %s", e.getHeaderLink(nodeInt.node.name)) - for _, msg := range nodeInt.Messages() { + for _, msg := range nodeInt.SentMessages() { e.w.PlainTextf("\t\t- %s", e.getHeaderLink(msg.name)) } } @@ -124,7 +124,7 @@ func (e *mdExporter) exportNode(node *NodeInterface) { hexNodeIIDStr := "0x" + strconv.FormatUint(uint64(node.node.id), 16) e.w.PlainTextf("Node ID: %s (dec), %s (hex)", md.Bold(fmt.Sprintf("%d", node.node.id)), md.Bold(hexNodeIIDStr)).LF() - for _, msg := range node.Messages() { + for _, msg := range node.SentMessages() { e.exportMessage(msg) } } diff --git a/message.go b/message.go index 8142fba..7db5f5b 100644 --- a/message.go +++ b/message.go @@ -359,7 +359,7 @@ func (m *Message) UpdateName(newName string) error { } if m.hasSenderNodeInt() { - if err := m.senderNodeInt.messageNames.verifyKeyUnique(newName); err != nil { + if err := m.senderNodeInt.sentMessageNames.verifyKeyUnique(newName); err != nil { return m.errorf(&UpdateNameError{ Err: &NameError{ Name: newName, @@ -368,7 +368,7 @@ func (m *Message) UpdateName(newName string) error { }) } - m.senderNodeInt.messageNames.modifyKey(m.name, newName, m.entityID) + m.senderNodeInt.sentMessageNames.modifyKey(m.name, newName, m.entityID) } m.name = newName @@ -593,13 +593,45 @@ func (m *Message) StartDelayTime() int { } // AddReceiver adds a receiver to the [Message]. -func (m *Message) AddReceiver(receiver *NodeInterface) { - m.receivers.add(receiver.node.entityID, receiver) +// +// It returns an [ArgumentError] if the given receiver is nil or +// a [ErrReceiverIsSender] wrapped by an [AddEntityError] +// if the receiver is the same as the sender. +func (m *Message) AddReceiver(receiver *NodeInterface) error { + if receiver == nil { + return m.errorf(&ArgumentError{ + Name: "receiver", + Err: ErrIsNil, + }) + } + + if err := receiver.addReceivedMessage(m); err != nil { + return m.errorf(&AddEntityError{ + EntityID: receiver.node.entityID, + Name: receiver.node.name, + Err: err, + }) + } + + return nil } // RemoveReceiver removes a receiver from the [Message]. -func (m *Message) RemoveReceiver(receiverEntityID EntityID) { - m.receivers.remove(receiverEntityID) +// +// It returns an [ErrNotFound] wrapped by a [RemoveEntityError] +// if the receiver with the given entity id is not found. +func (m *Message) RemoveReceiver(receiverEntityID EntityID) error { + receiver, err := m.receivers.getValue(receiverEntityID) + if err != nil { + return m.errorf(&RemoveEntityError{ + EntityID: receiverEntityID, + Err: err, + }) + } + + receiver.removeReceivedMessage(m) + + return nil } // Receivers returns a slice of all receivers of the [Message]. diff --git a/message_test.go b/message_test.go index 398772b..c283f7f 100644 --- a/message_test.go +++ b/message_test.go @@ -254,13 +254,13 @@ func Test_Message_SetStaticCANID(t *testing.T) { msg1 := NewMessage("msg_1", 1, 1) assert.NoError(msg1.SetStaticCANID(500)) assert.Equal(CANID(500), msg1.GetCANID()) - assert.NoError(nodeInt1.AddMessage(msg1)) + assert.NoError(nodeInt1.AddSentMessage(msg1)) msg2 := NewMessage("msg_2", 2, 1) assert.NoError(msg2.SetStaticCANID(500)) - assert.Error(nodeInt1.AddMessage(msg2)) + assert.Error(nodeInt1.AddSentMessage(msg2)) assert.NoError(msg2.SetStaticCANID(600)) - assert.NoError(nodeInt1.AddMessage(msg2)) + assert.NoError(nodeInt1.AddSentMessage(msg2)) node2 := NewNode("node_2", 2, 1) nodeInt2 := node2.Interfaces()[0] @@ -268,15 +268,45 @@ func Test_Message_SetStaticCANID(t *testing.T) { msg3 := NewMessage("msg_3", 3, 1) assert.NoError(msg3.SetStaticCANID(600)) - assert.Error(nodeInt2.AddMessage(msg3)) + assert.Error(nodeInt2.AddSentMessage(msg3)) assert.NoError(msg3.SetStaticCANID(700)) - assert.NoError(nodeInt1.AddMessage(msg3)) + assert.NoError(nodeInt1.AddSentMessage(msg3)) node3 := NewNode("node_3", 3, 1) nodeInt3 := node3.Interfaces()[0] msg4 := NewMessage("msg_4", 4, 1) assert.NoError(msg4.SetStaticCANID(700)) - assert.NoError(nodeInt3.AddMessage(msg4)) + assert.NoError(nodeInt3.AddSentMessage(msg4)) assert.Error(bus.AddNodeInterface(nodeInt3)) } + +func Test_Message_AddReceiver(t *testing.T) { + assert := assert.New(t) + + node0 := NewNode("node_0", 0, 2) + nodeInt00 := node0.Interfaces()[0] + nodeInt01 := node0.Interfaces()[1] + + node1 := NewNode("node_1", 1, 1) + nodeInt1 := node1.Interfaces()[0] + + node2 := NewNode("node_2", 2, 1) + nodeInt2 := node2.Interfaces()[0] + + msg := NewMessage("msg", 1, 1) + assert.NoError(nodeInt00.AddSentMessage(msg)) + + assert.Error(msg.AddReceiver(nodeInt00)) + assert.NoError(msg.AddReceiver(nodeInt1)) + + assert.Len(msg.Receivers(), 1) + assert.Len(nodeInt1.ReceivedMessages(), 1) + + assert.NoError(msg.AddReceiver(nodeInt01)) + assert.NoError(msg.AddReceiver(nodeInt2)) + + assert.Len(msg.Receivers(), 3) + assert.Len(nodeInt01.ReceivedMessages(), 1) + assert.Len(nodeInt2.ReceivedMessages(), 1) +} diff --git a/node_iterface.go b/node_iterface.go index d672498..aca6204 100644 --- a/node_iterface.go +++ b/node_iterface.go @@ -11,10 +11,12 @@ import ( type NodeInterface struct { parentBus *Bus - messages *set[EntityID, *Message] - messageNames *set[string, EntityID] - messageIDs *set[MessageID, EntityID] - messageStaticCANIDs *set[CANID, EntityID] + sentMessages *set[EntityID, *Message] + sentMessageNames *set[string, EntityID] + sentMessageIDs *set[MessageID, EntityID] + sentMessageStaticCANIDs *set[CANID, EntityID] + + receivedMessages *set[EntityID, *Message] number int node *Node @@ -24,10 +26,12 @@ func newNodeInterface(number int, node *Node) *NodeInterface { return &NodeInterface{ parentBus: nil, - messages: newSet[EntityID, *Message](), - messageNames: newSet[string, EntityID](), - messageIDs: newSet[MessageID, EntityID](), - messageStaticCANIDs: newSet[CANID, EntityID](), + sentMessages: newSet[EntityID, *Message](), + sentMessageNames: newSet[string, EntityID](), + sentMessageIDs: newSet[MessageID, EntityID](), + sentMessageStaticCANIDs: newSet[CANID, EntityID](), + + receivedMessages: newSet[EntityID, *Message](), number: number, node: node, @@ -57,12 +61,18 @@ func (ni *NodeInterface) stringify(b *strings.Builder, tabs int) { b.WriteString(fmt.Sprintf("%snode:\n", tabStr)) ni.node.stringify(b, tabs+1) - if ni.messages.size() == 0 { + if ni.sentMessages.size() == 0 { return } - b.WriteString(fmt.Sprintf("%ssended_messages:\n", tabStr)) - for _, msg := range ni.Messages() { + 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') } @@ -75,7 +85,7 @@ func (ni *NodeInterface) String() string { } func (ni *NodeInterface) verifyMessageName(name string) error { - err := ni.messageNames.verifyKeyUnique(name) + err := ni.sentMessageNames.verifyKeyUnique(name) if err != nil { return &NameError{ Name: name, @@ -86,7 +96,7 @@ func (ni *NodeInterface) verifyMessageName(name string) error { } func (ni *NodeInterface) verifyMessageID(msgID MessageID) error { - err := ni.messageIDs.verifyKeyUnique(msgID) + err := ni.sentMessageIDs.verifyKeyUnique(msgID) if err != nil { return &MessageIDError{ MessageID: msgID, @@ -97,7 +107,7 @@ func (ni *NodeInterface) verifyMessageID(msgID MessageID) error { } func (ni *NodeInterface) verifyStaticCANID(staticCANID CANID) error { - if err := ni.messageStaticCANIDs.verifyKeyUnique(staticCANID); err != nil { + if err := ni.sentMessageStaticCANIDs.verifyKeyUnique(staticCANID); err != nil { return &CANIDError{ CANID: staticCANID, Err: err, @@ -116,11 +126,27 @@ func (ni *NodeInterface) verifyStaticCANID(staticCANID CANID) error { return nil } -// AddMessage adds a [Message] that the [NodeInterface] can send. +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) AddMessage(message *Message) error { +func (ni *NodeInterface) AddSentMessage(message *Message) error { if message == nil { return &ArgumentError{ Name: "message", @@ -148,30 +174,30 @@ func (ni *NodeInterface) AddMessage(message *Message) error { ni.parentBus.messageStaticCANIDs.add(message.staticCANID, message.entityID) } - ni.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.messageIDs.add(message.id, message.entityID) + ni.sentMessageIDs.add(message.id, message.entityID) } - ni.messages.add(message.entityID, message) - ni.messageNames.add(message.name, message.entityID) + ni.sentMessages.add(message.entityID, message) + ni.sentMessageNames.add(message.name, message.entityID) message.senderNodeInt = ni return nil } -// RemoveMessage removes a [Message] sent by the [NodeInterface]. +// 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) RemoveMessage(messageEntityID EntityID) error { - msg, err := ni.messages.getValue(messageEntityID) +func (ni *NodeInterface) RemoveSentMessage(messageEntityID EntityID) error { + msg, err := ni.sentMessages.getValue(messageEntityID) if err != nil { return ni.errorf(&RemoveEntityError{ EntityID: messageEntityID, @@ -181,26 +207,26 @@ func (ni *NodeInterface) RemoveMessage(messageEntityID EntityID) error { msg.senderNodeInt = nil - ni.messages.remove(messageEntityID) - ni.messageNames.remove(msg.name) + ni.sentMessages.remove(messageEntityID) + ni.sentMessageNames.remove(msg.name) if msg.hasStaticCANID { - ni.messageStaticCANIDs.remove(msg.staticCANID) + ni.sentMessageStaticCANIDs.remove(msg.staticCANID) if ni.hasParentBus() { ni.parentBus.messageStaticCANIDs.remove(msg.staticCANID) } } else { - ni.messageIDs.remove(msg.id) + ni.sentMessageIDs.remove(msg.id) } return nil } -// RemoveAllMessages removes all the messages sent by the [NodeInterface]. -func (ni *NodeInterface) RemoveAllMessages() { - for _, tmpMsg := range ni.messages.entries() { +// 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 { @@ -208,23 +234,23 @@ func (ni *NodeInterface) RemoveAllMessages() { } } - ni.messages.clear() - ni.messageNames.clear() - ni.messageIDs.clear() - ni.messageStaticCANIDs.clear() + ni.sentMessages.clear() + ni.sentMessageNames.clear() + ni.sentMessageIDs.clear() + ni.sentMessageStaticCANIDs.clear() } -// GetMessageByName returns the [Message] with the given name. +// 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) GetMessageByName(name string) (*Message, error) { - id, err := ni.messageNames.getValue(name) +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.messages.getValue(id) + msg, err := ni.sentMessages.getValue(id) if err != nil { panic(err) } @@ -232,9 +258,69 @@ func (ni *NodeInterface) GetMessageByName(name string) (*Message, error) { return msg, nil } -// Messages returns a slice of messages sended by the [NodeInterface]. -func (ni *NodeInterface) Messages() []*Message { - msgSlice := ni.messages.getValues() +// 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) }) diff --git a/node_test.go b/node_test.go index fba2de5..f073e9c 100644 --- a/node_test.go +++ b/node_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_Node_AddMessage(t *testing.T) { +func Test_Node_AddSentMessage(t *testing.T) { assert := assert.New(t) node := NewNode("node", 0, 1).Interfaces()[0] @@ -16,26 +16,26 @@ func Test_Node_AddMessage(t *testing.T) { msg2 := NewMessage("msg_2", 3, 1) // should add msg0, msg1, and msg2 without errors - assert.NoError(node.AddMessage(msg0)) - assert.NoError(node.AddMessage(msg1)) - assert.NoError(node.AddMessage(msg2)) + assert.NoError(node.AddSentMessage(msg0)) + assert.NoError(node.AddSentMessage(msg1)) + assert.NoError(node.AddSentMessage(msg2)) expectedIDs := []MessageID{1, 2, 3} expectedNames := []string{"msg_0", "msg_1", "msg_2"} - for idx, tmpMsg := range node.Messages() { + for idx, tmpMsg := range node.SentMessages() { assert.Equal(expectedIDs[idx], tmpMsg.ID()) assert.Equal(expectedNames[idx], tmpMsg.Name()) } // should return an error because id 3 is already taken dupIDMsg := NewMessage("", 3, 1) - assert.Error(node.AddMessage(dupIDMsg)) + assert.Error(node.AddSentMessage(dupIDMsg)) // should return an error because name msg_2 is already taken dupNameMsg := NewMessage("msg_2", 4, 1) - assert.Error(node.AddMessage(dupNameMsg)) + assert.Error(node.AddSentMessage(dupNameMsg)) } -func Test_Node_RemoveMessage(t *testing.T) { +func Test_Node_RemoveSentMessage(t *testing.T) { assert := assert.New(t) node := NewNode("node", 0, 1).Interfaces()[0] @@ -45,25 +45,25 @@ func Test_Node_RemoveMessage(t *testing.T) { msg2 := NewMessage("msg_2", 3, 1) msg3 := NewMessage("msg_3", 4, 1) - assert.NoError(node.AddMessage(msg0)) - assert.NoError(node.AddMessage(msg1)) - assert.NoError(node.AddMessage(msg2)) - assert.NoError(node.AddMessage(msg3)) + assert.NoError(node.AddSentMessage(msg0)) + assert.NoError(node.AddSentMessage(msg1)) + assert.NoError(node.AddSentMessage(msg2)) + assert.NoError(node.AddSentMessage(msg3)) // should be able to remove msg1 and to cause the other ids to re-generate with the exeption of msg3 - assert.NoError(node.RemoveMessage(msg1.EntityID())) + assert.NoError(node.RemoveSentMessage(msg1.EntityID())) expectedIDs := []MessageID{1, 3, 4} expectedNames := []string{"msg_0", "msg_2", "msg_3"} - for idx, tmpMsg := range node.Messages() { + for idx, tmpMsg := range node.SentMessages() { assert.Equal(expectedIDs[idx], tmpMsg.ID()) assert.Equal(expectedNames[idx], tmpMsg.Name()) } // should return an error because the entity id is invalid - assert.Error(node.RemoveMessage("dummy-id")) + assert.Error(node.RemoveSentMessage("dummy-id")) } -func Test_Node_RemoveAllMessages(t *testing.T) { +func Test_Node_RemoveSentMessages(t *testing.T) { assert := assert.New(t) node := NewNode("node", 0, 1).Interfaces()[0] @@ -73,14 +73,14 @@ func Test_Node_RemoveAllMessages(t *testing.T) { msg2 := NewMessage("msg_2", 3, 1) msg3 := NewMessage("msg_3", 4, 1) - assert.NoError(node.AddMessage(msg0)) - assert.NoError(node.AddMessage(msg1)) - assert.NoError(node.AddMessage(msg2)) - assert.NoError(node.AddMessage(msg3)) + assert.NoError(node.AddSentMessage(msg0)) + assert.NoError(node.AddSentMessage(msg1)) + assert.NoError(node.AddSentMessage(msg2)) + assert.NoError(node.AddSentMessage(msg3)) - node.RemoveAllMessages() + node.RemoveAllSentMessages() - assert.Equal(0, len(node.Messages())) + assert.Equal(0, len(node.SentMessages())) } func Test_Node_UpdateName(t *testing.T) { diff --git a/saver.go b/saver.go index f1974f2..bbf0337 100644 --- a/saver.go +++ b/saver.go @@ -320,7 +320,7 @@ func (s *saver) saveNodeInterface(nodeInt *NodeInterface) *acmelibv1.NodeInterfa s.refNodes[nodeEntID] = nodeInt.node pNodeint.NodeEntityId = nodeEntID.String() - for _, msg := range nodeInt.Messages() { + for _, msg := range nodeInt.SentMessages() { pNodeint.Messages = append(pNodeint.Messages, s.saveMessage(msg)) } diff --git a/saver_test.go b/saver_test.go index 4d6c3df..6e45c5f 100644 --- a/saver_test.go +++ b/saver_test.go @@ -24,7 +24,7 @@ func initTestNetwork(assert *assert.Assertions) *Network { // msg0 has a signal in big endian order and a multiplexer signal msg0 := NewMessage("msg_0", 1, 8) - assert.NoError(node0Int.AddMessage(msg0)) + assert.NoError(node0Int.AddSentMessage(msg0)) size4Type, err := NewIntegerSignalType("4_bits", 4, false) assert.NoError(err) @@ -53,7 +53,7 @@ func initTestNetwork(assert *assert.Assertions) *Network { // msg1 has a 2 level nested multiplexer signals msg1 := NewMessage("msg_1", 2, 8) - assert.NoError(node0Int.AddMessage(msg1)) + assert.NoError(node0Int.AddSentMessage(msg1)) msg1.SetDesc("msg1 description") muxSig1, err := NewMultiplexerSignal("mux_sig_1", 4, 16) @@ -79,7 +79,7 @@ func initTestNetwork(assert *assert.Assertions) *Network { // msg2 has an enum signal msg2 := NewMessage("msg_2", 3, 8) - assert.NoError(node0Int.AddMessage(msg2)) + assert.NoError(node0Int.AddSentMessage(msg2)) msg2.AddReceiver(recNode0Int) enum := NewSignalEnum("enum") @@ -94,7 +94,7 @@ func initTestNetwork(assert *assert.Assertions) *Network { // msg3 is big endian msg3 := NewMessage("msg_3", 4, 1) msg3.SetByteOrder(MessageByteOrderBigEndian) - assert.NoError(node0Int.AddMessage(msg3)) + assert.NoError(node0Int.AddSentMessage(msg3)) stdSig1, err := NewStandardSignal("std_sig_1", size4Type) assert.NoError(err) stdSig2, err := NewStandardSignal("std_sig_2", size4Type) diff --git a/utils.go b/utils.go index 9f8a962..4c1fbea 100644 --- a/utils.go +++ b/utils.go @@ -39,7 +39,7 @@ func CalculateBusLoad(bus *Bus, defCycleTime int) (float64, error) { consumedBitsPerSec := float64(0) for _, tmpInt := range bus.nodeInts.getValues() { - for _, tmpMsg := range tmpInt.messages.getValues() { + for _, tmpMsg := range tmpInt.sentMessages.getValues() { stuffingBits := (headerStuffingBits + tmpMsg.sizeByte*8 - 1) / 4 msgBits := tmpMsg.sizeByte*8 + headerBits + trailerBits + stuffingBits diff --git a/utils_test.go b/utils_test.go index 2f414a9..b0e2452 100644 --- a/utils_test.go +++ b/utils_test.go @@ -18,10 +18,10 @@ func Test_CalculateBusLoad(t *testing.T) { msg0 := NewMessage("msg_0", 1, 8) msg0.SetCycleTime(10) - assert.NoError(nodeInt.AddMessage(msg0)) + assert.NoError(nodeInt.AddSentMessage(msg0)) msg1 := NewMessage("msg_1", 2, 8) msg1.SetCycleTime(10) - assert.NoError(nodeInt.AddMessage(msg1)) + assert.NoError(nodeInt.AddSentMessage(msg1)) load, err := CalculateBusLoad(bus, 500) assert.NoError(err)