From 187d53fd29375db3f9b0da2cbb2544af769066ae Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 16 Oct 2018 15:23:16 +0200 Subject: [PATCH] Fix Reach services discovery - Use github.com/grandcat/zeroconf lib instead of github.com/oleksandr/bonjour --- emlid/discovery.go | 51 +++++++++++++++------------------------ emlid/discovery_test.go | 6 ++++- example/discovery/main.go | 41 +++++++++++++++++++++++++++++++ go.mod | 7 +++--- go.sum | 14 ++++++----- 5 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 example/discovery/main.go diff --git a/emlid/discovery.go b/emlid/discovery.go index fba711e..f713207 100644 --- a/emlid/discovery.go +++ b/emlid/discovery.go @@ -1,65 +1,52 @@ package emlid import ( + "context" "net" "sync" - "time" - "github.com/oleksandr/bonjour" + "github.com/grandcat/zeroconf" ) // Service is a ReachRS service discovered via MDNS-SD type Service struct { Name string - AddrV4 net.IP + AddrV4 *net.IP Port int } -// Discover tries to discover ReachRS services on the local network via MDNS-SD -func Discover(timeout time.Duration) ([]Service, error) { +// Discover tries to discover ReachRS services on the local network via mDNS-SD +func Discover(ctx context.Context) ([]Service, error) { var wg sync.WaitGroup wg.Add(1) - resolver, err := bonjour.NewResolver(nil) + resolver, err := zeroconf.NewResolver(nil) if err != nil { return nil, err } services := make([]Service, 0) - entries := make(chan *bonjour.ServiceEntry) - timer := time.NewTimer(timeout) - defer timer.Stop() - - stop := func() { - resolver.Exit <- true - wg.Done() - } + entries := make(chan *zeroconf.ServiceEntry) go func() { - for { - select { - - case e, ok := <-entries: - if !ok { - stop() - return - } - services = append(services, Service{ - Name: e.Instance, - AddrV4: e.AddrIPv4, - Port: e.Port, - }) - - case <-timer.C: - stop() - return + for e := range entries { + var addr *net.IP + if len(e.AddrIPv4) > 0 { + addr = &e.AddrIPv4[0] } + srv := Service{ + Name: e.Instance, + AddrV4: addr, + Port: e.Port, + } + services = append(services, srv) } + wg.Done() }() - if err = resolver.Browse("_reach._tcp", ".local", entries); err != nil { + if err = resolver.Browse(ctx, "_reach._tcp", ".local", entries); err != nil { return nil, err } diff --git a/emlid/discovery_test.go b/emlid/discovery_test.go index 33fe64c..5f5241f 100644 --- a/emlid/discovery_test.go +++ b/emlid/discovery_test.go @@ -1,6 +1,7 @@ package emlid import ( + "context" "testing" "time" ) @@ -11,7 +12,10 @@ func TestDiscovery(t *testing.T) { t.Skip("To run this test, use: go test -discovery-integration") } - services, err := Discover(3 * time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + services, err := Discover(ctx) if err != nil { t.Fatal(err) } diff --git a/example/discovery/main.go b/example/discovery/main.go new file mode 100644 index 0000000..ab7b310 --- /dev/null +++ b/example/discovery/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "flag" + "log" + "time" + + "forge.cadoles.com/Pyxis/orion/emlid" +) + +var ( + timeout = 5 * time.Second +) + +func init() { + flag.DurationVar(&timeout, "timeout", timeout, "mDNS scan timeout") +} + +func main() { + + flag.Parse() + + log.Println("Searching for ReachRS services on network...") + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + services, err := emlid.Discover(ctx) + if err != nil { + log.Fatal(err) + } + + for _, s := range services { + log.Println("Found:") + log.Printf(" Name: '%s'", s.Name) + log.Printf(" Hosts: %s", s.AddrV4) + log.Printf(" Port: '%d'", s.Port) + } + +} diff --git a/go.mod b/go.mod index c1cbc6b..4989193 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,14 @@ module forge.cadoles.com/Pyxis/orion require ( forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 - github.com/caarlos0/env v3.3.0+incompatible + github.com/caarlos0/env v3.4.0+incompatible + github.com/cenkalti/backoff v2.0.0+incompatible // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-chi/chi v3.3.3+incompatible github.com/gorilla/websocket v1.4.0 // indirect + github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 github.com/miekg/dns v1.0.12 // indirect - github.com/mitchellh/mapstructure v1.0.0 - github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 + github.com/mitchellh/mapstructure v1.1.2 github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 // indirect diff --git a/go.sum b/go.sum index 70906a3..63a97cb 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,21 @@ forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 h1:o3G5+9RjczCK1xAYFaRMknk1kY9Ule6PNfiW6N6hEpg= forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0= -github.com/caarlos0/env v3.3.0+incompatible h1:jCfY0ilpzC2FFViyZyDKCxKybDESTwaR+ebh8zm6AOE= -github.com/caarlos0/env v3.3.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y= +github.com/caarlos0/env v3.4.0+incompatible h1:FRwBdvENjLHZoUbFnULnFss9wKtcapdaM35DfxiTjeM= +github.com/caarlos0/env v3.4.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y= +github.com/cenkalti/backoff v2.0.0+incompatible h1:5IIPUHhlnUZbcHQsQou5k1Tn58nJkeJL9U+ig5CHJbY= +github.com/cenkalti/backoff v2.0.0+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8= github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE= +github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1/go.mod h1:YjKB0WsLXlMkO9p+wGTCoPIDGRJH0mz7E526PxkQVxI= github.com/miekg/dns v1.0.12 h1:814rTNaw7Q7pGncpSEDT06YS8rdGmpUEnKgpQzctJsk= github.com/miekg/dns v1.0.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= -github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I= -github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 h1:Cvfd2dOlXIPTeEkOT/h8PyK4phBngOM4at9/jlgy7d4= -github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228/go.mod h1:MGuVJ1+5TX1SCoO2Sx0eAnjpdRytYla2uC1YIZfkC9c= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=