Editing Documentation/Maemo 5 Developer Guide/DBus/Implementing and Using D-Bus Signals

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.

Warning: This page is 38 kilobytes long; some browsers may have problems editing pages approaching or longer than 32kb. Please consider breaking the page into smaller sections.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 3: Line 3:
Performing remote method invocations over the D-Bus is only one half of D-Bus capabilities. As was noted before, D-Bus also supports a ''broadcast'' method of communication, which is ''asynchronous''. This mechanism is called a ''signal'' (in D-Bus terminology), and it is useful when several receivers must be notified about a state change that could affect them. Some examples where signals could be useful are notifying a lot of receivers, when the system is being shut down, network connectivity has been lost, and similar system-wide conditions. This way, the receivers do not need to poll for the status continuously.
Performing remote method invocations over the D-Bus is only one half of D-Bus capabilities. As was noted before, D-Bus also supports a ''broadcast'' method of communication, which is ''asynchronous''. This mechanism is called a ''signal'' (in D-Bus terminology), and it is useful when several receivers must be notified about a state change that could affect them. Some examples where signals could be useful are notifying a lot of receivers, when the system is being shut down, network connectivity has been lost, and similar system-wide conditions. This way, the receivers do not need to poll for the status continuously.
-
However, signals are not the solution to all problems. If a recipient is not processing its D-Bus messages quickly enough (or there just are too many), a signal might get lost on its way to the recipient. Other complications can also occur, as with any RPC mechanism. For these reasons, if the application requires extra reliability, you need to think how to arrange it. One possibility is to occasionally check the state that the application is interested in, assuming it can be checked over the D-Bus. This, however, should not be done too often; the recommended interval is once a minute or less often than that, and only when the application is already active for other reasons. This model leads to a reduction in battery life, so its use should be carefully weighed.
+
However, signals are not the solution to all problems. If a recipient is not processing its D-Bus messages quickly enough (or there just are too many), a signal might get lost on its way to the recipient. Other complications can also occur, as with any RPC mechanism. For these reasons, if the application requires extra reliability, you need to think how to arrange it. One possibility is to occasionally check the state that the application is interested in, assuming it can be checked over the D-Bus. This, however, should not be done too often; the recommended interval is once a minute or less than that, and only when the application is already active for other reasons. This model leads to a reduction in battery life, so its use should be carefully weighed.
Signals in D-Bus are able to carry information. Each signal has its own name (specified in the interface XML), as well as "arguments". In signal's case, the argument list is actually a list of information that is passed along the signal and should not be confused with method call parameters (although both are delivered in the same manner).
Signals in D-Bus are able to carry information. Each signal has its own name (specified in the interface XML), as well as "arguments". In signal's case, the argument list is actually a list of information that is passed along the signal and should not be confused with method call parameters (although both are delivered in the same manner).
Line 51: Line 51:
</source>
</source>
-
The signal definitions are required if the <code>dbus-bindings-tool</code> is used; however, the argument specification for each signal is not required by the tool. In fact, the tool ignores all argument specifications, and as can be seen below, a lot of "manual coding" has to be made in order to implement and use the signals (on both the client and server side).
+
The signal definitions are required the dbus-bindings-tool is used; however, the argument specification for each signal is not required by the tool. In fact, the tool ignores all argument specifications, and as can be seen below, a lot of "manual coding" has to be made in order to implement and use the signals (on both the client and server side).
== Emitting Signals from GObject ==
== Emitting Signals from GObject ==
Line 288: Line 288:
The getvalue functions are identical to the versions before as is the Makefile.
The getvalue functions are identical to the versions before as is the Makefile.
-
Next, the server is built and started on the background (in preparation for testing with <code>dbus-send</code>):
+
Next, the server is built and started on the background (in preparation for testing with dbus-send):
<pre>
<pre>
Line 354: Line 354:
Because the threshold testing logic truncates the <code>gdouble</code> before testing against the (integer) thresholds, a value of 100.5 is detected as 100 and it still fits within the thresholds.
Because the threshold testing logic truncates the <code>gdouble</code> before testing against the (integer) thresholds, a value of 100.5 is detected as 100 and it still fits within the thresholds.
-
Instead of printing out the emitted signal names, the enumeration values of the emitted signals are printed. This could be rectified with a small enumeration to string table, but it was omitted from the program for simplicity.
+
Instead of printing out the emitted signal names, the enumeration values of the emitted signals are printed. This could be rectified with a small enumeration to string table, but it was emitted from the program for simplicity.
-
Other than seeing the server messages about emitting the signals, there is no trace of them being sent or received. This is because <code>dbus-send</code> does not listen for signals. A separate tool (<code>dbus-monitor</code>) can be used for tracing signals.
+
Other than seeing the server messages about emitting the signals, there is no trace of them being sent or received. This is because dbus-send does not listen for signals. A separate tool (dbus-monitor) can be used for tracing signals.
== Catching Signals in GLib/D-Bus Clients ==
== Catching Signals in GLib/D-Bus Clients ==
-
In order to receive D-Bus signals in the client, you need to perform several tasks per signal. This is because <code>dbus-bindings-tool</code> does not generate any code for signals. The aim is to make the GLib wrappers emit GLib signals whenever an interesting D-Bus signal arrives. This also means that registering the interest for a particular D-Bus signal is necessary.
+
In order to receive D-Bus signals in the client, you need to perform several tasks per signal. This is because <code>dbus-bindings-tool</code> does not generate any code for signals. The aim is to make the GLib wrappers emit Glib signals whenever an interesting D-Bus signal arrives. This also means that registering the interest for a particular D-Bus signal is necessary.
When implementing the callbacks for the signals, the prototype must be implemented correctly. Because the signals are sent with one attached string value, the callbacks receive at least the string parameter. Besides the signal attached arguments, the callback receives the proxy object, through which the signal was received, and optional user-specified data (which is not used in this example, so it is always <code>NULL</code>). [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/client.c glib-dbus-signals/client.c]
When implementing the callbacks for the signals, the prototype must be implemented correctly. Because the signals are sent with one attached string value, the callbacks receive at least the string parameter. Besides the signal attached arguments, the callback receives the proxy object, through which the signal was received, and optional user-specified data (which is not used in this example, so it is always <code>NULL</code>). [http://vcs.maemo.org/svn/maemoexamples/trunk/glib-dbus-signals/client.c glib-dbus-signals/client.c]
Line 416: Line 416:
</source>
</source>
-
The callback first determines what was the source value that caused the signal to be generated. For this, it uses the string argument of the signal. It then retrieves the current value using the respective RPC methods (<code>getvalue1</code> or <code>getvalue2</code>) and prints out the value.
+
The callback first determines what was the source value that caused the signal to be generated. For this, it uses the string argument of the signal. It then retrieves the current value using the respective RPC methods (<code>getvalue1</code> or getvalue2) and prints out the value.
-
If any errors occur during the method calls, the errors are printed out, but the program continues to run. If an error does occur, the <code>GError</code> object needs to be freed (performed with <code>g_clear_error</code>). The program does not terminate on RPC errors, because the condition might be temporary (the <code>Value</code> object server might be restarted later).
+
If any errors occur during the method calls, the errors are printed out, but the program continues to run. If an error does occur, the GError object needs to be freed (performed with <code>g_clear_error</code>). The program does not terminate on RPC errors, because the condition might be temporary (the <code>Value</code> object server might be restarted later).
The code for the <code>outOfRangeSignalHandler</code> callback has been omitted, because it does not contain anything beyond what <code>valueChangedSignalHandler</code> demonstrates.
The code for the <code>outOfRangeSignalHandler</code> callback has been omitted, because it does not contain anything beyond what <code>valueChangedSignalHandler</code> demonstrates.
Line 544: Line 544:
</source>
</source>
-
When adding the argument signatures for the signals (with <code>dbus_g_proxy_add_signal</code>), one needs to be very careful with the parameter list. The signal argument types must be exactly the same as are sent from the server (irrespective of the argument specification in the interface XML). This is because the current version of <code>dbus-bindings-tool</code> does not generate any checks to enforce signal arguments based on the interface. In this simple case, only one string is received with each different signal, so this is not a big issue. The implementation for the callback function needs to match the argument specification given to the <code>_add_signal</code>-function, otherwise data layout on the stack is incorrect and can cause problems.
+
When adding the argument signatures for the signals (with <code>dbus_g_proxy_add_signal</code>), one needs to be very careful with the parameter list. The signal argument types must be exactly the same as are sent from the server (irrespective of the argument specification in the interface XML). This is because the current version of <code>dbus-bindings-tool</code> does not generate any checks to enforce signal arguments based on the interface. In this simple case, only one string is received with each different signal, so this is not a big issue. The implementation for the callback function needs to match the argument specification given to the _add_signal-function, otherwise data layout on the stack is incorrect and can cause problems.
Building the client is done in the same manner as before (make client). Because the server is still (hopefully) running on the background, the client is now started in the same session:
Building the client is done in the same manner as before (make client). Because the server is still (hopefully) running on the background, the client is now started in the same session:
Line 594: Line 594:
Sometimes seeing which signals are actually carried on the buses can be useful, especially when adding signal handlers for signals that are emitted from undocumented interfaces. The <code>dbus-monitor</code> tool attaches to the D-Bus daemon, and asks it to watch for signals and report them back to the tool, so that it can decode the signals automatically, as they appear on the bus.
Sometimes seeing which signals are actually carried on the buses can be useful, especially when adding signal handlers for signals that are emitted from undocumented interfaces. The <code>dbus-monitor</code> tool attaches to the D-Bus daemon, and asks it to watch for signals and report them back to the tool, so that it can decode the signals automatically, as they appear on the bus.
-
While the server and client are still running, the next step is to start the <code>dbus-monitor</code> (in a separate session this time) to see whether the signals are transmitted correctly. It should be noted that signals appear on the bus even if there are no clients currently interested in them. In this case, signals are emitted by the server, based on client-issued RPC methods, so if the client is terminated, the signals cease.
+
While the server and client are still running, the next step is to start the dbus-monitor (in a separate session this time) to see whether the signals are transmitted correctly. It should be noted that signals appear on the bus even if there are no clients currently interested in them. In this case, signals are emitted by the server, based on client-issued RPC methods, so if the client is terminated, the signals cease.
<pre>
<pre>
Line 765: Line 765:
</pre>
</pre>
-
It is also possible to send signals from the command line, which is useful when some feature of a device needs to be emulated inside the SDK. This (like RPC method calls) can be performed with the <code>dbus-send</code> tool. An example of this kind of simulation was given in the [[Documentation/Maemo 5 Developer Guide/Application Development/LibOSSO library|LibOSSO section]].
+
It is also possible to send signals from the command line, which is useful when some feature of a device needs to be emulated inside the SDK. This (like RPC method calls) can be performed with the <code>dbus-send</code> tool. An example of this kind of simulation was given in the LibOSSO section.
[[Category:Development]]
[[Category:Development]]
[[Category:Documentation]]
[[Category:Documentation]]
[[Category:Fremantle]]
[[Category:Fremantle]]

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)