Skip to content

Commit

Permalink
route all messages with a TargetSystem and TargetComponent field (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Jun 19, 2023
1 parent ea571ee commit 217a5ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"os"
"reflect"
"regexp"
"strconv"
"sync"
Expand All @@ -29,20 +30,21 @@ var (
// decode/encode only a minimal set of messages.
// other messages change too frequently and cannot be integrated into a static tool.
func generateDialect(hbDisable bool, streamreqDisable bool) *dialect.Dialect {
msgs := []message.Message{
&common.MessageCommandLong{},
&common.MessageCommandAck{},
&common.MessageCommandInt{},
msgs := []message.Message{}

// add all messages with the TargetSystem and TargetComponent fields
var zero reflect.Value
for _, msg := range common.Dialect.Messages {
rv := reflect.ValueOf(msg).Elem()
if rv.FieldByName("TargetSystem") != zero && rv.FieldByName("TargetComponent") != zero {
msgs = append(msgs, msg)
}
}

if !hbDisable || !streamreqDisable {
msgs = append(msgs, &common.MessageHeartbeat{})
}

if !streamreqDisable {
msgs = append(msgs, &common.MessageRequestDataStream{})
}

return &dialect.Dialect{Version: 3, Messages: msgs}
}

Expand Down
16 changes: 8 additions & 8 deletions message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"reflect"
"sync"
"time"

Expand All @@ -16,16 +17,15 @@ const (
nodeInactiveAfter = 30 * time.Second
)

func getTarget(msg message.Message) (byte, byte, bool) {
switch msg := msg.(type) {
case *common.MessageCommandLong:
return msg.TargetSystem, msg.TargetComponent, true
var zero reflect.Value

case *common.MessageCommandAck:
return msg.TargetSystem, msg.TargetComponent, true
func getTarget(msg message.Message) (byte, byte, bool) {
rv := reflect.ValueOf(msg).Elem()
ts := rv.FieldByName("TargetSystem")
tc := rv.FieldByName("TargetComponent")

case *common.MessageCommandInt:
return msg.TargetSystem, msg.TargetComponent, true
if ts != zero && tc != zero {
return byte(ts.Uint()), byte(tc.Uint()), true
}

return 0, 0, false
Expand Down

0 comments on commit 217a5ca

Please sign in to comment.