本文介绍了如何使用DBus中的现有服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用dbus命令,例如dbus-send等.但是我没有获得如何有效使用dbus api编写示例应用程序的方法.

请问有人可以告诉我如何从dbus接收数据.我没有得到如何在dbus中使用现有服务,例如org.freedesktop.NetworkManager

I am able to use the dbus commands like dbus-send and etc. But i am not getting how to use the api's of dbus efficiently to write sample applications.

Can any one please tell me how to receive the data from the dbus. I am not getting how to use the existing services in dbus like org.freedesktop.NetworkManager

请告诉我访问和使用dbus服务的正确方法.请张贴一些示例示例,并向我建议使用服务时必须遵循的规则.

Please tell me the proper way to access and use the services of dbus.Please post some sample examples and also suggest me what are the rules we have to follow while using a service.

我正在寻找1)如何将我们自己的服务添加到系统/会话总线.同时,如何接收该服务. 2)如何使用现有服务,例如org.freedesktop.NetworkManager.GetDevices.我想要实现代码

i am looking for 1)How to add our own service to the system / session Bus.At the same time,how to receive that service. 2) How to use the existing services just like org.freedesktop.NetworkManager.GetDevices .I want implementation code

推荐答案

您的问题很开放,因为您没有对例如您计划在示例应用程序中使用什么语言和绑定(如果有).

Your question is quite open as you do not state any requirements on e.g. what language and binding (if any) you plan use in the sample application.

当您询问D-Bus的API时,这可能意味着不同的事情,具体取决于您打算编写代码的抽象级别.我将假设您打算使用与D-Bus的低级API集成的绑定,即您将使用的API比低级D-Bus C API的抽象级别更高.

When you ask about the APIs of D-Bus, that might mean different things depending on what level of abstraction you intend to write your code. I will make the assumption that you intend to use a binding that integrates with the low level API of D-Bus, i.e. the API you will use is at a higher level of abstraction than the low level D-Bus C API.

在python中,使用可用的绑定dbus-python之一,一个非常简单的服务可能看起来像这样:

In python, using one of the available bindings dbus-python, a very simple service might look like this:

import gobject
import dbus
import dbus.service

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)


OPATH = "/temp/Test"
IFACE = "temp.Test"
BUS_NAME = "temp.Test"


class MyService(dbus.service.Object):
    def __init__(self):
        bus = dbus.SessionBus()
        bus.request_name(BUS_NAME, dbus.bus.NAME_FLAG_REPLACE_EXISTING)
        bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, OPATH)

    @dbus.service.method(dbus_interface=IFACE, in_signature="s")
    def Echo(self, message):
        return "received: " + message


if __name__ == "__main__":
    my_service = MyService()
    loop = gobject.MainLoop()
    loop.run()

然后可以使用dbus-send调用Echo方法,如下所示:

The Echo method could then be called with dbus-send like this:

dbus-send --session --print-reply --dest=temp.Test /temp/Test temp.Test.Echo string:"hello"

继续python示例,上述服务的客户端可能看起来像这样:

Continuing the python example, a client to the above service might look like this:

import dbus

bus = dbus.SessionBus()
the_service = bus.get_object("temp.Test", "/temp/Test")
service_interface = dbus.Interface(service_object, dbus_interface="temp.Test")

print service_interface.Echo("hello")

有关dbus-python的详细信息,您可以查看教程.

For more information about the specifics of dbus-python you can check out this tutorial.

扩展客户端示例,调用org.freedesktop.NetworkManager.GetDevices的一种方法是:

Extending the client example, one way of calling org.freedesktop.NetworkManager.GetDevices is:

import dbus

bus = dbus.SystemBus()
service_object = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
service_interface = dbus.Interface(service_object, dbus_interface="org.freedesktop.NetworkManager")

print service_interface.GetDevices()

因此,通常,您需要找出与所选语言对应的绑定,然后找出要与之交互的任何特定服务的API.有关如何与服务进行交互的任何特定规则都应记录为API或设计文档等的一部分.

So in general you need to find out what bindings exist for your language of choice, then find out about the API of any specific services you want to interact with. Any particular rules about how to interact with services should be documented as part of the API or design documents and so on.

在客户端,您通常可以选择执行同步或异步调用(如果绑定和语言支持),这将对您的设计产生影响.

On the client side you will often have the option to do synchronous or asynchronous calls (if supported by the binding and language) and that will have an impact on your design.

这篇关于如何使用DBus中的现有服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:35