dbus java bindings name registery and introspection problem

ozgur cagdas ocagdas at yahoo.com
Thu Feb 11 05:30:19 PST 2010


Thanks for the patience with newbie questions and sorry for not seeing the information which is already in the documentation. DBusConnection class holds necessary methods I needed. On the other hand, there is obviously lack of working java dbus examples on the internet, at least I couldn't find any, so I'm posting my corrected mini reference application for anyone who might need it.


/*java dbus binding example*/
/*interface definition*/
package com.mycompany.wrapper_interface;

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.exceptions.DBusException;

@DBusInterfaceName("com.mycompany.wrapper_interface.DvbDemuxInterface")
public interface DvbDemuxInterface extends DBusInterface {

    public class DvbDemuxDbusSigNo1 extends DBusSignal {

        public DvbDemuxDbusSigNo1(String path, String name) throws DBusException {
            super(path, name);
        }
    }

    @org.freedesktop.DBus.Description("Print something for me")
    @org.freedesktop.DBus.Method.NoReply
    public void printSomethingForMe();
}

/*actual implementation of the interface*/
package com.mycompany.wrapper;

import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.*;

import com.mycompany.wrapper_interface.*;

public class DvbDemux implements DvbDemuxInterface{

    public static void main(String[] args) {
        System.out.println("Creating DvbDemuxConnection");

        DBusConnection conn = null;
        try {
            conn = DBusConnection.getConnection(DBusConnection.SESSION);
        } catch (DBusException De) {
            System.exit(1);
        }

        try {
            conn.requestBusName("com.mycompany.wrapper_interface");
        } catch (DBusException e1) {
            e1.printStackTrace();
        }

        try {
            conn.exportObject("/DvbDemuxInterface", new DvbDemux());
        } catch (DBusException e1) {
            e1.printStackTrace();
        }

        int count = 10;
        while(count != 0) {
            try {
                try {
                        conn.sendSignal(new DvbDemuxInterface.DvbDemuxDbusSigNo1(
                                        "/com/mycompany/wrapper_interface",
                                        "just_a_param"
                                        ));
                    } catch (DBusException De) {
                        System.exit(1);
                    }
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count--;
        }

        try {
            conn.releaseBusName("com.mycompany.wrapper_interface");
        } catch (DBusException e) {
            e.printStackTrace();
        }

        System.out.println("Exiting DvbDemuxConnection");
    }

    public boolean isRemote() {
        return false;
    }

    @Override
    public void printSomethingForMe() {
        System.out.println("Here is a print for you!!!\n");
    }

}

/*signal handler implementation together with simple method call on remote object*/
package com.mycompany.dvb;

import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import com.mycompany.wrapper_interface.*;

public class TsHandler implements DBusSigHandler<DvbDemuxInterface.DvbDemuxDbusSigNo1>
{
    private static DBusConnection conn = null;

    public void handle(DvbDemuxInterface.DvbDemuxDbusSigNo1 sig)
    {
        System.out.println("TsHanlder has received the signal!!!\n");

        DvbDemuxInterface remoteInterface = null;

        if(conn == null)
        {
            try {
                conn = DBusConnection.getConnection(DBusConnection.SESSION);
            } catch (DBusException DBe) {
                System.out.println("Could not connect to bus");
                System.exit(1);
            }
        }

        try {
            remoteInterface = (DvbDemuxInterface)conn.getRemoteObject("com.mycompany.wrapper_interface",
                                                                        "/DvbDemuxInterface");
        } catch (DBusException e) {
            e.printStackTrace();
        }

        if(remoteInterface != null) {
            remoteInterface.printSomethingForMe();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println("Creating TsHandlerConnection");
        TsHandler handlerObject = null;

        try {
            conn = DBusConnection.getConnection(DBusConnection.SESSION);
        } catch (DBusException DBe) {
            System.out.println("Could not connect to bus");
            System.exit(1);
        }

        try {
            conn.addSigHandler(DvbDemuxInterface.DvbDemuxDbusSigNo1.class,
                    handlerObject = new TsHandler());
            } catch (DBusException DBe) {
            conn.disconnect();
            System.exit(1);
        }

        int count = 10;
        while(count != 0) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count--;
        }

        if(handlerObject != null) {
            try {
                conn.removeSigHandler(DvbDemuxInterface.DvbDemuxDbusSigNo1.class, handlerObject);
            } catch (DBusException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Exiting TsHandlerConnection");
    }
}


      


More information about the dbus mailing list