Recieving DBUS messages in Java

Paul Sztajer Paul.Sztajer at nicta.com.au
Tue Feb 24 18:17:28 PST 2009


Hi Matt,

Thanks, that helped a lot. I've got it working and understand what's going on a lot better now.

Paul.
________________________________________
From: Matthew Johnson [dbus at matthew.ath.cx]
Sent: Friday, 20 February 2009 7:48 PM
To: Paul Sztajer
Cc: dbus at lists.freedesktop.org
Subject: Re: Recieving DBUS messages in Java

On Fri Feb 20 11:08, Paul Sztajer wrote:
> Hi all,
>
> I'm currently building a compiz plugin that needs two-way
> communication with a java application, and thus I'm trying to use dbus
> to do so. I've gotten sending from Java and recieving in compiz
> working, but I'm having issues going the other way.

What messages are you trying to send in each direction? AFAICT, you have
glib trying to call the method "test" on the Java, and Java listening
for the YellowSignal.connect/disconnect signals, but not exporting any
methods.

A couple of comments:

> 15            public static void main(String[] args){
> 16                    /**
> 17                     * First, send to compiz (this works, so I won't put the code in here)
> 18                     */
> ...
> 27
> 28                    /**
> 29                     * Now wait to recieve
> 30                     */
> 31                    DBusConnection conn = null;
> 32                    try {
> 33                            conn = DBusConnection
> 34                            .getConnection(DBusConnection.SYSTEM);
> 41                            conn.addSigHandler(YellowSignal.Connect.class,
> 42                                            new Yellower());

The normal pattern for this would be to add all the listeners and export
objects straight away. Also, presumably you don't have a separate
connection for sending and receiving? (although, actually it does give
you back the same object if you call it twice anyway).

> 54            public void handle(DBusSignal s) {
> 55                    if (s instanceof YellowSignal.Connect)
> 56                            System.out.println("Got a connect for "
> 57                                            +((YellowSignal.Connect) s).address);

This all looks like it should work for receiving signals. You should be
able to test it with:

dbus-send --system --type=signal /foo nicta.yellower.compizfusion.dbus.YellowSignal.Connect string:"29 Acacia Road"

> Now I realise that I haven't actually got a "test" method in the java
> code to call, but I'm not all that sure where to place it etc. I get
> the distinct impression that I'm missing something, or that I've
> gotten confused somewhere. Any help I could get would be much
> appreciated.

You need to declare in in an interface which extends DBusInterface and
then implement that on an object which you create and pass to
conn.exportObject. It's all in the docs, if a little terse.

So for your test method, it might be something like:

package nicta.yellower.compizfusion.dbus;
public interface TestInterface extends DBusInterface
{
   public void test();
}

...
public class TestImpl implements TestInterface
{
   public void test() { /* do someting */ }
}

...

conn = DBusConnection.getConnection(DBusConnection.SYSTEM);
conn.exportObject("/test", new TestImpl);

This would then allow someone to call:

dbus-send --type=method_call --dest=<your address> /test nicta.yellower.compizfusion.dbus.TestInterface.test

Lemme know if you have further problems.

Thanks,
Matt

--
www.matthew.ath.cx
D-Bus Java


More information about the dbus mailing list