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