User Tools

Site Tools


Network and communication

k24:k24.9:k24.9.8:k24.9.8.1:start Projects - Create, export and use D-Bus object

This chapter introduces a Gambas program that implements a D-Bus server that offers a specific service for d-bus-enabled programs. The implemented service provided by a D-Bus object exported to the Session D-Bus can be described in this way:

  • Calculation and return of the product of two real numbers, which are passed as parameters in method call 1.
  • Calculate and return the sum of two integers passed as parameters in method call 2.
  • Calculation and return of the current weekday as text, if the function value WeekDay(Now()) of the data type Integer is passed as parameter in method call 3.
  • Reads the value of the PValue property.
  • Change the PValue property. Unfortunately, an error in the gb.dbus component prevented the value of a property on a D-Bus server from being changed. The bug was fixed with

The error in the component gb.dbus was noticeable:


Figure Error display Project Server

The source code for each D-Bus server that provides a service consists of at least 2 class files. The reason is that the service is implemented in a D-Bus object that is described in a special class file. You can define the name freely - in this project Service.class. The D-Bus object is exported to the Session D-Bus in the start class FMain.class, so that the service can also be used by other d-bus-compatible programs.


For a D-Bus service, all methods are implemented as functions. If the function values are native data types - in this service integer, float and string - then you do not need to worry about converting Gambas data types to D-Bus data types. The conversion is automatic. Properties are declared in the usual way.

The source code of the Service.class class is pleasantly short and clear:

' Gambas class file
Inherits DBusObject	' This instruction is required
Create Static		' This instruction is required
Property PValue As Float         ' Public variable
Private $fPValue As Float = Pi() ' Local variable
Public Function ComputeAddInteger(Value1 As Integer, Value2 As Integer) As Integer
  Return Value1 + Value2
Public Function ComputeMulFloat(Value1 As Float, Value2 As Float) As Float
  Return Value1 * Value2
Public Function GetDayOfWeekText(iNumberOfDay As Integer) As String
  Dim aTagesListe As String[]
  aTagesListe = [("Sunday"),("Monday"),("Tuesday"),("Wednesday"),("Thursday"),("Friday"),("Saturday")]
  Return aTagesListe[iNumberOfDay]
Private Function PValue_Read() As Float
  Return $fPValue
Private Sub PValue_Write(Value As Float)
  $fPValue = Value

In the source code of FMain.class, the registration of the D-Bus object in line 12 is the central instruction after a new D-Bus object of type 'Service' has been created in line 3:

[1] ' Gambas class file
[3] Public hDBusObject As Service
[5] Public Sub Form_Open()
[7]   FMain.Resizable = False
[8]   FMain.Caption = ("The data server is activated")
[9]   DBus.Unique = True
[11]   hDBusObject = New Service
[12]   Try DBus.Session.Register(hDBusObject, "/Service")
[13]   If Error Then
[14]      Message.Error("An instance of " & Application.Name & " already exists.")
[15]      FMain.Close()
[16]   Endif
[18] End
[20] Public Sub Form_Close()
[21]   If DBus.IsRegistered(hDBusObject) Then DBus.Session.Unregister(hDBusObject)
[22]   FMain.Close()
[23] End Project Client

The source code for the client dbusclient1 is completely specified and then commented:

[1] ' Gambas class file
[3] Private $hDBusProxy As DBusProxy
[4] Private $sApplication As String
[5] Private $sObjectPath As String
[7] Public Sub Form_Open()
[9]   Dim sMessage As String
[11]   FMain.Resizable = False
[12]   FMain.Caption = ("Remote data enquiry via D-Bus")
[13]   Application.MainWindow = FMain
[15]   $sApplication = "org.gambas.dbusserver1"
[17]   If Not DBus.Session.Applications.Exist($sApplication) Then
[18]      sMessage = ("There is no suitable data server on the session bus!")
[19]      sMessage &= "<center><font color='red'>"
[20]      sMessage &= ("The program is terminated.")
[21]      sMessage &= "</font></center>"
[22]      Message.Warning(sMessage)
[23]      FMain.Close()
[24]   Else
[25]     $sObjectPath = "/Service"
[26]     $hDBusProxy = DBus[$sApplication][$sObjectPath]
[27]     GetData()
[28]   Endif
[30] End
[32] Public Sub btnGetData_Click()
[33]   GetData()
[34] End
[36] Private Sub GetData()
[38]   Dim i As Integer
[39]   Dim fF1, fF2 As Float
[40]   Dim iS1, iS2 As Integer
[42]   fF1 = Val(txbFactor1.Text)
[43]   fF2 = Val(txbFactor2.Text)
[44]   tboxMul.Text = CStr($hDBusProxy.ComputeMulFloat(fF1, fF2))
[46]   iS1 = Val(txbSummand1.Text)
[47]   iS2 = Val(txbSummand2.Text)
[48]   tboxSum.Text = CStr($hDBusProxy.ComputeAddInteger(iS1, iS2))
[50]   i = WeekDay(Now())
[51]   lblDOW.Text = $hDBusProxy.GetDayOfWeekText(i) & "!"
[53]   tboxPValue.Text = Round($hDBusProxy.PValue, -7)
[55] End
[57] Public Sub sboxChange_Change()
[59]   Dim fCurValue As Float
[61]   fCurValue = $hDBusProxy.PValue
[62]   Try $hDBusProxy.PValue = fCurValue + sboxChange.Value
[63]   tboxPValue.Text = Round($hDBusProxy.PValue, -7)
[65] End
[67] Public Sub btnIntrospection_Click()
[68]   FIntrospection.Show()
[69] End
[71] Public Sub Form_Close()
[72]   FMain.Close()
[73] End


  • Line 17 checks if the application with the D-Bus name org.gambas.dbusserver1 exists on the session bus. If this is the case, a proxy is created after the assignment in line 25 in line 26 and only this is used consistently.
  • The GetData() procedure calls all three implemented methods with the required arguments and displays the returns (function values) provided by the server in the controls provided. In addition, the PValue property is also read and displayed.
  • The procedure for changing the value of the PValue property could not yet be tested. Use of server and client



Figure Server GUI

The server is started, exports a D-Bus object to the Session D-Bus with the implemented service and waits for requests from d-bus-capable clients.

The presented client uses the offered service of the server:

Figure Calculation 1 with initial values

Figure Calculation 2

You can change the summands and the factors using input fields. You modify the value for PValue in integer steps using a SpinBox. Then the product, the total and the current day name are updated and displayed using three different method calls and the value of the PValue property is read out and also displayed in a text box.

At any time you can find out about the implemented methods, their signature and input parameters as well as about the defined'PValue' property via an introspection, which is implemented by clicking on the i button:

Figure Successful introspection of the “/Service” object


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.9/k24.9.8/k24.9.8.1/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools