3ee26daaf6823211bfb84fc3a9e8a4357dc7516d
[platform/upstream/dbus.git] / doc / dbus-tutorial.xml
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
4 [
5 ]>
6
7 <article id="index">
8   <articleinfo>
9     <title>D-BUS Tutorial</title>
10     <releaseinfo>Version 0.1</releaseinfo>
11     <date>02 October 2003</date>
12     <authorgroup>
13       <author>
14         <firstname>Havoc</firstname>
15         <surname>Pennington</surname>
16         <affiliation>
17           <orgname>Red Hat, Inc.</orgname>
18           <address>
19             <email>hp@pobox.com</email>
20           </address>
21         </affiliation>
22       </author>
23     </authorgroup>
24   </articleinfo>
25
26   <sect1 id="whatis">
27     <title>What is D-BUS?</title>
28     <para>
29       D-BUS is a system for <firstterm>interprocess communication</firstterm>
30       (IPC). Architecturally, it has several layers:
31
32       <itemizedlist>
33         <listitem>
34           <para>
35             A library, libdbus, that allows two applications to connect
36             to each other and exchange messages.
37           </para>
38         </listitem>
39         <listitem>
40           <para>
41             A message bus daemon executable, built on libdbus, that multiple 
42             applications can connect to. The daemon can route messages 
43             from one application to zero or more other applications.
44           </para>
45         </listitem>
46         <listitem>
47           <para>
48             Wrapper libraries based on particular application frameworks. 
49             For example, libdbus-glib and libdbus-qt. There are also 
50             bindings to languages such as Python. These wrapper libraries
51             are the API most people should use, as they simplify the 
52             details of D-BUS programming.
53           </para>
54         </listitem>
55       </itemizedlist>
56     </para>
57
58     <para>
59       libdbus only supports one-to-one connections, just like a raw network
60       socket. However, rather than sending byte streams over the connection, you
61       send <firstterm>messages</firstterm>. Messages have a header identifying
62       the kind of message, and a body containing a data payload. libdbus also
63       abstracts the exact transport used (sockets vs. whatever else), and
64       handles details such as authentication.
65     </para>
66
67     <para>
68       The message bus daemon forms the hub of a wheel. Each spoke of the wheel
69       is a one-to-one connection to an application using libdbus.  An
70       application sends a message to the bus daemon over its spoke, and the bus
71       daemon forwards the message to other connected applications as
72       appropriate. Think of the daemon as a router.
73     </para>
74
75     <para>
76       The bus daemon has multiple instances on a typical computer.  The
77       first instance is a machine-global singleton, that is, a system daemon
78       similar to sendmail or Apache. This instance has heavy security
79       restrictions on what messages it will accept, and is used for systemwide
80       communication. The other instances are created one per user login session.
81       These instances allow applications in the user's session to communicate 
82       with one another.
83     </para>
84
85     <para>
86       The systemwide and per-user daemons are separate.  Normal within-session
87       IPC does not involve the systemwide message bus process and vice versa.
88     </para>
89
90     <sect2 id="uses">
91       <title>D-BUS applications</title>
92       <para>
93         There are many, many technologies in the world that have "Inter-process
94         communication" or "networking" in their stated purpose: <ulink
95         url="http://www.mbus.org/">MBUS</ulink>, <ulink
96         url="http://www.omg.org">CORBA</ulink>, <ulink
97         url="http://www.xmlrpc.com">XML-RPC</ulink>, <ulink
98         url="http://www.w3.org/TR/SOAP/">SOAP</ulink>, and probably hundreds
99         more.  Each of these is tailored for particular kinds of application.
100         D-BUS is designed for two specific cases:
101         <itemizedlist>
102           <listitem>
103             <para>
104               Communication between desktop applications in the same desktop
105               session; to allow integration of the desktop session as a whole,
106               and address issues of process lifecycle (when do desktop components 
107               start and stop running).
108             </para>
109           </listitem>
110           <listitem>
111             <para>
112               Communication between the desktop session and the operating system, 
113               where the operating system would typically include the kernel 
114               and any system daemons or processes.
115             </para>
116           </listitem>
117         </itemizedlist>
118       </para>
119       <para>
120         For the within-desktop-session use case, the GNOME and KDE desktops 
121         have significant previous experience with different IPC solutions
122         such as CORBA and DCOP. D-BUS is built on that experience and 
123         carefully tailored to meet the needs of these desktop projects 
124         in particular.
125       </para>
126       <para>
127         The problem solved by the systemwide or communication-with-the-OS case 
128         is explained well by the following text from the Linux Hotplug project:
129         <blockquote>
130           <para>
131            A gap in current Linux support is that policies with any sort of
132            dynamic "interact with user" component aren't currently
133            supported. For example, that's often needed the first time a network
134            adapter or printer is connected, and to determine appropriate places
135            to mount disk drives. It would seem that such actions could be
136            supported for any case where a responsible human can be identified:
137            single user workstations, or any system which is remotely
138            administered.
139           </para>
140
141           <para>
142             This is a classic "remote sysadmin" problem, where in this case
143             hotplugging needs to deliver an event from one security domain
144             (operating system kernel, in this case) to another (desktop for
145             logged-in user, or remote sysadmin). Any effective response must go
146             the other way: the remote domain taking some action that lets the
147             kernel expose the desired device capabilities. (The action can often
148             be taken asynchronously, for example letting new hardware be idle
149             until a meeting finishes.) At this writing, Linux doesn't have
150             widely adopted solutions to such problems. However, the new D-Bus
151             work may begin to solve that problem.
152           </para>
153         </blockquote>
154       </para>
155       <para>
156         D-BUS may happen to be useful for purposes other than the one it was
157         designed for. Its general properties that distinguish it from 
158         other forms of IPC are:
159         <itemizedlist>
160           <listitem>
161             <para>
162               Binary protocol designed to be used asynchronously 
163               (similar in spirit to the X Window System protocol).
164             </para>
165           </listitem>
166           <listitem>
167             <para>
168               Stateful, reliable connections held open over time.
169             </para>
170           </listitem>
171           <listitem>
172             <para>
173               The message bus is a daemon, not a "swarm" or 
174               distributed architecture.
175             </para>
176           </listitem>
177           <listitem>
178             <para>
179               Many implementation and deployment issues are specified rather
180               than left ambiguous.
181             </para>
182           </listitem>
183           <listitem>
184             <para>
185               Semantics are similar to the existing DCOP system, allowing 
186               KDE to adopt it more easily.
187             </para>
188           </listitem>
189           <listitem>
190             <para>
191               Security features to support the systemwide mode of the 
192               message bus.
193             </para>
194           </listitem>
195         </itemizedlist>
196       </para>
197     </sect2>
198   </sect1>
199   <sect1 id="concepts">
200     <title>Concepts</title>
201     <para>
202       Some basic concepts apply no matter what application framework you're
203       using to write a D-BUS application. The exact code you write will be
204       different for GLib vs. Qt vs. Python applications, however.
205     </para>
206
207     <sect2 id="objects">
208       <title>Objects and Object Paths</title>
209       <para>
210         Each application using D-BUS contains <firstterm>objects</firstterm>,
211         which generally map to GObject, QObject, C++ objects, or Python objects
212         (but need not).  An object is an <emphasis>instance</emphasis> rather
213         than a type.  When messages are received over a D-BUS connection, they
214         are sent to a specific object, not to the application as a whole.
215       </para>
216       <para>
217         To allow messages to specify their destination object, there has to be a
218         way to refer to an object. In your favorite programming language, this
219         is normally called a <firstterm>pointer</firstterm> or
220         <firstterm>reference</firstterm>. However, these references are
221         implemented as memory addresses relative to the address space of your
222         application, and thus can't be passed from one application to another.
223       </para>
224       <para>
225         To solve this, D-BUS introduces a name for each object. The name 
226         looks like a filesystem path, for example an object could be 
227         named <literal>/org/kde/kspread/sheets/3/cells/4/5</literal>. 
228         Human-readable paths are nice, but you are free to create an 
229         object named <literal>/com/mycompany/c5yo817y0c1y1c5b</literal> 
230         if it makes sense for your application.
231       </para>
232       <para>
233         Namespacing object paths is smart, by starting them with the components
234         of a domain name you own (e.g. <literal>/org/kde</literal>). This 
235         keeps different code modules in the same process from stepping 
236         on one another's toes.
237       </para>
238     </sect2>    
239
240     <sect2 id="interfaces">
241       <title>Interfaces</title>
242       <para>
243         Each object supports one or more <firstterm>interfaces</firstterm>.
244         Think of an interface as a named group of methods and signals, 
245         just as it is in GLib or Qt. Interfaces define the 
246         <emphasis>type</emphasis> of an object instance.
247       </para>
248     </sect2>
249       
250     <sect2 id="messages">
251       <title>Message Types</title>
252       <para>
253         Messages are not all the same; in particular, D-BUS has 
254         4 built-in message types:
255         <itemizedlist>
256           <listitem>
257             <para>
258               Method call messages ask to invoke a method 
259               on an object.
260             </para>
261           </listitem>
262           <listitem>
263             <para>
264               Method return messages return the results 
265               of invoking a method.
266             </para>
267           </listitem>
268           <listitem>
269             <para>
270               Error messages return an exception caused by 
271               invoking a method.
272             </para>
273           </listitem>
274           <listitem>
275             <para>
276               Signal messages are notifications that a given signal 
277               has been emitted (that an event has occurred). 
278               You could also think of these as "event" messages.
279             </para>
280           </listitem>
281         </itemizedlist>
282       </para>
283       <para>
284         A method call maps very simply to messages, then: you send a method call
285         message, and receive either a method return message or an error message
286         in reply.
287       </para>
288     </sect2>
289
290     <sect2 id="services">
291       <title>Services</title>
292       
293       <para>
294         Object paths, interfaces, and messages exist on the level of 
295         libdbus and the D-BUS protocol; they are used even in the 
296         1-to-1 case with no message bus involved.
297       </para>
298
299       <para>
300         Services, on the other hand, are a property of the message bus daemon.
301         A <firstterm>service</firstterm> is simply a name mapped to 
302         some application connected to the message bus daemon.
303         These names are used to specify the origin and destination
304         of messages passing through the message bus. When a name is mapped 
305         to a particular application, the application is said to 
306         <firstterm>own</firstterm> that service.
307       </para>
308
309       <para>
310         On connecting to the bus daemon, each application immediately owns a
311         special name called the <firstterm>base service</firstterm>.  A base
312         service begins with a ':' (colon) character; no other services are
313         allowed to begin with that character. Base services are special because
314         each one is unique. They are created dynamically, and are never re-used
315         during the lifetime of the same bus daemon. You know that a given
316         base service name will have the same owner at all times.
317         An example of a base service name might be <literal>:34-907</literal>.
318       </para>
319
320       <para>
321         Applications may ask to own additional <firstterm>well-known
322         services</firstterm>. For example, you could write a specification to
323         define a service called <literal>com.mycompany.TextEditor</literal>.
324         Your definition could specify that to own this service, an application
325         should have an object at the path
326         <literal>/com/mycompany/TextFileManager</literal> supporting the
327         interface <literal>org.freedesktop.FileHandler</literal>.        
328       </para>
329       
330       <para>
331         Applications could then send messages to this service, 
332         object, and interface to execute method calls.
333       </para>
334
335       <para>
336         You could think of the base service names as IP addresses, and the
337         well-known services as domain names. So
338         <literal>com.mycompany.TextEditor</literal> might map to something like
339         <literal>:34-907</literal> just as <literal>mycompany.com</literal> maps
340         to something like <literal>192.168.0.5</literal>.
341       </para>
342       
343       <para>
344         Services have a second important use, other than routing messages.  They
345         are used to track lifecycle. When an application exits (or crashes), its
346         connection to the message bus will be closed by the operating system
347         kernel. The message bus then sends out notification messages telling
348         remaining applications that the application's services have lost their
349         owner. By tracking these notifications, your application can reliably
350         monitor the lifetime of other applications.
351       </para>
352
353     </sect2>
354
355     <sect2 id="addresses">
356       <title>Addresses</title>
357
358       <para>
359         Applications using D-BUS are either servers or clients.  A server
360         listens for incoming connections; a client connects to a server. Once
361         the connection is established, it is a symmetric flow of messages; the
362         client-server distinction only matters when setting up the 
363         connection.
364       </para>
365
366       <para>
367         A D-BUS <firstterm>address</firstterm> specifies where a server will
368         listen, and where a client will connect.  For example, the address
369         <literal>unix:path=/tmp/abcdef</literal> specifies that the server will
370         listen on a UNIX domain socket at the path
371         <literal>/tmp/abcdef</literal> and the client will connect to that
372         socket. An address can also specify TCP/IP sockets, or any other
373         transport defined in future iterations of the D-BUS specification.
374       </para>
375
376       <para>
377         When using D-BUS with a message bus, the bus daemon is a server 
378         and all other applications are clients of the bus daemon.
379         libdbus automatically discovers the address of the per-session bus 
380         daemon by reading an environment variable. It discovers the 
381         systemwide bus daemon by checking a well-known UNIX domain socket path
382         (though you can override this address with an environment variable).
383       </para>
384
385       <para>
386         If you're using D-BUS without a bus daemon, it's up to you to 
387         define which application will be the server and which will be 
388         the client, and specify a mechanism for them to agree on 
389         the server's address.
390       </para>
391
392     </sect2>
393
394     <sect2 id="bigpicture">
395       <title>Big Conceptual Picture</title>
396
397       <para>
398         Pulling all these concepts together, to specify a particular 
399         method call on a particular object instance, a number of 
400         nested components have to be named:
401         <programlisting>
402           Address -> [Service] -> Path -> Interface -> Method
403         </programlisting>
404         The service is in brackets to indicate that it's optional -- you only
405         provide a service name to route the method call to the right application
406         when using the bus daemon. If you have a direct connection to another
407         application, services aren't used.
408       </para>
409
410       <para>
411         The interface is also optional, primarily for historical 
412         reasons; DCOP does not require specifying the interface, 
413         instead simply forbidding duplicate method names 
414         on the same object instance. D-BUS will thus let you 
415         omit the interface, but if your method name is ambiguous 
416         it is undefined which method will be invoked.
417       </para>
418       
419     </sect2>
420
421   </sect1>
422
423   <sect1 id="glib-client">
424     <title>GLib API: Using Remote Objects</title>
425     <para>
426       
427     </para>
428   </sect1>
429
430   <sect1 id="glib-server">
431     <title>GLib API: Implementing Objects</title>
432     <para>
433       
434     </para>
435   </sect1>
436
437   <sect1 id="qt-client">
438     <title>Qt API: Using Remote Objects</title>
439     <para>
440       
441     </para>
442   </sect1>
443
444   <sect1 id="qt-server">
445     <title>Qt API: Implementing Objects</title>
446     <para>
447       
448     </para>
449   </sect1>
450
451
452   <sect1 id="python-client">
453     <title>Python API: Using Remote Objects</title>
454     <para>
455       
456     </para>
457   </sect1>
458
459   <sect1 id="python-server">
460     <title>Python API: Implementing Objects</title>
461     <para>
462       
463     </para>
464   </sect1>
465
466 </article>