| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the documentation of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | /*! | 
|---|
| 43 | \page intro-to-dbus.html | 
|---|
| 44 | \title Introduction to D-Bus | 
|---|
| 45 |  | 
|---|
| 46 | \keyword QtDBus | 
|---|
| 47 | \ingroup architecture | 
|---|
| 48 | \brief An introduction to Inter-Process Communication and Remote Procedure Calling with D-Bus. | 
|---|
| 49 |  | 
|---|
| 50 | \section1 Introduction | 
|---|
| 51 |  | 
|---|
| 52 | D-Bus is an Inter-Process Communication (IPC) and Remote Procedure | 
|---|
| 53 | Calling (RPC) mechanism originally developed for Linux to replace | 
|---|
| 54 | existing and competing IPC solutions with one unified protocol. It | 
|---|
| 55 | has also been designed to allow communication between system-level | 
|---|
| 56 | processes (such as printer and hardware driver services) and | 
|---|
| 57 | normal user processes. | 
|---|
| 58 |  | 
|---|
| 59 | It uses a fast, binary message-passing protocol, which is suitable | 
|---|
| 60 | for same-machine communication due to its low latency and low | 
|---|
| 61 | overhead. Its specification is currently defined by the | 
|---|
| 62 | \tt{freedesktop.org} project, and is available to all parties. | 
|---|
| 63 |  | 
|---|
| 64 | Communication in general happens through a central server | 
|---|
| 65 | application, called the "bus" (hence the name), but direct | 
|---|
| 66 | application-to-application communication is also possible. When | 
|---|
| 67 | communicating on a bus, applications can query which other | 
|---|
| 68 | applications and services are available, as well as activate one | 
|---|
| 69 | on demand. | 
|---|
| 70 |  | 
|---|
| 71 | \section1 The Buses | 
|---|
| 72 |  | 
|---|
| 73 | D-Bus buses are used to when many-to-many communication is | 
|---|
| 74 | desired. In order to achieve that, a central server is launched | 
|---|
| 75 | before any applications can connect to the bus: this server is | 
|---|
| 76 | responsible for keeping track of the applications that are | 
|---|
| 77 | connected and for properly routing messages from their source to | 
|---|
| 78 | their destination. | 
|---|
| 79 |  | 
|---|
| 80 | In addition, D-Bus defines two well-known buses, called the | 
|---|
| 81 | system bus and the session bus. These buses are special in the | 
|---|
| 82 | sense that they have well-defined semantics: some services are | 
|---|
| 83 | defined to be found in one or both of these buses. | 
|---|
| 84 |  | 
|---|
| 85 | For example, an application wishing to query the list of hardware | 
|---|
| 86 | devices attached to the computer will probably communicate to a | 
|---|
| 87 | service available on the system bus, while the service providing | 
|---|
| 88 | opening of the user's web browser will be probably found on the | 
|---|
| 89 | session bus. | 
|---|
| 90 |  | 
|---|
| 91 | On the system bus, one can also expect to find restrictions on | 
|---|
| 92 | what services each application is allowed to offer. Therefore, one | 
|---|
| 93 | can be reasonably certain that, if a certain service is present, | 
|---|
| 94 | it is being offered by a trusted application. | 
|---|
| 95 |  | 
|---|
| 96 | \section1 Concepts | 
|---|
| 97 |  | 
|---|
| 98 | \section2 Messages | 
|---|
| 99 |  | 
|---|
| 100 | On the low level, applications communicate over D-Bus by sending | 
|---|
| 101 | messages to one another. Messages are used to relay the remote | 
|---|
| 102 | procedure calls as well as the replies and errors associated | 
|---|
| 103 | with them. When used over a bus, messages have a destination, | 
|---|
| 104 | which means they are routed only to the interested parties, | 
|---|
| 105 | avoiding congestion due to "swarming" or broadcasting. | 
|---|
| 106 |  | 
|---|
| 107 | A special kind of message called a "signal message" | 
|---|
| 108 | (a concept based on Qt's \l {Signals and Slots} mechanism), | 
|---|
| 109 | however, does not have a pre-defined destination. Since its | 
|---|
| 110 | purpose is to be used in a one-to-many context, signal messages | 
|---|
| 111 | are designed to work over an "opt-in" mechanism. | 
|---|
| 112 |  | 
|---|
| 113 | The QtDBus module fully encapsulates the low-level concept of | 
|---|
| 114 | messages into a simpler, object-oriented approach familiar to Qt | 
|---|
| 115 | developers. In most cases, the developer need not worry about | 
|---|
| 116 | sending or receiving messages. | 
|---|
| 117 |  | 
|---|
| 118 | \section2 Service Names | 
|---|
| 119 |  | 
|---|
| 120 | When communicating over a bus, applications obtain what is | 
|---|
| 121 | called a "service name": it is how that application chooses to be | 
|---|
| 122 | known by other applications on the same bus. The service names | 
|---|
| 123 | are brokered by the D-Bus bus daemon and are used to | 
|---|
| 124 | route messages from one application to another. An analogous | 
|---|
| 125 | concept to service names are IP addresses and hostnames: a | 
|---|
| 126 | computer normally has one IP address and may have one or more | 
|---|
| 127 | hostnames associated with it, according to the services that it | 
|---|
| 128 | provides to the network. | 
|---|
| 129 |  | 
|---|
| 130 | On the other hand, if a bus is not used, service names are also | 
|---|
| 131 | not used. If we compare this to a computer network again, this | 
|---|
| 132 | would equate to a point-to-point network: since the peer is | 
|---|
| 133 | known, there is no need to use hostnames to find it or its IP | 
|---|
| 134 | address. | 
|---|
| 135 |  | 
|---|
| 136 | The format of a D-Bus service name is in fact very similar to a | 
|---|
| 137 | host name: it is a dot-separated sequence of letters and | 
|---|
| 138 | digits. The common practice is even to name one's service name | 
|---|
| 139 | according to the domain name of the organization that defined | 
|---|
| 140 | that service. | 
|---|
| 141 |  | 
|---|
| 142 | For example, the D-Bus service is defined by | 
|---|
| 143 | \tt{freedesktop.org} and can be found on the bus under the | 
|---|
| 144 | service name: | 
|---|
| 145 |  | 
|---|
| 146 | \snippet doc/src/snippets/code/doc_src_introtodbus.qdoc 0 | 
|---|
| 147 |  | 
|---|
| 148 | \section2 Object Paths | 
|---|
| 149 |  | 
|---|
| 150 | Like network hosts, applications provide specific services to | 
|---|
| 151 | other applications by exporting objects. Those objects are | 
|---|
| 152 | hierarchically organised, much like the parent-child | 
|---|
| 153 | relationship that classes derived from QObject possess. One | 
|---|
| 154 | difference, however, is that there is the concept of "root | 
|---|
| 155 | object", that all objects have as ultimate parent. | 
|---|
| 156 |  | 
|---|
| 157 | If we continue our analogy with Web services, object paths | 
|---|
| 158 | equate to the path part of a URL: | 
|---|
| 159 |  | 
|---|
| 160 | \img qurl-ftppath.png | 
|---|
| 161 |  | 
|---|
| 162 | Like them, object paths in D-Bus are formed resembling path | 
|---|
| 163 | names on the filesystem: they are slash-separated labels, each | 
|---|
| 164 | consisting of letters, digits and the underscore character | 
|---|
| 165 | ("_"). They must always start with a slash and must not end with | 
|---|
| 166 | one. | 
|---|
| 167 |  | 
|---|
| 168 | \section2 Interfaces | 
|---|
| 169 |  | 
|---|
| 170 | Interfaces are similar to C++ abstract classes and Java's | 
|---|
| 171 | \c interface keyword and declare the "contract" that is | 
|---|
| 172 | established between caller and callee. That is, they establish | 
|---|
| 173 | the names of the methods, signals and properties that are | 
|---|
| 174 | available as well as the behavior that is expected from either | 
|---|
| 175 | side when communication is established. | 
|---|
| 176 |  | 
|---|
| 177 | Qt uses a very similar mechanism in its \l {How to Create Qt | 
|---|
| 178 | Plugins}{Plugin system}: Base classes in C++ are associated | 
|---|
| 179 | with a unique identifier by way of the Q_DECLARE_INTERFACE() | 
|---|
| 180 | macro. | 
|---|
| 181 |  | 
|---|
| 182 | D-Bus interface names are, in fact, named in a manner similar to | 
|---|
| 183 | what is suggested by the Qt Plugin System: an identifier usually | 
|---|
| 184 | constructed from the domain name of the entity that defined that | 
|---|
| 185 | interface. | 
|---|
| 186 |  | 
|---|
| 187 | \section2 Cheat Sheet | 
|---|
| 188 |  | 
|---|
| 189 | To facilitate remembering of the naming formats and their | 
|---|
| 190 | purposes, the following table can be used: | 
|---|
| 191 |  | 
|---|
| 192 | \table 90% | 
|---|
| 193 | \header \o D-Bus Concept  \o Analogy            \o Name format | 
|---|
| 194 | \row    \o Service name   \o Network hostnames  \o Dot-separated | 
|---|
| 195 | ("looks like a hostname") | 
|---|
| 196 | \row    \o Object path    \o URL path component \o Slash-separated | 
|---|
| 197 | ("looks like a path") | 
|---|
| 198 | \row    \o Interface      \o Plugin identifier  \o Dot-separated | 
|---|
| 199 | \endtable | 
|---|
| 200 |  | 
|---|
| 201 | \section2 Further Reading | 
|---|
| 202 |  | 
|---|
| 203 | The following documents contain information about Qt's D-Bus integration | 
|---|
| 204 | features, and provide details about the mechanisms used to send and receive | 
|---|
| 205 | type information over the bus: | 
|---|
| 206 |  | 
|---|
| 207 | \list | 
|---|
| 208 | \o \l{Using QtDBus Adaptors} | 
|---|
| 209 | \o \l{The QtDBus Type System} | 
|---|
| 210 | \o \l{QtDBus XML compiler (qdbusxml2cpp)} | 
|---|
| 211 | \endlist | 
|---|
| 212 | */ | 
|---|