Java-dbus First steps

Arigead captain.deadly at gmail.com
Thu Oct 29 14:54:34 PDT 2009


Matthew Johnson wrote:
> On Thu Oct 29 12:41, Arigead wrote:
>> signal sender=:1.66 -> dest=(null destination) serial=16043
>> path=/org/gnome/Rhythmbox/Player; interface=org.gnome.Rhythmbox.Player;
>> member=playingChanged boolean false
>>
>> If I wanted to listen to this signal and simply print a line out when I
>> received the signal I'm not sure the values to fill into the
>> addSignalHandler() method. I could use the addSignalHandler() which
>> accepts a DBusMatchRule and create a rule:
> 
> You shouldn't need to use DBusMatchRule directly if you are using the
> high-level API (which you should).
> 
> Instead you should use the addSigHandler(Class<T> type, String source,
> DBusSigHandler<T> handler) method [0] or the addSigHandler(Class<T>
> type, DBusSigHandler<T> handler method[1].
> 
> The first argument is the type of the signal, which should be expressed
> as a Java class which inherits from DBusSignal, as described in the
> documentation[2]. The second argument is the bus name you expect to emit
> the signal (optional), the third is the object which is handling the callback when
> the signal is received. This should inherit from DBusSigHandler, which
> is also described in the documentation[3].
> 
> The signal class should be constructed from the introspection data (see
> the CreateInterface tool, provided with dbus-java) or from the
> dbus-monitor output.
> 
> For your example therefore you need:
> 
> org/gnome/Rhythmbox/Player.java:
> 
> package org.gnome.Rhythmbox;
> 
> public interface Player extends DBusInterface
> {
>    public class playingChanged extends DBusSignal
>    {
>       public final boolean value;
>       public playingChanged(String path, boolean value) throws
>          DBusException
>          {
>             super(path, value);
>             this.value = value;
>          }
>    }
> }
> 
> mypackage/Handler.java:
> 
> public class Handler extends
> DBusSigHandler<org.gnome.Rhythmbox.Player.playingChanged>
> {
>    public void handle(org.gnome.Rhythmbox.Player.playingChanged signal)
>    {
>       // do something with signal.value
>    }
> }
> 
> mypackage/Main.java:
> 
> .....
> 
> connection.addSigHandler(org.gnome.Rhythmbox.Player.playingChanged.class, new Handler());
> 
> and then everything else will be handled for you.
> 
> Matt
> 
> 0. http://dbus.freedesktop.org/doc/dbus-java/api/org/freedesktop/dbus/DBusConnection.html#addSigHandler(java.lang.Class, java.lang.String, org.freedesktop.dbus.DBusSigHandler)
> 1. http://dbus.freedesktop.org/doc/dbus-java/api/org/freedesktop/dbus/AbstractConnection.html#addSigHandler(java.lang.Class, org.freedesktop.dbus.DBusSigHandler)
> 2. http://dbus.freedesktop.org/doc/dbus-java/dbus-java/dbus-javase4.html#x17-170004
> 3. http://dbus.freedesktop.org/doc/dbus-java/dbus-java/dbus-javase6.html#x22-190006

Oh !

thanks a million for your response Matthew,

I didn't realise that I have to either include the Rhythmbox source code
if I was using their signal or write a "dummy" class. I've only ever
looked at DBus from Python before. What all the above means is that to
monitor a signal from an app if that app is written in a language other
then Java ye have to write the definition of the signal in Java. That
makes a lot of sense when you think about it.

Once again thanks a million



More information about the dbus mailing list