How to pass an image using dbus glib-binding

Sumit Kumar Jain sumitskj_20 at yahoo.com
Fri Feb 29 01:26:07 PST 2008


Hi,
I want to pass an image file which is around 10K using dbus glib bindings. I tried to find about it and the only data type that seems to suite my need is GArray. However debugging through the code, i found out that the actual message size is four times the size of my image file. That i think is because dbus maps GArray to DBUS_TYPE_ARRAY. GArray has a pointer of gchar * while DBUS_TYPE_ARRAY is an array of uint32. I guess that the reason of the message size increasing by 4 times as 3 NULL bytes are appended to every byte of the GArray. Now it seems that that GArray is not useful and i must use some other data type. Any suggestion as to which data type might work. Also some example code demonstrating the usage will be gr8. 

The following code demonstrates the case : 

------- client code ------
int main (int argc, char **argv)
{
    DBusGConnection *connection;
    GError *error;
    DBusGProxy *proxy;
    GArray *array;
    guint length;
    FILE *fp = NULL;
    int len;

    g_type_init ();

    error = NULL;
    connection = dbus_g_bus_get (DBUS_BUS_SESSION,
                               &error);
    if (connection == NULL)
    {
      g_printerr ("Failed to open connection to bus: %s\n",
                  error->message);
      g_error_free (error);
      exit (1);
    }

    proxy = dbus_g_proxy_new_for_name (connection,
                                     "com.example.SomeObject",
                                     "/com/example/SomeObject",
                                     "com.example.SomeObject");

    error = NULL;

    if(!com_example_SomeObject_method1(proxy,10,&array,&length,&error))
    {
        printf("Error --- %s\n",error->message);
        return 1;
    }

    printf("File details... length = %d\n",length);
    error = NULL;

    fp = fopen("copy-client","wb");
    len = fwrite(array->data,4,array->len,fp);
    fclose(fp);

    return 0;
}

---------- End Client code ------------

--------- Server code -------------
//some-object.h
#ifndef __SOME_OBJECT_H__
#define __SOME_OBJECT_H__

#include <glib-object.h>

typedef struct _SomeObject SomeObject;
struct _SomeObject
{
    GObject        parent_obj;
    gint         m_a;
    gchar*        m_b;
    gfloat        m_c;
};

typedef struct _SomeObjectClass SomeObjectClass;
struct _SomeObjectClass
{
    GObjectClass    parent_class;

    /* Some useful methods may follow. */
    gboolean    (*method1)    (SomeObject *self, gint x,GArray **y,gint *z,GError **error);
    void        (*method2)    (SomeObject *self, gchar*);
};

GType    some_object_get_type ();

gboolean some_object_method1 (SomeObject *self, gint x,GArray **y,gint *z,GError **);    /* virtual */
void    some_object_method2 (SomeObject *self, gchar*);    /* virtual */
void    some_object_method3 (SomeObject *self, gfloat);    /* non-virtual */


/* Handy macros */
#define SOME_OBJECT_TYPE        (some_object_get_type ())
#define SOME_OBJECT(obj)        (G_TYPE_CHECK_INSTANCE_CAST ((obj), SOME_OBJECT_TYPE, SomeObject))
#define SOME_OBJECT_CLASS(c)        (G_TYPE_CHECK_CLASS_CAST ((c), SOME_OBJECT_TYPE, SomeObjectClass))
#define SOME_IS_OBJECT(obj)        (G_TYPE_CHECK_TYPE ((obj), SOME_OBJECT_TYPE))
#define SOME_IS_OBJECT_CLASS(c)        (G_TYPE_CHECK_CLASS_TYPE ((c), SOME_OBJECT_TYPE))
#define SOME_OBJECT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), SOME_OBJECT_TYPE, SomeObjectClass))

#endif
// End some-object.h

/// some-object.c
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "some-object.h"
#include "some-object-glue.h"
#include <dbus/dbus-glib-bindings.h>
#include <errno.h>


static GObjectClass *parent_class = ((void *)0);
static void some_object_init (SomeObject *self);

char *FILENAME = "test.jpeg";
int SIZE = 0;

gboolean some_object_method1_impl (SomeObject *self, gint a, GArray **y,gint *z,GError **error)
{
    struct stat stat_buf;
    gchar *ptr = NULL;
    FILE *fp = NULL;
    int err;
    unsigned int length = 0;
    self->m_a = a;
    g_print ("Method1: %i\n", self->m_a);
    
    *y = g_array_new(FALSE,FALSE,1);

    stat(FILENAME,&stat_buf);
    SIZE = stat_buf.st_size;
    ptr = g_malloc(SIZE);
    errno = 0;
    fp = fopen(FILENAME,"rb");
    err = errno;
    if(fp)
    {
        FILE *fp2 = fopen("c:\\copy-server","wb");
        fread(ptr,1,SIZE,fp);
        
        if(fp2)
            fwrite(ptr,1,SIZE,fp2);
        
        g_array_append_vals(*y,ptr,SIZE/4);
        *z = SIZE;    
    }
    
    fclose(fp);
    
    free(ptr);
    
    return TRUE;
}

void some_object_method2_impl (SomeObject *self, gchar* b)
{
    self->m_b = b;
    g_print ("Method2: %s\n", self->m_b);
}


/* Public methods. */
gboolean some_object_method1 (SomeObject *self, gint a,GArray **y,gint *z,GError **error)
{
    return SOME_OBJECT_GET_CLASS (self)->method1 (self, a,y,z,error);
}

void    some_object_method2 (SomeObject *self, gchar* b)
{
    SOME_OBJECT_GET_CLASS (self)->method2 (self, b);
}

void    some_object_method3 (SomeObject *self, gfloat c)
{
    self->m_c = c;
    g_print ("Method3: %f\n", self->m_c);
}


void    some_object_dispose (GObject *self)
{
    static gboolean first_run = TRUE;

    if (first_run)
    {
        first_run = FALSE;
        
        /* Call g_object_unref on any GObjects that we hold, but don't break the object */

        parent_class-> dispose (self);
    }
}

void    some_object_finalize (GObject *self)
{
    parent_class-> finalize (self);
}

/* Here is where we override any functions. Since we have no properties or even fields, none of the below are needed. */
void    some_object_class_init        (gpointer g_class, gpointer class_data)
{
    GObjectClass    *object_class    = G_OBJECT_CLASS (g_class);
    SomeObjectClass    *this_class    = SOME_OBJECT_CLASS (g_class);
    
    //assign value to parent class
    parent_class = g_type_class_peek_parent (g_class);
    
    //assing pointer values to the base class members
    object_class-> dispose = &some_object_dispose;
    object_class-> finalize = &some_object_finalize;
    
    //assign value to derived class members
    this_class->method1 = &some_object_method1_impl;
    this_class->method2 = &some_object_method2_impl;

    dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(this_class),&dbus_glib__object_info);
}

void some_object_init (SomeObject *self)
{
    self->m_a = 1;
    self->m_c = 1.03f;
    self->m_b = "sumit";
}

GType some_object_get_type () 
{
    static GType g_define_type_id = 0; 
    if ((g_define_type_id == 0)) 
    { 
        static const GTypeInfo g_define_type_info = 
        { 
            sizeof (SomeObjectClass), 
            (GBaseInitFunc) ((void *)0), 
            (GBaseFinalizeFunc) ((void *)0), 
            (GClassInitFunc) some_object_class_init, 
            (GClassFinalizeFunc) ((void *)0), 
            ((void *)0), 
            sizeof (SomeObject), 
            0, 
            (GInstanceInitFunc) some_object_init, 
        }; 

        g_define_type_id = g_type_register_static 
        (
            G_TYPE_OBJECT, 
            "SomeObject", 
            &g_define_type_info, 
            (GTypeFlags) 0
        );
        
    } 

    return g_define_type_id; 
}

int main(int argc,char *argv[])
{
    SomeObject *so = NULL;
    DBusGConnection *bus;
    GMainLoop *mainLoop = NULL;
    unsigned int request_ret;
    GError *error = NULL;

    DBusGProxy *proxy = NULL;
    char *x;
    
    g_type_init();

    so = g_object_new(SOME_OBJECT_TYPE,NULL);

    bus = dbus_g_bus_get(DBUS_BUS_SESSION,NULL);

    proxy = dbus_g_proxy_new_for_name(bus,DBUS_SERVICE_DBUS,DBUS_PATH_DBUS,DBUS_INTERFACE_DBUS);

    dbus_g_connection_register_g_object(bus,"/com/example/SomeObject",G_OBJECT(so));

    if(!org_freedesktop_DBus_request_name(proxy,"com.example.SomeObject",0,&request_ret,&error))
    {
        g_print("Unable to register service\n");
        return 1;
    }

    mainLoop = g_main_loop_new(NULL,FALSE);
    g_main_loop_run(mainLoop);
    
    return 0;
}

----- End Server code ----

 
Regards,
Sumit





      Get the freedom to save as many mails as you wish. To know how, go to http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20080229/78d61195/attachment.html 


More information about the dbus mailing list