GDBus: Use Skeleton instead of Stub
[platform/upstream/glib.git] / gio / tests / gdbus-example-objectmanager-server.c
1
2 #include "gdbus-example-objectmanager-generated.h"
3
4 /* ---------------------------------------------------------------------------------------------------- */
5
6 static GDBusObjectManagerServer *manager = NULL;
7
8 static gboolean
9 on_animal_poke (ExampleAnimal          *animal,
10                 GDBusMethodInvocation  *invocation,
11                 gboolean                make_sad,
12                 gboolean                make_happy,
13                 gpointer                user_data)
14 {
15   if ((make_sad && make_happy) || (!make_sad && !make_happy))
16     {
17       g_dbus_method_invocation_return_dbus_error (invocation,
18                                                   "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
19                                                   "Exactly one of make_sad or make_happy must be TRUE");
20       goto out;
21     }
22
23   if (make_sad)
24     {
25       if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
26         {
27           g_dbus_method_invocation_return_dbus_error (invocation,
28                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
29                                                       "Sad animal is already sad");
30           goto out;
31         }
32
33       example_animal_set_mood (animal, "Sad");
34       example_animal_complete_poke (animal, invocation);
35       goto out;
36     }
37
38   if (make_happy)
39     {
40       if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
41         {
42           g_dbus_method_invocation_return_dbus_error (invocation,
43                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
44                                                       "Happy animal is already happy");
45           goto out;
46         }
47
48       example_animal_set_mood (animal, "Happy");
49       example_animal_complete_poke (animal, invocation);
50       goto out;
51     }
52
53   g_assert_not_reached ();
54
55  out:
56   return TRUE; /* to indicate that the method was handled */
57 }
58
59
60 static void
61 on_bus_acquired (GDBusConnection *connection,
62                  const gchar     *name,
63                  gpointer         user_data)
64 {
65   GDBusObjectSkeleton *object;
66   guint n;
67
68   g_print ("Acquired a message bus connection\n");
69
70   /* Create a new org.freedesktop.DBus.ObjectManager rooted at /example/Animals */
71   manager = g_dbus_object_manager_server_new (connection, "/example/Animals");
72
73   for (n = 0; n < 10; n++)
74     {
75       gchar *s;
76       ExampleAnimal *animal;
77
78       /* Create a new D-Bus object at the path /example/Animals/N where N is 000..009 */
79       s = g_strdup_printf ("/example/Animals/%03d", n);
80       object = g_dbus_object_skeleton_new (s);
81       g_free (s);
82
83       /* Make the newly created object export the interface
84        * org.gtk.GDBus.Example.ObjectManager.Animal (note
85        * that @object takes its own reference to @animal).
86        */
87       animal = example_animal_skeleton_new ();
88       example_animal_set_mood (animal, "Happy");
89       g_dbus_object_skeleton_add_interface (object, G_DBUS_INTERFACE_SKELETON (animal));
90       g_object_unref (animal);
91
92       /* Cats are odd animals - so some of our objects implement the
93        * org.gtk.GDBus.Example.ObjectManager.Cat interface in addition
94        * to the .Animal interface
95        */
96       if (n % 2 == 1)
97         {
98           ExampleCat *cat;
99           cat = example_cat_skeleton_new ();
100           g_dbus_object_skeleton_add_interface (object, G_DBUS_INTERFACE_SKELETON (cat));
101           g_object_unref (cat);
102         }
103
104       /* Handle Poke() D-Bus method invocations on the .Animal interface */
105       g_signal_connect (animal,
106                         "handle-poke",
107                         G_CALLBACK (on_animal_poke),
108                         NULL); /* user_data */
109
110       /* Export the object (@manager takes its own reference to @object) */
111       g_dbus_object_manager_server_export (manager, object);
112       g_object_unref (object);
113     }
114 }
115
116 static void
117 on_name_acquired (GDBusConnection *connection,
118                   const gchar     *name,
119                   gpointer         user_data)
120 {
121   g_print ("Acquired the name %s\n", name);
122 }
123
124 static void
125 on_name_lost (GDBusConnection *connection,
126               const gchar     *name,
127               gpointer         user_data)
128 {
129   g_print ("Lost the name %s\n", name);
130 }
131
132
133 gint
134 main (gint argc, gchar *argv[])
135 {
136   GMainLoop *loop;
137   guint id;
138
139   g_type_init ();
140
141   loop = g_main_loop_new (NULL, FALSE);
142
143   id = g_bus_own_name (G_BUS_TYPE_SESSION,
144                        "org.gtk.GDBus.Examples.ObjectManager",
145                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
146                        G_BUS_NAME_OWNER_FLAGS_REPLACE,
147                        on_bus_acquired,
148                        on_name_acquired,
149                        on_name_lost,
150                        loop,
151                        NULL);
152
153   g_main_loop_run (loop);
154
155   g_bus_unown_name (id);
156   g_main_loop_unref (loop);
157
158   return 0;
159 }