如何在没有桌面环境的CentOS中使用dbus

时间:2019-04-04 04:38:21

标签: x11 dbus pygobject gobject gdbus

我的系统是没有gui的centos。我有一个服务器应用程序,它在会话dbus中“侦听”方法调用。它显然工作正常。我已经安装好pydbuspython3-gobject,也有dbus-launch在工作。这是服务器应用程序:

from pydbus import SessionBus
from gi.repository import GLib
import time

# Variables / Constants / Instantiation...
bus = SessionBus()
BUS = "org.mybus.demo.test"
loop = GLib.MainLoop()
message_count = 0

class DBusService_XML():
    """
    DBus Service XML Definition.
    type = "i" for integer, "s" for string, "d" for double, "as" list of string data.
    """
    dbus = """
    <node>
        <interface name="{}">
            <method name='greeting'>
                <arg type="s" name="input" direction="in">
                </arg>
                <arg type="s" name="output" direction="out">
                </arg>
            </method>
        </interface>
    </node>
    """.format(BUS)

    def greeting(self, clientName):
        "Receive and send arg"
        print("{} is asking for name".format(clientName))
        return "Hello {}, Im Kyle".format(clientName)

if __name__ == "__main__":
    bus.publish(BUS, DBusService_XML())
    loop.run()

现在为了从另一个终端(同一用户)调用该服务器方法,我尝试使用失败的客户端应用程序,然后尝试了gdbus应用程序,该应用程序失败并出现以下相同错误:

# dbus-launch gdbus call --session --dest org.mybus.demo.test --object-path /org/mybus/demo/test --method org.mybus.demo.test.greeting "Julia"
Error: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.mybus.demo.test was not provided by any .service files

从具有桌面环境的另一台计算机上,一切正常。我四处搜寻,但找不到在这种情况下使用dbus的方法。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

除非从客户端调用该方法时服务已经运行,否则您需要为其启用服务激活,这涉及写入org.mybus.demo.test.service文件并将其放入/usr/share/dbus-1/services。参见the specification。可能看起来像这样:

[D-BUS Service]
Name=org.mybus.demo.test
Exec=/path/to/your/application.py