User Tools

Site Tools


Sidebar

Network and communication

k24:k24.9:k24.9.6:k24.9.6.1:start

24.9.6.1 Project 1 - Intercept D-Bus signal and display signal names

The task was to intercept each signal on the D-Bus and display the name of the signal that is sent from the D-Bus object org/gtk/Private/RemoteVolumeMonitor when a USB device is plugged in or unplugged after ejecting. Due to the restriction to intercepting and displaying the signal names, the source code is very short:

' Gambas class file
 
Private $hDBusSignal As DBusSignal
 
Public Sub Form_Open()
 
  FMain.Resizable = False
  FMain.Caption = ("Watch signals ...")
 
  $hDBusSignal = New DBusSignal(DBus.Session, "org.gtk.Private.RemoteVolumeMonitor", True) As "DevSignal"
 
  txaReport.Insert(gb.NewLine)
 
End
 
Public Sub DevSignal_Signal(Signal As String, Arguments As Variant[])
' All signals of application 'org.gtk.Private.RemoteVolumeMonitor' are intercepted.
' The array of arguments is not evaluated.
 
  txaReport.Insert("       " & "Signal = " & Signal & gb.NewLine)
  txaReport.Pos = txaReport.Length
  FMain.SetFocus()
 
End
 
Public Sub Form_Close()
  If $hDBusSignal Then $hDBusSignal.Enabled = False
  FMain.Close()
End

For a USB stick, this was the result:

USB-Stick

Figure 24.9.6.1.1: Display of all observed signals (USB stick)

HD

Figure 24.9.6.1.2: Display of all observed signals (USB hard disk)

24.9.6.2 Project 2 - Intercept and evaluate D-Bus signal

The second project is an adaptation of the D-Bus Observer project 2, the task was taken over and the Observer object replaced by a signal object. The source code is given in full - but no comments are made, since the evaluation did not contain any new aspects. The complex data type Struct has been omitted.

[1] ' Gambas class file
[2]
[3] Public hDBusSignal As DBusSignal
[4]
[5] Private $cDBusConnection As DBusConnection
[6] Private $sSDBusInterface As String
[7]
[8] Public Sub Form_Open()
[9]
[10]   FMain.Resizable = True
[11]   FMain.Caption = ("Intercepting and evaluating a D-Bus signal ==> VolumeAdded")
[12]
[13]   $cDBusConnection = DBus.Session
[14]   $sSDBusInterface = "org.gtk.Private.RemoteVolumeMonitor"
[15]
[16]   hDBusSignal = New DBusSignal($cDBusConnection, $sSDBusInterface, True) As "ObservedSignal"
[17]
[18] End
[19]
[20] Public Sub Observed_Signal(Signal As String, Arguments As Variant[])
[21]
[22]   Dim k As Integer = 1
[23]   Dim vElement As Variant
[24]   Dim cCollection1, cCollection2 As Collection
[25]
[26]   If Signal = "VolumeAdded" Then
[27]     txaResults.Insert("Arguments of the \"VolumeAdded\" Signal:" & gb.NewLine)
[28]     txaResults.Insert(String$(61, "-") & gb.NewLine)
[29]     txaResults.Insert("<signal name=\"VolumeAdded\"" & gb.NewLine)
[30]     txaResults.Insert("  <arg type=\"s\" name=\"dbus_name\"/>" & gb.NewLine)
[31]     txaResults.Insert("  <arg type=\"s\" name=\"id\"/>" & gb.NewLine)
[32]     txaResults.Insert("  <arg type=\"(ssssssbbssa{ss}sa{sv})\" name=\"volume\"/>" & gb.NewLine)
[33]     txaResults.Insert("</signal>" & gb.NewLine & gb.NewLine)
[34]
[35]     txaResults.Insert("Number of arguments for Variant-Array 'Arguments' = " & Arguments.Count)
[36]     txaResults.Insert(gb.NewLine & gb.NewLine)
[37]     txaResults.Insert("Type String | Argument 1 = " & Arguments[0] & gb.NewLine)
[38]     txaResults.Insert("Type String | Argument 2 = " & Arguments[1] & gb.NewLine)
[39]     txaResults.Insert("Type Complex data type: | Argument 3 " & gb.NewLine)
[40]   ' 6x String
[41]     txaResults.Insert("Type String | Arguments[2][0] = " & Arguments[2][0] & gb.NewLine)
[42]     txaResults.Insert("Type String | Arguments[2][1] = " & Arguments[2][1] & gb.NewLine)
[43]     txaResults.Insert("Type String | Arguments[2][2] = " & Arguments[2][2] & gb.NewLine)
[44]     txaResults.Insert("Type String | Arguments[2][3] = " & Arguments[2][3] & gb.NewLine)
[45]     txaResults.Insert("Type String | Arguments[2][4] = " & Arguments[2][4] & gb.NewLine)
[46]     txaResults.Insert("Type String | Arguments[2][5] = " & Arguments[2][5] & gb.NewLine)
[47]   ' 2x Boolean
[48]     txaResults.Insert("Type Boolean | Arguments[2][6] = " & Arguments[2][6] & gb.NewLine)
[49]     txaResults.Insert("Type Boolean | Arguments[2][7] = " & Arguments[2][7] & gb.NewLine)
[50]   ' 2x String
[51]     txaResults.Insert("Type String | Arguments[2][8] = " & Arguments[2][8] & gb.NewLine)
[52]     txaResults.Insert("Type String | Arguments[2][9] = " & Arguments[2][9] & gb.NewLine)
[53]   ' 1x Collection 1 -> KeyType = String and DataType = String
[54]     cCollection1 = New Collection
[55]     cCollection1 = Arguments[2][10]
[56]     txaResults.Insert("Type Collection | Arguments[2][10]" & gb.NewLine)
[57]     If cCollection1.Count > 0 Then
[58]        For Each vElement In cCollection1
[59]          txaResults.Insert("Element " & Str(k) & "  :   " & cCollection1.Key)
[60]          txaResults.Insert(" = " & vElement & gb.NewLine)
[61]          Inc k
[62]        Next
[63]     Else
[64]        txaResults.Insert("Attention: The collection 1 is empty!" & gb.NewLine)
[65]     Endif
[66]   ' 1x String
[67]     txaResults.Insert("Type String | Arguments[2][11] = " & Arguments[2][11] & gb.NewLine)
[68]     k = 1
[69]   ' 1x Collection 2 -> KeyType = String and DataType = Variant
[70]     cCollection2 = New Collection
[71]     cCollection2 = Arguments[2][12]
[72]     txaResults.Insert("Type Collection | Arguments[2][12]" & gb.NewLine)
[73]     If cCollection2.Count > 0 Then
[74]        For Each vElement In cCollection2
[75]          txaResults.Insert("Element " & Str(k) & "  :   " & cCollection2.Key)
[76]          txaResults.Insert(" = " & vElement & gb.NewLine)
[77]          Inc k
[78]        Next
[79]     Else
[80]        txaResults.Insert("Attention: The collection 2 is empty!")
[81]     Endif
[82]   Endif
[83]
[84] End
[85]
[86] Public Sub Form_Close()
[87]
[88]   If hDBusSignal Then hDBusSignal.Enabled = False
[89]   FMain.Close()
[90]
[91] End

The result is extensive because - in contrast to project 1 - the content of the intercepted signal 'VolumeAdded' was also evaluated:

Figure 24.9.6.2.1: Display of the content of the three arguments of the signal

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.6/k24.9.6.1/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools