Java-dbus First steps

View: New views
4 Messages — Rating Filter:   Alert me  

Java-dbus First steps

by Arigead :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,
    I'm a bit confused by java-Dbus as I can't find examples of, or see
how to, setup a signal handler. I used dbus-monitor on my laptop for
example and can see a signal coming from rhythm box:

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:

BusMatchRule match = new DBusMatchRule("type",
                                        "org.gnome.Rhythmbox.Player",
                                        "playingChanged");

But I'm not sure what the type should be. boolean? I've tried with a
"boolean" type but that's throwing up a compilation error, not in the
creation of my match but when I try and addSignalHandler which is
protected so I obviously can't call the method with the DBusMatchRule.

I need to use the method:

addSigHandler(Class<T> type, String source, DBusSigHandler<T> handler)

Once again I'm not sure of the type that this call is looking for, Boolean?

Any assistance would be great. I've looked through the API and dbus-java
documentation but I can't see anything that helps.


_______________________________________________
dbus mailing list
dbus@...
http://lists.freedesktop.org/mailman/listinfo/dbus

Re: Java-dbus First steps

by Will Thompson-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 29/10/09 12:41, Arigead wrote:
> BusMatchRule match = new DBusMatchRule("type",
> "org.gnome.Rhythmbox.Player",
> "playingChanged");
>
> But I'm not sure what the type should be. boolean?

"signal".

See
<http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules>
for the definition of match rules at the D-Bus level.

Regards,
--
Will
_______________________________________________
dbus mailing list
dbus@...
http://lists.freedesktop.org/mailman/listinfo/dbus

Re: Java-dbus First steps

by Matthew Johnson-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


_______________________________________________
dbus mailing list
dbus@...
http://lists.freedesktop.org/mailman/listinfo/dbus

signature.asc (852 bytes) Download Attachment

Re: Java-dbus First steps

by Arigead :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

_______________________________________________
dbus mailing list
dbus@...
http://lists.freedesktop.org/mailman/listinfo/dbus