最近在研究Linux上的新型技术,发现D-Bus是一个关于平台infrastructure方面的关键技术。这里就总结了一些技术要点作为一个basic的介绍。以后希望能有更多的时间来补充更多细节性的东西。本文其中大都是拷贝有关的英文原文按自己的理解重新编排而成。
What is D-Bus?
- a system for interprocess communication (IPC).
- an inter-process communication framework that lets applications interface with the system event bus
as well as allowing them to talk to one another in a peer-to-peer configuration.
D-Bus usecases
1) within-desktop-session
在linux系统中,每个用户登录后就对应一个用户的Session,在该Session下所有的应用程序可以用此Session的D-Bus进行通信
2) communication-with-the-OS
在系统范围内,不同模块直接可以用系统范围的d-bus进行通信
D-Bus characters
- Binary protocol designed to be used asynchronously
- State, reliable connections held open over time
- The message bus is a daemon
- Many implementation and deployment issues are specified rather than left ambiguous/configurable/pluggable
- Semantics are similar to the existing DCOP system, allowing KDE to adopt it more easily
- Security features to support the systemwide mode of the message bus
D-Bus components
- dbus library
one-to-one connections, just like a raw network socket
Messages have a header identifying the kind of message, and a body containing a data payload
abstracts the exact transport used (sockets vs. whatever else)
handles details such as authentication
- message bus daemon
- A router
- Multiple instances
- first instance is a machine-global singleton, used for system wide communication
- Other instances are created one per user login session. Allow applications in the user's session to communicate with one another
Language Binding
1) C binding is based on GLib
2) Java, Perl and Python
3) Qt http://doc.trolltech.com/4.2/intro-to-dbus.html
http://www.freedesktop.org/wiki/Software/DBusBindings
D-Bus Object Model
- Object (at the end point of D-Bus is object, object is for client offer its services on bus. One client can create any number of objects)
1.1)Object name (object path )
looks like a filesystem path. for example an object could be named /org/kde/kspread/sheets/3/cells/4/5
1.2)Methods and Signals
1.2.1)Each object has two kinds of members: methods and signals
1.2.2)Methods are operations that can be invoked on an object
1.2.3)Signals are broadcasts from the object to any interested observers of the object; signals may contain a data payload.
1.3)Interfaces
Each object supports one or more interfaces. a named group of methods and signals
DBus identifies interfaces with a simple namespaced string, something like org.freedesktop.Introspectable - proxy object
a convenient native object created to represent a remote object in another process
when you invoke a method on the proxy object, the binding converts it into a DBus method call message, waits for the reply message, unpacks the return value, and returns it from the native method..
Address & Connection
- Address
Every bus has an address describing how to connect to it
It can be:
.)a domain socket file name unix:path=/tmp/abcdef
.)TCP/IP Sockets
.)Any other transport - Connection
When each application connects to the bus daemon, the daemon immediately assigns it a name, called the unique connection name
When a connection is set up, the bus immediately assigns it an immutable bus name that it will retain for as long as the bus exists.
Message
- Types
a)Method call
messages ask to invoke a method on an object.
b)Method return
messages return the results of invoking a method.
c)Error messages
return an exception caused by invoking a method.
d)Signal messages
are notifications that a given signal has been emitted
- message has a header, including fields, and a body, including arguments
a) Header
Hold the address info
b) Body
Zero or more arguments - The maximum length of a message, including header, header alignment padding, and body is 2 to the 27th power or 134217728
Service D-Bus activation
- dbus daemon to automatically start a program that provide a given service
- A service is a program that can be launched by the bus daemon to provide some functionality to other programs. Services are normally launched according to the bus name they will have.
- To find an executable corresponding to a particular name, the bus daemon looks for service description files. Service description files define a mapping from names to executables.
Message Routing
- When a message is routed through the bus it is compared to clients' match rules. If any of the rules match, the message is dispatched to the client. If none of the rules match the message never leaves the bus.
- An example of a map rule:
"type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',path='/bar/foo',destination=':452345.34',arg2='bar'"
Differences with other IPC
- it is not only an IPC system; it also includes lifecycle tracking, service activation, security policy, and other higher-level structure and assumptions.
- Other IPCs
Corba
XML-RPC SOAP
DCOM COM
DCOP
Authentication protocol
- Map from SASL
SASL is the Simple Authentication and Security Layer, a method for adding authentication support to connection-based protocols. To use SASL, a protocol includes a command for identifying and authenticating a user to a server and for optionally negotiating protection of subsequent protocol interactions. If its use is negotiated, a security layer is inserted between the protocol and the connection.
- Commands
2.1)Commands from the client to the server are as follows:
AUTH [mechanism] [initial-response]
CANCEL
BEGIN
DATA <data in hex encoding>
ERROR [human-readable error explanation]
2.2)From server to client are as follows:
REJECTED <space-separated list of mechanism names>
OK <GUID in hex>
DATA <data in hex encoding>
ERROR
Unofficial extensions to the command set must begin with the letters "EXTENSION_", to avoid conflicts with future official commands. For example, "EXTENSION_COM_MYDOMAIN_DO_STUFF".
Standard Interfaces
- org.freedesktop.DBus.Peer
org.freedesktop.DBus.Peer.Ping ()
org.freedesktop.DBus.Peer.GetMachineId (out STRING machine_uuid) - org.freedesktop.DBus.Introspectable
org.freedesktop.DBus.Introspectable.Introspect (out STRING xml_data) - org.freedesktop.DBus.Properties
org.freedesktop.DBus.Properties.Get (in STRING interface_name, in STRING property_name, out VARIANT value);
org.freedesktop.DBus.Properties.Set (in STRING interface_name, in STRING property_name, in VARIANT value);
org.freedesktop.DBus.Properties.GetAll (in STRING interface_name, out DICT<STRING,VARIANT> props);
Example of D-Bus Usage
http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html
References
http://www.freedesktop.org/wiki/Software/dbus
http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide/DBus/DBus_Basics