Gdbus

2024-09-07 17:08
文章标签 gdbus

本文主要是介绍Gdbus,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  介绍一下基于DBus设计的应用程序的俩种形态及如何使用GDbus来实现。

基于DBus的应用程序可以是使用DBus Daemon的总线型结构,每个DBus的请求通过DBus Daemon转发;或者是点对点的星型结构,Client与Server之间是直接的Peer2Peer的连接。这俩种结构各有优缺点:总线型的结构比较清晰,Server需要维护的连接较少,实际上只有一个与DBus Daemon相连的连接,广播消息可以很容易的发送到各个Client;P2P形式的DBus通信中间因为少了DBus Daemon的中转,因此性能更好,大约提升30%。

基于GLib提供的GBus实现基于以上俩种形态的DBus应用还是非常简单的:

1. 准备工作

1.1 提供一个用于代码生成的XML文件

这份XML数据在GDBus中称为introspection data,用来描述提供服务的GObject的接口名与参数。用于gdbus-codegen可以使用这份XML文件生成在Client与Server侧使用的代码。对于总线型DBus应用和P2P型DBus应用,这份代码是通用的。

1.2 编译生成的代码

生成的代码需要分别链接到俩个进程中:带有Skeleton字样的代码,运行在Server侧;带有Proxy字样的代码,运行在Client侧。

 

gdbus-codegen 自动生成的代码的规则可参考:http://people.freedesktop.org/~david/gio-gdbus-codegen-20110412/gdbus-codegen.html

 

2. 总线型

2.1 Server

2.1.1 提供一个基于Default Context的GLib Mainloop

2.1.2 调用g_bus_own_name在总线上注册这个Server

2.1.3 提供on_name_acquried的回调函数,在回调函数中,创建一个skeleton对象,并调用g_dbus_interface_skeleton_export输出到总线上

2.2 Client

2.2.1 提供一个基于Default Context的GLib Mainloop

2.2.2 调用dbus_proxy_new_sync获取与Server的Skeleton对象相对应的Proxy对象,作为后续DBus方法调用的参数

A.Consider the following D-Bus Introspection XML.

复制代码
<interface name="net.Corp.MyApp.Frobber"><method name="HelloWorld"><arg name="greeting" direction="in" type="s"/><arg name="response" direction="out" type="s"/></method><signal name="Notification"><arg name="icon_blob" type="ay"/><arg name="height" type="i"/><arg name="messages" type="as"/></signal><property name="Verbose" type="b" access="readwrite"/>
</interface>
复制代码


B.在server端

复制代码
static gboolean
on_handle_hello_world (MyAppFrobber           *interface,GDBusMethodInvocation  *invocation,const gchar            *greeting,gpointer                user_data)
{if (g_strcmp0 (greeting, "Boo") != 0){gchar *response;response = g_strdup_printf ("Word! You said `%s'.", greeting);my_app_complete_hello_world (interface, invocation, response);g_free (response);}else{g_dbus_method_invocation_return_error (MY_APP_ERROR,MY_APP_ERROR_NO_WHINING,"Hey, %s, there will be no whining!",g_dbus_method_invocation_get_sender (invocation));}return TRUE;
}[...]interface = my_app_frobber_skeleton_new ();my_app_frobber_set_verbose (interface, TRUE);g_signal_connect (interface,"handle-hello-world",G_CALLBACK (on_handle_hello_world),some_user_data);[...]error = NULL;if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface),connection,"/path/of/dbus_object",&error)){/* handle error */}
复制代码

C.client 端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
MyAppFrobber *proxy;
GError *error;
error = NULL;
proxy = my_app_frobber_proxy_new_for_bus_sync (
             G_BUS_TYPE_SESSION,
             G_DBUS_PROXY_FLAGS_NONE,
             "net.Corp.MyApp" ,              /* bus name */
             "/net/Corp/MyApp/SomeFrobber" , /* object */
             NULL,                          /* GCancellable* */
             &error);
/* do stuff with proxy */
g_object_unref (proxy);

 

 

 

3. P2P型

3.1 Server

3.1.1 提供一个基于Default Context的GLib Mainloop

3.1.2 调用g_dbus_server_start启动一个Server

3.1.3 调用g_signal_connect,关联callback到Server对象的"new-connection"信号上

3.1.4 提供callback,在callback中创建一个skeleton对象,并调用g_dbus_interface_skeleton_export输出到这个新建立的连接上

3.2 Client

3.2.1 提供一个基于Default Context的GLib Mainloop

3.2.2 调用g_dbus_connection_new_for_address_sync建立一个到Server的连接

3.2.3 调用dbus_proxy_new_sync创建一个与Server侧skeleton对象对应的Proxy对象,作为后续DBus方法调用的参数

这篇关于Gdbus的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145642

相关文章

Kvm 管理器突然打不开 启动虚拟系统管理器出错: g-dbus-error-quark: GDBus.Error:org.freed

环境: Ubuntu20.04 KVM mobaxterm 11.1 问题描述: 启动虚拟系统管理器出错: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.NoReply: Message recipient disconnected from message bus without replying (4)Trac

Kvm 管理器突然打不开 启动虚拟系统管理器出错: g-dbus-error-quark: GDBus.Error:org.freed

环境: Ubuntu20.04 KVM mobaxterm 11.1 问题描述: 启动虚拟系统管理器出错: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.NoReply: Message recipient disconnected from message bus without replying (4)Trac

一种IPC通信机制Gdbus详解

一、DBus介绍 常规进程间通信有管道,消息队列,共享内存,以及socket等,每个都有优劣,这次我们介绍一种高阶的进程间通信方式DBus。 DBus通信是IPC通信机制的一种方式,本身是建立在socket机制之上,它支持远程函数调用和信号传递。 它有两种模式,分别为:session(会话模式)、system(总线模式) 总线模式(system bus):采用总线模式时,系统需要维护一个DBus

(Solved)error getting credentials - err: exit status GDBus.Error:org.freedesktop.DBus.Error.ServiceU

docker-compose报错解决 文章目录 docker-compose报错解决情景解决方法 情景 在使用docker-compose进行环境搭建的时候,出现以下报错 error getting credentials - err: exit status 1, out: GDBus.Error:org.freedesktop.DBus.Error.ServiceU