doc/src/frameworks-technologies/dbus-intro.qdoc
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*!
       
    43     \page intro-to-dbus.html
       
    44     \title Introduction to D-Bus
       
    45     \brief An introduction to Inter-Process Communication and Remote Procedure Calling with D-Bus.
       
    46 
       
    47     \keyword QtDBus
       
    48     \ingroup frameworks-technologies
       
    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     \section1 Debugging
       
   202 
       
   203     When developing applications that use D-Bus, it is sometimes useful to be able
       
   204     to see information about the messages that are sent and received across the
       
   205     bus by each application.
       
   206 
       
   207     This feature can be enabled on a per-application basis by setting the
       
   208     \c QDBUS_DEBUG environment variable before running each application.
       
   209     For example, we can enable debugging only for the car in the
       
   210     \l{D-Bus Remote Controlled Car Example} by running the controller and the
       
   211     car in the following way:
       
   212 
       
   213     \snippet doc/src/snippets/code/doc_src_introtodbus.qdoc QDBUS_DEBUG
       
   214 
       
   215     Information about the messages will be written to the console the application
       
   216     was launched from.
       
   217 
       
   218     \section1 Further Reading
       
   219 
       
   220     The following documents contain information about Qt's D-Bus integration
       
   221     features, and provide details about the mechanisms used to send and receive
       
   222     type information over the bus:
       
   223 
       
   224     \list
       
   225     \o \l{Using QtDBus Adaptors}
       
   226     \o \l{The QtDBus Type System}
       
   227     \o \l{QtDBus XML compiler (qdbusxml2cpp)}
       
   228     \endlist
       
   229 */