example-client.c

00001 #include <dbus/dbus-glib.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 
00005 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
00006 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
00007 
00008 static void
00009 lose (const char *str, ...)
00010 {
00011   va_list args;
00012 
00013   va_start (args, str);
00014 
00015   vfprintf (stderr, str, args);
00016   fputc ('\n', stderr);
00017 
00018   va_end (args);
00019 
00020   exit (1);
00021 }
00022 
00023 static void
00024 lose_gerror (const char *prefix, GError *error) 
00025 {
00026   lose ("%s: %s", prefix, error->message);
00027 }
00028 
00029 static void
00030 print_hash_value (gpointer key, gpointer val, gpointer data)
00031 {
00032   printf ("%s -> %s\n", (char *) key, (char *) val);
00033 }
00034 
00035 int
00036 main (int argc, char **argv)
00037 {
00038   DBusGConnection *bus;
00039   DBusGProxy *remote_object;
00040   DBusGProxy *remote_object_introspectable;
00041   GError *error = NULL;
00042   char **reply_list;
00043   char **reply_ptr;
00044   GValueArray *hello_reply_struct;
00045   GHashTable *hello_reply_dict;
00046   char *introspect_data;
00047   guint i;
00048 
00049   g_type_init ();
00050 
00051   {
00052     GLogLevelFlags fatal_mask;
00053     
00054     fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
00055     fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
00056     g_log_set_always_fatal (fatal_mask);
00057   }
00058 
00059   bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
00060   if (!bus)
00061     lose_gerror ("Couldn't connect to session bus", error);
00062   
00063   remote_object = dbus_g_proxy_new_for_name (bus,
00064                                              "org.designfu.SampleService",
00065                                              "/SomeObject",
00066                                              "org.designfu.SampleInterface");
00067 
00068   if (!dbus_g_proxy_call (remote_object, "HelloWorld", &error,
00069                           G_TYPE_STRING, "Hello from example-client.c!", G_TYPE_INVALID,
00070                           G_TYPE_STRV, &reply_list, G_TYPE_INVALID))
00071     lose_gerror ("Failed to complete HelloWorld", error);
00072 
00073   
00074   if (!dbus_g_proxy_call (remote_object, "GetTuple", &error,
00075                           G_TYPE_INVALID,
00076                           G_TYPE_VALUE_ARRAY, &hello_reply_struct, G_TYPE_INVALID))
00077     lose_gerror ("Failed to complete GetTuple", error);
00078   
00079   if (!dbus_g_proxy_call (remote_object, "GetDict", &error,
00080                           G_TYPE_INVALID,
00081                           DBUS_TYPE_G_STRING_STRING_HASHTABLE, &hello_reply_dict, G_TYPE_INVALID))
00082     lose_gerror ("Failed to complete GetDict", error);
00083 
00084   printf ("reply_list: ");
00085   for (reply_ptr = reply_list; *reply_ptr; reply_ptr++)
00086     printf ("\"%s\" ", *reply_ptr);
00087   printf ("\n");
00088   g_strfreev (reply_list);
00089 
00090   for (i = 0; i < hello_reply_struct->n_values; i++)
00091     {
00092       GValue strval = { 0, };
00093 
00094       g_value_init (&strval, G_TYPE_STRING);
00095       if (!g_value_transform (g_value_array_get_nth (hello_reply_struct, i), &strval))
00096         g_value_set_static_string (&strval, "(couldn't transform to string)");
00097       g_print ("%s: %s\n", g_type_name (G_VALUE_TYPE (g_value_array_get_nth (hello_reply_struct, i))),
00098                g_value_get_string (&strval));
00099     }
00100   g_value_array_free (hello_reply_struct);
00101   printf ("\n");
00102 
00103   g_hash_table_foreach (hello_reply_dict, print_hash_value, NULL);
00104   g_hash_table_destroy (hello_reply_dict);
00105 
00106   remote_object_introspectable = dbus_g_proxy_new_for_name (bus,
00107                                                             "org.designfu.SampleService",
00108                                                             "/SomeObject",
00109                                                             "org.freedesktop.DBus.Introspectable");
00110   if (!dbus_g_proxy_call (remote_object_introspectable, "Introspect", &error,
00111                           G_TYPE_INVALID,
00112                           G_TYPE_STRING, &introspect_data, G_TYPE_INVALID))
00113     lose_gerror ("Failed to complete Introspect", error);
00114   printf ("%s", introspect_data);
00115   g_free (introspect_data);
00116 
00117   g_object_unref (G_OBJECT (remote_object_introspectable));
00118   g_object_unref (G_OBJECT (remote_object));
00119 
00120   exit(0);
00121 }

Generated on Tue Feb 24 16:47:59 2009 for D-BUSGLibBindings by  doxygen 1.5.1