The component gb.dbus consists of two parts: The core is written in C and is located in gb.dbus/src. Based on this, there is a part written in gambas and is located in the path gb.dbus/src/gb.dbus. For example, the class DBusVariant is written in C and is in the file c_dbusvariant.c, while the class DBusValue is completely programmed in Gambas.
As far as gambas and D-Bus are concerned, two strategies are generally pursued:
Implementing these strategies enables you to do the following for any d-bus-enabled application:
D-Bus offers at least two buses for communication between processes. One is the global system bus and another is the session bus, to which every desktop of a logged on user is automatically connected within his desktop session. The default in Gambas is the session bus.
There are (at least) two D-Bus daemons running on a Linux system
$ env | grep DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-CTaH4kQuEM,guid=59a12bb14e4d0385a446dd2257f34ab5
You can also use the DBusView gambas program to get an overview of all programs currently registered on the D-Bus:
Figure 22.214.171.124.1: Registered programs on the D-Bus
You can find the archive for the DBusView program in the download area of Chapter 24.9.2.
You can use the following commands to read out a list of applications that are registered on the system bus or on the session bus. The list contains the D-Bus names and the unique connection IDs assigned to them, which are always preceded by a colon. The program qdbus is used:
$ qdbus --session | grep -v ":" org.gnome.SessionManager org.gtk.Private.UDisks2VolumeMonitor ... org.PulseAudio1 org.pulseaudio.Server org.freedesktop.DBus $ qdbus --system | grep ":" ... :1.44 :1.5 :1:21
Communication on the system bus and the session bus takes place via messages - similar to sending packets in TCP/IP networks. In contrast to the TCP/IP protocol, however, D-Bus guarantees the transmission of all messages in one go. There are four types of D-Bus messages:
Messages are sent or received as method calls or signals. Each call of a method is always acknowledged with a message - not only in the event of an error or the successful return of a value.
In the gambas class DBus (gb.dbus) the message types are declared using the four (integer) constants DBus.method (1), DBus.reply (2), DBus.error (3) and DBus.signal (4).
Since several applications - or their processes - can be connected to the system bus or the session bus, it is necessary to address each message uniquely. The address consists of the information
of the service (name of the application), the object path and (optionally) the interfaces.126.96.36.199.4 D-Bus-NameA service represents an active connection between a program and the System D-Bus or the Session D-Bus. This connection is described by the readable D-Bus name. A “ReverseDomainName” is used for this description. D-Bus names have the syntax: “ReverseDomainName”.application name. Note that D-Bus names are case-sensitive.
The syntax for D-Bus names in Gambas is: org.gambas.xxx with xxx as the Gambas project name, which is provided under the value of the Application.Name property and used for xxx. These D-Bus names, for example, are correct:
org.freedesktop.NetworkManager org.gambas.support org.Cinnamon.LookingGlass org.gtk.Private.RemoteVolumeMonitor
If more than one instance of an application is registered on the D-Bus, the process ID is appended to the name of the service so that a unique D-Bus name exists for the connection.
At the same time, a unique ID is generated for each connection of an application to the D-Bus. This ID starts with a colon (:) followed by a sequence of digits, a period and another sequence of digits. For example, a valid ID would be :1.63 .
Objects provide access to interfaces. They are identified by identifiers such as /org/freedesktop/NetworkManager or /org/gambas/P2DBUS, whose syntax reminds of file paths. The following rules define a valid object path:
qdbus --session \ org.gambas.export_dbus \ /org/gambas/export_dbus \ org.gambas.export_dbus.Compute(7,13)
Note: A d-bus-enabled application can ignore an incorrectly declared object path!
A valid address, for example, is made up of these three details:
and will be used as a valid address:
org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.DBus.Introspectable
An (active) interface has methods, properties and events as well as signals and is registered on the D-Bus. The description of an interface can be in an XML document to serve as a reference for developers and users (introspection).