forked from holoplot/go-avahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-browser.go
61 lines (48 loc) · 1.5 KB
/
service-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
package avahi
import (
"fmt"
dbus "github.com/godbus/dbus/v5"
)
// A ServiceBrowser browses for mDNS services
type ServiceBrowser struct {
object dbus.BusObject
addChannel chan Service
removeChannel chan Service
}
// ServiceBrowserNew creates a new browser for mDNS records
func ServiceBrowserNew(addChan, removeChan chan Service, conn *dbus.Conn, path dbus.ObjectPath) (ServiceBrowserInterface, error) {
c := new(ServiceBrowser)
c.object = conn.Object("org.freedesktop.Avahi", path)
c.addChannel = addChan
c.removeChannel = removeChan
return c, nil
}
var _ ServiceBrowserInterface = (*ServiceBrowser)(nil)
func (c *ServiceBrowser) interfaceForMember(method string) string {
return fmt.Sprintf("%s.%s", "org.freedesktop.Avahi.ServiceBrowser", method)
}
func (c *ServiceBrowser) Free() {
c.object.Call(c.interfaceForMember("Free"), 0)
}
func (c *ServiceBrowser) GetObjectPath() dbus.ObjectPath {
return c.object.Path()
}
func (c *ServiceBrowser) DispatchSignal(signal *dbus.Signal) error {
if signal.Name == c.interfaceForMember("ItemNew") || signal.Name == c.interfaceForMember("ItemRemove") {
var service Service
err := dbus.Store(signal.Body, &service.Interface, &service.Protocol, &service.Name, &service.Type, &service.Domain, &service.Flags)
if err != nil {
return err
}
if signal.Name == c.interfaceForMember("ItemNew") {
if c.addChannel != nil {
c.addChannel <- service
}
} else {
if c.removeChannel != nil {
c.removeChannel <- service
}
}
}
return nil
}