This is a creation in Article, where the information may have evolved or changed.
The D-bus core defines a self-reflection mechanism that is somewhat similar to introspection in object-oriented, that is, you can query information about an object at run time, including interfaces and properties. Most programs that support D-bus implement the standard interface for org.freedesktop.DBus.Introspectable, providing introspection support for the objects they export. This interface has only one method, Introspect, which returns the XML format description of the object.
Go.dbus gives an example of Server.go:
Package Mainimport ("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 stringfunc (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 {}}
To run Server.go, use the following command to invoke Org.freedesktop.DBus.Introspectable.Introspect on the/com/github/guelfey/demo object:
$ dbus-send--session--print-reply--dest= "Com.github.guelfey.Demo"/com/github/guelfey/demo Org.freedesktop.DBus.Introspectable.Introspect
You can get the introspection data for this object.
Of course, here the XML string is hard-coded in the source file. Handwritten XML is a very boring thing. One of the goals of this open-source summer camp is to automatically generate introspection data. The specific idea is to scan the source files statically, and to support the export of function parameter names and struct field names.
Go.dbus to D-bus's core protocol has been fully implemented, Go-dbus on this basis to provide a more user-friendly interface, the underlying is still dependent on Go.dbus. These days, by studying the source code of the Go.dbus, we have a deeper understanding of the d-bus and how to extend it. Now I finally feel that the project is a bit of a clear feeling, and then it should be coded to achieve the specific function.