example-service.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 typedef struct SomeObject SomeObject;
00030 typedef struct SomeObjectClass SomeObjectClass;
00031 
00032 GType some_object_get_type (void);
00033 
00034 struct SomeObject
00035 {
00036   GObject parent;
00037 };
00038 
00039 struct SomeObjectClass
00040 {
00041   GObjectClass parent;
00042 };
00043 
00044 #define SOME_TYPE_OBJECT              (some_object_get_type ())
00045 #define SOME_OBJECT(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), SOME_TYPE_OBJECT, SomeObject))
00046 #define SOME_OBJECT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), SOME_TYPE_OBJECT, SomeObjectClass))
00047 #define SOME_IS_OBJECT(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), SOME_TYPE_OBJECT))
00048 #define SOME_IS_OBJECT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), SOME_TYPE_OBJECT))
00049 #define SOME_OBJECT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), SOME_TYPE_OBJECT, SomeObjectClass))
00050 
00051 G_DEFINE_TYPE(SomeObject, some_object, G_TYPE_OBJECT)
00052 
00053 gboolean some_object_hello_world (SomeObject *obj, const char *hello_message, char ***ret, GError **error);
00054 gboolean some_object_get_tuple (SomeObject *obj, GValueArray **ret, GError **error);
00055 gboolean some_object_get_dict (SomeObject *obj, GHashTable **ret, GError **error);
00056 
00057 #include "example-service-glue.h"
00058 
00059 static void
00060 some_object_init (SomeObject *obj)
00061 {
00062 }
00063 
00064 static void
00065 some_object_class_init (SomeObjectClass *klass)
00066 {
00067 }
00068 
00069 gboolean
00070 some_object_hello_world (SomeObject *obj, const char *hello_message, char ***ret, GError **error)
00071 {
00072   printf ("%s\n", hello_message);
00073   *ret = g_new (char *, 3);
00074   (*ret)[0] = g_strdup ("Hello");
00075   (*ret)[1] = g_strdup (" from example-service.c");
00076   (*ret)[2] = NULL;
00077 
00078   return TRUE;
00079 }
00080 
00081 gboolean
00082 some_object_get_tuple (SomeObject *obj, GValueArray **ret, GError **error)
00083 {
00084   *ret = g_value_array_new (6);
00085   g_value_array_prepend (*ret, NULL);
00086   g_value_init (g_value_array_get_nth (*ret, 0), G_TYPE_STRING);
00087   g_value_set_string (g_value_array_get_nth (*ret, 0), "hello");
00088   g_value_array_prepend (*ret, NULL);
00089   g_value_init (g_value_array_get_nth (*ret, 0), G_TYPE_UINT);
00090   g_value_set_uint (g_value_array_get_nth (*ret, 0), 42);
00091   
00092   return TRUE;
00093 }
00094 
00095 gboolean
00096 some_object_get_dict (SomeObject *obj, GHashTable **ret, GError **error)
00097 {
00098   *ret = g_hash_table_new (g_str_hash, g_str_equal);
00099   g_hash_table_insert (*ret, "first", "Hello Dict");
00100   g_hash_table_insert (*ret, "second", " from example-service.c");
00101   return TRUE;
00102 }
00103 
00104 int
00105 main (int argc, char **argv)
00106 {
00107   DBusGConnection *bus;
00108   DBusGProxy *bus_proxy;
00109   GError *error = NULL;
00110   SomeObject *obj;
00111   GMainLoop *mainloop;
00112   guint request_name_result;
00113 
00114   g_type_init ();
00115 
00116   {
00117     GLogLevelFlags fatal_mask;
00118     
00119     fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
00120     fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
00121     g_log_set_always_fatal (fatal_mask);
00122   }
00123   
00124   dbus_g_object_type_install_info (SOME_TYPE_OBJECT, &dbus_glib_some_object_object_info);
00125 
00126   mainloop = g_main_loop_new (NULL, FALSE);
00127 
00128   bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
00129   if (!bus)
00130     lose_gerror ("Couldn't connect to session bus", error);
00131 
00132   bus_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DBus",
00133                                          "/org/freedesktop/DBus",
00134                                          "org.freedesktop.DBus");
00135 
00136   if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error,
00137                           G_TYPE_STRING, "org.designfu.SampleService",
00138                           G_TYPE_UINT, 0,
00139                           G_TYPE_INVALID,
00140                           G_TYPE_UINT, &request_name_result,
00141                           G_TYPE_INVALID))
00142     lose_gerror ("Failed to acquire org.designfu.SampleService", error);
00143 
00144   obj = g_object_new (SOME_TYPE_OBJECT, NULL);
00145 
00146   dbus_g_connection_register_g_object (bus, "/SomeObject", G_OBJECT (obj));
00147 
00148   printf ("service running\n");
00149 
00150   g_main_loop_run (mainloop);
00151 
00152   exit (0);
00153 }

Generated on Wed Oct 3 10:04:23 2007 for D-BUSGLibBindings by  doxygen 1.5.1