Skip to content

Commit

Permalink
Fix DNS-SSD discovery process
Browse files Browse the repository at this point in the history
  • Loading branch information
richtr committed Feb 20, 2015
1 parent 8f0090c commit 748febd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
17 changes: 1 addition & 16 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,25 @@ func (dc *DiscoveryService) Shutdown() {
type DiscoveryBrowser struct {
// Network Web Socket DNS-SD records currently unresolved by this proxy instance
cachedDNSRecords map[string]*DNSRecord
inprogress bool
closed bool
}

func NewDiscoveryBrowser() *DiscoveryBrowser {
discoveryBrowser := &DiscoveryBrowser{
cachedDNSRecords: make(map[string]*DNSRecord, 255),
inprogress: false,
closed: false,
}

return discoveryBrowser
}

func (ds *DiscoveryBrowser) Browse(service *Service, intervalSeconds, timeoutSeconds int) {

// Don't run two browse processes at the same time
if ds.inprogress {
return
}

ds.inprogress = true
func (ds *DiscoveryBrowser) Browse(service *Service, timeoutSeconds int) {

entries := make(chan *mdns.ServiceEntry, 255)

recordsCache := make(map[string]*DNSRecord, 255)

timeout := time.Duration(timeoutSeconds) * time.Second
interval := time.Duration(intervalSeconds) * time.Second

var targetIPv4 *net.UDPAddr
var targetIPv6 *net.UDPAddr
Expand All @@ -151,7 +141,6 @@ func (ds *DiscoveryBrowser) Browse(service *Service, intervalSeconds, timeoutSec
go func() {
complete := false
timeoutFinish := time.After(timeout)
intervalFinish := time.After(interval)

// Wait for responses until timeout
for !complete {
Expand Down Expand Up @@ -203,8 +192,6 @@ func (ds *DiscoveryBrowser) Browse(service *Service, intervalSeconds, timeoutSec
// Replace unresolved DNS records cache
ds.cachedDNSRecords = recordsCache

ds.inprogress = false
case <-intervalFinish:
complete = true
}
}
Expand All @@ -213,8 +200,6 @@ func (ds *DiscoveryBrowser) Browse(service *Service, intervalSeconds, timeoutSec
// Run the mDNS/DNS-SD query
err := mdns.Query(params)

time.Sleep(interval) // sleep until next loop is scheduled

if err != nil {
log.Printf("Could not perform mDNS/DNS-SD query. %v", err)
return
Expand Down
12 changes: 3 additions & 9 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (service *Service) Start() <-chan int {
service.StartProxyServer()

// Start mDNS/DNS-SD Network Web Socket discovery service
service.StartDiscoveryBrowser(10, 2)
service.StartDiscoveryBrowser(10)

return service.StopNotify()
}
Expand Down Expand Up @@ -155,14 +155,14 @@ func (service *Service) StartProxyServer() {
go http.Serve(tlsSrpListener, serveMux)
}

func (service *Service) StartDiscoveryBrowser(intervalSeconds, timeoutSeconds int) {
func (service *Service) StartDiscoveryBrowser(timeoutSeconds int) {
log.Printf("Listening for Network Web Socket services on the local network...")

go func() {
defer service.discoveryBrowser.Shutdown()

for !service.discoveryBrowser.closed {
service.discoveryBrowser.Browse(service, intervalSeconds, timeoutSeconds)
service.discoveryBrowser.Browse(service, timeoutSeconds)
}
}()
}
Expand Down Expand Up @@ -216,9 +216,6 @@ func (service *Service) serveWSCreatorRequest(w http.ResponseWriter, r *http.Req
channel := service.GetChannelByName(serviceName)
if channel == nil {
channel = NewChannel(service, serviceName)

// Trigger network service detection to speed up pairing (1)
go service.discoveryBrowser.Browse(service, 2, 2)
}

// Serve network web socket channel peer
Expand Down Expand Up @@ -247,9 +244,6 @@ func (service *Service) serveWSProxyRequest(w http.ResponseWriter, r *http.Reque
if channel.proxyPath == r.URL.Path {
channel.ServeProxy(w, r)

// Trigger network service detection to speed up pairing (2)
go service.discoveryBrowser.Browse(service, 2, 2)

return
}
}
Expand Down

0 comments on commit 748febd

Please sign in to comment.