0
点赞
收藏
分享

微信扫一扫

D-Bus学习(七):利用XML定义D-Bus之Client的例子


  继续学习D-Bus。之前学习了使用底层的API来发送,监听消息的方式。在 D- Bus学习(四):基础小例子(同步和异步) 之中,我们给出了利用proxy进行发送method_call,并等待method_reply或者error的client情况。在D-Bus中,可以将D-Bus接口定义用XML格式表述处理,并利用工具,自动生成头文件,给出工整的调用方式。下面是一个XML的例子。

<?xml version="1.0" encoding="UTF-8" ?>
 <node name="/com/wei/MyObject">
   <interface name="com.wei.MyObject.Sample">
     <method name="Test">
       <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="wei_response" /> 
       <arg type="u" name="x" direction="in" />
       <arg type="d" name="d_ret" direction="out" />
     </method >
   </interface >
 </node >

  其中annotation是作为server用,在下一次学习中使用。我们给这个xml文件一个名字,wei.xml。结合我们上两次学习中的图,node实际上就是相当于对象,用path来进行描述。使用dbus-bingding-tool来生成头文件:

dbus-binding-tool --mode=glib-client --prefix=com_wei wei.xml > wei_client.h    

  头文件中给出了同步调用和异步调用的内容,我们给出一个前缀com_wei,将在头文件中出现,头文件内容如下:

/* Generated by dbus-binding-tool; do not edit! */

 #include <glib.h>
 #include <dbus/dbus-glib.h>

 G_BEGIN_DECLS

 #ifndef _DBUS_GLIB_ASYNC_DATA_FREE
 #define _DBUS_GLIB_ASYNC_DATA_FREE
 static
 #ifdef G_HAVE_INLINE
 inline
 #endif
 void
 _dbus_glib_async_data_free (gpointer stuff)
 {
         g_slice_free (DBusGAsyncData, stuff);
 }
 #endif

 #ifndef DBUS_GLIB_CLIENT_WRAPPERS_com_wei_MyObject_Sample
 #define DBUS_GLIB_CLIENT_WRAPPERS_com_wei_MyObject_Sample

 static
 #ifdef G_HAVE_INLINE
 inline
 #endif 
//这是同步调用内容 
 gboolean
com_wei  _MyObject_Sample_test (DBusGProxy *proxy, const guint IN_x, gdouble* OUT_d_ret, GError **error)
 {
   return dbus_g_proxy_call (proxy, "Test", error, G_TYPE_UINT, IN_x, G_TYPE_INVALID, G_TYPE_DOUBLE, OUT_d_ret, G_TYPE_INVALID);
 } 
//下面是异步调用内容 
typedef void (*com_wei _MyObject_Sample_test_reply) (DBusGProxy *proxy, gdouble OUT_d_ret, GError *error, gpointer userdata); 
 static void
com_wei MyObject_Sample_test_async_callback  (DBusGProxy *proxy, DBusGProxyCall *call, void *user_data)
 {
   DBusGAsyncData *data = (DBusGAsyncData*) user_data;
   GError *error = NULL;
   gdouble OUT_d_ret;
   dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_DOUBLE, &OUT_d_ret, G_TYPE_INVALID);
   (*(com_wei_MyObject_Sample_test_reply)data->cb) (proxy, OUT_d_ret, error, data->userdata);
   return;
 }

 static
 #ifdef G_HAVE_INLINE
 inline
 #endif
 DBusGProxyCall*
com_ wei (DBusGProxy *proxy, const guint IN_x, com_wei_MyObject_Sample_test_reply callback, gpointer userdata)

 {
   DBusGAsyncData *stuff;
   stuff = g_slice_new (DBusGAsyncData);
   stuff->cb = G_CALLBACK (callback);
   stuff->userdata = userdata;
   return dbus_g_proxy_begin_call (proxy, "Test", com_wei_MyObject_Sample_test_async_callback, stuff, _dbus_glib_async_data_free, G_TYPE_UINT, IN_x, G_TYPE_INVALID);
 }
 #endif /* defined DBUS_GLIB_CLIENT_WRAPPERS_com_wei_MyObject_Sample */ 
 
G_END_DECLS

  在这个头文件中,给出了比较工整的函数定义prefix_interface_method,并且已经包含了一些底层API的代码。利用这个头文件,我们在写程序中,可以更为简洁和直观。下面是利用同步方式写的小例子:

void wei_request(DBusGProxy * proxy)
 {
   gdouble PUT_d_ret;
   GError * error = NULL;
 
   if(!com_wei_MyObject_Sample_test ( proxy,1000,&OUT_d_ret,&error   )){ 
     g_printerr("Error: %s/n",error->message);
     return;
   }

    g_print("Proxy get return d_ret = %f",d_ret);

 }

  proxy的获取见 D- Bus学习(四):基础小例子(同步和异步) ,显示建立D-Bus连接,为连接捆绑一个可读名字(well-known name),建立proxy,不再重复。异步小例子如下:

void wei_wait_reply (DBusGProxy * proxy, gdouble OUT_d_ret, GError * error,gpointer userdata)
 {
   if(error != NULL){
     g_printerr("Asyn Error : %s/n",error->message);
     return;
   }

   g_print("Proxy get return d_ret = %f",OUT_d_ret);
 }

 void wei_request_async(DBusGProxy * proxy)
 { 
   com_wei_MyObject_Sample_test_async (proxy,2000,wei_wait_reply, NULL);
}

  在异步的例子中,wei_request_async表示发起一个消息请求,当D-Bus返回应答时候,触发wei_wait_reply。

举报

相关推荐

Managed D-Bus

D-Bus简介

0 条评论