打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
D-Bus学习(九):利用XML定义D-Bus之Singal收发例子

 在前面我们学习了使用Glib的高程捆绑方式的method的收发,现在学习singal的收发,xml例子如下

<?xml version="1.0" encoding="UTF-8" ?>
<node name="/com/wei/MyObject">
  <interface name="com.wei.MyObject.Sample">
    <method name="Test">
      <arg name="x" type="u" direction="in" />
      <arg name="d_ret" type="d" direction="out" />
    </method >
    <signal name="Hello">
      <arg name="w_dialog" type="s" />
    </signal>
  </interface >
</node >

  在前面我们已经学习了方法Test的处理,现在增加一个信号Hello,为了是的处理方式更为简洁,这个信号只带来一个叫做w_dialog的字符串。同样我们使用dbus-binding-tool工具分别生成了server的头文件wei_server2.h和client的头文件wei_client2.h。(操作方式,请参阅上两次学习)。

Server的编写

  查看wei_server2.h,和之前只有method的相比,只有稍稍的改动,在对象的info中,增加了Hello的说明。如下:

... ...

const DBusGObjectInfo dbus_glib_com_wei_object_info = {
  0,
  dbus_glib_com_wei_methods,
  1,
"com.wei.MyObject.Sample/0Test/0S/0x/0I/0u/0d_ret/0O/0F/0N/0d/0/0/0",
"com.wei.MyObject.Sample/0Hello/0/0" ,
"/0"
};

  我们需要对象中增加相关的代码,头文件com_wei_myobject2.h如下:

#ifndef WEI_COM_WEI_MYOBJECT2_H
#define WEI_COM_WEI_MYOBJECT2_H

typedef struct ComWeiMyObject2 ComWeiMyObject2;
 
typedef struct ComWeiMyObject2Class ComWeiMyObject2Class;

struct ComWeiMyObject2
 
{
  GObject parent;
};

struct ComWeiMyObject2Class
 
{
  GObjectClass parent;
};

#define COM_WEI_MYOBJECT2_TYPE  (com_wei_myobject2_get_type())
 

GType com_wei_myobject2_get_type(void);
 
gboolean com_wei_test(ComWeiMyObject2 * obj , const guint  IN_x,gdouble * OUT_d_ret, GError ** error);
 
void com_wei_hello(ComWeiMyObject2 * obj, const char * w_dialog); 
#endif

  增加一个函数,用于发送Hello信号。源文件com_wei_myobject2.c如下:

#include "com_wei_myobject2.h"
#include <dbus/dbus-glib.h>
 
enum
{
  HELLO_MESSAGE,
  LAST_SIGNAL
};
 

static guint signals[LAST_SIGNAL]; 

G_DEFINE_TYPE(ComWeiMyObject2,com_wei_myobject2,G_TYPE_OBJECT)

static void com_wei_myobject2_init(ComWeiMyObject2 * object)
{
}
 
static void com_wei_myobject2_class_init(ComWeiMyObject2Class * klass)
{
  signals[HELLO_MESSAGE] = g_signal_new (
    "hello",
   //信号的名字,我曾写为"Hello",报告没有找到Hello的信号hello,因此有可能需要全部改为小写 
    G_OBJECT_CLASS_TYPE(klass),
    G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
    0,
    NULL,NULL,
    g_cclosure_marshal_VOID__STRING, 
//the function to translate arrays of parameter values to signal emissions into C language callback invocations. 在gmarshal.h中定,如果结构参数复杂需自行定义,参见一个很好的blog ,在我们这个例子中,我们给出一个参数STRING的简单模式 
    G_TYPE_NONE,
   //返回值,因为信号没有返回,所以为NONE 
    1, //参数数目 
    G_TYPE_STRING);   //参数类型 
}


gboolean com_wei_test(ComWeiMyObject2 * obj, const guint IN_x, 
                      gdouble* OUT_d_ret, GError ** error)
{
  printf("com_wei_test() get input param: x= %d/n",IN_x);
  * OUT_d_ret = 0.99;
  return TRUE;


void com_wei_hello(ComWeiMyObject2 * obj, const char * w_dialog)
{
  g_signal_emit ( obj,signals[HELLO_MESSAGE],0,w_dialog );   //信号释放 
}

  在对象中,最关键是定义信号,以及型号释放。对于源文件wei_server2.c,很简单,和之前的只有method的例子一样,需要信号释放时,调用com_wei_hello函数即可。

... ... 
  com_wei_hello(obj,"Hello, World!"); 
... ...

Client的编写

  通过dbus-bind-tool工具生成的wei_client2.h,和之前只有method的例子一样,不在描述。wei_client2.c如下,我们重点描述singal的部分,其他部分请参阅学习七

... ...

static void hello_signal_callback (DBusGProxy * proxy, const char * string, gpointer user_data)
{
  printf("Received hello signal %s/n",string);
}

... ...


int main(int argc ,char ** argv)
{
  DBusGConnection * conn;
  DBusGProxy * proxy;

... ... //建议dbus连接 
  //建立remote obj: proxy 
  proxy = dbus_g_proxy_new_for_name(conn,"com.wei.test", "/com/wei/MyObject","com.wei.MyObject.Sample");

  dbus_g_proxy_add_signal (proxy,"Hello",G_TYPE_STRING,G_TYPE_INVALID);
  dbus_g_proxy_connect_signal (proxy,"Hello",G_CALLBACK(hello_signal_callback),NULL,NULL);
 
... ...
}

  我们通过dbus-monitor进行跟踪,dbus_g_proxy_add_singal[Specifies the argument signature of a signal;.only necessary if the remote object does not support introspection. ] 和dbus_g_proxy_connect_singal[Connect a signal handler to a proxy for a remote interface.When the remote interface emits the specified signal, the proxy will emit a corresponding GLib signal.] 向dbus daemo注册要求监听这个接口的Hello信号。

method call sender=:1.72 -> dest=org.freedesktop.DBus serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
D-Bus学习(五):Signal的收发小例子
Keil调试命令(转载)
Bluetooth enabling process
NEXUS 关于 SPRINGFRAMEWORKD 的 源
DBUS基础知识
DBUS基础知识(非常全面)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服