1 /* -*- mode: C; c-file-style: "gnu" -*- */
7 static GMainLoop *loop = NULL;
8 static int n_times_foo_received = 0;
11 timed_exit (gpointer loop)
13 g_main_loop_quit (loop);
18 foo_signal_handler (DBusGProxy *proxy,
25 if (!dbus_message_is_signal (signal,
26 "org.freedesktop.TestSuite",
29 g_printerr ("Signal handler received the wrong message\n");
33 dbus_error_init (&derror);
34 if (!dbus_message_get_args (signal, &derror, DBUS_TYPE_DOUBLE,
35 &d, DBUS_TYPE_INVALID))
37 g_printerr ("failed to get signal args: %s\n", derror.message);
38 dbus_error_free (&derror);
42 n_times_foo_received += 1;
44 g_main_loop_quit (loop);
48 main (int argc, char **argv)
50 DBusConnection *connection;
54 DBusPendingCall *call;
63 loop = g_main_loop_new (NULL, FALSE);
66 connection = dbus_bus_get_with_g_main (DBUS_BUS_SESSION,
68 if (connection == NULL)
70 g_printerr ("Failed to open connection to bus: %s\n",
76 /* Create a proxy object for the "bus driver" */
78 driver = dbus_gproxy_new_for_service (connection,
79 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
80 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
81 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
83 /* Call ListServices method */
85 call = dbus_gproxy_begin_call (driver, "ListServices", DBUS_TYPE_INVALID);
88 if (!dbus_gproxy_end_call (driver, call, &error,
89 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
90 &service_list, &service_list_len,
93 g_printerr ("Failed to complete ListServices call: %s\n",
99 g_print ("Services on the message bus:\n");
101 while (i < service_list_len)
103 g_assert (service_list[i] != NULL);
104 g_print (" %s\n", service_list[i]);
107 g_assert (service_list[i] == NULL);
109 dbus_free_string_array (service_list);
111 /* Test handling of unknown method */
112 call = dbus_gproxy_begin_call (driver, "ThisMethodDoesNotExist",
114 "blah blah blah blah blah",
120 if (dbus_gproxy_end_call (driver, call, &error,
123 g_printerr ("Calling nonexistent method succeeded!\n");
127 g_print ("Got EXPECTED error from calling unknown method: %s\n",
129 g_error_free (error);
131 /* Activate a service */
132 call = dbus_gproxy_begin_call (driver, "ActivateService",
134 "org.freedesktop.DBus.TestSuiteEchoService",
140 if (!dbus_gproxy_end_call (driver, call, &error,
141 DBUS_TYPE_UINT32, &result,
144 g_printerr ("Failed to complete Activate call: %s\n",
146 g_error_free (error);
150 g_print ("Activation of echo service = 0x%x\n", result);
152 /* Activate a service again */
153 call = dbus_gproxy_begin_call (driver, "ActivateService",
155 "org.freedesktop.DBus.TestSuiteEchoService",
161 if (!dbus_gproxy_end_call (driver, call, &error,
162 DBUS_TYPE_UINT32, &result,
165 g_printerr ("Failed to complete Activate call: %s\n",
167 g_error_free (error);
171 g_print ("Duplicate activation of echo service = 0x%x\n", result);
173 /* Talk to the new service */
175 proxy = dbus_gproxy_new_for_service_owner (connection,
176 "org.freedesktop.DBus.TestSuiteEchoService",
177 "/org/freedesktop/TestSuite",
178 "org.freedesktop.TestSuite",
183 g_printerr ("Failed to create proxy for service owner: %s\n",
185 g_error_free (error);
189 call = dbus_gproxy_begin_call (proxy, "Echo",
195 if (!dbus_gproxy_end_call (proxy, call, &error,
196 DBUS_TYPE_STRING, &str,
199 g_printerr ("Failed to complete Echo call: %s\n",
201 g_error_free (error);
205 g_print ("String echoed = \"%s\"\n", str);
208 /* Test oneway call and signal handling */
210 dbus_gproxy_connect_signal (proxy, "Foo",
214 dbus_gproxy_call_no_reply (proxy, "EmitFoo",
217 dbus_connection_flush (connection);
219 g_timeout_add (5000, timed_exit, loop);
221 g_main_loop_run (loop);
223 if (n_times_foo_received != 1)
225 g_printerr ("Foo signal received %d times, should have been 1\n",
226 n_times_foo_received);
230 g_object_unref (G_OBJECT (driver));
231 g_object_unref (G_OBJECT (proxy));
233 g_print ("Successfully completed %s\n", argv[0]);