2016-04-10 12:36:38 -07:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-04-10 12:36:38 -07:00
|
|
|
|
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2016-04-13 11:50:40 -07:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2016-04-10 12:36:38 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type DiscoverFunc func(renewal, timeout time.Duration) []Device
|
|
|
|
|
|
|
|
var providers []DiscoverFunc
|
|
|
|
|
|
|
|
func Register(provider DiscoverFunc) {
|
|
|
|
providers = append(providers, provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func discoverAll(renewal, timeout time.Duration) map[string]Device {
|
2016-04-13 11:50:40 -07:00
|
|
|
wg := sync.NewWaitGroup()
|
|
|
|
wg.Add(len(providers))
|
|
|
|
|
|
|
|
c := make(chan Device)
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2016-04-10 12:36:38 -07:00
|
|
|
for _, discoverFunc := range providers {
|
2016-04-13 11:50:40 -07:00
|
|
|
go func(f DiscoverFunc) {
|
|
|
|
for _, dev := range f(renewal, timeout) {
|
|
|
|
c <- dev
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(discoverFunc)
|
2016-04-10 12:36:38 -07:00
|
|
|
}
|
2016-04-13 11:50:40 -07:00
|
|
|
|
|
|
|
nats := make(map[string]Device)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for dev := range c {
|
|
|
|
nats[dev.ID()] = dev
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(c)
|
|
|
|
<-done
|
|
|
|
|
2016-04-10 12:36:38 -07:00
|
|
|
return nats
|
|
|
|
}
|