Qt D-Bus Overview

D-Bus is an Inter-Process Communication (IPC) and Remote Procedure Calling (RPC) mechanism originally developed for Linux to replace existing and competing IPC solutions with one unified protocol. It was also designed to allow communication between system-level processes (such as printer and hardware driver services) and normal user processes.

它使用很快的二進製消息傳遞協議,適於同機通信 (由於它的低延遲和低開銷)。目前,其規範的定義是通過 freedesktop.org project and is available to all parties.

Communication, in general, happens through a central server application called the "bus" (hence the name), but direct application-to-application communication is also possible. When communicating on a bus, applications can query which other applications and services are available, as well as activate one on demand.

總綫

D-Bus buses are used when many-to-many communication is desired. In order to achieve that, a central server is launched before any application 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.

此外,D-Bus 定義瞭 2 個知名總綫 (稱為係統總綫和會話總綫)。這些總綫的特殊意義是它們具有定義良好的語義:一些服務被定義成能在其中一個或兩個總綫中被找到。

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 probably be found on the session bus.

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

概念

消息

在較低級彆,應用程序通過 D-Bus 相互發送消息進行通信。使用消息能中繼遠程過程調用、及與它們關聯的迴復和錯誤。當通過總綫使用時,消息具有目的地,這意味著它們僅能路由到感興趣各方,避免由於蜂擁 (或廣播) 的擁塞。

一種特殊消息,稱為信號消息 (概念基於 Qt 的 信號和槽 機製),不管怎樣,沒有預定義目的地。由於其目的是在一對多上下文中使用,所以信號消息被設計成在選擇加入機製下工作。

Qt D-Bus 模塊將低級消息概念完全封裝成 Qt 開發者所熟悉的更簡單、麵嚮對象的方式。在大多數情況下,開發者無需擔心消息的發送或接收。

服務名稱

當通過總綫通信時,應用程序獲得所謂的服務名稱:這就是該應用程序選擇被同一總綫中,其它應用程序知道的方式。服務名稱由 D-Bus 總綫代理程序守護,用於將消息從一個應用程序路由到另一個應用程序。與服務名稱類似的概念是 IP 地址和主機名:根據計算機提供給網絡的服務,一颱計算機通常具有一個 IP 地址,且可能具有一個或多個與其關聯的主機名。

另一方麵,若不使用總綫,也不會使用服務名稱。若再將其與計算機網絡進行比較,這相當於點對點網絡:由於對等方是已知的,因此不需要使用主機名來查找它 (或其 IP 地址)。

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 your service name according to the domain name of the organization that defined that service.

例如,D-Bus 服務的定義通過 freedesktop.org 且可以在總綫中找到以下服務名稱:

org.freedesktop.DBus
					

對象路徑

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

若繼續與 Web 服務進行類比,對象路徑等同於 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 使用非常類似的機製在其 插件係統 :C++ 中的基類關聯唯一標識符,通過方式 Q_DECLARE_INTERFACE () 宏。

事實上,D-Bus 接口名稱的命名方式與 Qt 插件係統的建議類似:標識符的構造通常來自定義該接口的實體的域名。

備忘單

為便於記住命名格式及其用途,可以使用下錶:

D-Bus 概念 類比 名稱格式
服務名稱 網絡主機名 點分隔 (看起來像主機名)
對象路徑 URL 路徑分量 斜杠分隔 (看起來像路徑)
接口 插件標識符 點分隔

調試

在使用 D-Bus 開發應用程序時,有時,能夠查看各應用程序跨總綫發送和接收的消息的相關信息是有用的。

可以在每個應用程序的基礎上啓用此特徵,通過設置 QDBUS_DEBUG 環境變量,在運行每個應用程序之前。例如,我們隻可以啓用調試,對於小車的 D-Bus Remote Controlled Car example by running the controller and the car in the following way:

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

有關消息的信息會被寫入到發起應用程序的控製颱。