-
Notifications
You must be signed in to change notification settings - Fork 8
/
domain-browser.go
85 lines (72 loc) · 2.21 KB
/
domain-browser.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package avahi
import (
"fmt"
dbus "github.com/godbus/dbus/v5"
)
// A DomainBrowser is used to browse for mDNS domains
type DomainBrowser struct {
object dbus.BusObject
AddChannel chan Domain
RemoveChannel chan Domain
closeCh chan struct{}
}
const (
// DomainBrowserTypeBrowse - Browse for a list of available browsing domains
DomainBrowserTypeBrowse = 0
// DomainBrowserTypeBrowseDefault - Browse for the default browsing domain
DomainBrowserTypeBrowseDefault = 1
// DomainBrowserTypeRegister - Browse for a list of available registering domains
DomainBrowserTypeRegister = 2
// DomainBrowserTypeRegisterDefault - Browse for the default registering domain
DomainBrowserTypeRegisterDefault = 3
// DomainBrowserTypeBrowseLegacy - Legacy browse domain - see DNS-SD spec for more information
DomainBrowserTypeBrowseLegacy = 4
)
// DomainBrowserNew returns a new domain browser
func DomainBrowserNew(conn *dbus.Conn, path dbus.ObjectPath) (*DomainBrowser, error) {
c := new(DomainBrowser)
c.object = conn.Object("org.freedesktop.Avahi", path)
c.AddChannel = make(chan Domain)
c.RemoveChannel = make(chan Domain)
c.closeCh = make(chan struct{})
return c, nil
}
func (c *DomainBrowser) interfaceForMember(method string) string {
return fmt.Sprintf("%s.%s", "org.freedesktop.Avahi.DomainBrowser", method)
}
func (c *DomainBrowser) free() {
if c.closeCh != nil {
close(c.closeCh)
}
c.object.Call(c.interfaceForMember("Free"), 0)
}
func (c *DomainBrowser) getObjectPath() dbus.ObjectPath {
return c.object.Path()
}
func (c *DomainBrowser) dispatchSignal(signal *dbus.Signal) error {
if signal.Name == c.interfaceForMember("ItemNew") || signal.Name == c.interfaceForMember("ItemRemove") {
var domain Domain
err := dbus.Store(signal.Body, &domain.Interface, &domain.Protocol, &domain.Domain, &domain.Flags)
if err != nil {
return err
}
if signal.Name == c.interfaceForMember("ItemNew") {
select {
case c.AddChannel <- domain:
case <-c.closeCh:
close(c.AddChannel)
close(c.RemoveChannel)
c.closeCh = nil
}
} else {
select {
case c.RemoveChannel <- domain:
case <-c.closeCh:
close(c.AddChannel)
close(c.RemoveChannel)
c.closeCh = nil
}
}
}
return nil
}