go - 无法导出两个 dbus 对象
问题描述
使用Godbus库并根据他们的服务器示例,我试图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 {}
}
解决方案
package main
import (
"fmt"
"os"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
)
type ping string
func (p ping) Ping() (string, *dbus.Error) {
fmt.Println(p)
return string(p), nil
}
type zing string
func (z zing) Zing() (string, *dbus.Error) {
fmt.Println(z)
return string(z), nil
}
func main() {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
replyP, errP := conn.RequestName("a.b.c.d.Ping",
dbus.NameFlagDoNotQueue)
if errP != nil {
panic(errP)
}
if replyP != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
p := ping("Pong")
var introP = &introspect.Node{
Name: "/a/b/c/d/Ping",
Interfaces: []introspect.Interface{
introspect.IntrospectData,
{
Name: "a.b.c.d.Ping",
Methods: introspect.Methods(p),
},
},
}
conn.Export(p, "/a/b/c/d/Ping", "a.b.c.d.Ping")
z := zing("Zong")
var introZ = &introspect.Node{
Name: "/a/b/c/Zing",
Interfaces: []introspect.Interface{
introspect.IntrospectData,
{
Name: "a.b.c.Zing",
Methods: introspect.Methods(z),
},
},
}
conn.Export(z, "/a/b/c/Zing", "a.b.c.Zing")
conn.Export(introspect.NewIntrospectable(&introspect.Node{
Name: "/",
Children: []introspect.Node{
{
Name: "a",
},
},
}), "/", "org.freedesktop.DBus.Introspectable")
conn.Export(introspect.NewIntrospectable(&introspect.Node{
Name: "/a",
Children: []introspect.Node{
{
Name: "b",
},
},
}), "/com", "org.freedesktop.DBus.Introspectable")
conn.Export(introspect.NewIntrospectable(&introspect.Node{
Name: "/a/b",
Children: []introspect.Node{
{
Name: "c",
},
},
}), "/a/b", "org.freedesktop.DBus.Introspectable")
conn.Export(introspect.NewIntrospectable(&introspect.Node{
Name: "/a/b/c",
Children: []introspect.Node{
{
Name: "d",
},
{
Name: "Zing",
},
},
}), "/a/b/c", "org.freedesktop.DBus.Introspectable")
conn.Export(introspect.NewIntrospectable(&introspect.Node{
Name: "/a/b/c/d",
Children: []introspect.Node{
{
Name: "Ping",
},
},
}), "/a/b/c/d", "org.freedesktop.DBus.Introspectable")
conn.Export(introspect.NewIntrospectable(introP), "/a/b/c/d/Ping",
"org.freedesktop.DBus.Introspectable")
conn.Export(introspect.NewIntrospectable(introZ), "/a/b/c/Zing",
"org.freedesktop.DBus.Introspectable")
fmt.Printf("Listening on %s / %s ...\n", "a.b.c...", "/a/b/c...")
select {}
}
推荐阅读
- javascript - 如何从 html 类中删除额外的“”集?
- javascript - 从菜单中单击页面会给我一个“找不到页面”错误:使用把手渲染
- jupyter-notebook - 在 ubuntu EC2 机器中“未找到”jupyter 内核
- angular - 如何使用将类属性定义为 spys 的 createSpyObj 进行 jasmine 测试
- javascript - 如何根据从我的下拉列表中选择的选项更新 Chart.js?
- python - 如何按字典的值排序字典,它是一个由 3 个值组成的数组?
- ms-word - Autohotkey 禁用或强制与一个窗口交互
- python - 如何在 Python 中以固定顺序获取元素的子列表?填充物呢?
- clang - 使用 emscripten 构建最新的 freetype
- python - 使用 python 请求登录任何网站?