Table of Contents

24.9.8.1 Projects - Creating, Exporting and Using a D-Bus Object

In this chapter you will be introduced to, among other things, a Gambas program that implements a D-Bus server that provides a specific service to d-bus enabled programmes. The implemented service provided by a D-bus object exported to the session D-bus can be described like this:

The error in the gb.dbus component made itself felt like this:

ERROR
Figure 24.9.8.1.1: Error display

24.9.8.1.1 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, which is described in a special class file. You can freely define the name - 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-capable programmes.

Service

The general rule for a D-Bus service is that 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 done automatically. Properties are declared in the usual way.

The source code of the class Service.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
End
 
Public Function ComputeMulFloat(Value1 As Float, Value2 As Float) As Float
  Return Value1 * Value2
End
 
Public Function GetDayOfWeekText(iNumberOfDay As Integer) As String
 
  Dim aTagesListe As String[]
 
  aTagesListe = [("Sunday"),("Monday"),("Tuesday"),("Wednesday"),("Thursday"),("Friday"),("Saturday")]
  Return aTagesListe[iNumberOfDay]
 
End
 
Private Function PValue_Read() As Float
  Return $fPValue
End
 
Private Sub PValue_Write(Value As Float)
  $fPValue = Value
End

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
[2]
[3] Public hDBusObject As Service
[4]
[5] Public Sub Form_Open()
[6]
[7]   FMain.Resizable = False
[8]   FMain.Caption = ("The data server is activated")
[9]   DBus.Unique = True
[10]
[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
[17]
[18] End
[19]
[20] Public Sub Form_Close()
[21]   If DBus.IsRegistered(hDBusObject) Then DBus.Session.Unregister(hDBusObject)
[22]   FMain.Close()
[23] End

24.9.8.1.2 Project Client

The source code for the client dbusclient1 is given in full and then commented on:

[1] ' Gambas class file
[2]
[3] Private $hDBusProxy As DBusProxy
[4] Private $sApplication As String
[5] Private $sObjectPath As String
[6]
[7] Public Sub Form_Open()
[8]
[9]   Dim sMessage As String
[10]
[11]   FMain.Resizable = False
[12]   FMain.Caption = ("Remote data enquiry via D-Bus")
[13]   Application.MainWindow = FMain
[14]
[15]   $sApplication = "org.gambas.dbusserver1"
[16]
[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
[29]
[30] End
[31]
[32] Public Sub btnGetData_Click()
[33]   GetData()
[34] End
[35]
[36] Private Sub GetData()
[37]
[38]   Dim i As Integer
[39]   Dim fF1, fF2 As Float
[40]   Dim iS1, iS2 As Integer
[41]
[42]   fF1 = Val(txbFactor1.Text)
[43]   fF2 = Val(txbFactor2.Text)
[44]   tboxMul.Text = CStr($hDBusProxy.ComputeMulFloat(fF1, fF2))
[45]
[46]   iS1 = Val(txbSummand1.Text)
[47]   iS2 = Val(txbSummand2.Text)
[48]   tboxSum.Text = CStr($hDBusProxy.ComputeAddInteger(iS1, iS2))
[49]
[50]   i = WeekDay(Now())
[51]   lblDOW.Text = $hDBusProxy.GetDayOfWeekText(i) & "!"
[52]
[53]   tboxPValue.Text = Round($hDBusProxy.PValue, -7)
[54]
[55] End
[56]
[57] Public Sub sboxChange_Change()
[58]
[59]   Dim fCurValue As Float
[60]
[61]   fCurValue = $hDBusProxy.PValue
[62]   Try $hDBusProxy.PValue = fCurValue + sboxChange.Value
[63]   tboxPValue.Text = Round($hDBusProxy.PValue, -7)
[64]
[65] End
[66]
[67] Public Sub btnIntrospection_Click()
[68]   FIntrospection.Show()
[69] End
[70]
[71] Public Sub Form_Close()
[72]   FMain.Close()
[73] End

Comment:

24.9.8.1.3 Use of Server and Client

Server

Server
Figure 24.9.8.1.2: 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 24.9.8.1.3: Calculation 1 with start values


Figure 24.9.8.1.4: Calculation 2

You can change the summands and the factors via input fields. You modify the value for PValue via a SpinBox in integer steps. Afterwards, the product, the sum and the current day name are updated and displayed via 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 get information about the implemented methods, their signature and input parameters as well as about the defined property 'PValue' via an introspection, which is realised by clicking on the i-button:


Figure 24.9.8.1.5: Successful introspection of the object '/Service'.

Download