Browse Source

Fix Reach services discovery

- Use github.com/grandcat/zeroconf lib instead of github.com/oleksandr/bonjour
develop
William Petit 10 months ago
parent
commit
187d53fd29
5 changed files with 77 additions and 42 deletions
  1. 19
    32
      emlid/discovery.go
  2. 5
    1
      emlid/discovery_test.go
  3. 41
    0
      example/discovery/main.go
  4. 4
    3
      go.mod
  5. 8
    6
      go.sum

+ 19
- 32
emlid/discovery.go View File

@@ -1,65 +1,52 @@
1 1
 package emlid
2 2
 
3 3
 import (
4
+	"context"
4 5
 	"net"
5 6
 	"sync"
6
-	"time"
7 7
 
8
-	"github.com/oleksandr/bonjour"
8
+	"github.com/grandcat/zeroconf"
9 9
 )
10 10
 
11 11
 // Service is a ReachRS service discovered via MDNS-SD
12 12
 type Service struct {
13 13
 	Name   string
14
-	AddrV4 net.IP
14
+	AddrV4 *net.IP
15 15
 	Port   int
16 16
 }
17 17
 
18
-// Discover tries to discover ReachRS services on the local network via MDNS-SD
19
-func Discover(timeout time.Duration) ([]Service, error) {
18
+// Discover tries to discover ReachRS services on the local network via mDNS-SD
19
+func Discover(ctx context.Context) ([]Service, error) {
20 20
 
21 21
 	var wg sync.WaitGroup
22 22
 
23 23
 	wg.Add(1)
24 24
 
25
-	resolver, err := bonjour.NewResolver(nil)
25
+	resolver, err := zeroconf.NewResolver(nil)
26 26
 	if err != nil {
27 27
 		return nil, err
28 28
 	}
29 29
 
30 30
 	services := make([]Service, 0)
31
-	entries := make(chan *bonjour.ServiceEntry)
32
-	timer := time.NewTimer(timeout)
33
-	defer timer.Stop()
34
-
35
-	stop := func() {
36
-		resolver.Exit <- true
37
-		wg.Done()
38
-	}
31
+	entries := make(chan *zeroconf.ServiceEntry)
39 32
 
40 33
 	go func() {
41
-		for {
42
-			select {
43
-
44
-			case e, ok := <-entries:
45
-				if !ok {
46
-					stop()
47
-					return
48
-				}
49
-				services = append(services, Service{
50
-					Name:   e.Instance,
51
-					AddrV4: e.AddrIPv4,
52
-					Port:   e.Port,
53
-				})
54
-
55
-			case <-timer.C:
56
-				stop()
57
-				return
34
+		for e := range entries {
35
+			var addr *net.IP
36
+			if len(e.AddrIPv4) > 0 {
37
+				addr = &e.AddrIPv4[0]
38
+			}
39
+			srv := Service{
40
+				Name:   e.Instance,
41
+				AddrV4: addr,
42
+				Port:   e.Port,
58 43
 			}
44
+			services = append(services, srv)
59 45
 		}
46
+		wg.Done()
60 47
 	}()
61 48
 
62
-	if err = resolver.Browse("_reach._tcp", ".local", entries); err != nil {
49
+	if err = resolver.Browse(ctx, "_reach._tcp", ".local", entries); err != nil {
63 50
 		return nil, err
64 51
 	}
65 52
 

+ 5
- 1
emlid/discovery_test.go View File

@@ -1,6 +1,7 @@
1 1
 package emlid
2 2
 
3 3
 import (
4
+	"context"
4 5
 	"testing"
5 6
 	"time"
6 7
 )
@@ -11,7 +12,10 @@ func TestDiscovery(t *testing.T) {
11 12
 		t.Skip("To run this test, use: go test -discovery-integration")
12 13
 	}
13 14
 
14
-	services, err := Discover(3 * time.Second)
15
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
16
+	defer cancel()
17
+
18
+	services, err := Discover(ctx)
15 19
 	if err != nil {
16 20
 		t.Fatal(err)
17 21
 	}

+ 41
- 0
example/discovery/main.go View File

@@ -0,0 +1,41 @@
1
+package main
2
+
3
+import (
4
+	"context"
5
+	"flag"
6
+	"log"
7
+	"time"
8
+
9
+	"forge.cadoles.com/Pyxis/orion/emlid"
10
+)
11
+
12
+var (
13
+	timeout = 5 * time.Second
14
+)
15
+
16
+func init() {
17
+	flag.DurationVar(&timeout, "timeout", timeout, "mDNS scan timeout")
18
+}
19
+
20
+func main() {
21
+
22
+	flag.Parse()
23
+
24
+	log.Println("Searching for ReachRS services on network...")
25
+
26
+	ctx, cancel := context.WithTimeout(context.Background(), timeout)
27
+	defer cancel()
28
+
29
+	services, err := emlid.Discover(ctx)
30
+	if err != nil {
31
+		log.Fatal(err)
32
+	}
33
+
34
+	for _, s := range services {
35
+		log.Println("Found:")
36
+		log.Printf("  Name: '%s'", s.Name)
37
+		log.Printf("  Hosts: %s", s.AddrV4)
38
+		log.Printf("  Port: '%d'", s.Port)
39
+	}
40
+
41
+}

+ 4
- 3
go.mod View File

@@ -2,13 +2,14 @@ module forge.cadoles.com/Pyxis/orion
2 2
 
3 3
 require (
4 4
 	forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
5
-	github.com/caarlos0/env v3.3.0+incompatible
5
+	github.com/caarlos0/env v3.4.0+incompatible
6
+	github.com/cenkalti/backoff v2.0.0+incompatible // indirect
6 7
 	github.com/davecgh/go-spew v1.1.1 // indirect
7 8
 	github.com/go-chi/chi v3.3.3+incompatible
8 9
 	github.com/gorilla/websocket v1.4.0 // indirect
10
+	github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1
9 11
 	github.com/miekg/dns v1.0.12 // indirect
10
-	github.com/mitchellh/mapstructure v1.0.0
11
-	github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228
12
+	github.com/mitchellh/mapstructure v1.1.2
12 13
 	github.com/pkg/errors v0.8.0
13 14
 	github.com/pmezard/go-difflib v1.0.0 // indirect
14 15
 	github.com/stretchr/testify v1.2.2 // indirect

+ 8
- 6
go.sum View File

@@ -1,19 +1,21 @@
1 1
 forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95 h1:o3G5+9RjczCK1xAYFaRMknk1kY9Ule6PNfiW6N6hEpg=
2 2
 forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95/go.mod h1:I6kYOFWNkFlNeQLI7ZqfTRz4NdPHZxX0Bzizmzgchs0=
3
-github.com/caarlos0/env v3.3.0+incompatible h1:jCfY0ilpzC2FFViyZyDKCxKybDESTwaR+ebh8zm6AOE=
4
-github.com/caarlos0/env v3.3.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
3
+github.com/caarlos0/env v3.4.0+incompatible h1:FRwBdvENjLHZoUbFnULnFss9wKtcapdaM35DfxiTjeM=
4
+github.com/caarlos0/env v3.4.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
5
+github.com/cenkalti/backoff v2.0.0+incompatible h1:5IIPUHhlnUZbcHQsQou5k1Tn58nJkeJL9U+ig5CHJbY=
6
+github.com/cenkalti/backoff v2.0.0+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
5 7
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6 8
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7 9
 github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5wsuSBEFI8=
8 10
 github.com/go-chi/chi v3.3.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
9 11
 github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
10 12
 github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
13
+github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1 h1:VSELJSxQlpi1bz4ZwT+93hPpzNLRcgytLr77iVRJpcE=
14
+github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1/go.mod h1:YjKB0WsLXlMkO9p+wGTCoPIDGRJH0mz7E526PxkQVxI=
11 15
 github.com/miekg/dns v1.0.12 h1:814rTNaw7Q7pGncpSEDT06YS8rdGmpUEnKgpQzctJsk=
12 16
 github.com/miekg/dns v1.0.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
13
-github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
14
-github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
15
-github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 h1:Cvfd2dOlXIPTeEkOT/h8PyK4phBngOM4at9/jlgy7d4=
16
-github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228/go.mod h1:MGuVJ1+5TX1SCoO2Sx0eAnjpdRytYla2uC1YIZfkC9c=
17
+github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
18
+github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
17 19
 github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
18 20
 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
19 21
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

Loading…
Cancel
Save