User Tools

Site Tools


Sidebar

Network and communication

k24:k24.9:k24.9.8:k24.9.8.1:start

24.9.8.1 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 https://gitlab.com/gambas/gambas/commit/be3375e0d29022f3e43adcfc38dafee9684060ba

The error in the component gb.dbus was noticeable:

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 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.

Service

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:

[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 completely specified and then commented:

[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:

  • 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.

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 initial values

Figure 24.9.8.1.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 24.9.8.1.5: Successful introspection of the “/Service” object

Download

This website uses cookies. By using the website, you agree with storing the cookies on your computer. More information in the privacy policy.
k24/k24.9/k24.9.8/k24.9.8.1/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools