Archive:Development/Tutorials/D-Bus/Accessing Interfaces (zh CN)
Tutorial Series | D-Bus |
Previous | [[../Introduction|介绍]] |
What's Next | [[../Creating Interfaces|创建D-Bus接口]], [[../Intermediate_D-Bus|中级D-Bus编程]] |
Further Reading | n/a |
摘要
D-Bus allows applications to expose their internal API to the outside world. These APIs can then be accessed at run-time via the D-Bus protocol using command line applications or D-Bus libraries and bindings themselves. This tutorial looks at the latter method with examples that you can use in your applications.
使用QDBusMessage
QDBusMessage模拟了可以在总线上收发的D-Bus消息。每个消息都是下列四种的一种,具体类型视需求而定:
- 方法调用
- 信号
- 回复
- 错误
QDBusMessage中的一个枚举类型包含所有的属性。消息的类型可以通过QDBusMessage::type()方法获得。
调用D-Bus方法
A QDBusMessage can be used directly to call methods in D-Bus services using the QDBusMessage::createMethodCall( const QString & service, const QString & path, const QString & interface, const QString & method ) static method. It returns a QDBusMessage object that you can then use to make the call.
The interface parameter is optional and only necessary if the method to be called is not unique in the object associated with the path. This can happen if the object implements multiple interfaces which have methods that are named the same. In such (rare) cases, if you do not explicitly define the interface to use there is not guarantee as to which method will actually get called. However, usually you can simply pass an empty string (e.g. "") as the argument for interface.
By way of example, to access the (fictional) ping method on the /network object in the org.foo.bar service, one might do this:
QDBusMessage m = QDBusMessage::createMethodCall("org.foo.bar",
"/network",
"",
"ping");
bool queued = QDBusConnection::sessionBus().send(m);
In line 5 of the above example we queue the message for sending on the current session bus. We get a bool returned letting us know if the queueing was successful or not.
这里有两个问题:
- 如何设置一个方法的参数?
- 如何获得D-Bus方法调用的返回值?(如果有的话)
参数设置
Sending arguments along with the method call is quite straight forward. First we need to create a QList of QVariant objects and then add those to our D-Bus message. So if the ping method in the above took a hostname as a parameter, we might alter the code in this way (note lines 5 through 7):
QDBusMessage m = QDBusMessage::createMethodCall("org.foo.bar",
"/network",
"",
"ping");
QList<QVariant> args;
args.append("kde.org");
m.setArguments(args);
bool queued = QDBusConnection::sessionBus().send(m);
Alternatively, QDBusMessage provides a convenience method to appending parameters to the message, by way of its "operator<<" function. 上面的代码可以写成:
QDBusMessage m = QDBusMessage::createMethodCall("org.foo.bar",
"/network",
"",
"ping");
m << "kde.org";
bool queued = QDBusConnection::sessionBus().send(m);
获取回应
If we wish to actually receive information back from the D-Bus method, we use the QDBusConnection::call method instead. It will block until there is a reply or the call times out. If our ping method returned information on the host we provided in the arguments above, we might alter our code to look like this:
QDBusMessage m = QDBusMessage::createMethodCall("org.foo.bar",
"/network",
"",
"ping");
m << "kde.org";
QDBusMessage response = QDBusConnection::sessionBus().call(m);
The response will be either of type QDBusMessage::ReplyMessage or QDBusMessage::ErrorMessage depending on whether it was successful or not. We can look through the values returned by retrieving the arguments with the QDBusMessage::arguments() method which returns a QList<QVariant>.
这是最佳方法么?
Using QDBusMessage directly in this way to invoke remote D-Bus methods is not the easiest, best or even recommend way of doing things. We will now look at the more convenient QDBusInterface class and then look at accessing remote D-Bus interfaces as if they were local methods using proxy classes auto-generated from XML.
使用QDBusInterface
QDBusInterface provides a simple and direct method to make D-Bus calls and connect to D-Bus signals.
A QDBusInterface object represents a given D-Bus interface. The constructor accepts as parameters (in order) a service name, an object path, an optional interface and optionally which bus (e.g. system or session) to use. If no bus is explicitly defined, it defaults to the session bus. If no interface is given, the returned object will be used to call all interfaces on the bus.
However, note that explicitly passing an interface name to the QDBusInterface constructor is recommended. Due to the internals of QtDBus, if you pass an empty interface, you will always cause a round-trip to the remote application to verify which methods are available. On the other hand, if you pass a non-empty interface name, QtDBus may cache the result for further uses.
As QDBusInterface is a QObject, you can also pass it a parent object. This helps simplify the bookkeeping associated with creating new QDBusInterface objects by letting Qt clean up for you when the parent object is deleted.
Here is an example of QDBusInterface usage which we will then step through line by line:
QString hostname("kde.org");
QDBusConnection bus = QDBusConnection::sessionBus();
QDBusInterface *interface = new QDBusInterface("org.foo.bar",
"/network",
"org.foo.bar.network",
bus,
this);
interface->call("ping");
interface->call("ping", hostname);
QList<QVariant> args;
args.append("kde.org");
interface->callWithArgumentList("ping", args);
QDBusReply<int> reply = interface->call("ping",
hostname);
if (reply.isValid())
{
KMessageBox::information(winId(),
i18n("Ping to %1 took %2s")
.arg(hostname)
.arg(reply.value()),
i18n("Pinging %1")
.arg(hostname));
}
args.clear();
interface->callWithCallback("listInterfaces", args,
this,
SLOT(interfaceList(QDBusMessage));
connect(interface, SIGNAL(interfaceUp(QString)),
this, SLOT(interfaceUp(QString)));
同步调用
The first thing we did was create a QDBusInterface on line 3 that represents the same object we were accessing in the QDBusMessage examples above.
We then called several D-Bus methods on that object using a few different techniques. On line 9 we make a simple call to a method called ping without any arguments. On line 10, we call the same method but with a parameter. Note that we didn't have to create a QList<QVariant> for the arguments. We can pass up to 8 arguments to a D-Bus method this way.
If you need to pass more than 8 arguments or for some other reason a QList<QVariant> is simply a better approach for the circumstances, then you may use the callWithArgumentList method instead as seen on lines 12-14 above.
处理回应
On line 16 we call the ping method yet again, but this time save the reply in a QDBusReply object. We check to make sure the reply was valid (e.g. no errors were returned and we did indeed get an int back) and then use the returned data to populate a message in an informational popup.
异步调用和信号
Up to this point in the example all of the calls made were synchronous and the application would block until a reply was received. The last two uses of QDBusInterface in the example show asynchronous usage of D-Bus, and in both cases we rely on Qt's signal and slot mechanism.
On line 29 we use callWithCallback and provide a regular QObject slot to be called when the D-Bus reply returns. This way the application will not block as callWithCallback returns immediately after queueing the message to be sent on the bus. Later, the interfaceList slot would get called. Note that this method requires a QList<QVariant>; there is no shortcut for us this time.
Finally, on line 33 we connect to a D-Bus signal. Using QDBusInterface to do this looks exactly like connecting to a regular, local signal in our own application. We even use the standard QObject::connect method! This is accomplished by QDBusInterface using Qt's meta object system to dynamically add the signals the D-Bus interface advertises. Very slick!
这个是最佳方法么?
This ease of use over QDBusMessage does come with some prices, however. First, since QDBusInterface is a QObject it carries the overhead that implies. It also will perform at least one round-trip to the requested D-Bus object on creation to set up the interface object with things such as the available signals. Often this additional overhead is negligible in the larger scheme of things and well made up for by the convenience it provides.
There are still some annoyances we have to deal with, however, such as having to know the name of the interface, setting up the correct QDBusReply object such as we did above by templating it with an int and having to debug method name typos and the like at runtime versus letting the compiler do it for us. So while it's an improvement over QDBusMessage, it's still not perfect.
And that's precisely where qdbusxml2cpp comes to our rescue.
使用从D-Bus XML产生的类
What would be truly great is if we could simply instantiate a local object that represented a given service and start using it right away. Perhaps something like this:
org::foo::bar::network *interface =
new org::foo::bar::network("org.foo.bar", "/network",
QDBusConnection::sessionBus(),
this);
interface->ping("kde.org");
Fortunately for us, this is precisely what Qt allows us to do. The only requirement is an XML file describing the D-Bus service. Such files are installed in the D-Bus prefix in the interfaces directory.
pkg-config dbus-1 --variable=prefix
We can also create our own XML files from the C++ header files and use those directly. This is covered in the next tutorial, [[../Creating_Interfaces|Creating D-Bus Interfaces]].
With the path to the XML in hand, we then add something like this to our CMakeLists.txt:
PKGCONFIG_GETVAR(dbus-1 prefix DBUS_PREFIX)
set(network_xml ${DBUS_PREFIX}/interfaces/org.foo.bar.xml)
QT4_ADD_DBUS_INTERFACE(myapp_SRCS ${network_xml} network_interface )
This will generate two files at build time, network_interface.h and network_interface.cpp, and add them to the compiled application. We can then simply #include "network_interface.h" and use the generated class as seen in the example above.
Examining the generated header file, we can see exactly what methods, signals as well as their signatures and return values are according to the provider of the service. Using the class directly will let the compiler do type checking on method calls leaving fewer run-time breakages to track down.
Due to the generated class being a subclass of QDBusAbstractInterface just as QDBusInterface is, anything we can do with QDBusInterface is also available to us.
Due to this combination of ease of use and compile-time checking, this is generally the preferred mechanism to use when accessing D-Bus interfaces.
作些拦截
It may also be helpful to find out if a given service is available or to check which application is providing it. Another QDBusAbstractInterface subclass, QDBusConnectionInterface, provides methods to query for such information as which services are registered and who owns them.
Once you have a service name, you can then use QDBusInterface to get the org.freedesktop.DBus.Introspectable interface and call Introspect on it. This will return an XML block describing the objects, which can in turn be introspected for what they provide. The XML itself can be processed using QDomDocument, making it a fairly simple process.
The qdbus application that ships with Qt4 provides a nice example of code doing exactly this. It can be found in tools/qdbus/tools/qdbus/qdbus.cpp in the Qt4 source distribution.