Closed simpandev closed 3 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());
}
}
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:
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.