Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3046190
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-05-09 11:49:10

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



 
   
     
     
   

   
     
   

 

  在前面我们已经学习了方法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
 
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

 

阅读(1393) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~