Initial upload
This commit is contained in:
commit
cefd7abe8a
19 changed files with 1027 additions and 0 deletions
57
fcast/discovery.go
Normal file
57
fcast/discovery.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package fcast
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/mdns"
|
||||
"log"
|
||||
)
|
||||
|
||||
type DiscoveredHost struct {
|
||||
Name string
|
||||
IPv4 string
|
||||
IPv6 string
|
||||
}
|
||||
|
||||
func Discover() ([]DiscoveredHost, error) {
|
||||
|
||||
entriesCh := make(chan *mdns.ServiceEntry)
|
||||
var discoveredHosts []DiscoveredHost
|
||||
|
||||
go func() {
|
||||
for entry := range entriesCh {
|
||||
if !validateEntry(entry) {
|
||||
continue
|
||||
}
|
||||
discoveredHosts = append(discoveredHosts, DiscoveredHost{
|
||||
Name: entry.Name,
|
||||
IPv4: entry.AddrV4.String(),
|
||||
IPv6: entry.AddrV6.String(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
err := mdns.Lookup("_fcast._tcp", entriesCh)
|
||||
if err != nil {
|
||||
log.Println("[ERROR] mdns: lookup error:", err)
|
||||
}
|
||||
close(entriesCh)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
return discoveredHosts, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func validateEntry(entry *mdns.ServiceEntry) bool {
|
||||
|
||||
if entry.Port != DefaultPort {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue