hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
185 stars 73 forks source link

Help request for broadcast signals #111

Closed simpandev closed 3 years ago

simpandev commented 4 years ago

Hello everyone! I'm looking for help.

I couldn't be able to retrieve a broadcast signal because of an IllegalArgumentException, it says "wrong number of arguments" (I read it using dbus-monitor). I'm using dbus-java 3.2.1.

Here's what i got:

signal time=1595842224.038214 sender=:1.409 -> destination=(null destination) serial=5 path=/it/simonepandolfi/emitter; interface=it.simonepandolfi.test_dbus.Signal; member=MySignal
error time=1595842224.040652 sender=:1.408 -> destination=:1.409 error_name=org.freedesktop.dbus.exceptions.DBusExecutionException reply_serial=5
   string "Error handling signal it.simonepandolfi.test_dbus.Signal.MySignal: java.lang.IllegalArgumentException: wrong number of arguments"

May anyone have a suggestion? Or provide an example? This is the repo of my test app https://github.com/simongsr/test_dbus

Thanks in advance.

hypfvieh commented 4 years ago

I would suggest you use the simpler signature of addSigHandler instead of providing custom match rules. Also you should add a Thread.sleep in server part to wait for a client connection and close the DBusConnection instance when the sleep time is over. Otherwise the application would leak connections and will never close properly.

Main.java:

public class Main {

    public static final String BUS_NAME = "it.simonepandolfi.test_dbus";
    public static final String OBJECT_PATH = "/it/simonepandolfi/test_dbus";
    public static final String OBJECT_PATH_EMITTER = "/it/simonepandolfi/emitter";

    public static void main(String[] args) throws DBusException {

        try (DBusConnection connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION)) {
            System.out.println(connection.getAddress());
            System.out.println(connection.getUniqueName());
            if (args.length > 0 && "server".equalsIgnoreCase(args[0])) {
                System.out.println("Starting server...");
                try {
                    connection.requestBusName(BUS_NAME);
                } catch (DBusException e) {
//                    e.printStackTrace();
                }

                MyInterface myInterface = new MyObject();
                connection.exportObject(OBJECT_PATH, myInterface);

                connection.addSigHandler(MySignal.class, new SignalHandler());
                Thread.sleep(10000L); // keep the server alive for 10 seconds, so we can run the client for testing
            } else {
                System.out.println("Starting client...");

                MyInterface myInterface = connection.getRemoteObject(BUS_NAME, OBJECT_PATH, MyInterface.class);
                String greeting = myInterface.greet("Simon");
                System.out.println("--> " + greeting);

                connection.sendMessage(new Signal.MySignal(OBJECT_PATH, "hello world"));
            }
        } catch (IOException | InterruptedException _ex) {
            _ex.printStackTrace();
        }
    }

Signal.java:

public interface Signal extends DBusInterface {

    public class MySignal extends DBusSignal {
        private final String message;

        public MySignal(String _path, String _message) throws DBusException {
            super(_path, _message);
            message = _message;
        }

        public String getMessage() {
            return message;
        }
    }
}

SignalHandler.java:

public class SignalHandler implements DBusSigHandler<Signal.MySignal> {

    @Override
    public void handle(Signal.MySignal mySignal) {
        System.out.println("SIGNAL says: " + mySignal.getMessage());
    }
}