/****************************************
*
*
*
* *************************************/
#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;
}
|