在dbus上导出Python对象并使用Java客户端进行远程方法调用

时间:2018-07-27 19:42:11

标签: java python dbus

我有一个Python脚本,可将对象导出到系统总线:

class PyObj(dbus.service.Object):
    def __init__(self, bus, object_path):
        dbus.service.Object.__init__(self, bus, object_path)

    @dbus.service.method(dbus_interface='com.test.PyObj', out_signature='s')
    def hello(self):
        return "hi"

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    sys_bus = dbus.SystemBus()
    name = dbus.service.BusName('com.test.PyService', sys_bus)
    obj = PyObj(sys_bus, '/com/test')

    mainloop = GLib.MainLoop()
    mainloop.run()

我可以使用命令行功能dbus-send

与服务进行对话
$ dbus-send --system --print-reply --dest=com.test.PyService /com/test com.test.PyObj.hello
method return time=1532719579.243322 sender=:1.10 -> destination=:1.13 serial=4 reply_serial=2
   string "hi"

我也想从Java客户端打个招呼。

public interface MyInterface extends DBusInterface {   
    String hello();
    boolean isRemote();

}

我扩展DbusInterface,然后使用getRemoteObject()来调用远程功能hello()

public class MyService {

    DBusConnection conn;
    String dbusNamespace = "com.test.PyService";
    String dbusMyPath = "/com/test";

    public MyService(DBusConnection conn) {
        this.conn = conn;
    }
    public String hello() {
        MyInterface myInterface = null;
        try {
            myInterface = conn.getRemoteObject(dbusNamespace, dbusMyPath, MyInterface.class);
            String hello = myInterface.hello();
            return hello;
        } catch (DBusException e) {
            e.printStackTrace();
            return null;
        }
    }
}

但是当我尝试使用此Java客户端访问方法hello()时,出现错误

dbus.exceptions.UnknownMethodException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method: hello is not a valid method of interface com.test.MyInterface

另一方面,当我使用相同的public interface MyInterfacepublic class MyService与在不同系统端点上运行的Java中创建的DbusInterface远程对象进行对话时,我的Java客户端成功。这使我认为问题出在python端,但是在documentationexamples中找不到任何可以帮助我发现问题的东西。

0 个答案:

没有答案