免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1941 | 回复: 0
打印 上一主题 下一主题

沒有glib的dbus使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-11 11:42 |只看该作者 |倒序浏览

                沒有glib的dbus使用
因為有時候可能會使用dbus,但你并不想使用glib提供的庫,或者說你沒有機會使用它的庫,這時候你又想使用dbus,于是可能就想知道該怎么使用,我前段時間就有種個需求,于是研究了一下,寫了一個實例程序,很丑,不過大體表達了我想表達的意思:
cat test_dbus.c
#include
#include
#include
#include
#define TEST_SERVICE            "com.woojoy.test_dbus"
#define TEST_PATH               "/com/woojoy/test_dbus"
#define MATCH_RULE              "type='signal'"
/*below is the signal filter to receive the others signal,ofcourse you should change the MATCH_RULE,so filter the signals you donot care*/
static DBusHandlerResult message_filter(DBusConnection *connection, DBusMessage *message, void *data)
{
                const char *interface = dbus_message_get_interface(message);
                printf("in test_dbus the interface is %s \n",interface);
                const char *member= dbus_message_get_member(message);
                printf("in test_dbus the member is %s \n",member);
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/*below is the message handler,here you should accept the others remote method invoke I just printf some info*/
DBusHandlerResult message_handler(DBusConnection *connection,
                                                                  DBusMessage *message,
                                                                                 void *user_data)
{
                printf("this is test_dbus message_handler\n");
                return (DBUS_HANDLER_RESULT_HANDLED);
}
/*here is to register to dbus server,so that it can send the info to us*/
static void
connect_hook(DBusConnection *connection, void *data)
{
                DBusError error;
                DBusObjectPathVTable vtable = { .message_function = message_handler, };
                dbus_error_init(&error);
                if (!dbus_bus_request_name(connection, TEST_SERVICE, 0, &error)) {
                                printf("dbus_bus_request_name error\n");
                                return;
                }
                dbus_bus_add_match(connection, MATCH_RULE, &error);
                if (dbus_error_is_set(&error)) {
                                printf("after dbus_bus_add_match error\n");
                                return;
                }
                if (!dbus_connection_register_object_path(connection, TEST_PATH, &vtable, NULL)) {
                                printf("dbus_connection_register_object_path error\n");
                                return;
                }
                dbus_error_free(&error);
                return;
}
int main(int argc,char **argv)
{
                DBusError derror;
                dbus_error_init(&derror);
                DBusConnection *dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &derror);
                if (dbus_connection == NULL) {
                                fprintf(stderr,"System DBus connection failed: %s", derror.message);
                                dbus_error_free(&derror);
                                return -1;
                }
                /*above is to get the dbusconnection,which actually is a socket fd*/
                connect_hook(dbus_connection,NULL);
                 /*above is to register to dbus server*/
                if (!dbus_connection_add_filter(dbus_connection, message_filter, NULL, NULL)) {
                                printf("after dbus_connection_add_filter error\n");
                                return -1;
                }
                  /*here is add some filter which will get the message before message_handler*/
                int fd=-1;
                dbus_connection_get_unix_fd(dbus_connection, &fd);
                printf("the fd after dbus_connection_get_unix_fd is %d\n",fd);
                 /*below is the main loop to get from the dbus server,and then dispatch the info */
                while(1){
                                fd_set rd_fds;
                                FD_ZERO( &rd_fds );
                                FD_SET( fd, &rd_fds );
                                int result=select(fd+1, &rd_fds, NULL, NULL, NULL);
                                if(result < 0){
                                                printf("select error\n");
                                                break;
                                }
                                if (FD_ISSET (fd, &rd_fds)){
                                                printf("there is something to read\n");
                                                do {
                                                                dbus_connection_read_write_dispatch(dbus_connection, 0);
                                                } while (dbus_connection_get_dispatch_status(dbus_connection) == DBUS_DISPATCH_DATA_REMAINS);
                                                continue;
                                }
                }
                return 0;
}
編譯方法:
gcc test_dbus.c `pkg-config --cflags --libs dbus-1`  -o test_dbus
運行:\n./test_dbus
如果你的系統有dbus消息的發送,我想就可以看到相應的打印信息了。
that is all!
thanks;
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/67984/showart_1713798.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP