-
Notifications
You must be signed in to change notification settings - Fork 8
/
record-browser.go
67 lines (55 loc) · 1.55 KB
/
record-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
package avahi
import (
"fmt"
dbus "github.com/godbus/dbus/v5"
)
// A RecordBrowser is a browser for mDNS records
type RecordBrowser struct {
object dbus.BusObject
AddChannel chan Record
RemoveChannel chan Record
closeCh chan struct{}
}
// RecordBrowserNew creates a new mDNS record browser
func RecordBrowserNew(conn *dbus.Conn, path dbus.ObjectPath) (*RecordBrowser, error) {
c := new(RecordBrowser)
c.object = conn.Object("org.freedesktop.Avahi", path)
c.AddChannel = make(chan Record)
c.RemoveChannel = make(chan Record)
c.closeCh = make(chan struct{})
return c, nil
}
func (c *RecordBrowser) interfaceForMember(method string) string {
return fmt.Sprintf("%s.%s", "org.freedesktop.Avahi.RecordBrowser", method)
}
func (c *RecordBrowser) free() {
close(c.closeCh)
close(c.AddChannel)
close(c.RemoveChannel)
c.object.Call(c.interfaceForMember("Free"), 0)
}
func (c *RecordBrowser) getObjectPath() dbus.ObjectPath {
return c.object.Path()
}
func (c *RecordBrowser) dispatchSignal(signal *dbus.Signal) error {
if signal.Name == c.interfaceForMember("ItemNew") || signal.Name == c.interfaceForMember("ItemRemove") {
var record Record
err := dbus.Store(signal.Body, &record.Interface, &record.Protocol, &record.Name,
&record.Class, &record.Type, &record.Rdata, &record.Flags)
if err != nil {
return err
}
if signal.Name == c.interfaceForMember("ItemNew") {
select {
case c.AddChannel <- record:
case <-c.closeCh:
}
} else {
select {
case c.RemoveChannel <- record:
case <-c.closeCh:
}
}
}
return nil
}