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:
The error in the component gb.dbus was noticeable:
Figure 220.127.116.11.1: Error display
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 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:
 ' Gambas class file   Public hDBusObject As Service   Public Sub Form_Open()   FMain.Resizable = False  FMain.Caption = ("The data server is activated")  DBus.Unique = True   hDBusObject = New Service  Try DBus.Session.Register(hDBusObject, "/Service")  If Error Then  Message.Error("An instance of " & Application.Name & " already exists.")  FMain.Close()  Endif   End   Public Sub Form_Close()  If DBus.IsRegistered(hDBusObject) Then DBus.Session.Unregister(hDBusObject)  FMain.Close()  End
The source code for the client dbusclient1 is completely specified and then commented:
 ' Gambas class file   Private $hDBusProxy As DBusProxy  Private $sApplication As String  Private $sObjectPath As String   Public Sub Form_Open()   Dim sMessage As String   FMain.Resizable = False  FMain.Caption = ("Remote data enquiry via D-Bus")  Application.MainWindow = FMain   $sApplication = "org.gambas.dbusserver1"   If Not DBus.Session.Applications.Exist($sApplication) Then  sMessage = ("There is no suitable data server on the session bus!")  sMessage &= "<center><font color='red'>"  sMessage &= ("The program is terminated.")  sMessage &= "</font></center>"  Message.Warning(sMessage)  FMain.Close()  Else  $sObjectPath = "/Service"  $hDBusProxy = DBus[$sApplication][$sObjectPath]  GetData()  Endif   End   Public Sub btnGetData_Click()  GetData()  End   Private Sub GetData()   Dim i As Integer  Dim fF1, fF2 As Float  Dim iS1, iS2 As Integer   fF1 = Val(txbFactor1.Text)  fF2 = Val(txbFactor2.Text)  tboxMul.Text = CStr($hDBusProxy.ComputeMulFloat(fF1, fF2))   iS1 = Val(txbSummand1.Text)  iS2 = Val(txbSummand2.Text)  tboxSum.Text = CStr($hDBusProxy.ComputeAddInteger(iS1, iS2))   i = WeekDay(Now())  lblDOW.Text = $hDBusProxy.GetDayOfWeekText(i) & "!"   tboxPValue.Text = Round($hDBusProxy.PValue, -7)   End   Public Sub sboxChange_Change()   Dim fCurValue As Float   fCurValue = $hDBusProxy.PValue  Try $hDBusProxy.PValue = fCurValue + sboxChange.Value  tboxPValue.Text = Round($hDBusProxy.PValue, -7)   End   Public Sub btnIntrospection_Click()  FIntrospection.Show()  End   Public Sub Form_Close()  FMain.Close()  End
Figure 18.104.22.168.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 22.214.171.124.3: Calculation 1 with initial values
Figure 126.96.36.199.4: 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 188.8.131.52.5: Successful introspection of the “/Service” object