Skip to content

Commit

Permalink
feat: add support for received messages in node interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Nov 24, 2024
1 parent e0b2af2 commit d803b75
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 97 deletions.
4 changes: 2 additions & 2 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions examples/markdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions md_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
44 changes: 38 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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].
Expand Down
42 changes: 36 additions & 6 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,29 +254,59 @@ 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]
assert.NoError(bus.AddNodeInterface(nodeInt2))

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)
}
Loading

0 comments on commit d803b75

Please sign in to comment.