Skip to content

Commit

Permalink
update gomavlib and implement channel open/close messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Mar 22, 2019
1 parent fdc7239 commit 794d596
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module mavp2p

require (
github.com/gswly/gomavlib v0.0.0-20190318210610-324fbffd973d
github.com/gswly/gomavlib v0.0.0-20190322210237-4a2fd9f7bdc8
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gswly/gomavlib v0.0.0-20190318210610-324fbffd973d h1:gikHFZFXBMZzGyPeyZV+G47gawdjFMdhTl+5vKAxMkI=
github.com/gswly/gomavlib v0.0.0-20190318210610-324fbffd973d/go.mod h1:1xQWxU9I/Y19PPQg8rJh+Wxk0TC1qvZR3btjKYH5WC4=
github.com/gswly/gomavlib v0.0.0-20190322210237-4a2fd9f7bdc8 h1:0v6v6o4dC318XM7EZk27bEmASm25CrgFnY7jpbzAZf0=
github.com/gswly/gomavlib v0.0.0-20190322210237-4a2fd9f7bdc8/go.mod h1:1xQWxU9I/Y19PPQg8rJh+Wxk0TC1qvZR3btjKYH5WC4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
54 changes: 27 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,10 @@ func main() {
}
return gomavlib.V1
}(),
SystemId: byte(*hbSystemId),
ComponentId: 1,
HeartbeatDisable: *hbDisable,
HeartbeatPeriod: (time.Duration(*hbPeriod) * time.Second),
ReturnParseErrors: true,
SystemId: byte(*hbSystemId),
ComponentId: 1,
HeartbeatDisable: *hbDisable,
HeartbeatPeriod: (time.Duration(*hbPeriod) * time.Second),
})
if err != nil {
initError(err.Error())
Expand All @@ -169,33 +168,34 @@ func main() {
}()
}

for {
// wait until a message is received.
res, ok := node.Read()
if ok == false {
break
}
for e := range node.Events() {
switch evt := e.(type) {
case *gomavlib.NodeEventChannelOpen:
log.Printf("channel opened: %s", evt.Channel)

case *gomavlib.NodeEventChannelClose:
log.Printf("channel closed: %s", evt.Channel)

case *gomavlib.NodeEventFrame:
// new node
nodeId := NodeId{
SystemId: evt.SystemId(),
ComponentId: evt.ComponentId(),
}
if _, ok := nodes[nodeId]; !ok {
nodes[nodeId] = struct{}{}
log.Printf("node appeared: sid=%d, cid=%d", evt.SystemId(), evt.ComponentId())
}

if res.Error != nil {
// route message to every other channel
node.WriteFrameExcept(evt.Channel, evt.Frame)

case *gomavlib.NodeEventParseError:
if *printErrors == true {
log.Printf("err: %s", res.Error)
log.Printf("err: %s", evt.Error)
} else {
errorCount++
}
continue
}

// new node
nodeId := NodeId{
SystemId: res.SystemId(),
ComponentId: res.ComponentId(),
}
if _, ok := nodes[nodeId]; !ok {
nodes[nodeId] = struct{}{}
log.Printf("new node (sid=%d, cid=%d)", res.SystemId(), res.ComponentId())
}

// route message to every other channel
node.WriteFrameExcept(res.Channel, res.Frame)
}
}

0 comments on commit 794d596

Please sign in to comment.