forked from holoplot/go-avahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-type-browser.go
82 lines (67 loc) · 2.05 KB
/
service-type-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
package avahi
import (
"fmt"
dbus "github.com/godbus/dbus/v5"
)
// A ServiceTypeBrowser is used to browser the mDNS network for services of a specific type
type ServiceTypeBrowser struct {
object dbus.BusObject
addChannel chan ServiceType
removeChannel chan ServiceType
closeCh chan struct{}
}
// ServiceTypeBrowserNew creates a new browser for mDNS service types
func ServiceTypeBrowserNew(conn *dbus.Conn, path dbus.ObjectPath) (ServiceTypeBrowserInterface, error) {
c := new(ServiceTypeBrowser)
c.object = conn.Object("org.freedesktop.Avahi", path)
c.addChannel = make(chan ServiceType)
c.removeChannel = make(chan ServiceType)
c.closeCh = make(chan struct{})
return c, nil
}
var _ ServiceTypeBrowserInterface = (*ServiceTypeBrowser)(nil)
func (c *ServiceTypeBrowser) AddChannel() chan ServiceType {
return c.addChannel
}
func (c *ServiceTypeBrowser) RemoveChannel() chan ServiceType {
return c.removeChannel
}
func (c *ServiceTypeBrowser) interfaceForMember(method string) string {
return fmt.Sprintf("%s.%s", "org.freedesktop.Avahi.ServiceTypeBrowser", method)
}
func (c *ServiceTypeBrowser) Free() {
if c.closeCh != nil {
close(c.closeCh)
}
c.object.Call(c.interfaceForMember("Free"), 0)
}
func (c *ServiceTypeBrowser) GetObjectPath() dbus.ObjectPath {
return c.object.Path()
}
func (c *ServiceTypeBrowser) DispatchSignal(signal *dbus.Signal) error {
if signal.Name == c.interfaceForMember("ItemNew") || signal.Name == c.interfaceForMember("ItemRemove") {
var serviceType ServiceType
err := dbus.Store(signal.Body, &serviceType.Interface, &serviceType.Protocol, &serviceType.Type, &serviceType.Domain, &serviceType.Flags)
if err != nil {
return err
}
if signal.Name == c.interfaceForMember("ItemNew") {
select {
case c.addChannel <- serviceType:
case <-c.closeCh:
close(c.addChannel)
close(c.removeChannel)
c.closeCh = nil
}
} else {
select {
case c.removeChannel <- serviceType:
case <-c.closeCh:
close(c.addChannel)
close(c.removeChannel)
c.closeCh = nil
}
}
}
return nil
}