D-bus framework based on Go (ii)

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.