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