forked from AndreasAbdi/gochromecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceFinder.go
executable file
·44 lines (37 loc) · 1.17 KB
/
DeviceFinder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cast
import (
"strings"
"time"
"github.com/hashicorp/mdns"
)
//Hard defined device buffer size for
//Chromecasts are chatty so we wouldn't need to worry too much about lots of devices in one network. It's not really feasible.
const deviceBufferSearchSize = 100
//FindDevices searches the LAN for chromecast devices via mDNS and sends them to a channel.
func FindDevices(timeout time.Duration, devices chan<- *Device) {
// Make a channel for results and start listening
entries := make(chan *mdns.ServiceEntry, deviceBufferSearchSize)
go lookupChromecastMDNSEntries(entries, timeout)
go createDeviceObjects(entries, devices)
}
func createDeviceObjects(entries <-chan *mdns.ServiceEntry, devices chan<- *Device) {
defer close(devices)
for entry := range entries {
if !strings.Contains(entry.Name, chromecastServiceName) {
return
}
device, err := NewDevice(entry.Addr, entry.Port)
if err != nil {
return
}
devices <- &device
}
}
func lookupChromecastMDNSEntries(entries chan<- *mdns.ServiceEntry, timeout time.Duration) {
defer close(entries)
mdns.Query(&mdns.QueryParam{
Service: chromecastServiceName,
Timeout: timeout,
Entries: entries,
})
}