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.guelf