Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58729
  • 博文数量: 20
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 225
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-29 01:11
文章分类
文章存档

2011年(1)

2010年(19)

我的朋友

分类: LINUX

2010-01-29 01:21:55

上面的例子看起来有些老了,在ubuntu 8.10上已经编译不起来了, 稍微修改了下

send:
先连接到dbus, 然后构造一个message, 发送出去

 #include
#include
#include

#define PING_OBJECT_PATH "/com/burtonini/dbus/ping"
#define PING_INTERFACE   "com.burtonini.dbus.Signal"

static gboolean send_ping(DBusConnection *bus);


    int
main ( int argc, char *argv[] )
{
    GMainLoop *loop;
    DBusConnection *bus;
    DBusError error;

    loop = g_main_loop_new(NULL, FALSE);
    dbus_error_init(&error);
    bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
    if (!bus) {
    g_warning("Failed to connect to the D-BUS daemon: %s", error.message);
    dbus_error_free(&error);
    return 1;
    }

    /*set up this connection to work in a GLib event loop*/
    dbus_connection_setup_with_g_main(bus, NULL);
    g_timeout_add(1000, (GSourceFunc) send_ping, bus);
    g_main_loop_run(loop);

    return 0;
}                /* ----------  end of function main  ---------- */


 static gboolean
send_ping ( DBusConnection *bus )
{

    DBusMessage *message;

    message = dbus_message_new_signal (PING_OBJECT_PATH, PING_INTERFACE,
        "Ping");
    if (!message) {
    g_error("create signal message error\n");
    return FALSE;
    }
    g_print("start to append arguments\n");
    char *msg = "Ping!";
    dbus_message_append_args(message, DBUS_TYPE_STRING, &msg, DBUS_TYPE_INVALID);

    /* Send the signal */
    dbus_connection_send(bus, message, NULL);
    dbus_message_unref(message);
    g_print("Ping!\n");


    /* Return TRUE to tell the event loop we want to be called again*/
    return TRUE;
}        /* -----  end of function send_ping  ----- */

listen:
连接到bus, filter signal, 这里一个问题就是得到的字符串是指向message的, 因此似乎不要自己亲自去释放这个字符串
 static DBusHandlerResult signal_filter ( DBusConnection *connection,
    DBusMessage *message, void *user_data);


    int
main ( int argc, char *argv[] )
{
    GMainLoop *loop;
    DBusConnection *bus;
    DBusError error;
    
    loop = g_main_loop_new(NULL, FALSE);
    dbus_error_init(&error);
    bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
    if (!bus) {
    g_warning("Failed to connect to the D-BUS daemon %s", error.message);
    dbus_error_free(&error);
    return 1;
    }

    dbus_connection_setup_with_g_main(bus, NULL);

    /* listening to message from all objects as no path is specified*/
    dbus_bus_add_match(bus, "type='signal', interface='com.burtonini.dbus.Signal'", NULL);
    dbus_connection_add_filter(bus, signal_filter, loop, NULL);
    g_main_loop_run(loop);
    return 0;
}                /* ----------  end of function main  ---------- */


static DBusHandlerResult
signal_filter ( DBusConnection *connection, DBusMessage *message, void *user_data )
{
    GMainLoop *loop = user_data;
    if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
        "Disconnected")) {
    g_main_loop_quit(loop);
    return DBUS_HANDLER_RESULT_HANDLED;
    }
    else if (dbus_message_is_signal(message, "com.burtonini.dbus.Signal", "Ping")) {
    DBusError error;
    char *s;
    dbus_error_init(&error);
    if (dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) {
        g_print("Ping received: %s\n", s);
        /* It seems you needn't to destory the value here, it just comes from the message, but who is responsible for destroying the value? */
       // dbus_free(s);
    } else {
        g_print("Ping received, but error getting message %s\n", error.message);
        dbus_error_free(&error);
    }
    return DBUS_HANDLER_RESULT_HANDLED;
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}        /* -----  end of function signal_filter  ----- */

阅读(669) | 评论(0) | 转发(0) |
0

上一篇:dbus地址

下一篇:D-BUS基础编程

给主人留下些什么吧!~~