GDBusMethod
public GDBusMethod(java.lang.String name,
java.lang.String[][] inputArgs,
java.lang.String[][] outputArgs,
java.util.function.Function<java.lang.Object[],java.lang.Object[]> userFunction)
Create a method that GDBus will listen to.
Parameters:
name
- of the method. It will be part of 'org.eclipse.swt.NAME'
inputArgs
- 2D array pair of Strings in the format of: (DBUS_TYPE_*, argument_name).
Where argument_name is only so that it's seen by command line by user.
outputArgs
- Same as inputArgs, but for returning values.
userFunction
- A Function, that you would like to run when the user calls the method over gdbus.
Note, input argument(s) are provided as an Object[] array. You need to cast items manually.
Output must always be an Object[] array or null. (E.g Object[] with only 1 element in it).
Here are examples that you can base your methods off of:
// Multiple input types and multiple output types.
// From command line, it can be called like this:
// gdbus call --session --dest org.eclipse.swt --object-path /org/eclipse/swt --method org.eclipse.swt.typeTest true "world" 1234
// The call will return a tuple (struct) like: (true, 'world', 5678)
GDBusMethod typeTest = new GDBusMethod(
"typeTest",
new String [][] {{OS.DBUS_TYPE_BOOLEAN, "boolean Test Val"}, {OS.DBUS_TYPE_STRING, "string Test Val"}, {OS.DBUS_TYPE_INT32, "int Test Val"}},
new String [][] {{OS.DBUS_TYPE_BOOLEAN, "boolean Response"}, {OS.DBUS_TYPE_STRING, "string Test Response"}, {OS.DBUS_TYPE_INT32, "int Test Response"}},
(args) -> {
System.out.println(args[0] + " " + args[1] + " " + args[2]); // A
return new Object[] {Boolean.valueOf(true), "world", Integer.valueOf(5678)} ;
});
// Single input and single output. Observe input is an array with one item. Output is an array with one item.
// It can be called from cmd via:
// gdbus call --session --dest org.eclipse.swt --object-path /org/eclipse/swt --method org.eclipse.swt.singleValueTest 123
// The return is a tuple with one element: (456,)
GDBusMethod singleValTest = new GDBusMethod(
"singleValueTest",
new String[][] {{OS.DBUS_TYPE_INT32, "int val"}},
new String[][] {{OS.DBUS_TYPE_INT32, "int ret"}},
(arg) -> {
System.out.println("Input int: " + arg[0]);
return new Object[] {(Integer) 456};
});
// A simple method for clients to check if org.eclipse.swt is up and running. (getting/parsing interface xml is too tedious).
// Reached via: gdbus call --session --dest org.eclipse.swt --object-path /org/eclipse/swt --method org.eclipse.swt.PingResponse
// return is: (true,) #i.e (b)
new GDBusMethod(
"PingResponse",
new String [0][0], // No input args.
new String [][] {{OS.DBUS_TYPE_BOOLEAN, "Ping boolean response"}}, // Single boolean res
(args) -> {
return new Object[] {true}; // You should 'g_variant_unref(result)' on client side.
})