基于Go的D-Bus框架(二)
mathmonkey · · 6897 次点击 · · 开始浏览D-Bus 核心定义了一套自省机制,这个自省跟面向对象里的自省有点类似,即可以在运行时查询对象的相关信息,包括接口和属性等。大部分支持 D-Bus 的程序都实现了 org.freedesktop.DBus.Introspectable 这个标准接口,为它们导出的对象提供自省支持。这个接口只有一个方法 Introspect,它返回对象的XML格式描述。
go.dbus 给出了一个示例,server.go:package main
import (
"fmt"
"github.com/guelfey/go.dbus"
"github.com/guelfey/go.dbus/introspect"
"os"
)
const intro = `
<node>
<interface name="com.github.guelfey.Demo">
<method name="Foo">
<arg direction="out" type="s"/>
</method>
</interface>` + introspect.IntrospectDataString + `</node> `
type foo string
func (f foo) Foo() (string, *dbus.Error) {
fmt.Println(f)
return string(f), nil
}
func main() {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
reply, err := conn.RequestName("com.github.guelfey.Demo",
dbus.NameFlagDoNotQueue)
if err != nil {
panic(err)
}
if reply != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
f := foo("Bar!")
conn.Export(f, "/com/github/guelfey/Demo", "com.github.guelfey.Demo")
conn.Export(introspect.Introspectable(intro), "/com/github/guelfey/Demo",
"org.freedesktop.DBus.Introspectable")
fmt.Println("Listening on com.github.guelfey.Demo / /com/github/guelfey/Demo ...")
select {}
}
运行server.go,使用如下命令对 /com/github/guelfey/Demo 这个对象调用 org.freedesktop.DBus.Introspectable.Introspect:
$ dbus-send --session --print-reply --dest="com.github.guelfey.Demo" /com/github/guelfey/Demo org.freedesktop.DBus.Introspectable.Introspect
就可以得到这个对象的自省数据。
当然,这里XML字符串是硬编码在源文件中的。手写XML是一件很无聊的事。这次开源夏令营的目标之一就是自动生成自省数据。具体思路是静态扫描源文件,并且支持函数参数名称和结构体字段名称导出。
go.dbus 对 D-Bus 的核心协议已经有了完整的实现,go-dbus 在此基础上提供一个更加易用的接口,其底层依然是依赖go.dbus的。这几天通过研究go.dbus的源代码,对于D-Bus底层以及如何扩展它都有了更深的了解。现在终于感觉这个项目有点眉目清晰的感觉了,接下来就应该编码来实现具体功能了。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
D-Bus 核心定义了一套自省机制,这个自省跟面向对象里的自省有点类似,即可以在运行时查询对象的相关信息,包括接口和属性等。大部分支持 D-Bus 的程序都实现了 org.freedesktop.DBus.Introspectable 这个标准接口,为它们导出的对象提供自省支持。这个接口只有一个方法 Introspect,它返回对象的XML格式描述。
go.dbus 给出了一个示例,server.go:package main
import (
"fmt"
"github.com/guelfey/go.dbus"
"github.com/guelfey/go.dbus/introspect"
"os"
)
const intro = `
<node>
<interface name="com.github.guelfey.Demo">
<method name="Foo">
<arg direction="out" type="s"/>
</method>
</interface>` + introspect.IntrospectDataString + `</node> `
type foo string
func (f foo) Foo() (string, *dbus.Error) {
fmt.Println(f)
return string(f), nil
}
func main() {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}
reply, err := conn.RequestName("com.github.guelfey.Demo",
dbus.NameFlagDoNotQueue)
if err != nil {
panic(err)
}
if reply != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "name already taken")
os.Exit(1)
}
f := foo("Bar!")
conn.Export(f, "/com/github/guelfey/Demo", "com.github.guelfey.Demo")
conn.Export(introspect.Introspectable(intro), "/com/github/guelfey/Demo",
"org.freedesktop.DBus.Introspectable")
fmt.Println("Listening on com.github.guelfey.Demo / /com/github/guelfey/Demo ...")
select {}
}
运行server.go,使用如下命令对 /com/github/guelfey/Demo 这个对象调用 org.freedesktop.DBus.Introspectable.Introspect:
$ dbus-send --session --print-reply --dest="com.github.guelfey.Demo" /com/github/guelfey/Demo org.freedesktop.DBus.Introspectable.Introspect
就可以得到这个对象的自省数据。
当然,这里XML字符串是硬编码在源文件中的。手写XML是一件很无聊的事。这次开源夏令营的目标之一就是自动生成自省数据。具体思路是静态扫描源文件,并且支持函数参数名称和结构体字段名称导出。
go.dbus 对 D-Bus 的核心协议已经有了完整的实现,go-dbus 在此基础上提供一个更加易用的接口,其底层依然是依赖go.dbus的。这几天通过研究go.dbus的源代码,对于D-Bus底层以及如何扩展它都有了更深的了解。现在终于感觉这个项目有点眉目清晰的感觉了,接下来就应该编码来实现具体功能了。