【发布时间】:2021-04-08 09:54:24
【问题描述】:
使用godbus 库并根据他们的服务器example,我正在尝试export(服务)具有不同接口的两个不同对象。一个对象路径是/a/b/c,另一个是/a/b/c/d。如果我导出其中一个,一切正常。即使没有重叠,一切正常(/a/b/c & /w/x/y/z)。但是导出/a/b/c 和/a/b/c/d 只会导致其中一个出现在dbus 上。这是我的代码:
package main
import (
"fmt"
"os"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
)
const introP = `
<node>
<interface name="a.b.c.Ping">
<method name="Ping">
<arg direction="out" type="s"/>
</method>
</interface>` + introspect.IntrospectDataString + `</node> `
type ping string
func (p ping) Ping() (string, *dbus.Error) {
fmt.Println(p)
return string(p), nil
}
const introZ = `
<node>
<interface name="a.b.c.d.Zing">
<method name="Zing">
<arg direction="out" type="s"/>
</method>
</interface>` + introspect.IntrospectDataString + `</node> `
type zing string
func (z zing) Zing() (string, *dbus.Error) {
fmt.Println(z)
return string(z), nil
}
func main() {
conn, err := dbus.ConnectSessionBus()
if err != nil {
panic(err)
}
defer conn.Close()
reply, err := conn.RequestName("a.b.c",
dbus.NameFlagDoNotQueue)
if err != nil {
panic(err)
}
if reply != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
p := ping("Pong")
conn.Export(p, "/a/b/c", "a.b.c.Ping")
conn.Export(introspect.Introspectable(introP), "/a/b/c",
"org.freedesktop.DBus.Introspectable")
z := zing("Zong")
conn.Export(z, "/a/b/c/d", "a.b.c.d.Zing")
conn.Export(introspect.Introspectable(introZ), "/a/b/c/d",
"org.freedesktop.DBus.Introspectable")
fmt.Println("Listening on dbus...")
select {}
}
【问题讨论】: