forked from holoplot/go-avahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain-browser.go
68 lines (55 loc) · 1.9 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
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
}
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.DomainBrowser", path)
c.AddChannel = make(chan Domain, 10)
c.RemoveChannel = make(chan Domain, 10)
return c, nil
}
func (c *DomainBrowser) interfaceForMember(method string) string {
return fmt.Sprintf("%s.%s", "org.freedesktop.Avahi.DomainBrowser", method)
}
func (c *DomainBrowser) free() {
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") {
c.AddChannel <- domain
} else {
c.RemoveChannel <- domain
}
}
return nil
}