Java-dbus First steps

Matthew Johnson dbus at matthew.ath.cx
Thu Oct 29 08:30:03 PDT 2009


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
-- 
Matthew Johnson
www.matthew.ath.cx
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: Digital signature
Url : http://lists.freedesktop.org/archives/dbus/attachments/20091029/558bff00/attachment.pgp 


More information about the dbus mailing list