Dbus 使用方式

    转自http://blog.csdn.net/coroutines/article/details/39313825

        DBus应用的俩种形态及实现                         

 

介绍一下基于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 端

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方法调用的参数

posted on 2015-09-22 16:08  木星来客  阅读(6472)  评论(0编辑  收藏  举报

导航