Add service discovery helper

Use MDNS-SD to discover ReachRS services on the local network
This commit is contained in:
wpetit 2018-10-09 14:05:38 +02:00
parent fd8657a495
commit 9bd20331ef
6 changed files with 129 additions and 0 deletions

70
emlid/discovery.go Normal file
View File

@ -0,0 +1,70 @@
package emlid
import (
"net"
"sync"
"time"
"github.com/oleksandr/bonjour"
)
// Service is a ReachRS service discovered via MDNS-SD
type Service struct {
Name string
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) {
var wg sync.WaitGroup
wg.Add(1)
resolver, err := bonjour.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()
}
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
}
}
}()
if err = resolver.Browse("_reach._tcp", ".local", entries); err != nil {
return nil, err
}
wg.Wait()
return services, nil
}

29
emlid/discovery_test.go Normal file
View File

@ -0,0 +1,29 @@
package emlid
import (
"testing"
"time"
)
func TestDiscovery(t *testing.T) {
if !*runDiscoveryIntegrationTests {
t.Skip("To run this test, use: go test -discovery-integration")
}
services, err := Discover(3 * time.Second)
if err != nil {
t.Fatal(err)
}
if g, e := len(services), 1; g != e {
t.Fatalf("len(services): got '%d', expected '%d'", g, e)
}
s := services[0]
if g, e := s.Name, "reach"; g != e {
t.Errorf("services[0].Name: got '%s', expected '%s'", g, e)
}
}

View File

@ -34,6 +34,13 @@ func WithEndpoint(host string, port int) OptionFunc {
}
}
// WithService configures the client to target the given ReachRS service
func WithService(service Service) OptionFunc {
return func(opts *Options) {
opts.Endpoint = gosocketio.GetUrl(service.AddrV4.String(), service.Port, false)
}
}
// WithPingInterval configures the client to use the given ping interval
func WithPingInterval(interval time.Duration) OptionFunc {
return func(opts *Options) {

8
emlid/util_test.go Normal file
View File

@ -0,0 +1,8 @@
package emlid
import "flag"
var runDiscoveryIntegrationTests = flag.Bool(
"discovery-integration", false,
"Run the 'Discovery' integration tests (in addition to the unit tests)",
)

5
go.mod
View File

@ -6,10 +6,15 @@ require (
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/miekg/dns v1.0.12 // indirect
github.com/mitchellh/mapstructure v1.0.0
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 // indirect
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 // indirect
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e // indirect
golang.org/x/text v0.3.0 // indirect
)

10
go.sum
View File

@ -8,15 +8,25 @@ github.com/go-chi/chi v3.3.3+incompatible h1:KHkmBEMNkwKuK4FdQL7N2wOeB9jnIx7jR5w
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/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/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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4 h1:Vk3wNqEZwyGyei9yq5ekj7frek2u7HUfffJ1/opblzc=
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e h1:EfdBzeKbFSvOjoIqSZcfS8wp0FBLokGBEs9lz1OtSg0=
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=