Qt D-Bus

介绍

D-Bus 是最初为 Linux 开发的 IPC (进程间通信) 和 RPC (远程过程调用) 机制,采用一统一协议替换现有-竞争 IPC 解决方案。它还被设计成允许系统级进程 (譬如:打印机和硬件驱动程序服务) 和普通用户进程之间通信。

它使用快速的二进制消息传递协议,适合同机通信 (由于它的低潜伏时间和低开销)。目前,其规范定义由 freedesktop.org 工程,且所有各方可用。

通信一般透过称为 Bus (总线,因此得名) 的中心服务器应用程序发生,但直接应用程序到应用程序的通信也是可能的。当在 Bus (总线) 上进行通信时,应用程序可以查询哪些其它应用程序和服务是可用的,还可按需激活一个。

总线

D-Bus buses are used to when many-to-many communication is desired. In order to achieve that, a central server is launched before any applications can connect to the bus: this server is responsible for keeping track of the applications that are connected and for properly routing messages from their source to their destination.

In addition, D-Bus defines two well-known buses, called the system bus and the session bus. These buses are special in the sense that they have well-defined semantics: some services are defined to be found in one or both of these buses.

For example, an application wishing to query the list of hardware devices attached to the computer will probably communicate to a service available on the system bus, while the service providing opening of the user's web browser will be probably found on the session bus.

On the system bus, one can also expect to find restrictions on what services each application is allowed to offer. Therefore, one can be reasonably certain that, if a certain service is present, it is being offered by a trusted application.

概念

消息

On the low level, applications communicate over D-Bus by sending messages to one another. Messages are used to relay the remote procedure calls as well as the replies and errors associated with them. When used over a bus, messages have a destination, which means they are routed only to the interested parties, avoiding congestion due to "swarming" or broadcasting.

A special kind of message called a "signal message" (a concept based on Qt's 信号和槽 mechanism), however, does not have a pre-defined destination. Since its purpose is to be used in a one-to-many context, signal messages are designed to work over an "opt-in" mechanism.

The Qt D-Bus module fully encapsulates the low-level concept of messages into a simpler, object-oriented approach familiar to Qt developers. In most cases, the developer need not worry about sending or receiving messages.

服务名称

When communicating over a bus, applications obtain what is called a "service name": it is how that application chooses to be known by other applications on the same bus. The service names are brokered by the D-Bus bus daemon and are used to route messages from one application to another. An analogous concept to service names are IP addresses and hostnames: a computer normally has one IP address and may have one or more hostnames associated with it, according to the services that it provides to the network.

On the other hand, if a bus is not used, service names are also not used. If we compare this to a computer network again, this would equate to a point-to-point network: since the peer is known, there is no need to use hostnames to find it or its IP address.

The format of a D-Bus service name is in fact very similar to a host name: it is a dot-separated sequence of letters and digits. The common practice is even to name one's service name according to the domain name of the organization that defined that service.

For example, the D-Bus service is defined by freedesktop.org and can be found on the bus under the service name:

org.freedesktop.DBus
							

对象路径

Like network hosts, applications provide specific services to other applications by exporting objects. Those objects are hierarchically organised, much like the parent-child relationship that classes derived from QObject possess. One difference, however, is that there is the concept of "root object", that all objects have as ultimate parent.

If we continue our analogy with Web services, object paths equate to the path part of a URL:

Like them, object paths in D-Bus are formed resembling path names on the filesystem: they are slash-separated labels, each consisting of letters, digits and the underscore character ("_"). They must always start with a slash and must not end with one.

接口

接口类似 C++ 抽象类和 Java 的 interface keyword and declare the "contract" that is established between caller and callee. That is, they establish the names of the methods, signals and properties that are available as well as the behavior that is expected from either side when communication is established.

Qt 使用非常类似的机制在其 插件系统 : Base classes in C++ are associated with a unique identifier by way of the Q_DECLARE_INTERFACE () macro.

D-Bus interface names are, in fact, named in a manner similar to what is suggested by the Qt Plugin System: an identifier usually constructed from the domain name of the entity that defined that interface.

备忘单

To facilitate remembering of the naming formats and their purposes, the following table can be used:

D-Bus 概念 类比 名称格式
服务名称 网络主机名 点分隔 (看起来像主机名)
对象路径 URL 路径分量 斜杠分隔 (看起来像路径)
Interface 插件标识符 点分隔

调试

When developing applications that use D-Bus, it is sometimes useful to be able to see information about the messages that are sent and received across the bus by each application.

This feature can be enabled on a per-application basis by setting the QDBUS_DEBUG environment variable before running each application. For example, we can enable debugging only for the car in the D-Bus 远程控制车范例 by running the controller and the car in the following way:

examples/dbus/remotecontrolledcar/controller/controller &
QDBUS_DEBUG=1 examples/dbus/remotecontrolledcar/car/car &
							

Information about the messages will be written to the console the application was launched from.

许可和归属

Qt D-Bus 模块在商业许可下是可用的来自 Qt 公司 。此外,它在自由软件许可下是可用的。从 Qt 5.4 起,这些自由软件许可是 GNU LGPL (次一般公共许可) 第 3 版 ,或 GNU GPL (一般公共许可) 第 2 版 。见 Qt 许可 进一步了解细节。

Furthermore, Qt D-Bus in Qt 5.12.9 may contain third party modules under following permissive licenses:

libdus-1 headers, version 1.12

Academic Free License v2.1, or GNU General Public License v2.0 or later

延伸阅读

The following documents contain information about Qt's D-Bus integration features, and provide details about the mechanisms used to send and receive type information over the bus: