Recieving DBUS messages in Java

Paul Sztajer Paul.Sztajer at nicta.com.au
Thu Feb 19 16:08:35 PST 2009


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.

I've got code that should be sending messages from compiz, but am having issues working out how to get the dbus java library to listen for these at the other end.

So my code on the C side is as follows:

19 	static void yellowSendDbusMessage(CompDisplay *d)
20 	{
21 	        GError *error;
22 	        char **name_list;
23 	        char **name_list_ptr;
24 	        YELLOWISER_DISPLAY(d);
25 	
26 	    error = NULL;
27 	    if (!dbus_g_proxy_call (yd->proxy, "test", &error, G_TYPE_INVALID,
28 	                            G_TYPE_INVALID))
29 	    {
30 	        if (error->domain == DBUS_GERROR &&
31 	                error->code == DBUS_GERROR_REMOTE_EXCEPTION)
32 	            compLogMessage("yellowiser-dbus", CompLogLevelError,
33 	                "Caught remote method execption %s: %s",
34 	                dbus_g_error_get_name (error),
35 	                error->message);
36 	        else
37 	                compLogMessage("yellowiser-dbus", CompLogLevelError,
38 	                    "%s", error->message);
39 	
40 	        return;
41 	       
42 	    }
43 	
44 	    return;
45 	}


On the java side, I've tried to keep it as simple as possible in an attempt to make it work, which has lead to the following code:

1 	package nicta.yellower.compizfusion.dbus;
...
13 	public class Yellower implements DBusSigHandler {
14 	
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); 
35 	                } catch (DBusException DBe) { 
36 	                        System.out.println("Could not connect to bus"); 
37 	                        System.exit(1); 
38 	                } 
39 	
40 	                try { 
41 	                        conn.addSigHandler(YellowSignal.Connect.class, 
42 	                                        new Yellower()); 
43 	                        conn.addSigHandler(YellowSignal.Disconnect.class, 
44 	                                        new Yellower());   
45 	                } catch (DBusException DBe) { 
46 	                        conn.disconnect(); 
47 	                        System.exit(1); 
48 	                } 
49 	
50 	                return;
51 	        }
52 	
53 	        @Override
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); 
58 	                else if (s instanceof YellowSignal.Disconnect) 
59 	                        System.out.println("Got a disconnect for " 
60 	                                        +((YellowSignal.Disconnect) s).address); 
61 	        }
62 	}

1 	package nicta.yellower.compizfusion.dbus;
...
7 	public interface YellowSignal extends DBusInterface {
8 	
9 	        public static class Connect extends DBusSignal 
10 	        { 
11 	                public final String address; 
12 	                public Connect(String path, String address) 
13 	                throws DBusException 
14 	                { 
15 	                        super(path, address); 
16 	                        this.address = address; 
17 	                } 
18 	        } 
19 	        public static class Disconnect extends DBusSignal 
20 	        { 
21 	                public final String address; 
22 	                public Disconnect(String path, String address) 
23 	                throws DBusException 
24 	                { 
25 	                        super(path, address); 
26 	                        this.address = address; 
27 	                } 
28 	        } 
29 	}

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.

Thanks,

Paul.


More information about the dbus mailing list