dbus-glib笔记

拜拜、爱过 提交于 2019-11-26 05:14:38
dbus-glib笔记2009-08-21 11:02:15

分类: LINUX

use command "make server" to make server while "make client" to make client.
 
说明:附件程序演示如下内容:
  • 如何在服务端同步以及异步处理客户端提交的请求。
  • 如何在客户端同步以及异步调用服务端提供的服务。
  • 信号的使用以及参数的传递。
  • 如何防止客户端调用超时。

client端

/*********************************************
 *
 *
 *
 *
 *
 *
 * ******************************************/

#include <stdio.h>
#include <stdlib.h>

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

#include "study_proxy.h"

void plusone_cbk (DBusGProxy *proxy, guint result, GError *error, gpointer userdata);
static void signal_cbk(DBusGProxy *proxy, guint ret, gpointer user_data);

int main(int argc, char **argv)
{
       GMainLoop *mainloop = NULL;
       mainloop = g_main_loop_new(NULL, FALSE);

 DBusGConnection *connection = NULL;
 GError *err = NULL;
 g_type_init();
 if((connection = dbus_g_bus_get(DBUS_BUS_SESSION, &err)) == NULL)
 {
      printf("dbus_g_bus_get--------------failed!\n");
      return 0;
 }

 DBusGProxy *proxy = NULL;
 if((proxy = dbus_g_proxy_new_for_name(connection, "bzd.object", "/bzd/bzd_object", "ddb.study")) == NULL)
 {
      printf("dbus_g_proxy_new_for_name-------------failed!\n");
      return 0;
 }

 dbus_g_proxy_set_default_timeout (proxy, INT_MAX);

 ddb_study_plusone_in_10_min_async_async(proxy, 9, plusone_cbk, NULL);

 guint result = 0;
 ddb_study_plustwo_in_10_min(proxy, 10, &result, &err);
 printf("sync result = %d\n", result);

 dbus_g_proxy_add_signal(proxy, "return_three", G_TYPE_UINT, G_TYPE_INVALID);
 dbus_g_proxy_connect_signal(proxy, "return_three", G_CALLBACK (signal_cbk), NULL, NULL);

 g_main_loop_run(mainloop);
 return 0;

}

static void signal_cbk(DBusGProxy *proxy, guint ret, gpointer user_data)
{
     printf ("receive message from server, value = %d\n", ret);
}

void plusone_cbk(DBusGProxy *proxy, guint result, GError *error, gpointer userdata)
{
     printf("result = %d\n", result);
}


 

 

server端

 

/****************************************
 *
 *
 *
 * *************************************/

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

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

void *thread_func(void *);

typedef struct _BzdObject BzdObject;
typedef struct _BzdObjectClass BzdObjectClass;
GType bzd_object_get_type(void);

struct _BzdObject
{
     GObject parent;
};

struct _BzdObjectClass
{
     GObjectClass parent;
};

static guint bzd_object_signals[2] = { 0 };

#define DDB_TYPE_OBJECT (bzd_object_get_type())

G_DEFINE_TYPE(BzdObject, bzd_object, G_TYPE_OBJECT)

gboolean   bzd_object_plusone_in_10_min_async(BzdObject *obj, const guint value, DBusGMethodInvocation *context);
gboolean   bzd_object_plustwo_in_10_min(BzdObject *obj, guint value, guint *ret, GError **err);

#include "study_glue.h"

static void  bzd_object_init(BzdObject *obj)
{


}

static void  bzd_object_class_init(BzdObjectClass *obj_class)
{
     bzd_object_signals[0] = g_signal_new ("return_three",
     G_OBJECT_CLASS_TYPE (obj_class),
     (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED),
     0,
     NULL, NULL,
     g_cclosure_marshal_VOID__UINT,
     G_TYPE_NONE, 1, G_TYPE_UINT);

}
/***********************for thread**************************/
typedef struct _ThreadArg
{
     BzdObject *obj;
     DBusGMethodInvocation *context;
     guint value;
}ThreadArgStr;


void *thread_func(void *arg)
{
         ThreadArgStr *pkg = (ThreadArgStr *)arg;
         sleep(10);
         dbus_g_method_return(pkg->context, pkg->value+1);

         g_signal_emit(pkg->obj, bzd_object_signals[0], 0, 3);

         free(pkg);
}
/************************************************************/

gboolean bzd_object_plusone_in_10_min_async(BzdObject *obj, const guint value, DBusGMethodInvocation *context)
{
         printf("value = %d\n", value);
         ThreadArgStr *pkg = NULL;
         pkg = (ThreadArgStr *)malloc(sizeof(ThreadArgStr));
         pkg->obj = obj;
         pkg->context = context;
         pkg->value = value;

         pthread_t tid;
         if(pthread_create(&tid, NULL, thread_func, (void *)pkg) != 0)
         {
                   printf("pthread_create------------failed!\n");
         }
        return TRUE;
}

gboolean bzd_object_plustwo_in_10_min(BzdObject *obj, guint value, guint *ret, GError **err)
{
         printf("value = %d\n", value);
         (*ret) = value + 2;
         return TRUE;
}

int main(int argc, char **argv)
{
            GMainLoop *main_loop = NULL;
            g_type_init();
           if((main_loop = g_main_loop_new(NULL, FALSE)) == NULL)
           {
                     printf("g_main_loop_new -------------failed!\n");
                    return 0;
           }

            DBusGConnection *connection = NULL;
           GError *err = NULL;
            if((connection = dbus_g_bus_get(DBUS_BUS_SESSION, &err)) == NULL)
            {
                       printf("dbus_g_bus_get---------------failed!\n");
                       return 0;
           }

           DBusGProxy *proxy = NULL;
           guint request_name_result;
           proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS,
                                                                                     DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
          if (!dbus_g_proxy_call(proxy, "RequestName", &err,G_TYPE_STRING, "bzd.object",

                                      G_TYPE_UINT, 0, G_TYPE_INVALID,
                                     G_TYPE_UINT, &request_name_result, G_TYPE_INVALID))
          {
                           printf("Failed to acquire org.example.TestService: %s", err->message);
                           dbus_g_connection_unref(connection);
                           return 0;
          }

          dbus_g_object_type_install_info(DDB_TYPE_OBJECT, &dbus_glib_bzd_object_object_info);

          BzdObject *obj = NULL;
          obj = g_object_new(DDB_TYPE_OBJECT, NULL);

          dbus_g_connection_register_g_object(connection, "/bzd/bzd_object", G_OBJECT(obj));

          g_main_loop_run(main_loop);

          dbus_g_connection_unref(connection);
          return 0;
}

 

 

XML文件

 

<?xml version="1.0" encoding="UTF-8" ?>
<node name="/ddb/study">
 <interface name="ddb.study">
  <method name="plusone_in_10_min_async">
   <annotation name="org.freedesktop.DBus.GLib.Async" value="plusone_in_10_min_async_imp" />
   <arg type="u" name="value" direction="in" />
   <arg type="u" name="result" direction="out" />
  </method>
  <method name="plustwo_in_10_min">
   <arg type="u" name="value" direction="in"/>
   <arg type="u" name="result" direction="out" />
  </method>

  <!--signal-->
  <signal name="return_three">
   <arg type="u" name="signal_value" direction="out" />
  </signal>
 </interface>
</node>

 

marshal.list文件

 

VOID:UINT

 
 
Makefile
 

SERVER = server
CLIENT = client
CC = g++
GCC = gcc
RM = rm
LD = ld
MAKE = make

SERVER_SRC = server.c
SERVER_OBJ = $(SERVER_SRC:%.c=%.o)

CLIENT_SRC = client.c
CLIENT_OBJ = $(CLIENT_SRC:%.c=%.o)

#compiler flags

CFLAGS += -c -g
CFLAGS += -I/usr/include/glib-2.0-I/usr/lib/glib-2.0/include
CFLAGS += -I/usr/include/dbus-1.0-I/usr/lib/dbus-1.0/include

#Ld Flags

LDFLAGS += -L/usr/lib/
LDFLAGS += -ldbus-1 -ldbus-glib-1-lgthread-2.0-lpthread

study_server.h:study.xml
    dbus-binding-tool--mode=glib-server--prefix=bzd_objectstudy.xml> study_glue.h

study_client.h:study.xml
    dbus-binding-tool--mode=glib-client--prefix=bzd_objectstudy.xml> study_proxy.h

marshal.c:marshal.list
    glib-genmarshal --prefix=bzd_marshal--body marshal.list> marshal.c

marshal.h:marshal.list
    glib-genmarshal --prefix=bzd_marshal--header marshal.list> marshal.h

server:study_server.h marshal.h marshal.c$(SERVER_OBJ)
    $(GCC)$(SERVER_OBJ)$(LDFLAGS)-o$(SERVER)

$(SERVER_OBJ):$(SERVER_SRC)
    $(GCC)$(SERVER_SRC)$(CFLAGS)

client:study_client.h marshal.h marshal.c$(CLIENT_OBJ)
    $(GCC)$(CLIENT_OBJ)$(LDFLAGS)-o$(CLIENT)

$(CLIENT_OBJ):$(CLIENT_SRC)
    $(GCC)$(CLIENT_SRC)$(CFLAGS)

clean:
    rm *.o $(SERVER)$(CLIENT) marshal.c marshal.h study_glue.h study_proxy.h


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!