This repository has been archived on 2021-07-08. You can view files and clone it, but cannot push or open issues or pull requests.
opensend/deviceDiscovery.go

81 lines
2.6 KiB
Go
Raw Normal View History

/*
Copyright © 2021 Arsen Musayelyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-12-03 10:12:43 +00:00
package main
import (
"context"
"os"
"time"
2021-06-19 07:36:48 +00:00
"github.com/grandcat/zeroconf"
"github.com/rs/zerolog/log"
2020-12-03 10:12:43 +00:00
)
// Discover opensend receivers on the network
func DiscoverReceivers() ([]string, []string) {
// Use ConsoleWriter logger
// Create zeroconf resolver
resolver, err := zeroconf.NewResolver(nil)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error creating zeroconf resolver")
}
2020-12-03 10:12:43 +00:00
// Create channel for zeroconf entries
entries := make(chan *zeroconf.ServiceEntry)
// Create slice to store hostnames of discovered receivers
var discoveredReceivers []string
// Create slice to store IPs of discovered receivers
var discoveredReceiverIPs []string
// Concurrently run mDNS query
go func(results <-chan *zeroconf.ServiceEntry) {
// For each entry
for entry := range results {
// Append hostname to discoveredReceivers
discoveredReceivers = append(discoveredReceivers, entry.HostName)
// Append IP to discoveredReceiverIPs
discoveredReceiverIPs = append(discoveredReceiverIPs, entry.AddrIPv4[0].String())
}
}(entries)
// Create context with 4 second timeout
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
// Cancel context at the end of this function
defer cancel()
// Browse for mDNS entries
err = resolver.Browse(ctx, "_opensend._tcp", "local.", entries)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error browsing zeroconf services")
}
2020-12-03 10:12:43 +00:00
// Send Done signal to context
<-ctx.Done()
// Return discovered receiver slices
return discoveredReceivers, discoveredReceiverIPs
}
// Register opensend zeroconf service on the network
func RegisterService() func() {
// Get computer hostname
hostname, _ := os.Hostname()
// Register zeroconf service {hostname}._opensend._tcp.local.
server, err := zeroconf.Register(hostname, "_opensend._tcp", "local.", 9797, []string{"txtv=0", "lo=1", "la=2"}, nil)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error registering zeroconf service")
}
2020-12-03 10:12:43 +00:00
// Return server.Shutdown() function to allow for shutdown in main()
return server.Shutdown
2020-12-21 07:18:42 +00:00
}