From 794d596ce0b222a267fc7917f8565ca6ac4ef6a3 Mon Sep 17 00:00:00 2001 From: gswly <46489434+gswly@users.noreply.github.com> Date: Fri, 22 Mar 2019 22:05:49 +0100 Subject: [PATCH] update gomavlib and implement channel open/close messages --- go.mod | 2 +- go.sum | 4 ++-- main.go | 54 +++++++++++++++++++++++++++--------------------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 5c66b34..2caaf8a 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index e43f59a..1f7742f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index eca72f0..13c4aef 100644 --- a/main.go +++ b/main.go @@ -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()) @@ -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) } }