Add service discovery helper
Use MDNS-SD to discover ReachRS services on the local network
This commit is contained in:
70
emlid/discovery.go
Normal file
70
emlid/discovery.go
Normal 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
29
emlid/discovery_test.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
@ -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
8
emlid/util_test.go
Normal 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)",
|
||||
)
|
Reference in New Issue
Block a user