Skip to content

Commit

Permalink
A few bug fixes and requested changes.
Browse files Browse the repository at this point in the history
 * Enums (and other non-primitive types?) not working.
 * GetAll not working
 * Removed all usages of `var`
  • Loading branch information
brett-smith committed Sep 30, 2023
1 parent f2ae5a8 commit 42cf75d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ public Object invoke(Object _proxy, Method _method, Object[] _args) throws Throw
} else if (_method.getName().equals("toString")) {
return remote.toString();
} else if (_method.getAnnotation(DBusProperty.class) != null) {
var name = DBusNamingUtil.getPropertyName(_method);
var access = PropertyRef.accessForMethod(_method);
String name = DBusNamingUtil.getPropertyName(_method);
Access access = PropertyRef.accessForMethod(_method);
if (access == Access.READ) {
var propGetMethod = Properties.class.getMethod("Get", String.class, String.class);
return executeRemoteMethod(remote, propGetMethod, conn, CALL_TYPE_SYNC, null, DBusNamingUtil.getInterfaceName(_method.getDeclaringClass()), name);
Method propGetMethod = Properties.class.getMethod("Get", String.class, String.class);
return executeRemoteMethod(remote, propGetMethod,
new Type[] {_method.getGenericReturnType()}, conn, CALL_TYPE_SYNC, null, DBusNamingUtil.getInterfaceName(_method.getDeclaringClass()), name);
} else {
var propSetMethod = Properties.class.getMethod("Set", String.class, String.class, Object.class);
return executeRemoteMethod(remote, propSetMethod, conn, CALL_TYPE_SYNC, null, DBusNamingUtil.getInterfaceName(_method.getDeclaringClass()), name, _args[0]);
Method propSetMethod = Properties.class.getMethod("Set", String.class, String.class, Object.class);
return executeRemoteMethod(remote, propSetMethod,
new Type[] {_method.getGenericReturnType()}, conn, CALL_TYPE_SYNC, null, DBusNamingUtil.getInterfaceName(_method.getDeclaringClass()), name, _args[0]);
}
}

Expand All @@ -106,6 +108,10 @@ public Object invoke(Object _proxy, Method _method, Object[] _args) throws Throw
// CHECKSTYLE:ON

public static Object convertRV(String _sig, Object[] _rp, Method _m, AbstractConnection _conn) throws DBusException {
return convertRV(_sig, _rp, new Type[] {_m.getGenericReturnType()}, _m, _conn);
}

public static Object convertRV(String _sig, Object[] _rp, Type[] _types, Method _m, AbstractConnection _conn) throws DBusException {
Class<? extends Object> c = _m.getReturnType();
Object[] rp = _rp;
if (rp == null) {
Expand All @@ -119,12 +125,10 @@ public static Object convertRV(String _sig, Object[] _rp, Method _m, AbstractCon
LoggingHelper.logIf(LOGGER.isTraceEnabled(), () -> LOGGER.trace("Converting return parameters from {} to type {}",
Arrays.deepToString(_rp), _m.getGenericReturnType()));

rp = Marshalling.deSerializeParameters(rp, new Type[] {
_m.getGenericReturnType()
}, _conn);
rp = Marshalling.deSerializeParameters(rp, _types, _conn);
} catch (Exception _ex) {
LOGGER.debug("Wrong return type.", _ex);
throw new DBusException(String.format("Wrong return type (failed to de-serialize correct types: %s )", _ex.getMessage()));
throw new DBusException(String.format("Wrong return type (failed to de-serialize correct types: %s )", _ex.getMessage()), _ex);
}
}

Expand Down Expand Up @@ -154,7 +158,13 @@ public static Object convertRV(String _sig, Object[] _rp, Method _m, AbstractCon
}
}

public static Object executeRemoteMethod(RemoteObject _ro, Method _m, AbstractConnection _conn, int _syncmethod, CallbackHandler<?> _callback, Object... _args) throws DBusException {
public static Object executeRemoteMethod(final RemoteObject _ro, final Method _m,
final AbstractConnection _conn, final int _syncmethod, final CallbackHandler<?> _callback, Object... _args) throws DBusException {
return executeRemoteMethod(_ro, _m, new Type[] {_m.getGenericReturnType()}, _conn, _syncmethod, _callback, _args);
}

public static Object executeRemoteMethod(final RemoteObject _ro, final Method _m,
final Type[] _types, final AbstractConnection _conn, final int _syncmethod, final CallbackHandler<?> _callback, Object... _args) throws DBusException {
Type[] ts = _m.getGenericParameterTypes();
String sig = null;
Object[] args = _args;
Expand Down Expand Up @@ -221,10 +231,10 @@ public static Object executeRemoteMethod(RemoteObject _ro, Method _m, AbstractCo
}

try {
return convertRV(reply.getSig(), reply.getParameters(), _m, _conn);
return convertRV(reply.getSig(), reply.getParameters(), _types, _m, _conn);
} catch (DBusException _ex) {
LOGGER.debug("", _ex);
throw new DBusExecutionException(_ex.getMessage());
throw new DBusExecutionException(_ex.getMessage(), _ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.*;
import org.freedesktop.dbus.interfaces.*;
import org.freedesktop.dbus.interfaces.Properties;
import org.freedesktop.dbus.messages.*;
import org.freedesktop.dbus.types.Variant;
import org.freedesktop.dbus.utils.LoggingHelper;
Expand Down Expand Up @@ -801,17 +802,17 @@ private void handleMessage(final MethodCall _methodCall) throws DBusException {
}
}

var params = _methodCall.getParameters();
Object[] params = _methodCall.getParameters();
if (params.length == 2 && params[0] instanceof String
&& params[1] instanceof String
&& params[0].equals(_methodCall.getInterface())
&& _methodCall.getName().equals("Get")) {

// 'Get' This MIGHT be a property reference
var propertyRef = new PropertyRef((String) params[1], null, Access.READ);
var propMeth = exportObject.getPropertyMethods().get(propertyRef);
PropertyRef propertyRef = new PropertyRef((String) params[1], null, Access.READ);
Method propMeth = exportObject.getPropertyMethods().get(propertyRef);
if (propMeth != null) {
// This IS a property reference
var object = exportObject.getObject().get();
Object object = exportObject.getObject().get();

receivingService.execMethodCallHandler(() -> {
_methodCall.setArgs(new Object[0]);
Expand All @@ -823,17 +824,16 @@ private void handleMessage(final MethodCall _methodCall) throws DBusException {
} else if (params.length == 3
&& params[0] instanceof String
&& params[1] instanceof String
&& params[0].equals(_methodCall.getInterface())
&& _methodCall.getName().equals("Set")) {
// 'Set' This MIGHT be a property reference

var propertyRef = new PropertyRef((String) params[1], null, Access.WRITE);
var propMeth = exportObject.getPropertyMethods().get(propertyRef);
PropertyRef propertyRef = new PropertyRef((String) params[1], null, Access.WRITE);
Method propMeth = exportObject.getPropertyMethods().get(propertyRef);
if (propMeth != null) {
// This IS a property reference
var object = exportObject.getObject().get();
var type = PropertyRef.typeForMethod(propMeth);
var val = params[2] instanceof Variant ? ((Variant<?>) params[2]).getValue() : params[2];
Object object = exportObject.getObject().get();
Class<?> type = PropertyRef.typeForMethod(propMeth);
Object val = params[2] instanceof Variant ? ((Variant<?>) params[2]).getValue() : params[2];
receivingService.execMethodCallHandler(() -> {
try {
_methodCall.setArgs(Marshalling.deSerializeParameters(new Object[] {val}, new Type[] {type}, this));
Expand All @@ -848,56 +848,69 @@ private void handleMessage(final MethodCall _methodCall) throws DBusException {
return;
}
} else if (params.length == 1 && params[0] instanceof String
&& params[0].equals(_methodCall.getInterface())
&& _methodCall.getName().equals("GetAll")) {
// 'GetAll'
var allPropertyMethods = exportObject.getPropertyMethods().entrySet();
var object = exportObject.getObject().get();

if (meth == null && object instanceof DBusProperties) {
/* If the object implements DBusProperties as well, we
* need the actual GetAll method as well
*/
meth = exportObject.getMethods().get(new MethodTuple(_methodCall.getName(), _methodCall.getSig()));
if (null == meth) {
sendMessage(new Error(_methodCall, new UnknownMethod(String.format(
"The method `%s.%s' does not exist on this object.", _methodCall.getInterface(), _methodCall.getName()))));
return;
}
}
var originalMeth = meth;

receivingService.execMethodCallHandler(() -> {
var resultMap = new HashMap<String, Variant<?>>();
for (var propEn : allPropertyMethods) {
var propMeth = propEn.getValue();
Set<Entry<PropertyRef, Method>> allPropertyMethods = exportObject.getPropertyMethods().entrySet();
/* If there are no property methods on this object, just process as normal */
if (!allPropertyMethods.isEmpty()) {
Object object = exportObject.getObject().get();

if (meth == null && object instanceof DBusProperties) {
meth = exportObject.getMethods().get(new MethodTuple(_methodCall.getName(), _methodCall.getSig()));
if (null == meth) {
sendMessage(new Error(_methodCall, new UnknownMethod(String.format(
"The method `%s.%s' does not exist on this object.", _methodCall.getInterface(), _methodCall.getName()))));
return;
}
} else if (meth == null) {
try {
_methodCall.setArgs(new Object[0]);
var val = invokeMethod(_methodCall, propMeth, object);
resultMap.put(propEn.getKey().getName(), new Variant<>(val));
} catch (Throwable _ex) {
meth = Properties.class.getDeclaredMethod("GetAll", String.class);
} catch (NoSuchMethodException | SecurityException _ex) {
logger.debug("", _ex);
handleException(_methodCall, new UnknownMethod("Failure in de-serializing message: " + _ex));
return;
handleException(_methodCall,
new DBusExecutionException(String.format("Error Executing Method %s.%s: %s",
_methodCall.getInterface(), _methodCall.getName(), _ex.getMessage())));
}
}

if (object instanceof DBusProperties) {
resultMap.putAll((Map<? extends String, ? extends Variant<?>>) setupAndInvoke(_methodCall, originalMeth, object, true));
}
Method originalMeth = meth;

try {
invokedMethodReply(_methodCall, originalMeth, resultMap);
} catch (DBusExecutionException _ex) {
logger.debug("", _ex);
handleException(_methodCall, _ex);
} catch (Throwable _ex) {
logger.debug("", _ex);
handleException(_methodCall,
receivingService.execMethodCallHandler(() -> {
Map<String, Variant<?>> resultMap = new HashMap<>();
for (Entry<PropertyRef, Method> propEn : allPropertyMethods) {
Method propMeth = propEn.getValue();
if (propEn.getKey().getAccess() == Access.READ) {
try {
_methodCall.setArgs(new Object[0]);
Object val = invokeMethod(_methodCall, propMeth, object);
resultMap.put(propEn.getKey().getName(), new Variant<>(val));
} catch (Throwable _ex) {
logger.debug("", _ex);
handleException(_methodCall, new UnknownMethod("Failure in de-serializing message: " + _ex));
return;
}
}
}

if (object instanceof DBusProperties) {
resultMap.putAll((Map<? extends String, ? extends Variant<?>>) setupAndInvoke(_methodCall, originalMeth, object, true));
}

try {
invokedMethodReply(_methodCall, originalMeth, resultMap);
} catch (DBusExecutionException _ex) {
logger.debug("", _ex);
handleException(_methodCall, _ex);
} catch (Throwable _ex) {
logger.debug("", _ex);
handleException(_methodCall,
new DBusExecutionException(String.format("Error Executing Method %s.%s: %s",
_methodCall.getInterface(), _methodCall.getName(), _ex.getMessage())));
}
});
}
});

return;
}
}

if (meth == null) {
Expand All @@ -923,7 +936,7 @@ private void handleMessage(final MethodCall _methodCall) throws DBusException {

private void queueInvokeMethod(final MethodCall _methodCall, Method _meth, final Object _ob) {
logger.trace("Adding Runnable for method {}", _meth);
var noReply = 1 == (_methodCall.getFlags() & Message.Flags.NO_REPLY_EXPECTED);
boolean noReply = 1 == (_methodCall.getFlags() & Message.Flags.NO_REPLY_EXPECTED);
receivingService.execMethodCallHandler(() -> {
setupAndInvoke(_methodCall, _meth, _ob, noReply);
});
Expand All @@ -933,11 +946,11 @@ private Object setupAndInvoke(final MethodCall _methodCall, Method _meth, final
logger.debug("Running method {} for remote call", _meth);
try {
Type[] ts = _meth.getGenericParameterTypes();
var params2 = _methodCall.getParameters();
Object[] params2 = _methodCall.getParameters();
_methodCall.setArgs(Marshalling.deSerializeParameters(params2, ts, this));
LoggingHelper.logIf(logger.isTraceEnabled(), () -> {
try {
var params3 = _methodCall.getParameters();
Object[] params3 = _methodCall.getParameters();
logger.trace("Deserialised {} to types {}", Arrays.deepToString(params3), Arrays.deepToString(ts));
} catch (Exception _ex) {
logger.trace("Error getting method call parameters", _ex);
Expand All @@ -954,7 +967,7 @@ private Object setupAndInvoke(final MethodCall _methodCall, Method _meth, final

private Object invokeMethodAndReply(final MethodCall _methodCall, final Method _me, final Object _ob, final boolean _noreply) {
try {
var result = invokeMethod(_methodCall, _me, _ob);
Object result = invokeMethod(_methodCall, _me, _ob);
if (!_noreply) {
invokedMethodReply(_methodCall, _me, result);
}
Expand Down Expand Up @@ -994,19 +1007,19 @@ private void invokedMethodReply(final MethodCall _methodCall, final Method _me,

private Object invokeMethod(final MethodCall _methodCall, final Method _me, final Object _ob)
throws DBusException, IllegalAccessException, Throwable {
var info = new DBusCallInfo(_methodCall);
DBusCallInfo info = new DBusCallInfo(_methodCall);
INFOMAP.put(Thread.currentThread(), info);
try {
LoggingHelper.logIf(logger.isTraceEnabled(), () -> {
try {
var params4 = _methodCall.getParameters();
Object[] params4 = _methodCall.getParameters();
logger.trace("Invoking Method: {} on {} with parameters {}", _me, _ob, Arrays.deepToString(params4));
} catch (DBusException _ex) {
logger.trace("Error getting parameters from method call", _ex);
}
});

var params5 = _methodCall.getParameters();
Object[] params5 = _methodCall.getParameters();
return _me.invoke(_ob, params5);
} catch (InvocationTargetException _ex) {
logger.debug(_ex.getMessage(), _ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public DBusExecutionException(String _message) {
super(_message);
}

/**
* Create an exception with the specified message
* @param _message message
* @param _cause cause
*/
public DBusExecutionException(String _message, Throwable _cause) {
super(_message, _cause);
}

public void setType(String _type) {
this.type = _type;
}
Expand Down
Loading

0 comments on commit 42cf75d

Please sign in to comment.